Track 08 · Lesson 18 of 20Attacks and defenses
- 01What HTTPS protects
- 02The same-origin policy
- 03CORS
- 04Security headers
- 05Cookies done right
- 06Cross-site scripting (XSS)
- 07Content Security Policy
- 08Cross-site request forgery (CSRF)
- 09Clickjacking
- 10Unsafe URLs and open redirects
- 11Prompt injection
- 12SQL and command injection
- 13Prototype pollution, CRLF and SVG
- 14Server-side request forgery (SSRF)
- 15Remote code execution and denial of service
- 16Spoofed headers
- 17Rate limiting
- 18Cache poisoning
- 19The OWASP Top 10
- 20The attack lab
Track 08 · Attacks and defenses
Cache poisoning
Lesson 18 of 20· 6 min
Cache poisoning tricks a shared cache into storing a response shaped by one attacker’s request, then serving it to everyone. One request, every visitor, until the cache expires.
Poison the home page
Attacker first, then Ana. Then try the defenses
const host = headers().get("x-forwarded-host") ?? "example.com"
return <script src={`https://${host}/static/app.js`} />Shared cache
(empty)- Send the attacker’s request, then visit as Ana.
Keyed and unkeyed#
A cache decides whether two requests are “the same” by its cache key, usually the method, host and path. Anything else the server reads, like a header or a cookie, is unkeyed. If an unkeyed input changes the page, whoever arrives first decides what everyone else gets.
Three ways out#
- Don’t let request headers shape cached pages. Build URLs from a configured base URL; that removes the bug.
- Have the proxy overwrite forwarding headers, so attackers can’t set them at all.
- Add the header to the cache key (
Vary). It contains the damage, but splits the cache into many copies.
Frameworks cache for you#
Static generation and incremental regeneration (ISR) store pages and share them with everyone, which is exactly what makes them fast and exactly what poisoning abuses. Frameworks have shipped cache-poisoning fixes before, so keep yours patched: the next track covers advisories.
In a code review
LowApp + DevOpsTrusting the Host or X-Forwarded-Host header
Fix: Use build-time config for environment checks and a configured base URL for links and redirects; have the proxy overwrite the header.
0 of 2 checked
Check yourself