Lesson 20 of 20 · 16m
X-Forwarded-* and Host: the headers your framework believes
Proxies rebuild the request's identity from X-Forwarded-For, X-Forwarded-Proto, and Host — trust hop counts and pinned hosts, never the header.
Your app does not terminate connections from the internet. Something in front did — nginx, an ALB, Cloudflare — and it rebuilt the request's identity from headers: X-Forwarded-For for the client IP, X-Forwarded-Proto for TLS, Host for where the request was aimed. req.ip, request.is_secure, and every absolute URL your app generates are downstream of those three strings, and all three are attacker-influenced unless your stack deliberately overwrites them.
X-Forwarded-For is a list, and the left end lies
Proxies append: each hop adds the address it saw on its socket. The leftmost entries are whatever the client sent — read x-forwarded-for.split(",")[0] and every request claims 127.0.0.1 or 10.0.0.5, folding your IP allowlist and your login rate limiter. The trustworthy entries are the rightmost hops your own infrastructure appended. The correct shapes:
app.set("trust proxy", "loopback") // or a hop count: 1set_real_ip_from 10.0.0.0/8; # your LB range
real_ip_header X-Forwarded-For;ProxyFix(app, x_for=1, x_proto=1, x_host=1) # trust exactly one hopAn integer "trust N hops" is the robust form — it walks from the socket end inward and stops before attacker bytes. A bare trust proxy = true is the bug: it accepts forwarded-for from anyone, which is worse than reading none. The consequences fan out further than auth — audit logs, geo-IP checks, "impossible travel" detection, and abuse throttles all key on the same address. The inverse failure is real too: an app that doesn't trust the proxy reads the load balancer's IP for everyone, so one rate-limit lockout becomes a lockout of your entire user base.
Two name variants matter in the wild: Forwarded (RFC 7239) is the standardized header almost nothing sends, and CF-Connecting-IP / True-Client-IP are what Cloudflare and Akamai actually set — if your ingress is Cloudflare, X-Forwarded-For is whatever the client provided unless you read the CF header or real_ip_header maps it.
Proto spoofing and Host poisoning
X-Forwarded-Proto: https sent by the client satisfies requireHTTPS, secure cookie rules, and HSTS decisions on an app actually speaking plain HTTP — honor it only after your ingress overwrites it (Spring: server.forward-headers-strategy: framework; Django: SECURE_PROXY_SSL_HEADER). Host is worse because it writes links: frameworks build absolute URLs for password-reset and magic-link emails from Host, and WordPress's CVE-2017-8295 (May 2017) poisoned reset emails through a crafted SERVER_NAME. Django's ALLOWED_HOSTS and Rails' config.action_mailer.default_url_options exist for this: state the canonical host in config, don't derive it from the request.
Two wrinkles worth knowing: proxy_set_header Host $host vs $http_host — $host silently normalizes away a spoofed name while $http_host forwards it raw, which is the difference between a poisoned and a safe upstream. And under HTTP/2 there is no Host header — :authority plays its role — so a proxy translating h2 to h1 has to synthesize Host, and what it synthesizes is policy, not wire data.
The cache makes it durable
An unkeyed reflected header — X-Forwarded-Host, X-Scheme — stored in the shared cache becomes the canonical page for every later visitor; PortSwigger's practical cache-poisoning research turned "harmless reflected header" into stored XSS at scale. Vary controls what the cache keys on, and a header reflected into canonical/og:url output is a poisoning vector whether or not the app treats it as input.
Practice
Trust a hop count, not a header: set trust proxy/set_real_ip_from/ProxyFix/ForwardedHeaders to exactly your ingress, pin the canonical host in config, and strip X-Forwarded-* at the edge for anything your proxies didn't append. Audit every place the app turns a request into an absolute URL — emailers, redirects, og: tags — because Host is an attacker string wearing a suit.
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
RSA in JavaScript Part 2: The JWT Is Where You'll Actually Meet It
Most JavaScript developers never call an RSA function directly. They verify an RS256 token, and two classic attacks turn that into an authentication bypass — both demonstrated here, both fixed by one line.
RSA Signatures: The Version in Most Tutorials Is Forgeable
Signing is encryption with the keys swapped, and that symmetry is exactly what makes the naive implementation breakable. Here are two forgeries executed against textbook RSA signatures, and what PSS does that hashing alone cannot.
Ask Java for AES and You Get ECB. Nobody Warns You.
Cipher.getInstance("AES") compiles, runs, encrypts, decrypts, and hands you the weakest mode in the box. Here is the OpenJDK source that makes that decision, and the line where ECB gets chosen for you.