Web cache poisoning
PortSwigger Web Security Academy · Expert
- #Web cache poisoning

Web Cache Poisoning to Exploit a DOM Vulnerability via a Cache with Strict Cacheability Criteria
The vulnerability
The home page loads a JSON file from the host in the X-Forwarded-Host header and writes the content into the page. The header isn’t part of the cache key. If you set it to your server, everyone who gets the cached page loads your file.
Fix: Ignore the header, or make it part of the cache key. Don’t write JSON into the page as HTML.
The lab
This lab contains a DOM-based vulnerability that can be exploited as part of a web cache poisoning attack. A user visits the home page roughly once a minute. Note that the cache used by this lab has stricter criteria for deciding which responses are cacheable, so you will need to study the cache behavior closely.
To solve the lab, poison the cache with a response that executes
alert(document.cookie)in the visitor's browser.
Solution
1. Look at Cache Behavior and Find a Hidden Header: Visit the homepage and examine the GET / request in Burp Proxy's HTTP history. The X-Cache: hit response header confirms that the page is cached. Send the request to Repeater. Use a tool like Param Miner to find hidden headers and discover that X-Forwarded-Host is an unkeyed header.
2. Create a Malicious JSON File: Go to the Exploit Server. Create a file at the path /resources/json/geolocate.json with the following content. This payload contains a DOM-based XSS vulnerability. Add the Access-Control-Allow-Origin: * header to enable CORS. Store the file.
{
"country": "<img src=1 onerror=alert(document.cookie) />"
}
3. Poison the Cache: In Repeater, add a cache buster to the URL (e.g., /?cachebuster=1) and the X-Forwarded-Host header pointing to your Exploit Server. Send the request twice. The X-Cache: hit response header on the second request confirms that the cache has been poisoned.
4. Wait for the Victim: Remove the cache buster and wait for the victim to visit the homepage. When the victim’s browser loads the poisoned page, it will fetch the malicious JSON file from your Exploit Server, and the XSS payload will execute, and the lab is solved.
Combining Web Cache Poisoning Vulnerabilities
The vulnerability
The translations are also loaded from the host in X-Forwarded-Host, but the English page doesn’t need any. So you poison the Spanish page first, and then use X-Original-URL to send everyone there.
Fix: Ignore headers the application doesn’t need, or make them part of the cache key.
The lab
This lab is susceptible to web cache poisoning, but only if you construct a complex exploit chain.
A user visits the home page roughly once a minute and their language is set to English. To solve this lab, poison the cache with a response that executes
alert(document.cookie)in the visitor's browser.
Solution
1. Look at Cache Behavior and Find Hidden Headers: Visit the homepage and examine the GET / request in Burp Proxy's HTTP history. The X-Cache: hit response header confirms that the page is cached. Send the request to Repeater. Use a tool like Param Miner to find hidden headers and discover that X-Forwarded-Host and X-Original-Url are unkeyed.
2. Create a Malicious JSON File: Go to the Exploit Server. Create a file at the path /resources/json/translations.json with the following content. This payload contains a DOM-based XSS vulnerability. Add the Access-Control-Allow-Origin: * header to enable CORS. Store the file.
{
"en": {
"name": "English"
},
"es": {
"name": "español",
"translations": {
"Return to list": "Volver a la lista",
"View details": "</a><img src=1 onerror='alert(document.cookie)' />",
"Description:": "Descripción"
}
}
}
3. Exploit X-Forwarded-Host and X-Original-Url: In Repeater, first poison the cache for the Spanish version of the website.
- Send the
GETrequest with a cache buster (/?cb=1) and theX-Forwarded-Hostheader pointing to your Exploit Server. Send the request twice to get anX-Cache: hit. - Now, poison the cache for the English version. Send the
GETrequest (with a cache buster) and theX-Original-Urlheader with the value/setlang\es. This redirects the victim to the Spanish version of the website. Send the request twice to get anX-Cache: hit.
4. Wait for the Victim: Remove the cache busters and wait for the victim to visit the homepage. When the victim’s browser loads the poisoned page, it will be redirected to the Spanish version of the website and fetch the malicious JSON file from your Exploit Server. The XSS payload will then execute, and the lab is solved.
Cache Key Injection
The vulnerability
utm_content isn’t part of the cache key, and the Origin header ends up in the response headers. If you build a request with the same cache key as a normal one, the victim gets your JavaScript.
Fix: Encode the values before they go into the cache key, and don’t copy input into response headers.
The lab
This lab contains multiple independent vulnerabilities, including cache key injection. A user regularly visits this site’s home page using Chrome.
To solve the lab, combine the vulnerabilities to execute
alert(1)in the victim's browser. Note that you will need to make use of thePragma: x-get-cache-keyheader in order to solve this lab.
Solution
1. Look at Cache Behavior and Find Unkeyed Parameters: Send the GET /login?lang=en request to Repeater. Use a tool like Param Miner to find hidden parameters and discover that utm_content is an unkeyed parameter.
2. Create a Malicious Payload: The vulnerability allows an attacker to inject a payload into the response via a response header injection. The payload will be alert(1).
3. Inject the Payload: Send the GET /js/localize.js?lang=en?utm_content=clawhak&cors=1&x=1 request to Repeater. Add the Origin header with a malicious payload: Origin: x%0d%0aContent-Length:%208%0d%0a%0d%0aalert(1)$$$$. Send the request twice.
4. Poison the Cache: In a second tab in Repeater, send the GET /login?lang=en?utm_content=x%26cors=1%26x=1$$origin=x%250d%250aContent-Length:%208%250d%250a%250d%250aalert(1)$$%23 request. Send the request until the X-Cache header is set to hit.
5. Wait for the Victim: Remove the cache buster and wait for the victim to visit the homepage. When the victim’s browser loads the poisoned page, the alert(1) payload will execute, and the lab is solved.
Internal Cache Poisoning
The vulnerability
There is a second cache inside the application that stores parts of the page. One of them contains the host from X-Forwarded-Host. Once your server is in there, every visitor loads your JavaScript, even without the header.
Fix: Ignore the header, or make it part of the cache key in both caches.
The lab
This lab is vulnerable to web cache poisoning. It uses multiple layers of caching. A user regularly visits this site’s home page using Chrome.
To solve the lab, poison the internal cache so that the home page executes
alert(document.cookie)in the victim's browser.
Solution
1. Look at Cache Behavior and Find Unkeyed Headers: Send the GET / request to Repeater. Use a tool like Param Miner to find hidden headers and discover that X-Forwarded-Host is an unkeyed parameter.
2. Create a Malicious JavaScript File: Go to the Exploit Server. Create a file called /js/geolocate.js and add the payload alert(document.cookie) to the body. Store the file.
3. Poison the Internal Cache: In Repeater, add the X-Forwarded-Host header with the value of your Exploit Server. Send the request. The website will search for the JavaScript file on your Exploit Server and cache it.
4. Wait for the Victim: Remove the X-Forwarded-Host header and send the request. The website will now load the malicious JavaScript file from the internal cache. Wait for the victim to visit the homepage, and the alert(document.cookie) payload will execute, and the lab is solved.