The five attributes that change behaviour
Even with no Expires/Max-Age, a Set-Cookie header can carry several other attributes that meaningfully change how the session cookie behaves:
HttpOnly— JavaScript cannot read the cookie throughdocument.cookie. This matters because it blocks XSS attacks (malicious scripts injected into a page) from stealing session IDs. A session cookie missing this flag is a security bug.Secure— the cookie is sent only over HTTPS (encrypted) connections, never plain HTTP. Every modern session cookie should have it.SameSite=Lax|Strict|None— controls whether the cookie rides along on requests coming from other sites.Lax(today's default) sends it when you click through to the site but not on cross-site sub-requests like<img>tags or XHR (background JavaScript fetches).Strictblocks every cross-site request.Noneallows full cross-site sending but requiresSecure— used by embedded widgets and federated login. Most CSRF protection (defence against forged cross-site requests) comes fromSameSite=Lax.Domain— widens the cookie from origin-only to a whole parent domain (e.g.Domain=example.commakes it visible toapi.example.com,www.example.com, and so on). Without it, RFC 6265 keeps the cookie tied to the exact origin that set it.Path— restricts the cookie to certain URL paths (e.g.Path=/accountsends it on/account/*but not/blog/*). Used less often.
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 plant a session cookie value (via a subdomain takeover, a network MITM — man-in-the-middle interception — or a too-permissive
Domainattribute), they can hijack the victim's session after the victim logs in. Defence: regenerate the session ID at every privilege boundary (login, role change) and always setHttpOnly+Secure+SameSite=Lax. - Session hijacking via XSS. Without
HttpOnly, injected JavaScript can readdocument.cookieand ship the session ID off to an attacker-controlled URL. TheHttpOnlyflag is the single most important protection for session cookies; it should be on by default for every one of them. - GDPR and consent. Strictly necessary session cookies (login, shopping cart, CSRF tokens) need no consent under GDPR. Analytics and tracking cookies do — even when they are technically session cookies in the RFC sense. The legal line is drawn by purpose, not by lifetime.
- Third-party session cookies. Chrome's 2024 phase-out of third-party cookies killed the cross-site session cookie use case (federated login, ad attribution). First-party session cookies are untouched and remain core to web authentication.
- Session cookie length. Common in 2026: 128–256 bits of entropy (entropy here meaning how hard the value is to guess), base64 or URL-safe encoded. Shorter is brute-forceable; longer is wasteful. Always generated server-side from a CSPRNG (a cryptographically secure random generator), never from time or user data.
