Skip to content

scan X-Forwarded-For right-to-left in get_client_ip#2503

Merged
yhirose merged 1 commit into
yhirose:masterfrom
metsw24-max:forwarded-for-rightmost-scan
Jul 21, 2026
Merged

scan X-Forwarded-For right-to-left in get_client_ip#2503
yhirose merged 1 commit into
yhirose:masterfrom
metsw24-max:forwarded-for-rightmost-scan

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

Client-spoofable source address in get_client_ip

With set_trusted_proxies configured, req.remote_addr is derived by scanning X-Forwarded-For left to right and returning the entry just before the first trusted-proxy address (or the leftmost entry when none is listed). Each hop appends the address it received the request from, so the trustworthy entries sit on the right while the left is whatever the client chose to send. A client can therefore pin the result to any value with X-Forwarded-For: <forged>, <trusted-proxy-ip>: the real proxy still appends the client's true address on the right, but the left-to-right scan stops at the injected trusted-proxy token and returns <forged>. Because remote_addr tends to back IP allowlists, rate limits and audit logs, that is a source-address spoof.

The scan now runs right to left and returns the first address that is not a trusted proxy, i.e. the furthest hop still written by our own infrastructure. Well-formed proxy chains resolve to the same client as before; the only behavioural change is the "no trusted proxy in the header" fallback, which now prefers the rightmost address over the client-controlled leftmost one. The existing fallback test is updated to match and a new test covers the spoof case. The helper keeps its shape, so it should stay easy to follow.

@yhirose
yhirose merged commit 6c4cbd4 into yhirose:master Jul 21, 2026
27 checks passed
@yhirose

yhirose commented Jul 21, 2026

Copy link
Copy Markdown
Owner

@metsw24-max thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants