Behind a load balancer or reverse proxy, your server sees the proxy's address, and the client's address arrives in the X-Forwarded-For header. Each proxy appends the address it received the connection from, so the header reads left to right from the original client towards your server. The catch: the client can send its own X-Forwarded-For, and proxies append to it rather than replace it, so the first entry is whatever the client wrote. Read the entry added by the last proxy you control instead: with one proxy in front of you, that's the last entry. In my test, a sign-up limiter of 10 per IP keyed on the first entry let 100 of 100 spoofed requests through; keyed on the proxy's entry, it allowed exactly 10. In Express, app.set("trust proxy", 1) does the right thing, and trust proxy: true does the wrong one.
Count from the right, by the number of proxies you run. Everything to the left of that is a claim, not a fact.
How does the header get built?
Suppose your app runs behind one proxy (a hosting platform's load balancer). A normal request arrives like this:
one proxy, honest client
client 203.0.113.9 → proxy 10.0.0.2 → app X-Forwarded-For: 203.0.113.9 (added by the proxy) socket address: 10.0.0.2 Now the client sends its own header:
one proxy, client sends a header
X-Forwarded-For: 198.51.100.7, 203.0.113.9 ↑ client wrote this ↑ proxy appended the real address The proxy can't know whether 198.51.100.7 is a real earlier proxy or an invention, so it keeps it. Only the entries added by your proxies are trustworthy, and they're at the right-hand end.
How badly does the first entry fail?
xff.mjs
// Each proxy appends the address it received the request from; only your own proxies' entries can be trusted. export function clientIp(xff, socketIp, trustedHops) { const chain = (xff ?? "") .split(",") .map((s) => s.trim()) .filter(Boolean) .concat(socketIp); return chain[Math.max(0, chain.length - 1 - trustedHops)]; } The chain is the header plus the socket address. With trustedHops = 1, it returns the entry the one trusted proxy added. Then a limiter that allows 10 sign-up emails per IP, and an attacker sending 100 requests from one machine, each with a different fake first entry:
xff.test.mjs
const reqs = Array.from({ length: 100 }, (_, i) => ({ xff: `198.51.100.${i}, 203.0.113.9`, socket: "10.0.0.2" })); const first = limiter((r) => r.xff.split(",")[0].trim()); const hop = limiter((r) => clientIp(r.xff, r.socket, 1)); reqs.filter(first).length; // 100: every request looks like a new person reqs.filter(hop).length; // 10: the limit works Keyed on the first entry, the limit never triggers, because each request claims a fresh address. In the CRM I built, a review found the sign-up endpoint reading the client IP from the first entry. The fix was to read it from the proxy's own hop, and to add a global hourly cap on sign-up emails, so the form can't be used to send mail to strangers from the app's domain even by someone who really does rotate addresses.
What does Express do?
Express computes req.ip from X-Forwarded-For according to its trust proxy setting. I checked Express 5.3.0 with a request carrying X-Forwarded-For: 1.2.3.4, 203.0.113.9:
trust proxy | req.ip | Why |
|---|---|---|
1 | 203.0.113.9 | Trusts one hop: the proxy's entry |
true | 1.2.3.4 | Trusts every entry: the client's invention wins |
true is the setting that "makes it work" in a quick test and is wrong in production. Set the number of proxies you actually run, or the proxies' address ranges ("loopback", a CIDR list). If a CDN sits in front of a load balancer, that's two hops.
What else to check
- Know your hop count. Hosting platforms document whether they add one proxy or more. Count wrong by one in either direction and you read either the proxy's own address (everyone shares one rate limit) or a spoofable one.
- Prefer a header your edge sets and overwrites, such as Cloudflare's
CF-Connecting-IP, when the app only ever receives traffic through that edge, and block direct access to the origin so nobody can send the header themselves. - Don't use the IP as identity. It's fine for rate limiting and abuse signals; for anything else, users behind one office NAT or mobile carrier share addresses.
- Log the whole chain next to the derived IP. When a limit misbehaves, the raw header shows why.
Summary
-
X-Forwarded-Foris appended to, not replaced: the first entry is client-controlled. - Take the entry added by the last proxy you run: count trusted hops from the right.
- Keyed on the first entry, a 10-per-IP limit allowed 100 of 100 spoofed requests; keyed on the proxy's hop, 10.
- In Express, set
trust proxyto the hop count, nevertrue.
The same review added a per-user API limit; for how to build one, see a token bucket rate limiter in Node.js. Other checks that belong at the edge of a server: SSRF protection for server-side fetch and validating uploads by magic bytes. The CRM's database got its own fixes in the same pass: indexing foreign keys in Postgres.
Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article
