scan X-Forwarded-For right-to-left in get_client_ip#2503
Merged
Conversation
Owner
|
@metsw24-max thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Client-spoofable source address in get_client_ip
With
set_trusted_proxiesconfigured,req.remote_addris derived by scanningX-Forwarded-Forleft 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 withX-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>. Becauseremote_addrtends 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.