How I Made $16,500 Hacking CDN Caching Servers — Part 2
A web cache poisoning vulnerability allowed an attacker to inject an XSS payload via the X-Forwarded-For header into a cached JavaScript file, which would then execute in victims' browsers. The attacker could poison the cache and send the URL to a victim to run arbitrary JavaScript.
infosecwriteups.com
Published on infosecwriteups.com
Bug Bounty
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.
Context
The target was a public bug bounty program. The researcher does not name the program or the product, and the host in the request is shown as Redacted. What the report does describe is a caching layer in front of an application: a URL that was not being cached could be made cacheable by appending a cacheable file extension such as .js or .css to the end of it. The response body contained a script that reflected values from the request, including the client IP address and cookie values.
The write-up is part two of a series on CDN cache poisoning. The bug it covers is an XSS hidden behind cache poisoning, delivered to visitors of a poisoned URL.
Setup
No accounts, roles, tools or versions are described. The work started from Google Dorking to find candidate URLs. The only precondition that matters is the caching behaviour: a URL without a cacheable extension was not cached, while the same URL with .js or .css appended was cached.
Finding it
While Google Dorking, the researcher found a particular URL. This time the response was not being cached, but appending a cacheable extension (.js, .css) to the end of the URL caused the response to be cached.
With a cacheable URL in hand, the next step was to find an XSS. An injection point was found in a cookie, but the WAF triggered when anything was added after %20:
Cookie: cookiename=xss</script%20
While trying to bypass the WAF, the researcher noticed that their own IP address was also reflected in the same script:
guid="</script ","[redacted]"
Because the IP was reflected, the X-Forwarded-For header became interesting: it lets the client supply the value that ends up in that position. That allowed closing the <script> tag without tripping the WAF, which triggered on <[anything]>.
Exploitation
Step 1: put the payload in X-Forwarded-For
The request carries three X-Forwarded-For headers. The first is a plain xss, the second holds the actual payload, and the third is a bare >.
GET /xxx/xx/xxx.xx/x.js?t=2021111121 HTTP/2 Host: RedactedX-Forwarded-For: xss X-Forwarded-For: xss><svg/onload=globalThis[`al`+/ert/.source]`1`// X-Forwarded-For: > Cookie: gdId=xss</script%20
The path ends in x.js with a query string t=2021111121, which is what makes the response cacheable. The second header closes the surrounding script context with > and injects an SVG element whose onload runs globalThis[al+/ert/.source]1``, that is, alert(1) assembled from string fragments so the literal word does not appear. The trailing // comments out whatever follows in the generated script. The cookie still carries the original xss</script%20 attempt.
Step 2: read the reflected response
The response shows the injected values landing inside the guid assignment:
...guid="</script ","[redacted]","xss","xss><svg/onload=globalThis[`al`+/ert/.source]`1`//,">...
The three X-Forwarded-For values appear in order after the IP, and the payload breaks out of the string and the script element.
Step 3: poison the cache and deliver the URL
Once the URL was poisoned with the XSS, the attacker only needed to send it to the victim:
redacted.com/xxx/xx/xxx.xx/x.js?t=2021111121
Because the response is cached, the poisoned URL can be sent to the victim. The researcher calls this a nice way to hide XSS.
Root cause
The application reflects the X-Forwarded-For header and the client IP into a script, so a client-controlled header can break out of the script context. The WAF blocked the obvious cookie-based attempt but did not stop the same payload arriving through a header. On top of that, the response for a URL ending in a cacheable extension is cached, so the poisoned response is stored. The report does not describe the cache key configuration or the encoding used beyond what is shown above.
Impact
An attacker can poison a URL with an XSS payload and send it to the victim. The researcher rates this as their favourite cache poisoning finding and notes it was found on a public program.
Fix
The timeline shows the issue was fixed on March 7, 2022.
Timeline
| Event | Date |
|---|---|
| Reported | December 11, 2021 |
| Triaged | December 14, 2021 |
| Bounty awarded | January 7, 2022 |
| Fixed | March 7, 2022 |
The bounty for this finding was $2,000. The report links to HackerOne report 1424094. A follow-up part covers cache poisoning DoS via the X-Forwarded-Scheme header.
Variants to test
- Append a cacheable extension (
.js,.css) to a non-cacheable URL and check whether the response is now stored by the cache. A positive result is the same body being returned on a second request without the origin being hit. - Send
X-Forwarded-Forwith a value containing><svg/onload=...>and look for it reflected unencoded inside a script. A positive result is the payload appearing verbatim in the response body. - Send multiple
X-Forwarded-Forheaders, as in the request above, and check whether the payload reaches the response while the WAF stays quiet. A positive result is the payload reaching the response while the WAF stays quiet. - Look for other client-controlled headers reflected into scripts, such as forwarding or host headers, and test whether they are cached. A positive result is a cached response containing attacker-controlled script.
- Check whether the cache key ignores the query string or parts of it. A positive result is a poisoned response served for a URL that differs from the one used to poison it.
What happened, in plain words
A security researcher found a way to make a website serve malicious JavaScript to anyone who visited a specially crafted link. The trick relied on two things: the site's caching system (which stores copies of pages to serve them faster) and a part of the site that echoed back information from the request without properly cleaning it. By combining these, the researcher could "poison" the cache so that the malicious script was stored and served from the cache. The researcher was paid $2,000 for this finding, which was part of a public bug bounty program.
Background: what is a CDN and what is caching?
A CDN (Content Delivery Network) is a network of servers placed around the world that store copies of website content. When you visit a website, your request might be handled by a nearby CDN server instead of the main origin server. This makes the site load faster and reduces load on the origin.
Caching is the process of storing a copy of a response so that future requests for the same thing can be served quickly from the cache. For example, when you load an image, your browser caches it so it doesn't have to download it again on the next page. Similarly, CDNs cache responses based on rules. Some URLs are marked as cacheable (like those ending in .js or .css), while others are not.
The setup
The researcher started by using Google Dorking, which is a technique of using advanced search operators in Google to find specific files or pages on a website. They were looking for URLs that might be vulnerable.
They found a URL that was not being cached by the CDN. However, they discovered that if they added a cacheable file extension like .js or .css to the end of the URL, the response would then be cached. This is important because it means the response could be cached.
Finding the XSS
XSS (Cross-Site Scripting) is a vulnerability where an attacker can inject malicious JavaScript into a web page viewed by other users. The script runs in the victim's browser and can steal data, perform actions on behalf of the user, or deface the site.
The researcher found an injection point in a cookie. Cookies are small pieces of data that websites store in your browser to remember information about you, like login sessions. The site was reflecting the cookie value into a script on the page, but a WAF (Web Application Firewall) was blocking attempts to inject certain characters. Specifically, the WAF triggered when anything was added after %20 (which is a URL-encoded space).
They tried this payload in a cookie:
Cookie: cookiename=xss</script%20
This line is an HTTP header. Cookie: is the header name, and cookiename=xss</script%20 is the value. It sets a cookie named cookiename to the value xss</script%20. The idea was to close a <script> tag (using </script) and then add a space (%20). But the WAF blocked it.
While trying to bypass the WAF, the researcher noticed that their own IP address was also being reflected in the same script. They saw something like:
guid="</script ","[redacted]"
This is a snippet of JavaScript code. guid is a variable name, and it's being assigned a string that contains </script and the IP address [redacted]. The IP address is the researcher's own public IP, which means the site is inserting the client's IP into the script. This is a common practice for analytics or logging.
Because the IP was reflected, the researcher realised they could control it by using the X-Forwarded-For header. This header is used by proxies to indicate the original IP address of the client. Many applications trust this header and use its value as the client's IP. So by setting X-Forwarded-For, the researcher could inject their own value into the script.
The WAF was triggering on <[anything]>, meaning it blocked any input that contained a < followed by something and then >. But by using the X-Forwarded-For header, the researcher could close the <script> tag and inject an SVG element with an onload event, which would execute JavaScript. The WAF did not block the payload sent through the header.
Exploitation
Step 1: put the payload in X-Forwarded-For
The researcher sent a request with three X-Forwarded-For headers. The first was a simple xss, the second contained the actual payload, and the third was just >.
GET /xxx/xx/xxx.xx/x.js?t=2021111121 HTTP/2 Host: RedactedX-Forwarded-For: xss X-Forwarded-For: xss><svg/onload=globalThis[`al`+/ert/.source]`1`// X-Forwarded-For: > Cookie: gdId=xss</script%20
Let's break this down line by line:
GET /xxx/xx/xxx.xx/x.js?t=2021111121 HTTP/2: This is the request line.GETis the HTTP method used to retrieve a resource. The path is/xxx/xx/xxx.xx/x.jswith a query stringt=2021111121. The path ends with.js, which makes it cacheable.HTTP/2is the protocol version.Host: Redacted: TheHostheader specifies the domain name of the server. Here it's redacted in the report.X-Forwarded-For: xss: The firstX-Forwarded-Forheader with valuexss.X-Forwarded-For: xss><svg/onload=globalThis[al+/ert/.source]1//: The secondX-Forwarded-Forheader. The value starts withxss>which closes the current HTML tag (the script tag) and then adds<svg/onload=...>. This is an SVG element with anonloadattribute that executes JavaScript when the SVG loads. The JavaScript isglobalThis[al+/ert/.source]1``. This is a way to writealert(1)without using the literal string "alert".globalThisrefers to the global object (likewindowin browsers).al+/ert/.sourceconcatenates the stringalwith the source of the regex/ert/, which isert, resulting inalert. Then it's called with argument1. The trailing//` comments out the rest of the line in the generated script.X-Forwarded-For: >: The thirdX-Forwarded-Forheader with just>. This might be to balance the angle brackets or to ensure the payload is parsed correctly.Cookie: gdId=xss</script%20: The cookie header, still containing the original attempt.
The request carries three X-Forwarded-For headers.
Step 2: read the reflected response
The server responded with the injected values inside the guid assignment:
...guid="</script ","[redacted]","xss","xss><svg/onload=globalThis[`al`+/ert/.source]`1`//,">...
This shows that the guid variable now contains the original IP, then the three X-Forwarded-For values in order. The payload breaks out of the string and the script element, so the SVG with onload will execute.
Step 3: poison the cache and deliver the URL
Once the URL was poisoned with the XSS, the attacker only needed to send it to the victim:
redacted.com/xxx/xx/xxx.xx/x.js?t=2021111121
Because the response is cached, the malicious script is stored and served from the cache. The victim doesn't need to send any special headers; they just need to visit the link. This is why it's called cache poisoning: the attacker poisons the cache with a malicious response.
The researcher calls this "a nice way to hide XSS".
Root cause
The application reflects the X-Forwarded-For header (and the client IP) into a script. Output encoding means converting special characters into safe representations so they can't break out of the context. Here, the lack of encoding allows an attacker to inject arbitrary JavaScript. The WAF blocked the cookie-based attempt but didn't stop the same payload via a header. Additionally, the cache stores the response for a URL ending in a cacheable extension, so the poisoned response is cached.
Impact
An attacker can poison a URL so that it serves JavaScript of their choosing to any user who opens it. This means the attacker's JavaScript runs when a victim opens the poisoned URL. The researcher rates this as their favourite cache poisoning finding and notes it was found on a public program. The report does not state which users or data were affected beyond that.
Fix
The report does not describe the fix that was applied. The timeline shows the issue was fixed on March 7, 2022.
Timeline
| Event | Date |
|---|---|
| Reported | December 11, 2021 |
| Triaged | December 14, 2021 |
| Bounty awarded | January 7, 2022 |
| Fixed | March 7, 2022 |
The bounty for this finding was $2,000. The report links to HackerOne report 1424094. A follow-up part covers cache poisoning DoS via the X-Forwarded-Scheme header.
Variants to test
- Append a cacheable extension (
.js,.css) to a non-cacheable URL and check whether the response is now stored by the cache. A positive result is the same body being returned on a second request without the origin being hit. - Send
X-Forwarded-Forwith a value containing><svg/onload=...>and look for it reflected unencoded inside a script. A positive result is the payload appearing verbatim in the response body. - Send multiple
X-Forwarded-Forheaders, as in the request above, and check whether the payload reaches the response while the WAF stays quiet. A positive result is the payload reaching the response while the WAF stays quiet. - Look for other client-controlled headers reflected into scripts, such as forwarding or host headers, and test whether they are cached. A positive result is a cached response containing attacker-controlled script.
- Check whether the cache key ignores the query string or parts of it. A positive result is a poisoned response served for a URL that differs from the one used to poison it.
Glossary
- CDN (Content Delivery Network): A system of distributed servers that deliver web content to users based on their geographic location, improving speed and reliability.
- Caching: The process of storing copies of data so that future requests can be served faster.
- Cacheable extension: A file extension (like
.jsor.css) that is typically cached by CDNs because such files are static and rarely change. - Google Dorking: Using advanced search operators in Google to find specific information or files on websites.
- XSS (Cross-Site Scripting): A vulnerability that allows an attacker to inject malicious scripts into web pages viewed by other users.
- WAF (Web Application Firewall): A security system that monitors and blocks malicious HTTP traffic based on predefined rules.
- Cookie: A small piece of data stored by a website in the user's browser to remember information such as login state or preferences.
- HTTP header: Additional information sent with an HTTP request or response, such as
CookieorX-Forwarded-For. - X-Forwarded-For: An HTTP header used to identify the originating IP address of a client connecting through a proxy or load balancer.
- IP address: A unique number assigned to each device on a network, used to identify and locate it.
- SVG (Scalable Vector Graphics): An XML-based format for vector images that can include interactive elements and scripts.
- onload event: An event that fires when an object (like an image or SVG) has finished loading.
- globalThis: A global object in JavaScript that refers to the global scope (like
windowin browsers). - Regex (Regular Expression): A pattern used to match character combinations in strings.
- Output encoding: The process of converting special characters into a safe format to prevent them from being interpreted as code.
- Cache poisoning: An attack where an attacker injects malicious content into a cache so that it is served to other users.
- DoS (Denial of Service): An attack that aims to make a service unavailable by overwhelming it with traffic or exploiting vulnerabilities.
- X-Forwarded-Scheme: An HTTP header that indicates the protocol (HTTP or HTTPS) used by the client, often used by proxies.