The lab
here is how portswigger describes it
To solve the lab, find the API key for the user carlos. You can log in to your own account using the following credentials:
wiener:peter.
the new pieces in this lab#
since this is our first web cache deception lab, a short introduction, and a contrast with the cache poisoning we did earlier.
- web cache deception (WCD) tricks a cache into storing a victim's private, personalised response so the attacker can retrieve it afterwards. this is the mirror image of cache poisoning: poisoning puts a malicious response into the cache to serve to victims; deception gets a victim's sensitive response into the cache to serve to the attacker.
- the engine of WCD is a discrepancy between how the cache decides what to store and how the origin server decides what to return. caches usually store static assets, and often decide "is this cacheable?" from the URL, e.g. anything ending in
.js,.css,.pngis a static file, cache it. the origin server, meanwhile, may map the URL differently and still return dynamic, personal content. - a cache rule here fires on static extensions: the cache sees a path ending in
.jsand caches the response for a while (Cache-Control: max-age=30). the headers that tell us what's happening:X-Cache: miss, this response was generated fresh, not served from cache.X-Cache: hit, this response came from the cache.Cache-Control: max-age=30, if cached, keep it for 30 seconds.
- a path-mapping discrepancy: the origin server abstracts the path, it reads
/my-account/abc.jsas just/my-accountand returns our account page (with the API key), ignoring the extra segment and extension. the cache, however, takes the URL literally and sees a.jsstatic file. so the origin serves private data while the cache thinks it's caching a harmless script. the method: we confirm the origin ignores extra path segments (so/my-account/abc.jsstill returns the account page), confirm the cache stores.jsresponses, then make the victim carlos visit a.jsURL under his account, his personalised response (with his API key) gets cached, and we read it from the cache.
why the path-mapping discrepancy leaks the API key#
the flaw is that the cache and the origin server disagree about what the URL /my-account/abc.js is. to the origin it's the account page, it strips the trailing segment/extension down to /my-account and returns the logged-in user's private data. to the cache it's a static JavaScript file, matching a rule that says "cache anything ending in .js". neither component is individually wrong by its own logic, but together they cache a response that is dynamic and per-user as if it were a public static asset. so when carlos is lured to /my-account/wcd.js, the origin builds his account page (his session, his API key) and the cache stores it keyed on that URL, no authentication required to fetch it back. we then request the same URL and the cache hands us carlos's cached page. the secret leaks because the thing deciding "cacheable" and the thing deciding "what to return" never agree on what the request actually was.
Step 1 - confirm the API key is in the response#
we log in as wiener:peter and notice the response contains our API key, so /my-account returns sensitive, per-user data.

Step 2 - find the path-mapping discrepancy#
in Proxy > HTTP history we send the GET /my-account request to Repeater. we add an arbitrary segment to the path, e.g. /my-account/abc, and send it. we still receive a response containing our API key, which means the origin server abstracts the path back to /my-account and ignores the extra segment.

Step 3 - add a static extension#
we add a static extension to the path, e.g. /my-account/abc.js, and send it.

Step 4 - confirm the cache rule on .js#
the response carries X-Cache: miss and Cache-Control: max-age=30, not served from cache, but cacheable for 30 seconds. we resend the same request within 30 seconds, and X-Cache changes to hit, the response was served from the cache. so the cache treats /my-account/abc.js as a cacheable .js static file, while the origin still returned our account page. that's our exploit primitive.

Step 5 - craft the exploit#
we go to the exploit server and, in the Body, craft a redirect that sends carlos to a malicious .js URL under his account. we change the arbitrary segment (to wcd) so the victim's response isn't confused with our own earlier cached entry:
<script>document.location="https://YOUR-LAB-ID.web-security-academy.net/my-account/wcd.js"</script>

Step 6 - deliver and retrieve from the cache#
we click Deliver exploit to victim. when carlos's browser follows the redirect, the origin builds his account page (with his API key) and the cache stores it keyed on /my-account/wcd.js. we then visit the same URL ourselves:
https://YOUR-LAB-ID.web-security-academy.net/my-account/wcd.js
the cached response contains carlos's API key. we copy it.

Step 7 - submit the key#
we submit carlos's API key to solve the lab.

with this, the lab is solved!
