Track 08 · Lesson 5 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
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 todocument.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.Strictis 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