Secure, HttpOnly, SameSite & the __Host- prefix
Cookies that protect sessions
Lesson 11 of 20 · 12 min · Module 3: Passwords and sessions
After you sign in, a session cookie is the only thing that proves who you are. If a script can read it, or another site can make the browser send it, the attacker becomes you.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Read the attributes of a Set-Cookie header
- Protect session cookies with Secure, HttpOnly and SameSite
- Use the __Host- prefix and short lifetimes
The idea
Servers set cookies with the Set-Cookie header, and its attributes decide how the browser treats them. Secure sends the cookie only over HTTPS, HttpOnly hides it from JavaScript so XSS cannot read it, and SameSite=Lax or Strict keeps it away from most requests started by other sites. A name that starts with __Host- makes the browser insist on Secure, Path=/ and no Domain.
Set-Cookie: __Host-session=9f8e7d; Path=/; Secure; HttpOnly; SameSite=Lax
01HttpOnly limits what XSS can steal
Scripts cannot read an HttpOnly cookie through document.cookie, so an injected script cannot simply copy the session and use it elsewhere. It can still act inside the page while it runs, which is why XSS must be fixed too.
02SameSite decides when the cookie travels
With Strict, the cookie is sent only on requests that start on your own site. Lax also allows following a link from another site, which keeps people signed in when they arrive. None sends it everywhere and requires Secure.
Set-Cookie: __Host-session=…; Path=/; Secure; HttpOnly; SameSite=Lax
03Shorter is safer
A session that lasts a year is a year-long opportunity for whoever steals it. Keep sessions short, renew them while people are active, and end them on the server when people sign out.
Your turn
parseCookie(header): the part before the first ; is name=value (split at the first =, trim both), and every other part is an attribute stored in attributes under its lowercase name, with its trimmed value, or true when it has none. Then write auditCookie(header) returning, in this order: not-secure without Secure; readable-by-scripts without HttpOnly; weak-samesite unless SameSite is Lax or Strict, in any capitalisation; shared-with-subdomains with a Domain; long-lived when Max-Age is over 30 days (2592000 seconds); invalid-host-prefix when the name starts with __Host- but the cookie lacks Secure, has a Path other than /, or has a Domain.What the checks look for
- Cookie headers are parsed
- A well-configured session cookie passes
- Missing protections are reported
- SameSite=None and Domain are flagged
- Attribute names and values ignore capitals
- Long-lived cookies are noted
- __Host- cookies must follow their rules
- The page shows the name and the warnings
Take this with you
Session cookies: __Host- prefix, Secure, HttpOnly, SameSite=Lax or Strict, and a short life.
Go a little further
Warn when a cookie whose name suggests a session, such as sid, session or token, has no HttpOnly attribute.