Repository navigation
Conversation
ExtractIPFromXFFHeader scans the X-Forwarded-For chain from the right, returning the first untrusted IP. That stops at the public egress IP of edge proxies such as Azure Front Door instead of reaching the client. XFFLeftmost() opts into scanning from the left, returning the first untrusted IP from that direction, while all existing trust options keep applying. Only use it when the outermost proxy strips incoming X-Forwarded-For headers and is the sole writer of the chain; otherwise the leftmost entry is client-controlled and can be spoofed. Default behavior is unchanged.
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.
Summary
Fixes #2803.
ExtractIPFromXFFHeaderscans theX-Forwarded-Forchain from the right, returning the first untrusted IP. Behind Azure Front Door that stops at the proxy's public egress IP instead of reaching the client: withXFF: client, <Front Door egress>and a privateRemoteAddr, the extractor returns the egress IP.This PR adds a new
TrustOption,XFFLeftmost(), that opts into scanning from the left, returning the first untrusted IP from that direction. All existing trust options keep applying (a trusted leftmost entry is skipped), and default behavior is unchanged.Safety caveat
Only use
XFFLeftmost()when the outermost proxy strips incomingX-Forwarded-Forheaders and is the sole writer of the chain. Otherwise the leftmost entry is client-controlled and can be spoofed (a client can prepend arbitrary addresses; Front Door appends the real client IP to the end). This is documented on the option and in the IP-address guide inip.go. If the edge does not strip incoming headers, trust the proxy's address ranges withTrustIPRangeinstead.Tests
XFF: 203.0.113.195, 203.0.113.199,RemoteAddr: 10.0.0.5) the default extractor returns203.0.113.199(the proxy); withXFFLeftmost()it returns203.0.113.195(the client).TestExtractIPFromXFFHeaderLeftmost: 8 cases covering the reporter scenario, trusted entries skipped on the left, all-trusted fallback, invalid entry falling back toRemoteAddr, no-XFF fallback, multiple header values, bracketed IPv6, and composition withTrustIPRange.go test ./...: root package passes except 4 pre-existing environment failures (TestEchoStaticRedirectIndex,TestStartConfig_*) that fail identically on the clean tree;middlewarefully passes.gofmtandgo vetclean.Prepared with AI assistance.