Cookie Security: Secure, HttpOnly and SameSite
Chapter Seventy-Five
Syllabus topic Module 2, "Session Hijacking and Token Security: ... prevention mechanisms like secure flags and HTTPS"
Pages 358 to 361 of 578
In one line
Three cookie attributes close three attack routes for free: Secure ensures the cookie is sent only over HTTPS (stopping sniffing), HttpOnly hides it from script (stopping cross-site-scripting theft), and SameSite limits when the cookie is sent from other sites (stopping cross-site request forgery).
In examination wording: cookie security attributes control how the browser handles a cookie; the Secure attribute restricts transmission to encrypted connections, the HttpOnly attribute prevents access by client-side scripts, and the SameSite attribute restricts whether the cookie is sent on requests originating from other sites, together mitigating interception, script-based theft, and cross-site request forgery.
Why these attributes matter
The previous chapters identified how a session token is stolen, planted or abused. Several of the defences are these three cookie attributes, which the browser enforces once the server sets them. They are worth their own chapter for three reasons: MU names them, each maps to a specific attack, and they are cheap and commonly missing, so they are among the most frequent findings in a web assessment and among the easiest to fix. Setting them is a matter of configuration, not code, and a session cookie without them is a token needlessly exposed.
Secure: HTTPS only
The Secure attribute tells the browser to send the cookie only over an encrypted (HTTPS) connection, never over plain HTTP.
The route it closes is sniffing. The previous chapters showed that a session cookie sent over plain HTTP is captured in the clear, and that even a site using HTTPS for login can expose the cookie if some later request goes over plain HTTP. The Secure attribute prevents that last case: the browser will simply not send the cookie on any unencrypted request, so it can never be transmitted where a sniffer could read it, even by accident or misconfiguration.
Secure and HTTPS-everywhere work together: HTTPS-everywhere ensures requests are encrypted, and Secure ensures the cookie is withheld if any request somehow is not. A session cookie without Secure, on a site that has any HTTP endpoint, is a sniffing exposure, and it is a common finding.
HttpOnly: invisible to script
The HttpOnly attribute tells the browser that the cookie cannot be read by JavaScript running on the page. The browser still sends it with requests, so the session works, but script cannot access its value.
The route it closes is theft through script, that is, cross-site scripting. The previous chapter explained that if an attacker's script runs on the page, it can read the session cookie and exfiltrate it, and that HTTPS and strong tokens do not help because the script runs on the decrypted page and reads rather than guesses. HttpOnly closes this: an HttpOnly session cookie is invisible to the injected script, so even a site with a cross-site-scripting flaw does not leak the session token through it.
Cookie Security: Secure, HttpOnly and SameSite
Two clarifications:
- HttpOnly does not fix the cross-site-scripting flaw itself, which can still do other harm; it protects the token specifically even where such a flaw exists. Fixing the flaw is still necessary (the XSS chapters).
- HttpOnly applies to cookies. A token kept in other browser storage that scripts read is not protected by it, which is an argument for holding session tokens in HttpOnly cookies rather than in script-accessible storage.
SameSite: not sent from other sites
The SameSite attribute controls whether the browser sends the cookie on requests that originate from a different site. It has values that range from sending the cookie only on requests from the same site, through a middle setting that sends it on top-level navigations but not on background cross-site requests, to sending it always (the old default).
The route it closes is cross-site request forgery, the subject of the next chapter. That attack relies on the browser automatically attaching the session cookie to a request that a different, malicious site caused the victim's browser to make, so that the request arrives authenticated. SameSite limits exactly this: if the cookie is not sent on cross-site requests, a request forged by another site arrives without the session cookie and is not treated as authenticated, so the forgery fails.
SameSite is why the next chapter can say the attack is substantially mitigated by a cookie attribute; it is treated fully there. For now, it is the third of the three attributes, closing the third route.
The three together
The attributes map cleanly to three of the block's attack routes:
| Attribute | Route it closes | How |
|---|---|---|
| Secure | Sniffing | Cookie sent only over HTTPS, never in the clear |
| HttpOnly | Script theft (XSS) | Cookie invisible to JavaScript |
| SameSite | Cross-site request forgery | Cookie not sent on cross-site requests |
A session cookie should have all three set appropriately (Secure, HttpOnly, and a restrictive SameSite), plus sensible scope attributes (limiting the cookie to the paths and domains that need it) and an expiry that supports session timeout. Setting them is configuration, and their absence is a finding precisely because it is so easily fixed and so commonly overlooked.
A note on completeness: these attributes protect the cookie, but they are not the whole of session security. Strong random tokens (against prediction), token regeneration at login (against fixation), and the full defences of the next-but-one chapter are also required. The attributes close three routes; they do not close prediction or fixation, which is why the block does not end here.
A worked example, framed defensively
An assessor inspects the session cookie's attributes, a quick and high-value check.
Cookie Security: Secure, HttpOnly and SameSite
- The session cookie lacks Secure, and the site has an HTTP endpoint. Finding: sniffing exposure; the cookie can be sent in the clear. Fix: set Secure.
- The cookie lacks HttpOnly, and the site has a cross-site-scripting flaw. Finding: the token can be stolen by injected script. Fix: set HttpOnly (and fix the XSS).
- The cookie has no SameSite attribute (so it is sent on cross-site requests), and the site performs state-changing actions on simple requests. Finding: exposure to cross-site request forgery. Fix: set a restrictive SameSite and add anti-forgery tokens (next chapter).
- The cookie's expiry is set far in the future with no inactivity timeout. Finding: a stolen token is useful for a long time. Fix: add inactivity and absolute timeouts.
After setting Secure, HttpOnly and a restrictive SameSite, three routes are closed at once by configuration. The assessor notes that these were the cheapest findings in the report to fix and among the most important, which is the chapter's point.
What beginners get wrong
- Omitting the attributes because the session "works" without them. It works and is needlessly exposed; each attribute closes a real route at no cost.
- Setting one and thinking the cookie is secure. Secure, HttpOnly and SameSite close different routes; all three are needed, plus strong tokens and regeneration for the routes they do not cover.
- Believing HttpOnly fixes cross-site scripting. It protects the token from script theft; the XSS flaw itself must still be fixed and can do other harm.
- Assuming Secure is unnecessary on an all-HTTPS site. It guards against any HTTP endpoint or misconfiguration causing the cookie to be sent in the clear; set it regardless.
- Keeping session tokens in script-readable storage. HttpOnly cannot protect them there; prefer HttpOnly cookies for session tokens.
- Ignoring scope and expiry. A cookie scoped too broadly or lasting too long widens exposure; limit both.
Quick revision
- Secure: cookie sent only over HTTPS, closing the sniffing route (guards against any HTTP endpoint sending it in the clear).
- HttpOnly: cookie invisible to JavaScript, closing the script-theft (XSS) route; protects the token even where an XSS flaw exists, though the flaw must still be fixed. Applies to cookies, not other browser storage.
- SameSite: cookie not sent on cross-site requests, closing the cross-site request forgery route (next chapter).
- A session cookie should set all three, plus restrictive scope and an expiry supporting timeout. They are cheap configuration and commonly missing, hence a frequent, easy finding.
- They close three routes but not prediction or fixation; strong tokens and token regeneration are also required.
Test yourself
- What does the Secure attribute do, and which route does it close?
Cookie Security: Secure, HttpOnly and SameSite
It instructs the browser to send the cookie only over an encrypted HTTPS connection and never over plain HTTP, closing the sniffing route. It guards against a session cookie being transmitted in the clear on any unencrypted request, including where a site is mostly HTTPS but has an HTTP endpoint or misconfiguration, so that a network eavesdropper cannot capture it.
- What does HttpOnly protect against, and what does it not fix?
It prevents client-side JavaScript from reading the cookie, closing the route by which a cross-site-scripting flaw's injected script steals the session token; the browser still sends the cookie, so the session works, but script cannot access it. It does not fix the cross-site-scripting flaw itself, which can still cause other harm and must be remediated separately, and it protects only cookies, not tokens held in script-readable browser storage.
- What does SameSite restrict, and which attack does that mitigate?
It restricts whether the browser sends the cookie on requests that originate from a different site, so that a cross-site request does not automatically carry the session cookie. This mitigates cross-site request forgery, which relies on the browser attaching the session cookie to a request forged by a malicious site; without the cookie, the forged request is not treated as authenticated and fails.
- Why are these attributes described as cheap and commonly-found findings?
Because setting them is a matter of configuration rather than code, so they cost almost nothing to apply, yet they are frequently omitted, leaving a session cookie needlessly exposed to sniffing, script theft or cross-site request forgery. Their absence is therefore both a real exposure and among the easiest issues to remediate, which makes them a common and high-value finding in a web assessment.
- Why do the three cookie attributes not, by themselves, secure a session completely?
Because they close only three routes, sniffing, script theft and cross-site request forgery, and do not address token prediction or session fixation. Securing the session also requires strong, high-entropy tokens to prevent prediction and regeneration of the token at login and on privilege change to prevent fixation, so the cookie attributes are necessary but not sufficient.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself for the same subject.