# Next.js and cache poisoning: a quest for the black hole

Next.js honors client-supplied internal x-invoke-status header to override HTTP status and render /_error, letting attackers cache-poison CDN-fronted sites with a 200-status error page.

- Payout: $3,000
- Severity: high
- Program: Vercel
- Found by: Rachid.A
- Disclosed: 2024-06-24
- Write-up quality: 7/10
- Original: https://zhero-web-sec.github.io/research-and-things/nextjs-and-cache-poisoning-a-quest-for-the-black-hole
- Library page: https://bugbountylibrary.com/r/blog%3Azhero-web-sec-github-io-research-and-things-nextjs-and-cache-poisoning-a-quest-for-the-black-hole
- Tags: area:web domain:web impact:dos kind:bounty platform:blog sev:high tech:cdn tech:nextjs tech:nodejs technique:recon technique:source-review vuln:cache-poisoning vuln:dos

**Target:** Next.js Node.js server (minimal mode) behind CDN
**Root cause:** The server reads req.headers['x-invoke-status'] and assigns it to res.statusCode before rendering /_error; the internal header is not stripped from client requests and is not added to Vary, so a client can force a 200-status error page that CDNs cache.
**Steps:**
1. Identify a Next.js app behind a CDN that caches HTML responses.
2. Send `GET / HTTP/1.1` with headers `x-invoke-status: 200` and `x-invoke-error: {"message":"pwned"}`.
3. Observe response returns the /_error page with HTTP 200.
4. Confirm the CDN caches the poisoned response (repeat request without headers returns error page).
5. Keep re-poisoning at cache TTL intervals to maintain DoS.
**How found:** Code review of Next.js source revealed res.statusCode = Number(req.headers['x-invoke-status']) followed by renderError to /_error; local test with x-invoke-status: 888 confirmed the header was not stripped, prompting cache-poisoning hypothesis since 200 satisfies CDN cache rules.
**Exploitation:** Attacker sends a single request with x-invoke-status: 200 (and optional x-invoke-error JSON message) to a cacheable route; the CDN stores the 200-status error page and serves it to all subsequent visitors, taking the page offline indefinitely by re-poisoning each TTL.
**Escalation:** Mass-scanned bug bounty asset database (200+ vulnerable assets) and re-poisoned at cache TTL to sustain DoS; also chained with RSC Vary-bypass and x-middleware-prefetch variants.
**Insight:** Internal/debug headers (x-invoke-*, x-middleware-*) that influence response status or body are prime cache-poisoning vectors: grep framework source for req.headers['x-...'] assignments to res.statusCode or render paths, then test whether the header is stripped and whether it lands in Vary.

## Proof of concept (copied exactly from the original report)

**x-invoke-status header overwrites status code**

```
res.statusCode = Number(req.headers['x-invoke-status'])

(...)

return this.renderError(err, req, res, '/_error', parsedUrl.query)
```

**x-invoke-error custom error message JSON**

```
if (typeof req.headers['x-invoke-error'] === 'string') {
 const invokeError = JSON.parse(
 req.headers['x-invoke-error'] || '{}'
 )
 err = new Error(invokeError.message)
```

