Cookies Explained: Attributes, Scope and Security
Cookies carry state across requests. Learn the attributes, the SameSite values, the security flags, and the size and scope limits.
By Velloreth
What a cookie is
A cookie is a name-value pair the server asks the browser to store.
HTTP/1.1 200 OK
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600
The browser sends it back on every matching request:
GET /dashboard HTTP/1.1
Cookie: session=abc123
This is how a stateless protocol carries state. HTTP has no memory between requests, and the cookie is the memory.
The attributes
| Attribute | Meaning |
|---|---|
Expires / Max-Age | When the cookie expires |
Domain | Which hosts receive it |
Path | Which paths receive it |
Secure | Send only over HTTPS |
HttpOnly | Hide from JavaScript |
SameSite | Cross-site sending policy |
Without Expires or Max-Age, the cookie is a session cookie and is deleted when the browser closes.
Max-Age is in seconds and takes precedence over Expires.
Scope
Domain controls which hosts receive the cookie.
Set-Cookie: session=abc; Domain=example.com
This sends the cookie to example.com and every subdomain, including api.example.com.
Without a Domain, the cookie is host-only, which means it is sent only to the exact host that set it. This is the safer default.
Path controls which paths receive it.
Set-Cookie: session=abc; Path=/app
This sends the cookie only to a path under /app.
Path is not a security boundary. A script on the same origin can read a cookie for any path, because the path is not part of the origin. It is a scope control, not an access control.
HttpOnly
Set-Cookie: session=abc; HttpOnly
The cookie is invisible to document.cookie.
This is essential for a session cookie. A cross-site scripting attack runs JavaScript in the page, and without HttpOnly it can read the session cookie and send it to an attacker.
With HttpOnly, the script cannot read it, so the session cannot be stolen that way. The attacker can still act within the page, but cannot exfiltrate the session.
Secure
Set-Cookie: session=abc; Secure
The cookie is sent only over HTTPS.
Without it, the cookie is sent over a plain HTTP request, where anyone on the network can read it.
A Secure cookie is required for SameSite=None. A browser rejects SameSite=None without Secure, which is a common cause of a cookie that is not set.
SameSite
SameSite=Strict — the cookie is sent only on a same-site request. A link from another site does not send it, so the user appears logged out on arrival.
SameSite=Lax — the cookie is sent on a top-level navigation (a link click) but not on a cross-site POST or an embedded request. This is the modern default, and it blocks the classic cross-site request forgery.
SameSite=None — the cookie is sent on every request, including a cross-site one. It requires Secure, and it is needed for a legitimate cross-site use such as an embedded widget.
The default changed. A browser without an explicit SameSite now treats the cookie as Lax, which broke a number of cross-site integrations when it shipped.
Size and count
A cookie is limited to about 4KB, including the name and the attributes.
A browser limits the number of cookies per domain to around 50.
A cookie is sent on every matching request, so a large cookie adds to every request’s size. A 4KB cookie on a page with 50 requests is 200KB of overhead.
The practical rule: keep a cookie small, and store only an identifier. The data belongs on the server, keyed by the identifier.
Cookies versus other storage
| Cookie | localStorage | sessionStorage | |
|---|---|---|---|
| Sent to the server | Yes, automatically | No | No |
| Size | ~4KB | ~5MB | ~5MB |
| Lifetime | Configurable | Until cleared | Until the tab closes |
| HttpOnly | Yes | No | No |
Use a cookie for a session, because it is sent automatically and can be HttpOnly.
Use localStorage for a client-side preference, such as a theme, because it is not sent to the server.
Never store a token in localStorage if a cookie with HttpOnly would work, because localStorage is readable by any script on the page.
Common problems
A cookie is not set. SameSite=None without Secure, a Domain that does not match, or a Path that excludes the request.
A cookie is not sent. The Domain or Path does not match, the cookie expired, or SameSite blocked it.
A cookie is sent to a subdomain unexpectedly. A Domain of example.com sends it to every subdomain. Use a host-only cookie instead.
A session is lost on a redirect. A SameSite=Strict cookie is not sent on the cross-site navigation, so the user appears logged out. Use Lax.
A cookie is too large. The browser silently drops it, or truncates it.
Practical guidance
- Set
HttpOnlyon a session cookie. - Set
Secureon every cookie. - Set
SameSite=Laxunless a cross-site use requiresNone. - Omit
Domainfor a host-only cookie. - Keep the cookie small.
- Store an identifier, not the data.
- Never store a token in
localStoragewhen a cookie would work. - Check the
Set-Cookieheader when debugging. - Remember
Pathis not a security boundary.
For the related guides, see csrf-explained and cors-explained. For the broader context, content-security-policy-guide and encryption-at-rest-vs-transit.
Quotable summary
A cookie is a name-value pair the server sets and the browser sends on every matching request, which is how a stateless protocol carries state. HttpOnly hides the cookie from JavaScript so a cross-site scripting attack cannot steal it, Secure restricts it to HTTPS, and SameSite controls whether it is sent on a cross-site request, with Lax as the modern default that blocks the cookie on a cross-site POST. A cookie is limited to about 4KB and is sent on every request, so it is not a place for large data, and the practical rule is to store only an identifier and keep the data on the server. A browser rejects SameSite=None without Secure, which is a common cause of a cookie that is not set.