Track 08 · Lesson 16 of 20Attacks and defenses

Track 08 · Attacks and defenses

Spoofed headers

Lesson 16 of 20· 7 min

Some request headers are written by your own infrastructure; others are typed by whoever sent the request. X-Forwarded-For and X-Forwarded-Host look official, but unless something you control overwrote them, they’re whatever the attacker wanted.

What does your app believe?

Route, binding, proxy and the header you read

The attacker connects

Headers the attacker typed (real IP 203.0.113.7)

X-Forwarded-For: 9.9.9.9
CF-Connecting-IP: 9.9.9.9
X-Real-IP: 9.9.9.9
X-Forwarded-Host: staging.example.com
The app container listens on
The app reads the client IP from
const ip = req.headers["x-forwarded-for"].split(",")[0]

What reaches the app

X-Forwarded-For: 9.9.9.9, 203.0.113.7, 172.68.1.1
CF-Connecting-IP: 203.0.113.7
X-Real-IP: 9.9.9.9
X-Forwarded-Host: staging.example.com
The app thinks the client is 9.9.9.9. The attacker can type a new IP on every request: rate limits, IP bans and audit logs all believe it.
The app thinks it’s running on staging.example.com. It switches on a staging-only debug page, and builds password-reset links pointing at that host.

Two separate fixes#

Read the right value. X-Forwarded-For is a list each proxy appends to, so its first entry is whatever the client wrote. Read the client IP only from the hop you trust: the header your CDN sets, or the one your reverse proxy overwrites.

Close the back door. All of that assumes requests came through the CDN. If the app is reachable directly, by binding to 0.0.0.0 or with Docker publishing a port past the firewall, the attacker can forge even the CDN’s header. Bind to 127.0.0.1 behind the proxy, or accept only the CDN’s address ranges.

The host header too#

X-Forwarded-Host tells the app which hostname the user typed. Apps that use it to decide behaviour (“are we on staging?”) or to build links (“reset your password at…”) can be pointed anywhere. Use build-time configuration for the environment and a configured base URL for links.

Spoofed client IP

The attack, then the fix

Spoofed client IP

Attacker

CDN

Reverse proxy

nginx

App server

:3000

Your API

backend

Step 1 of 5 · Attacker → App server

Direct to host:3000

The container is published on 0.0.0.0 and Docker bypasses ufw. CDN and proxy are skipped.

X-Forwarded-For and CF-Connecting-IP:
Headers that carry the real client IP through proxies. X-Forwarded-For is a list each proxy appends to, so its first entry is whatever the client wrote. CF-Connecting-IP is set by Cloudflare and trustworthy only if the request really came from Cloudflare.
Origin server and direct-to-origin access:
The real machine hosting the app, behind the CDN. If its IP is reachable directly, attackers bypass every protection the CDN provides: bot filtering, IP rules and the trustworthy client-IP header.
Port binding: 0.0.0.0 vs 127.0.0.1:
A server listens on an address and port. 127.0.0.1 (localhost) means 'only this machine can connect'. 0.0.0.0 means 'every network interface', which includes the public internet.
Firewall (ufw) and the Docker bypass:
ufw is Ubuntu's simple firewall. A well-known trap: Docker writes its own iptables rules for published ports, and those rules run before ufw's, so a port blocked in ufw can still be wide open.
Rate limiting:
Allowing each client only N requests per time window (e.g. 20/min for link preview). The key that identifies 'each client' is everything: if attackers can change it, the limit vanishes. In-memory limiters also need cleanup or a size cap, or the map of keys becomes a memory leak.

In a code review

MediumDevOps + App

Origin server reachable directly; client IP spoofable

Fix: Bind to localhost behind the reverse proxy or allow only CDN ranges; have the proxy overwrite forwarding headers; read client IP only from the trusted hop.

0 of 3 checked

Check yourself

A request arrives with X-Forwarded-For: 9.9.9.9, 203.0.113.7. Which entry did the client control?