Our login rate limit trusted X-Forwarded-For, so attackers just sent a new IP each time
A credential stuffing run sent 180,000 login attempts from a handful of servers, and our per-IP limit never triggered. The app read the client IP from the leftmost X-Forwarded-For value, which the client controls. How proxies append to the header, the rightmost-untrusted rule, and the config that fixed it.
Our authentication service rate-limits login attempts per client IP: 20 attempts per 10 minutes, then a temporary block. During a credential stuffing attack one night, the login endpoint received about 180,000 attempts in two hours. The rate limiter blocked almost none of them. About 300 accounts with reused passwords were successfully accessed before an on-call engineer noticed the spike in failed logins and enabled a stricter rule at the edge.
The rate limiter's logs showed why it never triggered:
02:14:07.118 ratelimit login key=ip:198.51.100.23 count=1
02:14:07.121 ratelimit login key=ip:203.0.113.190 count=1
02:14:07.125 ratelimit login key=ip:192.0.2.77 count=1
02:14:07.129 ratelimit login key=ip:198.51.100.241 count=1
Every attempt had a different client IP. A botnet could explain that, but the load balancer's access log, which records the actual TCP peer address, showed only six source addresses for the whole attack. The attempts came from six servers. Our application saw thousands of distinct IPs.
The header
The raw requests, as they reached the application:
POST /login HTTP/1.1
Host: app.example.com
X-Forwarded-For: 198.51.100.23, 203.0.113.9
X-Forwarded-Proto: https
POST /login HTTP/1.1
Host: app.example.com
X-Forwarded-For: 203.0.113.190, 203.0.113.9
203.0.113.9 was the attacker's real address, appended by our load balancer. The first value was random on
every request. The attacker's script sent its own X-Forwarded-For header with a random IP, and our
application took the first value in the header as the client IP:
def client_ip(request) -> str:
xff = request.headers.get("X-Forwarded-For")
if xff:
return xff.split(",")[0].strip() # leftmost value
return request.client.host
How X-Forwarded-For is built
X-Forwarded-For (XFF) is a list. Each proxy that forwards a request appends the address of the peer it
received the request from. If a client sends the header itself, the first proxy keeps the client's value and
appends the real peer address after it.
For a client at 203.0.113.9, sending through our load balancer and then nginx:
client sends: X-Forwarded-For: 198.51.100.23 (forged)
load balancer appends: X-Forwarded-For: 198.51.100.23, 203.0.113.9
nginx appends LB address: X-Forwarded-For: 198.51.100.23, 203.0.113.9, 10.0.1.15
Only the entries added by proxies you operate are trustworthy. Everything to the left of the first trusted entry was supplied by someone you do not control. The leftmost value is the least trustworthy part of the header, not the most.
The MDN documentation for the header says exactly this: the leftmost addresses are only trustworthy if every proxy in the chain is trusted, and for security purposes you should use the rightmost address that was added by a proxy you trust.
The rightmost-untrusted rule
To find the real client IP:
- Start from the immediate TCP peer. If it is not one of your proxies, it is the client; ignore XFF.
- Otherwise, walk the XFF list from right to left, skipping addresses that belong to your trusted proxies.
- The first address that is not a trusted proxy is the client IP.
In the example: 10.0.1.15 is nginx (trusted, skip), 203.0.113.9 is not ours, so it is the client. The
forged 198.51.100.23 is never reached.
The Forwarded header (RFC 7239) standardises the same information with explicit fields, and the same rule
applies to it: only trust entries added by your own proxies.
Environment
| Item | Value |
|---|---|
| Path | client to cloud load balancer to nginx to Python app |
| App client IP logic | leftmost XFF value |
| Rate limit | 20 login attempts per IP per 10 min, in Redis |
| Attack | 180k attempts from 6 servers, about 300 accounts accessed |
1. Determine the client IP once, at the proxy, with trusted addresses
nginx's realip module implements the right-to-left rule when real_ip_recursive is on:
# trust only our load balancer subnet and the nginx host itself
set_real_ip_from 10.0.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
proxy_set_header X-Real-IP $remote_addr; # now the resolved client IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
With real_ip_recursive on, nginx walks the XFF list from the right and skips addresses in
set_real_ip_from ranges. The first address outside them becomes $remote_addr. Without
real_ip_recursive, nginx takes only the last address, which is correct for one proxy hop but not for two.
2. Application: trust the proxy's answer, nothing else
The application now reads the client IP from X-Real-IP, set by nginx, and only when the request came from
nginx:
TRUSTED_PROXIES = ipaddress.ip_network("10.0.0.0/16")
def client_ip(request) -> str:
peer = ipaddress.ip_address(request.client.host)
if peer in TRUSTED_PROXIES:
return request.headers.get("X-Real-IP", str(peer))
return str(peer)
nginx overwrites X-Real-IP with proxy_set_header, so a client cannot supply its own. In a framework with
built-in proxy support (Werkzeug's ProxyFix, Uvicorn's --forwarded-allow-ips, Express's trust proxy),
configure the number of hops or the trusted addresses explicitly; the defaults vary and some trust everything.
3. Rate-limit on more than IP
Even with the correct IP, credential stuffing from many real IPs (a botnet) is common. We added limits keyed by account as well: 10 failed attempts per username per hour across all IPs triggers a step-up challenge for that account. We also check submitted passwords against a breached password list at login and force a reset for matches.
4. Strip the header at the edge for good measure
The load balancer now removes any incoming X-Forwarded-For from clients before appending its own entry, so
the header reaching nginx contains only addresses our infrastructure added. With the right-to-left logic this
is not strictly necessary, but it removes a whole class of mistakes in other services that read the header.
1. A spoofing test against staging.
for i in $(seq 1 25); do
curl -s -o /dev/null -w "%{http_code}\n" https://staging.example.com/login \
-H "X-Forwarded-For: 192.0.2.$i" \
--data 'username=probe@example.com&password=wrong'
done | sort | uniq -c
20 401
5 429
The first 20 attempts were rejected as wrong credentials, then the limiter blocked the source with 429, despite 25 different forged addresses. Before the fix, all 25 returned 401.
2. Logs show the real address. The rate limiter's key for those requests was the test machine's public IP, matching the load balancer's access log.
3. Unit tests for the parsing logic, with chains of one, two and three proxies, forged entries, IPv6 addresses and an untrusted peer sending XFF directly to the app port (which must be ignored).
4. Incident response. We reset passwords for the roughly 300 accessed accounts, ended their sessions, notified the owners, and reviewed account activity during the window. Every service that reads client IPs (audit logs, geo checks, fraud scoring) was checked for the same leftmost-value bug; two more had it.
5. Security implications. Client IP is used for rate limits, audit trails, allow lists and fraud signals. Anywhere it comes from a header, the trust boundary must be explicit. An admin endpoint protected by an IP allow list would have been bypassable the same way.
Takeaways
X-Forwarded-Foris a list that clients can prefill. The leftmost value is attacker-controlled.- Walk the list from the right and skip only your own trusted proxies. The first other address is the client.
- Resolve the client IP once, at your edge proxy, and pass it on in a header the proxy overwrites.
- Rate-limit logins by account as well as by IP.
References: MDN X-Forwarded-For, nginx ngx_http_realip_module, RFC 7239, Forwarded HTTP Extension, OWASP Credential Stuffing Prevention Cheat Sheet.
Add a label
Embed this post
<iframe src="https://hibug.me/x-forwarded-for-spoofed-rate-limit-bypass" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.