Cache key injection

9 min read Insane Portswigger
Web cache poisoning
Contents

On this page

The lab

here is how portswigger describes it

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 the Pragma: x-get-cache-key header in order to solve this lab.

this is the hardest lab in the poisoning track, and unlike the others it is not one bug with a twist, it is four separate bugs that are each useless alone and only lethal when stacked in the right order. we are going to poison a cached javascript file so that its body becomes alert(1), and separately poison the login page so that it imports that exact poisoned javascript. getting the two cache entries to line up is the hard part, and the tool that makes it possible is the Pragma: x-get-cache-key header, which lets us see the cache key the server computes and therefore lets us forge it.

before the walkthrough it is worth slowing right down, because this lab only makes sense once you understand what each of the four bugs actually does.

the four vulnerabilities, one at a time#

1. Flawed cache-key regex (unkeyed content on lang)#

the cache is supposed to exclude the utm_content parameter from the cache key, same idea as the unkeyed-query-parameter lab. but it does it with a sloppy regex, and the regex is loose enough that we can smuggle extra content into the key-excluded region by writing it as part of the lang parameter:

/login?lang=en?utm_content=anything

notice the second ? instead of &. the cache's regex sees utm_content and strips everything around it from the key, so anything we append here is unkeyed, invisible to the cache key, while the application still reads it. that gives us a place to hide payload bytes that do not change which cache entry we land on.

2. client-side parameter pollution via lang#

the login page imports a script, /js/localize.js, and the client-side code builds the url for that import using the lang value, without url-encoding it. that is client-side parameter pollution, if we put url metacharacters inside lang, they are taken literally when the fetch url is assembled. so by sneaking an encoded & into lang we can bolt extra query parameters onto the internal request the page makes to localize.js, for example forcing cors=1 and other parameters onto it even though the victim never typed them.

3. response header injection via the Origin header#

/js/localize.js has a second flaw. when the cors parameter is set to 1, it reflects the incoming Origin request header into the response headers (to build a CORS Access-Control-Allow-Origin). it does not strip carriage-return / line-feed characters from Origin, so a CRLF in Origin lets us inject our own response headers and even a response body. in http, \r\n (%0d%0a) ends a header line, and two of them in a row end the header block and start the body. so an Origin like:

x\r\nContent-Length: 8\r\n\r\nalert(1)

makes the server emit a response whose body the browser reads as exactly 8 bytes, alert(1). since localize.js is loaded as a script, that body runs as javascript. alert(1) is 8 characters, which is why Content-Length: 8 is chosen, it tells the browser to treat only those 8 bytes as the script and ignore the real file.

4. cache key injection (the glue)#

this is the one the lab is named after, and the Pragma: x-get-cache-key header is how we find it. sending that header makes the server tell us the cache key it computed for our request. doing that we discover that the Origin header is reflected into the cache key. that is huge, it means the cache key is not just method + url, it also contains attacker-influenced bytes, and if we can influence the key through a crafted url, we can make two different-looking requests resolve to the same cache entry. that is what lets the login-page poison (request 2) target the very same cached object as the direct localize.js poison (request 1).

how they chain: bug 3 lets us turn localize.js into alert(1). bug 4 lets us control the cache key for that poisoned object so we can reproduce it. bug 2 lets the login page be steered into importing localize.js with cors=1 and our crafted Origin. bug 1 lets us hide all of that crafted junk in the unkeyed region so the login page's own cache key still matches the plain login page the victim requests. poison both entries, hold them, and the victim loading the home page gets the login page that imports the poisoned script that runs alert(1).

Step 1 - Map the entry point#

on accessing the lab we are redirected to /login.

1

Step 2 - Confirm the flawed cache-key regex#

we test the regex flaw by appending unkeyed content to lang through a second ?utm_content=:

/login?lang=en?utm_content=anything

the response still serves from the same cache entry regardless of what we put after utm_content, confirming that region is excluded from the key. this is where our payload bytes will hide.

2

Step 3 - Confirm client-side parameter pollution in localize.js#

we observe that /login/ imports /js/localize.js, and that the import url is built from the lang parameter without url-encoding. so an injected & inside lang lets us pollute the internal request with extra parameters like cors=1.

3

Step 4 - Confirm response header injection via Origin#

we confirm that /js/localize.js, when cors=1, reflects the Origin request header into the response headers, and that it does not sanitize CRLF, so Origin can inject headers and a body.

Step 5 - confirm cache key injection with Pragma: x-get-cache-key#

we add the Pragma: x-get-cache-key header and the server returns the cache key it computed. we see the Origin value reflected inside that key, which proves cache key injection, the header injection can be reproduced through a crafted url because the key itself is attacker-influenced.

4

Step 6 - poison localize.js directly (request 1)#

now we combine everything. first we poison the script object itself so its body is alert(1):

http
GET /js/localize.js?lang=en?utm_content=z&cors=1&x=1 HTTP/2
Origin: x%0d%0aContent-Length:%208%0d%0a%0d%0aalert(1)$$$$

reading this request:

  • cors=1 switches on the Origin-reflection behaviour.
  • the Origin header carries the CRLF injection. %0d%0a is \r\n. so it reads x, then a new header Content-Length: 8, then a blank line (%0d%0a%0d%0a), then the body alert(1). the browser now sees a script whose 8-byte body is alert(1).
  • lang=en?utm_content=z uses the flawed regex so the extra bits do not disturb the key in the way we need.
  • the trailing $$$$ are the cache's own key delimiters, revealed by Pragma: x-get-cache-key. we use them to mark exactly where the injected bytes sit in the key, so that request 2 can rebuild the identical key and hit this same poisoned entry.
5

Step 7 - Poison the login page to import the poisoned script (request 2)#

with the script poisoned, we poison the login page so that it imports localize.js with the exact same crafted Origin, i.e. so its internal fetch resolves to the entry we just poisoned:

http
GET /login?lang=en?utm_content=x%26cors=1%26x=1$$origin=x%250d%250aContent-Length:%208%250d%250a%250d%250aalert(1)$$%23 HTTP/2

reading this request, from the outside in:

  • lang=en?utm_content=x triggers the flawed regex, so everything from utm_content onward is in the unkeyed region. that is critical, because all the ugly payload that follows must not change the login page's cache key, otherwise the victim's clean /login request would never match our poisoned entry.
  • %26 is &. so %26cors=1%26x=1 is the client-side parameter pollution from bug 2, it appends &cors=1&x=1 to the url that the page builds when it imports localize.js, switching on the CORS reflection on that internal request.
  • $$origin=...$$ is the cache key injection from bug 4, we are writing an origin directly into the cache key using the $$ delimiters, so the internal localize.js request's key matches request 1's key.
  • %250d%250a is double-encoded CRLF. %25 is %, so after one round of decoding %250d%250a becomes %0d%0a, and after the second it becomes the real \r\n. the value passes through two decode steps on its way to becoming the Origin header, so it has to start out double-encoded to arrive as genuine CRLF at the injection point.
  • %23 is #, a url fragment marker, which truncates everything after it so the rest of the real url is discarded and does not interfere.
6

Step 8 - hold both poisons and solve#

we replay both requests to keep both cache entries poisoned at once, the localize.js object whose body is alert(1), and the /login page that imports it with the matching key. the victim, who regularly loads the home page in Chrome, is served the poisoned login page, which imports the poisoned script, which runs alert(1) in their browser.

7

with this, the lab is solved!