The five attributes that change behaviour
Even without Expires/Max-Age, a Set-Cookie header carries several attributes that materially change how the session cookie behaves:
HttpOnly— the cookie is not accessible to JavaScript viadocument.cookie. Critical for session cookies because it neutralises XSS attacks that try to steal session IDs. If your session cookie is missing this flag, that is a security bug.Secure— the cookie is sent only over HTTPS connections, never over plain HTTP. Should be set on every modern session cookie.SameSite=Lax|Strict|None— controls cross-site request behaviour.Lax(the modern default) sends the cookie on top-level navigations but not on cross-site sub-requests like<img>or XHR.Strictblocks all cross-site requests.NonerequiresSecureand allows full cross-site sending — used by embedded widgets and federated login. Most CSRF protection comes fromSameSite=Lax.Domain— broadens the cookie scope from origin-only to a parent domain (e.g.Domain=example.commakes the cookie visible toapi.example.com,www.example.com, etc.). Without this, RFC 6265 restricts the cookie to the exact origin that set it.Path— narrows the cookie to specific URL paths (e.g.Path=/accountmeans the cookie is sent on/account/*but not/blog/*). Less commonly used.
A modern, well-configured session cookie looks like:
Set-Cookie: sessionId=abc123; Path=/; HttpOnly; Secure; SameSite=LaxSecurity and privacy considerations
- Session fixation. If an attacker can set a session cookie value (via a subdomain takeover, network MITM, or a permissive
Domainattribute), they can hijack a victim's session after the victim authenticates. Defence: regenerate the session ID on every privilege boundary (login, role change), and always setHttpOnly+Secure+SameSite=Lax. - Session hijacking via XSS. Without
HttpOnly, JavaScript can readdocument.cookieand exfiltrate the session ID via an attacker-controlled URL. TheHttpOnlyflag is the single most important hardening for session cookies; it should be on every session cookie by default. - GDPR and consent. Strictly necessary session cookies (login, shopping cart, CSRF tokens) do not require consent under GDPR. Analytics and tracking cookies do — even if they are technically session cookies in the RFC sense. The legal distinction is purpose, not lifetime.
- Third-party session cookies. Chrome's 2024 phase-out of third-party cookies removed the cross-site session cookie use case (federated login, ad attribution). First-party session cookies are unaffected and remain core to web authentication.
- Session cookie length. Common in 2026: 128–256 bits of entropy, base64 or URL-safe encoded. Shorter than that is brute-forceable; longer is wasteful. Generated server-side from a CSPRNG, never from time or user data.
