CCyberwiki

Cookie tossing

Cookie tossing
ClassAuthentication
OWASP Top 10A07:2025 Authentication Failures
Typical severityMedium
AffectsApplications whose session or CSRF cookies are read on a domain that also has subdomains running other people's code or forgotten services

A script on blog.example.com runs document.cookie = "session=abc; Domain=example.com; Path=/". The browser stores the cookie for the whole of example.com and from then on attaches it to requests for example.com and for app.example.com. Planting a cookie on a parent domain from one of its subdomains in this way is called cookie tossing.1 The receiving application cannot see that the value came from elsewhere, so a subdomain an attacker controls can choose the session or the CSRF token that its siblings read.2

Why the server cannot tell

A response may set Domain to its own host or to any parent of it, as long as the parent is not a public suffix such as com or co.uk. A cookie with a Domain attribute reaches that domain and every subdomain below it; one set without it is host-only, sent back only to the host that set it.3 Under the cookie standard, sibling domains get no integrity guarantee from cookies, and a host that receives a cookie set by its sibling may be unable to tell it from one of its own.2

A browser sends a single Cookie header made of name and value pairs; the Domain, Path, Secure and HttpOnly attributes stay in the browser.1 When a planted cookie has the same name as the real one, both arrive:

GET / HTTP/1.1
Host: app.example.com
Cookie: session=abc; session=7f3c9e...

Which one the application uses depends on the order and on its parser. The current draft of the standard asks browsers to list cookies with longer paths first, then older ones first,2 and Rack, the Ruby layer under Rails and Sinatra, keeps the first value of a repeated name.1 A tossed cookie with a narrow Path such as /account therefore comes first on that page and wins with such a parser, and the Path attribute gives no protection because a browser accepts any path a response names.2

An outsider can get a subdomain to toss from through a Subdomain takeover.4 A company that serves its customers' pages under its own domain hands the same ability to every customer.1 A network attacker can inject cookies without any subdomain, by faking the reply to a plain-http request for the site.2

Cookie tossing writes cookies but cannot read them; a host-only HttpOnly session cookie stays out of the attacker's reach.1 Writing is enough to break session integrity, the confidence that a session belongs to the person using it:

  • Session fixation. The attacker plants a session ID they already know before the victim signs in.5
  • Forged requests. A CSRF defence that only compares a cookie with a form field, the naive double-submit cookie, fails once the attacker can set the cookie: they choose a value and put the same one in the forged form.4
  • Signing users out. Browsers cap how many cookies they keep, and a flood of new ones forces old ones out, the real session cookie among them.2

A 2023 study of browsers and of 13 popular web frameworks found session integrity flaws of this kind in 9 of the frameworks; the work led to 12 CVEs and to changes in the cookie standard.5

GitHub Pages, 2013

Until April 2013, user sites on GitHub Pages were served under *.github.com. GitHub's session cookie was host-only, Secure and HttpOnly, so a Pages site could not read it, but script on a Pages site could toss a second _session cookie to github.com. The researcher Egor Homakov published a proof of concept that could sign users out; its reach was limited because GitHub reset CSRF tokens at every sign-in.1

While it moved Pages to a new domain, GitHub added middleware that looked for a repeated _session name and answered with a redirect carrying a Set-Cookie that expired the copy set for .github.com. Each variant needed another patch. A cookie scoped to a deeper path needed one expiry per path segment; a name written as _%73ession was decoded to _session by the Rack parser, so the raw names had to be checked. The overflow variant could not be patched: Chrome then sent only the 180 newest cookies, so a Pages site could push out every parent cookie and leave one fabricated _session that looked normal to the server. GitHub's conclusion was that user content had to live on a separate domain, and Pages moved to github.io.1

Defences

The __Host- prefix

A browser accepts a cookie whose name starts with __Host- only if it comes from an https page with Secure, Path=/ and no Domain. Such a cookie is sent only to the host that set it and cannot be shadowed on a deeper path, so a sibling cannot plant a cookie with that name.3 The port is the one part of the origin it does not bind.2

Set-Cookie: __Host-session=7f3c9e...; Secure; HttpOnly; Path=/; SameSite=Lax

The prefix suits session cookies and CSRF cookies. A browser that does not know the prefixes accepts such a cookie without the checks.3 Cookie attributes covers the other attributes.

A separate domain for other people's content

The boundary for cookies is the registrable domain, the public suffix plus one label.2 User uploads, customer sites and preview builds belong on a different registrable domain from the application. Hosting services add their shared domains to the Public Suffix List, a Mozilla-started list that browsers use to refuse cookies set across a whole suffix.6 github.io, vercel.app, netlify.app, pages.dev and supabase.co are on it,7 so two projects on vercel.app cannot toss cookies to each other.3

Tokens bound to the session

A CSRF token that is an HMAC over a value tied to the server-side session cannot be forged by planting a cookie; a token that is only signed, without that binding, still can.4 Signing and encrypting cookie contents in general helps only in part, because the attacker can replay a genuine cookie they received from the server for their own account.2 Where an application checks Sec-Fetch-Site, a value of same-site counts as safe only if every sibling subdomain is trusted.4

Prompt for a coding agent

Find every cookie this app sets for sign-in, sessions and CSRF protection. Rename each one to start with __Host- and set it with Secure, HttpOnly where scripts do not need it, Path=/ and no Domain attribute; update every place that reads it. If a CSRF check compares a cookie with a form field or header, make the token an HMAC over the server-side session ID with a server secret. List each cookie you changed, and any cookie that has to stay shared across subdomains.

In SecFlash

SecFlash reports a subdomain that points at an unclaimed outside service, one way to gain a subdomain to toss cookies from, as SF-SEC-308 on the Security page.

See also

References

  1. a b c d MDN contributors. "Set-Cookie header". MDN Web Docs.
  2. a b c d "Cross-Site Request Forgery Prevention Cheat Sheet". OWASP Cheat Sheet Series.