Track 08 · Lesson 16 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
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
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
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.comTwo 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 + AppOrigin 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