Track 08 · Lesson 5 of 20Attacks and defenses

Track 08 · Attacks and defenses

Cookies done right

Lesson 5 of 20· 6 min

Session cookies are the keys to your users’ accounts. A few attributes decide who can read them, when browsers send them, and which hosts get them. Build one and see which attacks it survives.

Each attribute, one job#

  • HttpOnly: invisible to document.cookie, so an XSS script can’t steal it.
  • Secure: only sent over HTTPS.
  • SameSite=Lax: not sent on background requests started by other sites. Strict is tighter, but people arriving from a link land logged out.
  • No Domain: only this exact host gets it, not every subdomain.
  • A sensible Max-Age: how long a stolen cookie stays useful.

The __Host- prefix#

A cookie named __Host-session is a promise the browser enforces: it must be Secure, have Path=/ and no Domain. A subdomain you don’t fully control can’t overwrite it.

Browsers reject some combinations#

SameSite=None without Secure is dropped silently, and so is a __Host- cookie that breaks its rules. If a cookie “never gets set”, check the console for a rejection warning.

Check yourself

Which attribute stops an XSS script from reading a session cookie?