fix(session): walk every X-Forwarded-For header line for the client IP - #132
Merged
Merged
Conversation
get_client_ip() read only the first X-Forwarded-For line. A proxy that adds its own line instead of appending to the caller's left a caller-chosen first line in charge of the rightmost-untrusted walk, so a forged address could satisfy ip_binding. All lines are now joined into one chain, as RFC 9110 treats them. The middleware tests built request.headers from a plain dict, which has no getlist(); the forwarded-header ones now use Starlette's Headers. Closes #104
allen0099
force-pushed
the
fix/forwarded-for-all-lines
branch
from
September 25, 2026 10:56
d077d44 to
b091390
Compare
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.
Closes #104.
Problem
get_client_ip()usedheaders.get("x-forwarded-for"), which returns only the first header line. Some proxies add their ownX-Forwarded-Forline instead of appending to the caller's. The caller's line then stayed first, the rightmost-untrusted walk ran over a value the caller chose, and a forged address could satisfyip_binding.Changes
get_client_ip()joins everyX-Forwarded-Forline with,before walking. RFC 9110 §5.3 treats repeated lines as one comma-separated value.docs/SESSION.mdnotes that multiple lines are read as one chain.[Unreleased]Fixed entry.Tests
test_forwarded_chain_spans_every_header_line: a caller-sent first line plus a proxy-added second line must resolve to the proxy's entry. It fails on the old code and passes with the fix.tests/session/test_middleware.pybuiltrequest.headersfrom a plain dict, which has nogetlist(). They now use Starlette'sHeaders, which is whatRequest.headersactually is.Local results:
zensical build --strict: passes.CACHEX_REQUIRE_LIVE_SERVERS=1against Redis and Memcached: 717 passed, 100% coverage.