Secure, HttpOnly and SameSite in practice
These three attributes do most of the protective work.
Secure
Without Secure, a cookie is also sent over plain HTTP, where anyone on the network path can read it. Every cookie on an HTTPS site should be Secure. Browsers also refuse to let an insecure page overwrite a Secure cookie.
HttpOnly
HttpOnly keeps a cookie out of reach of page scripts. If an attacker injects a script, they cannot read an HttpOnly session cookie and send it away. It does not stop the script from making requests that carry the cookie, so it limits damage rather than preventing it. Session cookies should be HttpOnly unless your own JavaScript must read them.
SameSite
| Value | Sent on cross-site requests? | Typical use |
|---|---|---|
Strict | Never, not even when following a link from another site | High-value actions where a fresh sign-in is acceptable |
Lax | Only on top-level navigations with safe methods, such as following a link | Most session cookies |
None | Always; requires Secure | Cookies that must work in cross-site frames or requests |
Chrome and Edge treat a cookie without SameSite as Lax. Other browsers have their own defaults, so set it explicitly. SameSite=None without Secure is rejected outright.
Expiry, Max-Age and lifetime limits
A cookie with neither Max-Age nor Expires is a session cookie: it ends when the browser session ends, though browsers that restore sessions may keep it longer. Max-Age gives a lifetime in seconds and takes precedence over Expires when both are present. A Max-Age of zero or less, or an Expires date in the past, deletes the cookie.
Browsers also cap lifetimes. Current versions of Chrome limit cookie lifetimes to 400 days whatever the server asks. Size is limited too: a name and value over 4,096 bytes are dropped.
What this analyzer does and does not do
- It reads one or more
Set-Cookielines, with or without the header name. - For each cookie it checks Secure, HttpOnly, SameSite, prefixes, Domain, Path, lifetime, size and unknown attributes, and rates the findings.
- It recognizes Keycloak’s cookies and says what each is for.
- It reads text only. It does not visit your site or read your browser’s cookies.
- It cannot see the context a cookie is set in, such as whether the page is HTTPS or which host set it. Keep that in mind when reading findings about Domain and Secure.
Common mistakes
- A session cookie without
SecureorHttpOnly. SameSite=NonewithoutSecure, which browsers reject, so the cookie never arrives.- A broad
Domainsuch as.example.com, so every subdomain, including ones run by others, receives the session cookie. - A
__Host-cookie with aDomainor a path other than/, which browsers drop. - Very long lifetimes for session cookies. Prefer short sessions with a refresh mechanism.
- Setting a cookie from a different host and expecting a different
PathorDomainto delete it. To delete, match the original name, domain and path.
Best practices
- Session cookies:
__Host-prefix,Secure,HttpOnly,SameSite=LaxorStrict,Path=/. - Use
SameSite=Noneonly for cookies that genuinely need cross-site use, and always withSecure. - Leave out
Domainunless subdomains must share the cookie. - Keep session lifetimes short and end sessions on the server at logout, not only by deleting the cookie.
- Never store tokens in cookies readable by JavaScript.
Security notes
Cookie attributes reduce risk but do not replace server-side checks. A stolen session cookie still works until the session ends on the server, so logout must end the session there, and sensitive actions should ask for a fresh sign-in.
SameSite is a helpful defense against cross-site request forgery, not a complete one. Requests from other subdomains of the same site count as same-site, so state-changing endpoints still need CSRF protection.
Do not paste real session cookie values into tools or tickets. The analyzer only needs the attributes; replace the value with a placeholder.