Skip to content

Lesson 2 of 14 · 15m

Cookie Flags That Actually Matter

HttpOnly, Secure, SameSite, Domain, and __Host- each answer a different question the browser asks before attaching a cookie.

What the flags actually do

Set-Cookie: __Host-sess=abc; Secure; HttpOnly; SameSite=Lax; Path=/ is six small decisions, and each one answers a different question the browser asks when deciding whether to attach the cookie to a request.

  • HttpOnly — removed from document.cookie and the cookie APIs. XSS can't read it. Note the precision: XSS can still ride it — an injected script can't exfiltrate the value, but it can fire fetch('/api/transfer') from the victim's page and the cookie attaches automatically. HttpOnly stops theft, not use.
  • Secure — only sent over HTTPS. Without it the cookie goes out in plaintext on any HTTP request to the domain, and one captive-portal redirect or sslstrip session later it's gone. On production there's no defensible reason to omit it.
  • SameSite — the CSRF lever. Strict: only same-site requests carry it. Lax: same-site plus top-level GET navigations carry it; cross-site POSTs don't. None: everything carries it — and Chrome requires Secure alongside it. Since Chrome 80 (February 2020) a cookie with no SameSite attribute is treated as Lax, with a quirk called Lax+POST: cookies set without an explicit SameSite within the last two minutes still ride top-level cross-site POSTs, to keep legacy SSO flows from breaking. Firefox and Safari followed with their own defaults. SameSite killed most ambient-authority CSRF — but "most" is doing work in that sentence, and the CSRF lesson covers the rest.
  • Path — a coarse routing hint. The RFC 6265 path-match rule sends a Path=/app cookie to /app and /app/admin, but not to /apple — the character after the prefix must be /. Still a prefix rule, not a security boundary: anything under /app sees it.
  • Domain — the dangerous one, covered below.
  • __Host- / __Secure- prefixes (RFC 6265bis) — the browser enforces the contract: __Host- requires Secure, Path=/, and no Domain. Any deviation and the cookie is rejected. It's a lint rule baked into the parser.
  • Partitioned (CHIPS) — the post-third-party-cookie mechanism: a Partitioned cookie on an embedded third-party context is keyed by the top-level site, so the tracker-in-an-iframe can't correlate visits across sites. Only relevant if you build embeddable widgets; for a first-party session cookie it solves a problem you don't have.

The wrong mental model

"Cookies are scoped to my site." They are not — they're scoped to a domain suffix the server can widen, and they ignore two boundaries people assume exist: ports and subdomains.

Cookies don't see ports. A cookie set by a service on evil.example.com:8080 is readable and overridable for example.com:443. Cookies also flow downward by default when Domain is set: Domain=example.com means every subdomain — including the stale staging.example.com A-record pointing at an IP you don't control anymore — can read the session cookie, and can also set cookies the parent sees. That second direction is cookie tossing: a hostile subdomain (or any compromised sibling) injects a cookie with a name the app trusts, and the browser happily sends both; server-side ordering quirks mean the attacker's often wins. Subdomain takeovers have repeatedly escalated into full account takeover through exactly this path.

What to actually do

Session cookie recipe: __Host-sess=<random>; Secure; HttpOnly; SameSite=Lax; Path=/ — omit Domain entirely so it stays host-only. Only reach for SameSite=None when a third-party context genuinely needs the cookie, and know that None deletes your CSRF margin so you owe the endpoint another check. Audit for stray Domain= attributes and for cookies set by sibling subdomains — Set-Cookie responses are enumerable in DevTools' Network panel, and sloppy Domain=.corp.com cookies show up constantly in real estates. When you find one, narrowing it to host-only is a one-attribute fix with a real payoff.

Keep reading

Related articles from the SudoSecurity blog.