Definition
Cookie Tossing
Cookie tossing is an attack where a same-site attacker uses a subdomain such as
evil.bank.comto set aDomain=bank.comcookie that shadows the victim’s session cookie onbank.com. When the victim requests/account/, the browser sends the attacker’s cookie first; the session id it names is now the attacker’s, so the victim is logged in as the attacker — a session swapping that lets the attacker track or manipulate the victim’s activity.
Why Browsers Let This Happen
Obs
The browser keys cookies by , but the
Cookierequest header sends only name/value pairs. The server therefore has no way to tell which cookie is for which domain or path, and most servers accept the first matching one. Most browsers order cookies oldest first, and put longer (more specific) paths first — so apath=/accountcookie planted from a subdomain wins over the genuinepath=/cookie.
Defence
Defence: cookie prefixes
A cookie named
__Host-sidis accepted only if it isSecure, comes from a secure origin, has noDomainattribute, and setsPath=/— i.e. it is host-locked. The__Secure-prefix is the weaker variant that merely pins HTTPS. See Cookie Prefix.
Example
Tossing the victim into the attacker's session
The attacker controls
evil.bank.com(a same-site attacker). Crucially, a subdomain may set a cookie withDomain=bank.com, so the browser stores that cookie as scoped to the whole ofbank.com, not just toevil.bank.com— that is the legitimate relaxation domain cookies afford, and exactly the lever the attack pulls.
- The attacker logs in to
bank.comthemselves and receives a real, valid session ids_atkfor their own account.- From
evil.bank.comthe attacker runsSet-Cookie: SESSID=s_atk; domain=bank.com; path=/account/. Becausedomain=bank.com, the browser places this cookie inbank.com’s jar, withpath=/account/.- The victim logs in to
bank.comand getsSet-Cookie: SESSID=s_bob; path=/— the genuine session id, host-locked onbank.com.- The victim requests
GET /account/index.html. Both cookies match the URL, so the browser sends both name/value pairs:Cookie: SESSID=s_atk; SESSID=s_bob. It puts the more-specific-pathcookie (/account/, the planted one) first.- The server, seeing two
SESSIDvalues and the application convention of taking the first, authenticates the request ass_atk— i.e. as the attacker. The page printsWelcome, Attacker!.The victim is now operating inside the attacker’s session, so any activity the victim performs on
/account/(pages viewed, searches, data entered believing it is their own session) is visible to the attacker, who knowss_atkbecause it is theirs. This is the “session swapping” the slide name refers to — the victim is swapped into the attacker’s session, not the other way round.The whole trick lives in the jar’s ordering rule, not in anything the server does wrong: two same-named cookies both legitimately match the request, and the browser’s most-specific-
path-first rule happens to put the subdomain-planted one on top. A [[Knowledge/Cookie Prefix|__Host-prefix]] removes the lever by forbidding theDomainattribute and pinningPath=/, so a subdomain simply cannot plant a host-locked cookie to compete with the genuine one.