fix: honor accept_only_proxied_requests=False when known_networks is set - #695
Open
sridhar-3009 wants to merge 1 commit into
Open
Conversation
should_validate_client_ip() combined its conditions as:
accept_only_proxied_requests and any(known_proxies) or any(known_networks)
Since 'and' binds tighter than 'or' in Python, this evaluates as
(accept_only_proxied_requests and any(known_proxies)) or
any(known_networks) - so whenever known_networks was non-empty, the
method returned True regardless of accept_only_proxied_requests,
contradicting its own docstring ("If accept_only_proxied_requests is
set to False... the server will accept both requests that are proxied
and requests that are hitting the web server directly").
In practice this meant that setting accept_only_proxied_requests=False
alongside known_networks still validated (and rejected) the client IP
of every direct, non-proxied request, since should_validate_client_ip()
never actually skipped validation for that configuration.
Parenthesize the intended grouping so the flag is honored regardless of
which of known_proxies/known_networks is configured.
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
BaseForwardedHeadersMiddleware.should_validate_client_ip()combines its conditions like this:Since
andbinds tighter thanorin Python, this is actually evaluated as:So whenever
known_networksis configured (non-empty), the method returnsTrueregardless ofaccept_only_proxied_requests- contradicting the method's own docstring:In practice, this means a server configured with
known_networks=[...]andaccept_only_proxied_requests=False(intending to accept both proxied and direct requests) will still validate the client IP against the known proxies/networks for every direct request, and reject any direct request whose IP isn't in that list with a 400Proxy IP not recognizederror - even though the developer explicitly asked for direct requests to be accepted too.This doesn't reproduce when only
known_proxiesis used (the default), since in that case both terms of theordepend onaccept_only_proxied_requestscorrectly. It's specific to theknown_networksconfiguration.Fix
Parenthesize the intended grouping:
Test plan
mainfirst (a direct, non-proxied request was rejected with 400 despiteaccept_only_proxied_requests=False) before applying the fixtest_x_forwarded_headers_middleware_accepts_direct_requests_with_known_networks, covering theknown_networks+accept_only_proxied_requests=Falsecombinationpytest tests/test_forwarding.py→ 25 passedpytest tests/→ 1923 passed, 1 skippedblack --check,isort --check-only,flake8all clean on changed files