Cookie Chaos: How to bypass Host and Secure cookie prefixes
Browsers enforce cookie prefixes like __Host- to prevent attackers from setting cookies from subdomains or insecure origins. The researcher found that by prefixing a cookie name with a Unicode whitespace character, the browser treats it as an ordinary cookie, but some server frameworks (Django, ASP.NET) trim the whitespace and interpret it as a __Host- cookie, allowing an attacker to overwrite protected cookies and potentially achieve XSS or session fixation.
portswigger.net
Published on portswigger.net
Browsers added cookie prefixes to protect your sessions and stop attackers from setting harmful cookies. In this post, you’ll see how to bypass cookie defenses using discrepancies in browser and server logic.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a parsing discrepancy: browsers and server-side frameworks handle Unicode whitespace and legacy cookie syntax differently, so a cookie that the browser considers unrestricted is normalised by the server into a protected name.
What the attacker needed
Attacker can set cookies from a subdomain or insecure origin; server framework trims Unicode whitespace or parses legacy…; application reflects cookie value without escaping (for XSS)
The fix
The text does not state a fix; Django declined to treat it as a vulnerability, citing existing documentation warning against permitting cookies from untrusted subdomains, so the implied fix is to avoid accepting cookies from untrusted subdomains and to normalise cookie names consistently.
How it was found
- Find an XSS vulnerability where a cookie value is reflected without escaping.
- Use JavaScript to set a cookie whose name begins with a Unicode whitespace character followed by __Host-name, with Domain set to the target domain.
- The browser accepts the cookie as non-prefixed and sends it alongside the original cookie.
- The server trims the whitespace and treats the attacker-controlled cookie as __Host-name, often preferring the last value.
- The reflected cookie value executes as script, or the forged cookie is used for CSRF or session fixation.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
Cookies are small pieces of data websites store in your browser to remember you. Browsers have special rules for cookies whose names start with __Host- or __Secure- to stop bad guys from creating fake copies. This research shows that if you put an invisible space character before the name, the browser doesn't apply those rules, but some server software later removes the space and treats it as the protected cookie, letting an attacker replace your session cookie.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.