Cookie attributes
| Type | HTTP header parameters |
|---|---|
| Header | Set-Cookie |
| Specification | RFC 6265, revised by draft-ietf-httpbis-rfc6265bis |
| CWE | CWE-1004 (HttpOnly), CWE-614 (Secure), CWE-1275 (SameSite) |
| OWASP Top 10 | A07:2025 Authentication Failures |
| Affects | Session cookies and any other cookie that proves who the visitor is |
A server asks the browser to keep a cookie by sending a Set-Cookie response header. The first part is the name and value; every part after a semicolon is an attribute, which tells the browser which hosts and paths receive the cookie, whether it may travel over plain http, whether it goes along with requests started by other sites, and whether scripts on the page may read it.1 The browser keeps these rules to itself. On later requests the Cookie header carries only names and values, so a server has no way to see how a cookie it receives was set.2
Set-Cookie: __Host-SessionID=a3fWa; Secure; HttpOnly; SameSite=Strict; Path=/This line is the form recommended for a session identifier.3
HttpOnly and Secure
HttpOnly hides the cookie from scripts: document.cookie does not show it. The browser still attaches it to requests that page scripts send with fetch().4 It keeps the value secret and no more: a script injected through cross-site scripting cannot copy the session ID out to the attacker, but it can send requests from the victim's page, and those requests carry the cookie.3
Secure limits the cookie to https requests, with an exception for localhost, and a page served over http cannot set a Secure cookie at all.4 The attribute matters even on a site that never answers on http. Without it, an attacker on the network path can insert a plain http link to the site into any page the victim loads, and the browser sends the session cookie over it unencrypted.3 Secure does not stop an active network attacker from changing the cookie, and it is independent of HttpOnly, so a session cookie carries both.1
SameSite
SameSite decides whether the cookie goes with requests that another site started. A site here is the scheme plus the registrable domain, which is the public suffix and one label to its left: for https://www.site.example it is site.example.1 A site is wider than an origin, so app.example.com and blog.example.com count as the same site (see Same-origin policy).
| Value | The browser sends the cookie |
|---|---|
Strict | Only on requests started from the same site |
Lax | Also on cross-site top-level navigations with a safe method: a link click or a GET form. Not on fetch(), images, iframes or POST, PUT and DELETE |
None | On every request; the cookie must also be Secure |
An unrecognised value is treated as Lax.1 Some browsers also apply Lax when the attribute is missing, in a looser form that still sends the cookie on a POST made within two minutes of the cookie being set.4 Chrome moved to this default, together with rejecting SameSite=None without Secure, in 2020. The rollout began with Chrome 80 in February, was paused on 3 April because of COVID-19, resumed with Chrome 84 on 14 July, and on 11 August targeted every user of Chrome 80 and later.5
Against cross-site request forgery, Lax blocks forged POST requests, but an attacker's page can still open a new window on the target or send the whole tab there, and those requests carry the cookie. The specification calls that route "only a speedbump along the road to exploitation".1 Session cookies should name Strict or Lax explicitly instead of relying on a default that varies between browsers, and keep a CSRF token as the main defence.3
Domain and Path
A cookie set without Domain is host-only: the browser returns it only to the host that set it. Naming a domain shares the cookie with every subdomain of that domain. The value may be the server's own domain or a parent of it, never a public suffix, and a leading dot is ignored.4 Path limits which URLs receive the cookie but is not a security boundary, because any page on the host can set or read cookies for other paths.43
Cookies are not isolated by port or by scheme, and a subdomain can set a cookie for its parent domain that the browser then sends to every sibling subdomain, which cannot tell it from one of its own.1 Whoever controls any subdomain, including one gained through a Subdomain takeover, can plant cookies this way. The technique is called cookie tossing.2
GitHub Pages, 2013
GitHub's session cookie was host-only on github.com, with Secure and HttpOnly, so pages hosted on *.github.com could never read it. They could still write one. A user's page could run document.cookie = "_session=EVIL_SESSION_TOKEN; Path=/; Domain=.github.com", and the next request to github.com then carried two _session values with nothing to say which was real. The order of same-name cookies was unspecified, and Rack, the server interface under Rails, kept the first. GitHub had long planned to move Pages to a separate domain, github.io; a researcher's public proof of concept made it rush the move, completed in April 2013, and for the few weeks until then a server-side check cleared both cookies whenever a request arrived with two _session values.22
Name prefixes
Prefixes put the rules into the cookie's name, where the server can see them. A browser that supports them refuses a cookie whose name starts with the prefix unless the attributes match:41
| Prefix | Accepted only when |
|---|---|
__Secure- | Set with Secure from an https page |
__Host- | As __Secure-, plus Path=/ and no Domain |
__Http- | As __Secure-, plus HttpOnly; scripts cannot create or change it |
__Host-Http- | The rules of __Host- and __Http- together |
A cookie named __Host- was therefore set by this exact host over https and covers the whole host, so a sibling subdomain cannot toss a replacement; ports remain the one part of the origin it ignores.1 __Secure- suits a cookie that subdomains must share.3 Browsers without prefix support accept such cookies without checking them.4
Framework defaults
| Framework | Default | To change |
|---|---|---|
| Django 5.26 | HttpOnly on, SameSite Lax, Secure off; the CSRF cookie has HttpOnly and Secure off | SESSION_COOKIE_SECURE = True |
| express-session7 | HttpOnly on, Secure off, SameSite not set, no Domain, Path / | cookie: { secure: true, sameSite: "lax" } |
Next.js cookies().set()8 | Path /; other attributes only when passed | { httpOnly: true, secure: true, sameSite: "lax" } |
Next.js allows set() only in a Server Function or a Route Handler, because HTTP does not allow setting cookies once a streamed response has started.8 Expires, Max-Age and ending a session on the server belong to Session management.
Prompt for a coding agent
Find every place this project sets a cookie that identifies a user: session cookies, auth tokens, refresh tokens. For each, set HttpOnly, Secure and SameSite=Lax (or Strict where cross-site links into the app do not need the user signed in). Remove any Domain attribute unless subdomains must share the cookie, and rename session cookies with the __Host- prefix and Path=/ where the framework allows it. Do not weaken any existing setting. List each file and line you changed.
In SecFlash
On the Security page, SF-SEC-501 reports a sign-in cookie without HttpOnly, Secure or SameSite, and SF-SEC-761 points to the line of code that switches HttpOnly or Secure off. The ZAP scan reports the same gaps one by one as SF-SEC-628, SF-SEC-629 and SF-SEC-630.
See also
- Session management
- Session hijacking
- Cross-site request forgery
- Cross-site scripting
- Subdomain takeover
- Same-origin policy
- Securing Rails, Django and Laravel applications
References
- a b c d e f "Session Management Cheat Sheet". OWASP Cheat Sheet Series. OWASP Foundation.
- a b c d e f g "Set-Cookie header". MDN Web Docs. Mozilla.
- back "express-session". GitHub. Express.js project.
External links
- Using HTTP cookies, MDN Web Docs