Track 08 · Lesson 18 of 20Attacks and defenses

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)
  1. 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 + DevOps

Trusting 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

Why is “add the header to the cache key” the weakest of the three fixes?