Skip to content
Velloreth
Web Development 5 min read

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

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

AttributeMeaning
Expires / Max-AgeWhen the cookie expires
DomainWhich hosts receive it
PathWhich paths receive it
SecureSend only over HTTPS
HttpOnlyHide from JavaScript
SameSiteCross-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

CookielocalStoragesessionStorage
Sent to the serverYes, automaticallyNoNo
Size~4KB~5MB~5MB
LifetimeConfigurableUntil clearedUntil the tab closes
HttpOnlyYesNoNo

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 HttpOnly on a session cookie.
  • Set Secure on every cookie.
  • Set SameSite=Lax unless a cross-site use requires None.
  • Omit Domain for a host-only cookie.
  • Keep the cookie small.
  • Store an identifier, not the data.
  • Never store a token in localStorage when a cookie would work.
  • Check the Set-Cookie header when debugging.
  • Remember Path is 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.

Frequently asked questions

What is a cookie?
A small piece of data the server asks the browser to store and send back on subsequent requests to the same site, which is how a stateless protocol carries state.
What does HttpOnly do?
It makes the cookie invisible to JavaScript, so a cross-site scripting attack cannot read it. It is essential for a session cookie.
What is SameSite?
An attribute that controls whether the cookie is sent on a cross-site request. Strict sends it only on a same-site request, Lax sends it on a top-level navigation, and None sends it always.
What is the difference between a session cookie and a persistent cookie?
A session cookie has no expiry and is deleted when the browser closes. A persistent cookie has an Expires or Max-Age and survives.
Why is my cookie not being set?
A common cause is SameSite=None without Secure, which the browser rejects. Another is a domain or path that does not match the request.
Are cookies the same as localStorage?
No. A cookie is sent to the server on every request and can be HttpOnly. localStorage is available only to JavaScript and is never sent automatically.