01 //Introduction to Web Cache Poisoning
Web cache poisoning is an attack where an adversary manipulates a web cache into serving malicious content to unsuspecting users. The attacker exploits the gap between what a cache uses as a key (typically the URL and a subset of headers) and what the backend uses to build the response. If the backend incorporates unkeyed inputs — headers, cookies, or query parameters that the cache ignores — the attacker can inject a payload that gets stored in the cache and served to every subsequent visitor.
In this guide, you'll learn how caches decide what to store and when, how attackers discover unkeyed inputs, the difference between cache poisoning and cache deception, how to review code for vulnerable patterns, and how to defend your application at every layer — from backend code to CDN configuration.
Inject payload into shared cache entry
Trick cache into storing victim's private data
Exploit unkeyed inputs in cache key
What makes web cache poisoning different from most other web attacks?
02 //Real-World Scenario
The Scenario: You're reviewing a web application served behind Cloudflare. The app uses a reverse proxy (Nginx) and includes dynamic elements in its HTML based on request headers. The marketing team recently added A/B testing that reads the X-Forwarded-Host header to construct asset URLs.
1// Express middleware that sets asset URLs based on X-Forwarded-Host
2app.use((req, res, next) => {
3 // ❌ Trusts X-Forwarded-Host without validation
4 // This header is NOT part of the cache key
5 const host = req.headers['x-forwarded-host'] || req.headers.host;
6 res.locals.assetHost = `https://${host}/assets`;
7 next();
8});
9
10app.get('/', (req, res) => {
11 // ❌ Unkeyed header value is embedded directly in the HTML
12 res.send(`
13 <!DOCTYPE html>
14 <html>
15 <head>
16 <link rel="stylesheet" href="${res.locals.assetHost}/main.css">
17 <script src="${res.locals.assetHost}/app.js"></script>
18 </head>
19 <body>
20 <h1>Welcome to our site</h1>
21 </body>
22 </html>
23 `);
24});
25
26// Nginx cache config (upstream):
27// proxy_cache_key "$scheme$request_method$host$request_uri";
28// ❌ Cache key does NOT include X-Forwarded-Host
29// The poisoned response will be served to ALL usersThe Attack: The attacker sends a request with X-Forwarded-Host: evil.com. The backend generates HTML with <script src="https://evil.com/assets/app.js">. The cache stores this response keyed on the URL alone. Now every visitor to the homepage loads JavaScript from the attacker's server.
In the scenario above, why does the X-Forwarded-Host header enable cache poisoning?
03 //Cache Poisoning Mechanics
To understand cache poisoning, you need to understand how caches work. A cache stores responses indexed by a cache key. When a new request arrives, the cache computes the key and checks for a match. If found (cache hit), the stored response is returned without contacting the origin. If not (cache miss), the request is forwarded to the origin, and the response may be stored for future requests.
Cache Key Components
| Component | Typically Keyed? | Risk if Unkeyed |
|---|---|---|
| URL path | Yes | Low — always part of the key |
| Query string | Usually yes | Medium — some CDNs strip or ignore query parameters |
| Host header | Yes | Low — standard part of the key |
| X-Forwarded-Host | No | Critical — often reflected in responses for asset URLs, redirects, canonical tags |
| X-Forwarded-Scheme / X-Forwarded-Proto | No | High — can force HTTP→HTTPS redirect loops or inject mixed content |
| X-Original-URL / X-Rewrite-URL | No | Critical — can override the URL parsed by the backend while the cache keys on the original URL |
| Accept-Language | Varies | Medium — if response varies by language but cache ignores this header |
| Cookie | Usually no | High — if response varies by auth state but cache ignores cookies |
| User-Agent | Sometimes | Medium — mobile vs desktop response variations |
1# What the cache sees (the key):
2Cache Key = GET + https + example.com + /page
3
4# What the backend sees (the full request):
5GET /page HTTP/1.1
6Host: example.com
7X-Forwarded-Host: evil.com ← NOT in cache key, BUT used in response
8X-Forwarded-Proto: http ← NOT in cache key, BUT triggers redirect
9Cookie: session=abc123 ← NOT in cache key, BUT changes response content
10Accept-Language: en-US ← NOT in cache key, BUT selects response language
11
12# The gap between these two views IS the attack surface1# settings.py
2SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
3USE_X_FORWARDED_HOST = True # ❌ Trusts X-Forwarded-Host from any source
4
5# views.py
6def homepage(request):
7 # ❌ X-Forwarded-Host controls the base URL for all assets
8 # Django's request.build_absolute_uri() uses it internally
9 context = {
10 'canonical_url': request.build_absolute_uri(),
11 'og_url': request.build_absolute_uri(),
12 'base_url': f"https://{request.get_host()}",
13 }
14 return render(request, 'home.html', context)
15
16# home.html template
17# <link rel="canonical" href="{{ canonical_url }}">
18# <meta property="og:url" content="{{ og_url }}">
19# <script src="{{ base_url }}/static/app.js"></script>
20#
21# If X-Forwarded-Host: evil.com is sent:
22# canonical_url = https://evil.com/
23# og_url = https://evil.com/
24# base_url = https://evil.com
25# script src = https://evil.com/static/app.js ← XSS via cache poisoningA CDN caches responses using the key: scheme + host + path + query string. The backend reads the Accept-Language header to serve localized content. A German-speaking attacker poisons the cache. What happens?
04 //Unkeyed Input Exploitation
The core technique in cache poisoning is finding unkeyed inputs — request components that influence the response but are not part of the cache key. Attackers use tools like Param Miner (a Burp Suite extension) to systematically discover these inputs by sending requests with unique canary values in various headers and observing whether those values appear in the cached response.
1// --- Pattern 1: X-Forwarded-Host in link/script tags ---
2app.get('/page', (req, res) => {
3 // ❌ X-Forwarded-Host is unkeyed but controls script source
4 const host = req.headers['x-forwarded-host'] || 'cdn.example.com';
5 res.send(`<script src="https://${host}/bundle.js"></script>`);
6});
7
8// --- Pattern 2: X-Forwarded-Scheme forcing redirect ---
9app.use((req, res, next) => {
10 // ❌ Unkeyed header triggers a redirect that gets cached
11 const proto = req.headers['x-forwarded-proto'] || req.headers['x-forwarded-scheme'];
12 if (proto === 'http') {
13 // Cache stores a 301 redirect — all users get redirected
14 return res.redirect(301, `https://${req.headers.host}${req.url}`);
15 }
16 next();
17});
18
19// --- Pattern 3: X-Original-URL path override ---
20// Some frameworks (e.g., older Symfony, IIS) support this header
21app.use((req, res, next) => {
22 // ❌ X-Original-URL overrides the parsed path
23 // Cache keys on /public-page but backend serves /admin/secret
24 if (req.headers['x-original-url']) {
25 req.url = req.headers['x-original-url'];
26 }
27 next();
28});
29
30// --- Pattern 4: Unkeyed query parameter ---
31// Some CDNs exclude certain query params from the cache key
32app.get('/search', (req, res) => {
33 // CDN config: cache key ignores "utm_*" and "cb" parameters
34 // ❌ But the app reflects "cb" (cache buster) in the response
35 const cb = req.query.cb || '';
36 res.send(`<script>var cacheBust = "${cb}";</script>`);
37 // Attacker: /search?cb=";alert(document.cookie)//
38 // CDN caches this for /search (cb is excluded from key)
39});1import { URL } from 'url';
2
3const ALLOWED_HOSTS = new Set([
4 'www.example.com',
5 'cdn.example.com',
6 'static.example.com',
7]);
8
9// ✅ Validate X-Forwarded-Host against an allow-list
10function getValidatedHost(req) {
11 const forwardedHost = req.headers['x-forwarded-host'];
12 if (forwardedHost && ALLOWED_HOSTS.has(forwardedHost)) {
13 return forwardedHost;
14 }
15 return 'www.example.com';
16}
17
18// ✅ Never reflect unvalidated headers in responses
19app.get('/page', (req, res) => {
20 const host = getValidatedHost(req);
21 res.send(`<script src="https://${host}/bundle.js"></script>`);
22});
23
24// ✅ Use Vary header to tell caches about response-influencing headers
25app.get('/localized', (req, res) => {
26 const lang = req.headers['accept-language']?.split(',')[0] || 'en';
27 res.set('Vary', 'Accept-Language');
28 res.send(getLocalizedContent(lang));
29});
30
31// ✅ Reject unexpected override headers entirely
32app.use((req, res, next) => {
33 const dangerousHeaders = [
34 'x-original-url',
35 'x-rewrite-url',
36 'x-forwarded-prefix',
37 ];
38 for (const header of dangerousHeaders) {
39 if (req.headers[header]) {
40 delete req.headers[header];
41 }
42 }
43 next();
44});An application sets the Vary: Accept-Language header on its responses. How does this help prevent cache poisoning via the Accept-Language header?
05 //Web Cache Deception
Web cache deception is the mirror image of cache poisoning. In poisoning, the attacker poisons the cache so all users get malicious content. In deception, the attacker tricks the cache into storing a victim's private response, then retrieves it themselves. The attacker doesn't inject a payload — they trick the victim into generating a cacheable response that contains their private data.
Cache Poisoning vs. Cache Deception
| Property | Cache Poisoning | Cache Deception |
|---|---|---|
| Attacker goal | Serve malicious content to all users | Steal a specific victim's private data |
| Who is affected? | All users who request the cached URL | The attacker (who retrieves the cached private data) |
| Attack direction | Attacker → Cache → All victims | Victim → Cache → Attacker |
| Payload location | In the request (unkeyed header) | In the response (victim's session data) |
| Cache behavior exploited | Cache stores attacker-influenced response | Cache stores private response as if it were public |
| Typical prerequisite | Unkeyed input reflected in response | Path confusion between origin and cache (e.g., path normalization differences) |
1# Step 1: Attacker crafts a URL that tricks the cache
2# The origin server serves /account (private, requires auth)
3# But the cache sees the .css extension and treats it as static/cacheable
4
5# Attacker sends this link to the victim (e.g., via email, chat):
6https://example.com/account/anything.css
7
8# Step 2: Victim clicks the link (they are logged in)
9# Origin: path normalization ignores "/anything.css" → serves /account
10# Cache: sees .css extension → caches the response as static content
11#
12# Origin response (private data):
13# HTTP/1.1 200 OK
14# Content-Type: text/html
15# <h1>Welcome, Alice</h1>
16# <p>Email: alice@company.com</p>
17# <p>API Key: sk-abc123...</p>
18#
19# Cache stores this keyed on: /account/anything.css
20
21# Step 3: Attacker requests the same URL (unauthenticated)
22GET /account/anything.css
23# Cache hit! Returns Alice's private account page
24# Attacker now has Alice's email, API key, session tokens, etc.1// ✅ Defense 1: Always set Cache-Control on private responses
2app.get('/account', requireAuth, (req, res) => {
3 res.set({
4 'Cache-Control': 'no-store, no-cache, must-revalidate, private',
5 'Pragma': 'no-cache',
6 'Vary': 'Cookie',
7 });
8 res.send(renderAccountPage(req.user));
9});
10
11// ✅ Defense 2: Strict path matching — reject unexpected path suffixes
12app.get('/account', requireAuth, (req, res) => {
13 // Only serve if the path is exactly /account — no trailing segments
14 if (req.path !== '/account' && req.path !== '/account/') {
15 return res.status(404).send('Not Found');
16 }
17 res.send(renderAccountPage(req.user));
18});
19
20// ✅ Defense 3: CDN configuration — only cache specific file types
21// Cloudflare Page Rule / Cache Rule example:
22// Match: *.example.com/assets/*
23// Cache Level: Cache Everything
24// Edge TTL: 1 day
25//
26// Everything else: default behavior (respect Cache-Control from origin)
27//
28// ❌ Do NOT use: "Cache Everything" on wildcard paths
29// ❌ Do NOT cache based on file extension alone
30
31// ✅ Defense 4: Validate Content-Type matches the extension
32app.use((req, res, next) => {
33 const originalSend = res.send.bind(res);
34 res.send = (body) => {
35 const ext = req.path.split('.').pop()?.toLowerCase();
36 const contentType = res.get('Content-Type') || '';
37
38 const extensionTypeMap = {
39 css: 'text/css',
40 js: 'application/javascript',
41 json: 'application/json',
42 png: 'image/png',
43 jpg: 'image/jpeg',
44 };
45
46 if (ext && ext in extensionTypeMap) {
47 if (!contentType.includes(extensionTypeMap[ext])) {
48 res.status(404);
49 return originalSend('Not Found');
50 }
51 }
52 return originalSend(body);
53 };
54 next();
55});An attacker sends a victim a link to https://app.com/account/profile.css. The origin serves the /account page (ignoring the suffix), and the CDN caches it because of the .css extension. What type of attack is this?
06 //Vulnerable Patterns in Code
During code review, look for patterns where request headers, cookies, or other inputs that are not part of the cache key influence the response. These are the injection points for cache poisoning.
1// --- Next.js: headers() in cached pages ---
2// In Next.js App Router, pages can be statically cached
3// but still read headers at request time
4
5// app/page.tsx
6import { headers } from 'next/headers';
7
8export default async function Page() {
9 const headersList = await headers();
10 // ❌ Reading unkeyed header in a potentially cached page
11 const host = headersList.get('x-forwarded-host') || 'example.com';
12 const theme = headersList.get('x-theme') || 'light';
13
14 return (
15 <html data-theme={theme}>
16 <head>
17 {/* ❌ Unkeyed header reflected in a link tag */}
18 <link rel="canonical" href={`https://${host}/`} />
19 </head>
20 <body>
21 <script src={`https://${host}/analytics.js`} />
22 </body>
23 </html>
24 );
25}
26
27// --- Ruby on Rails: request.host in cached views ---
28# ❌ Rails uses X-Forwarded-Host to determine request.host
29# config/environments/production.rb
30# config.action_dispatch.trusted_proxies = :all # ❌ Trusts all proxies
31#
32# app/views/layouts/application.html.erb
33# <script src="<%= "https://#{request.host}/packs/app.js" %>"></script>
34#
35# If the view is fragment-cached or page-cached, the host value
36# from the first request is served to all subsequent users1// --- Pattern: Cached error pages with reflected input ---
2app.use((err, req, res, next) => {
3 // ❌ Error page reflects the requested URL
4 // If error pages are cached (common with CDN "always online" features)
5 res.status(500).send(`
6 <h1>Something went wrong</h1>
7 <p>Error processing: ${req.url}</p>
8 <p>Host: ${req.headers.host}</p>
9 `);
10});
11
12// --- Pattern: Open redirect that gets cached ---
13app.get('/redirect', (req, res) => {
14 const target = req.query.url || '/';
15 // ❌ If the CDN caches 301/302 redirects, this redirect is served
16 // to all users — even if the CDN strips the query string from the key
17 res.redirect(301, target);
18});
19
20// --- Pattern: JSONP callback in cached API response ---
21app.get('/api/data', (req, res) => {
22 const callback = req.query.callback;
23 const data = JSON.stringify({ users: 100, revenue: 50000 });
24
25 if (callback) {
26 // ❌ If CDN caches /api/data without the callback param in the key
27 res.type('application/javascript');
28 res.send(`${callback}(${data})`);
29 // Attacker: /api/data?callback=stealData
30 // Cached JSONP response calls attacker's function
31 } else {
32 res.json(JSON.parse(data));
33 }
34});1// ✅ Pattern 1: Hardcode the canonical host — never trust headers
2const CANONICAL_HOST = process.env.CANONICAL_HOST || 'www.example.com';
3const CDN_HOST = process.env.CDN_HOST || 'cdn.example.com';
4
5app.get('/page', (req, res) => {
6 res.send(`
7 <link rel="canonical" href="https://${CANONICAL_HOST}/page">
8 <script src="https://${CDN_HOST}/bundle.js"></script>
9 `);
10});
11
12// ✅ Pattern 2: Separate dynamic and static cache rules
13// Cache-Control for static assets (safe to cache)
14app.use('/assets', express.static('public', {
15 maxAge: '1y',
16 immutable: true,
17 setHeaders: (res) => {
18 res.set('Cache-Control', 'public, max-age=31536000, immutable');
19 },
20}));
21
22// Cache-Control for dynamic pages (only cache if safe)
23app.get('/page', (req, res) => {
24 res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
25 res.set('Vary', 'Accept-Encoding');
26 // Only vary on headers that are actually keyed by the CDN
27 res.send(renderPage());
28});
29
30// Cache-Control for private pages (never cache in shared caches)
31app.get('/dashboard', requireAuth, (req, res) => {
32 res.set('Cache-Control', 'private, no-store');
33 res.send(renderDashboard(req.user));
34});
35
36// ✅ Pattern 3: Validate JSONP callbacks
37const SAFE_CALLBACK = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
38app.get('/api/data', (req, res) => {
39 const callback = req.query.callback;
40 if (callback && !SAFE_CALLBACK.test(callback)) {
41 return res.status(400).json({ error: 'Invalid callback name' });
42 }
43 // Also add callback to Vary or ensure CDN keys on it
44 if (callback) {
45 res.set('Cache-Control', 'private, no-store');
46 }
47 // ...
48});A Next.js application reads X-Forwarded-Host from headers() to build canonical URLs in a statically cached page. What is the risk?
07 //Code Review Defenses
1# ✅ Include all response-influencing headers in the cache key
2proxy_cache_key "$scheme$request_method$host$request_uri$http_accept_language";
3
4# ✅ Strip dangerous headers before forwarding to the origin
5proxy_set_header X-Original-URL "";
6proxy_set_header X-Rewrite-URL "";
7proxy_set_header X-Forwarded-Prefix "";
8
9# ✅ Only allow trusted values for X-Forwarded-Host
10map $http_x_forwarded_host $validated_forwarded_host {
11 "www.example.com" "www.example.com";
12 "api.example.com" "api.example.com";
13 default "www.example.com";
14}
15proxy_set_header X-Forwarded-Host $validated_forwarded_host;
16
17# ✅ Never cache responses with Set-Cookie (indicates a session)
18proxy_no_cache $upstream_http_set_cookie;
19proxy_cache_bypass $upstream_http_set_cookie;
20
21# ✅ Respect Cache-Control from origin
22proxy_cache_valid 200 301 302 10m;
23proxy_cache_use_stale error timeout updating;
24
25# ✅ Add a header so you can verify caching behavior
26add_header X-Cache-Status $upstream_cache_status always;1// ✅ Middleware that enforces safe cache headers
2function cacheControlMiddleware(req, res, next) {
3 // Default: don't cache anything
4 res.set('Cache-Control', 'no-store');
5
6 // Override per-route:
7 // - Static assets: public, long TTL
8 // - Public pages: short TTL with validation
9 // - Private pages: no-store (default)
10 next();
11}
12
13// ✅ Middleware to strip dangerous headers before they reach the app
14function stripDangerousHeaders(req, res, next) {
15 const headersToStrip = [
16 'x-original-url',
17 'x-rewrite-url',
18 'x-forwarded-prefix',
19 'x-host',
20 'x-forwarded-server',
21 ];
22
23 for (const header of headersToStrip) {
24 delete req.headers[header];
25 }
26
27 // ✅ Validate X-Forwarded-Host if present
28 const forwardedHost = req.headers['x-forwarded-host'];
29 if (forwardedHost) {
30 const allowedHosts = process.env.ALLOWED_HOSTS?.split(',') || [];
31 if (!allowedHosts.includes(forwardedHost)) {
32 delete req.headers['x-forwarded-host'];
33 }
34 }
35
36 next();
37}
38
39// ✅ Apply to all routes
40app.use(stripDangerousHeaders);
41app.use(cacheControlMiddleware);1describe('Cache poisoning prevention', () => {
2 const UNKEYED_HEADERS = [
3 ['X-Forwarded-Host', 'evil.com'],
4 ['X-Forwarded-Scheme', 'nothttps'],
5 ['X-Original-URL', '/admin'],
6 ['X-Rewrite-URL', '/admin'],
7 ['X-Forwarded-Proto', 'http'],
8 ['X-Forwarded-Prefix', '/evil'],
9 ['X-Host', 'evil.com'],
10 ];
11
12 for (const [header, value] of UNKEYED_HEADERS) {
13 it(`should not reflect ${header} in the response body`, async () => {
14 const response = await request(app)
15 .get('/')
16 .set(header, value);
17
18 // ✅ Verify the header value does NOT appear in the response
19 expect(response.text).not.toContain(value);
20 expect(response.text).not.toContain('evil.com');
21 });
22 }
23
24 it('should set Cache-Control: no-store on authenticated pages', async () => {
25 const response = await request(app)
26 .get('/dashboard')
27 .set('Authorization', 'Bearer valid_token');
28
29 expect(response.headers['cache-control']).toMatch(/no-store|private/);
30 });
31
32 it('should not cache responses with Set-Cookie', async () => {
33 const response = await request(app)
34 .post('/login')
35 .send({ username: 'user', password: 'pass' });
36
37 if (response.headers['set-cookie']) {
38 expect(response.headers['cache-control']).toMatch(/no-store|private/);
39 }
40 });
41
42 it('should not serve private content at path with static extension', async () => {
43 const response = await request(app)
44 .get('/account/anything.css')
45 .set('Authorization', 'Bearer valid_token');
46
47 // Should be 404, not the account page
48 expect(response.status).toBe(404);
49 });
50});Which of the following is the most effective defense against both cache poisoning and cache deception?
08 //Advanced Poisoning Techniques
Beyond basic header injection, advanced cache poisoning techniques exploit subtle behaviors in HTTP parsing, cache key normalization, and the interaction between multiple caching layers. These attacks often bypass standard defenses and require deeper understanding of the cache infrastructure.
1# Some caches and frameworks process the body of GET requests
2# The cache keys on the URL, but the backend reads the body
3
4# Attacker sends a GET request WITH a body:
5GET /api/config HTTP/1.1
6Host: example.com
7Content-Type: application/x-www-form-urlencoded
8Content-Length: 23
9
10redirect=https://evil.com
11
12# Backend reads req.body.redirect and includes it in the response
13# Cache stores the response keyed only on GET /api/config
14# All subsequent users get the response with evil.com redirect1// CDN and origin parse query strings differently
2// CDN cache key: /search?q=test (treats ; as part of the value)
3// Origin (Ruby): params[:q] = "test", params[:callback] = "evil"
4
5// Request:
6// GET /search?q=test;callback=evil HTTP/1.1
7//
8// CDN cache key: /search?q=test;callback=evil (one parameter)
9// Ruby on Rails: q=test, callback=evil (two parameters)
10//
11// If the origin reflects the callback parameter in the response
12// but the CDN doesn't key on it (because it sees one param),
13// the poisoned response is served for /search?q=test;callback=evil
14// which the CDN treats as equivalent to /search?q=test
15
16// ✅ Defense: Normalize query parameters at the edge
17// ✅ Use consistent URL parsing between CDN and origin
18// ✅ Reject requests with ambiguous delimiters (;, ||, etc.)1# Combine request smuggling with cache poisoning for devastating effect
2# The smuggled request poisons the cache for a different URL
3
4# Front-end (CDN) sees one request:
5POST / HTTP/1.1
6Host: example.com
7Content-Length: 130
8Transfer-Encoding: chunked
9
100
11
12GET /login HTTP/1.1
13Host: example.com
14X-Forwarded-Host: evil.com
15Content-Length: 0
16
17# Back-end sees TWO requests:
18# 1. POST / (harmless)
19# 2. GET /login with X-Forwarded-Host: evil.com (poisons the login page)
20
21# The cache stores the poisoned /login response
22# All users requesting /login now load scripts from evil.com
23# Combined with phishing: the login form submits to attacker's serverA CDN and the origin server parse the semicolon (;) differently in query strings. The CDN treats /search?q=test;cb=evil as a single parameter, while the origin parses it as two parameters (q=test, cb=evil). How can an attacker exploit this?
09 //CDN-Specific Vulnerabilities
Each CDN has unique caching behavior, cache key construction, and header handling. Understanding these differences is critical for both attacking and defending cache infrastructure.
CDN Cache Key Behavior
| CDN | Default Cache Key | Known Quirks |
|---|---|---|
| Cloudflare | Scheme + Host + Path + Query (sorted) | Ignores X-Forwarded-Host. Transforms pages feature can introduce unkeyed inputs. Workers can modify cache keys. |
| Akamai | Configurable via Property Manager | Complex cache key customization can lead to misconfigurations. "Include all query parameters" vs. "Include specific" is a common mistake. |
| Fastly | URL + Host (configurable via VCL) | VCL hash logic is fully customizable — powerful but error-prone. bereq.http.* headers can be accidentally included/excluded. |
| AWS CloudFront | URL + selected headers/cookies/query strings | Cache policy controls exactly which headers/cookies/query strings are keyed. "Forward all" ≠ "Cache key includes all." |
| Varnish (self-hosted) | req.url + req.http.Host (default) | vcl_hash is fully customizable. Common mistake: using req.http.* in vcl_recv but not including it in vcl_hash. |
1# vcl_recv processes the request and may use unkeyed headers
2sub vcl_recv {
3 # ❌ Modifies backend request based on an unkeyed header
4 if (req.http.X-Forwarded-Proto == "http") {
5 return (synth(301, "https://" + req.http.Host + req.url));
6 # This synthetic 301 redirect gets cached!
7 # Attacker sends X-Forwarded-Proto: http → cached 301 for all users
8 }
9
10 # ❌ Uses X-Forwarded-Host but doesn't include it in the hash
11 if (req.http.X-Forwarded-Host) {
12 set req.http.Host = req.http.X-Forwarded-Host;
13 }
14}
15
16sub vcl_hash {
17 # Cache key only includes URL and Host (which was already overwritten!)
18 hash_data(req.url);
19 hash_data(req.http.Host);
20 # ❌ The original Host and X-Forwarded-Host are not distinguished
21}1sub vcl_recv {
2 # ✅ Strip dangerous override headers at the edge
3 unset req.http.X-Original-URL;
4 unset req.http.X-Rewrite-URL;
5 unset req.http.X-Forwarded-Prefix;
6
7 # ✅ Validate X-Forwarded-Host against allow-list
8 if (req.http.X-Forwarded-Host &&
9 req.http.X-Forwarded-Host !~ "^(www.example.com|api.example.com)$") {
10 unset req.http.X-Forwarded-Host;
11 }
12
13 # ✅ Never generate cached redirects from unvalidated headers
14 # Handle HTTPS redirect at the load balancer, not in Varnish
15
16 # ✅ Don't cache responses with Set-Cookie
17 if (req.http.Cookie) {
18 return (pass); # Bypass cache for authenticated requests
19 }
20}
21
22sub vcl_hash {
23 hash_data(req.url);
24 hash_data(req.http.Host);
25
26 # ✅ Include response-influencing headers in the hash
27 if (req.http.Accept-Language) {
28 hash_data(req.http.Accept-Language);
29 }
30}
31
32sub vcl_backend_response {
33 # ✅ Don't cache responses with Set-Cookie
34 if (beresp.http.Set-Cookie) {
35 set beresp.uncacheable = true;
36 return (deliver);
37 }
38
39 # ✅ Don't cache error pages (often reflect request data)
40 if (beresp.status >= 400) {
41 set beresp.uncacheable = true;
42 set beresp.ttl = 0s;
43 return (deliver);
44 }
45}An AWS CloudFront distribution is configured to 'Forward all headers' to the origin but only uses the URL in the cache key. What is the risk?
10 //Automated Detection
Detecting cache poisoning vulnerabilities at scale requires automated tooling that can identify unkeyed inputs, verify cache behavior, and test for response variations. The key challenge is distinguishing between keyed and unkeyed inputs without access to the cache configuration.
1import requests
2import uuid
3import time
4from typing import Optional
5
6class CachePoisonDetector:
7 """Detect unkeyed inputs that may enable cache poisoning."""
8
9 UNKEYED_HEADERS = [
10 'X-Forwarded-Host',
11 'X-Forwarded-Scheme',
12 'X-Forwarded-Proto',
13 'X-Original-URL',
14 'X-Rewrite-URL',
15 'X-Forwarded-Prefix',
16 'X-Host',
17 'X-Forwarded-Server',
18 'X-Forwarded-Port',
19 'X-HTTP-Method-Override',
20 'Origin',
21 'X-Custom-IP-Authorization',
22 'X-Debug',
23 'X-Amz-Website-Redirect-Location',
24 ]
25
26 def __init__(self, target_url: str, verify_ssl: bool = True):
27 self.target_url = target_url
28 self.verify_ssl = verify_ssl
29 self.session = requests.Session()
30
31 def _cache_buster(self) -> str:
32 return f"cb={uuid.uuid4().hex[:8]}"
33
34 def _get_response(self, headers: Optional[dict] = None,
35 params: Optional[dict] = None) -> requests.Response:
36 return self.session.get(
37 self.target_url,
38 headers=headers or {},
39 params=params or {},
40 verify=self.verify_ssl,
41 allow_redirects=False,
42 )
43
44 def detect_unkeyed_headers(self) -> list[dict]:
45 findings = []
46
47 for header in self.UNKEYED_HEADERS:
48 canary = f"canary-{uuid.uuid4().hex[:8]}.example.com"
49
50 # Step 1: Send request with canary header + cache buster
51 bust = self._cache_buster()
52 resp = self._get_response(
53 headers={header: canary},
54 params={'_cb': bust},
55 )
56
57 if canary in resp.text:
58 # Step 2: Verify the canary persists in cache
59 time.sleep(1)
60 verify = self._get_response(params={'_cb': bust})
61
62 if canary in verify.text:
63 findings.append({
64 'header': header,
65 'canary': canary,
66 'reflected_in_cache': True,
67 'severity': 'critical',
68 'evidence': f'Header {header} reflected in cached response',
69 })
70 else:
71 findings.append({
72 'header': header,
73 'canary': canary,
74 'reflected_in_cache': False,
75 'severity': 'medium',
76 'evidence': f'Header {header} reflected but not cached (may still be exploitable)',
77 })
78
79 return findings
80
81 def detect_cache_deception(self, auth_cookie: str) -> list[dict]:
82 findings = []
83 test_paths = [
84 '/account/test.css',
85 '/account/test.js',
86 '/account/test.png',
87 '/account/.css',
88 '/account/..%2ftest.css',
89 ]
90
91 for path in test_paths:
92 url = self.target_url.rstrip('/') + path
93 resp = self.session.get(
94 url,
95 headers={'Cookie': auth_cookie},
96 verify=self.verify_ssl,
97 allow_redirects=False,
98 )
99
100 if resp.status_code == 200 and 'no-store' not in resp.headers.get('Cache-Control', ''):
101 findings.append({
102 'path': path,
103 'status': resp.status_code,
104 'cache_control': resp.headers.get('Cache-Control', 'MISSING'),
105 'severity': 'high',
106 'evidence': 'Private page served at static-looking path without no-store',
107 })
108
109 return findingsA security team runs an automated scan that sends X-Forwarded-Host: canary.example.com with a cache buster, finds the canary in the response, then re-requests without the header and finds the canary persists. What does this confirm?
11 //Tools & Resources
Cache Poisoning Testing & Prevention Tools
| Category | Name | Description |
|---|---|---|
| Discovery | Param Miner (Burp Suite) | Automated discovery of unkeyed parameters and headers — the go-to tool for finding cache poisoning vectors |
| Discovery | Web Cache Vulnerability Scanner | Open-source scanner specifically designed for web cache poisoning and deception detection |
| Discovery | CacheBuster (Nuclei template) | Nuclei templates for automated cache poisoning checks in CI/CD pipelines |
| Testing | Burp Suite Pro | Manual and automated testing of cache behavior, including Param Miner extension for header discovery |
| Testing | curl + custom scripts | Send requests with specific headers and verify cache behavior using X-Cache/Age/CF-Cache-Status headers |
| Prevention | Cloudflare Cache Rules | Fine-grained control over what gets cached, including path-based rules and custom cache keys |
| Prevention | Fastly VCL / Compute@Edge | Programmable cache logic for precise control over cache keys and response handling |
| Prevention | Varnish Cache + custom VCL | Self-hosted reverse proxy with full control over cache key composition via VCL |
| Monitoring | CDN analytics dashboards | Monitor cache hit rates, response codes, and anomalous patterns that may indicate poisoning attempts |
| Reference | PortSwigger Research | James Kettle's foundational research on "Practical Web Cache Poisoning" and "Web Cache Entanglement" |
Essential Reading: "Practical Web Cache Poisoning" (James Kettle, PortSwigger, 2018), "Web Cache Entanglement: Novel Pathways to Poisoning" (James Kettle, 2020), "Cached and Confused: Web Cache Deception in the Wild" (Mirheidari et al., 2020), PortSwigger Web Security Academy — Web Cache Poisoning labs, Cloudflare Cache documentation, RFC 7234 (HTTP Caching), RFC 9111 (HTTP Caching, updated).
12 //Conclusion & Code Review Checklist
Web cache poisoning and cache deception are powerful attack classes that exploit the fundamental gap between what a cache uses as a key and what the backend uses to build a response. They are particularly dangerous because they amplify a single attacker request into a mass compromise. During code review, every place where request headers, cookies, or non-standard inputs influence the response should be scrutinized against the caching layer's key configuration.
Web Cache Poisoning Code Review Checklist
| Category | Check | Priority |
|---|---|---|
| Header Trust | Does the application reflect X-Forwarded-Host, X-Forwarded-Proto, or other proxy headers in the response without validation? | Critical |
| Header Trust | Are canonical URLs, asset URLs, and redirects derived from hardcoded config or from request headers? | Critical |
| Cache-Control | Do authenticated/private pages set Cache-Control: no-store or private? | Critical |
| Cache-Control | Are responses with Set-Cookie excluded from caching? | High |
| Vary Header | Does the Vary header list all headers that influence the response content? | High |
| CDN Config | Are CDN cache rules path-based (not extension-based)? | High |
| CDN Config | Does the cache key include all response-influencing inputs? | Critical |
| Path Handling | Does the application return 404 for unexpected path suffixes (e.g., /account/anything.css)? | High |
| Path Handling | Is URL normalization consistent between CDN, proxy, and application? | High |
| Override Headers | Are X-Original-URL, X-Rewrite-URL, and similar headers stripped at the edge? | Critical |
| Error Pages | Do error pages avoid reflecting request data? Are error responses excluded from caching? | Medium |
| JSONP/Callbacks | Are JSONP endpoints marked as private/no-store, or is the callback parameter included in the cache key? | High |
| Testing | Is there automated testing for unkeyed input reflection in cached responses? | High |
| Monitoring | Are cache hit rates and anomalous response patterns monitored for poisoning indicators? | Medium |