CIDADAXSEC
← All topics
Expert

Web cache poisoning

Web cache poisoning injects malicious content into the cache via unkeyed headers or parameters, then serves it to other users.

Material

A web cache stores copies of responses so the origin does not have to be contacted every time. The cache key decides which requests count as identical; parameters left out of the key mean two different users share one cache entry. If an attacker injects content into a cached response, other users receive that content without the origin ever seeing their request.

How caching and cache keys work

The cache key is usually built from the method, host, path, and some query string parameters. Headers such as Cookie, X-Forwarded-For, or User-Agent often decide which content variant is served, yet are not always part of the key. An attacker can therefore send a marker header, make the origin return the "attacker" variant, then trigger an uncached page so that variant gets stored as the shared representation.

http
GET /news HTTP/1.1
Host: example.com
X-Forwarded-Host: evil.example

http
GET /product?x=1 HTTP/1.1
Host: example.com
Cookie: session=attacker

Distinguish web cache poisoning from targeted cache poisoning. In the targeted case the attacker picks one specific URL and injects a payload into that URL cache entry, so victims who open the same URL are affected. The technique combines an unkeyed parameter with a header that influences the response, and it touches a single page.

Attack forms

  • Unkeyed header: X-Forwarded-Host, X-Original-URL, or an origin-internal header is used to inject content into a cached response.
  • Unkeyed cookie: a cookie value changes how the page renders but is not part of the cache key.
  • Unkeyed query string: parameters such as utm_source are not keys and can be cloaked or stripped when normalising.
  • Parameter cloaking: two inputs that are each harmless combine into a working payload.
  • Targeted attack: focus on a single high-value URL such as an account or checkout page.
  • Internal cache: the poisoned response is used by another origin on the network, widening the victim pool.

Prevention

  • Build the cache key from every input that changes the response, including relevant headers and cookies.
  • Allowlist query parameters rather than denylisting them, so unknown parameters are ignored outright.
  • Set Vary correctly on headers, or normalise the request before the cache lookup.
  • Never reflect raw input into pages without escaping; cached output makes a payload persist far longer.
  • Send response headers such as Cache-Control: no-store or private for sensitive and per-person pages.
  • Test cache behaviour on every endpoint, since a single overlooked parameter opens the hole.
http
# Sabot keamanan: reflecting header tanpa escaping -> tersimpan di cache
GET /search?q=beli HTTP/1.1
Host: example.com
X-Forwarded-Host: evil.example

Cache poisoning needs two ingredients: an input that changes the response but is missing from the cache key, and output that stays unescaped when stored. Fix the key and the escaping; fixing only one usually leaves the hole open.

Labs in this topic

Labs run on a domain separate from this site, so a payload cannot reach your account session.

Labs for this category land in Phase 2. The material is readable now.

Challenges in this category

Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.