Closed
Conversation
Fixes Kludex#2789 X-Forwarded-For entries can include port numbers (e.g. '1.2.3.4:1024', '[::1]:8080'). Previously, the port was discarded and hardcoded to 0, and entries with ports were not recognized as trusted proxies. Changes: - Add _parse_host_and_port() to extract host and port from XFF entries - Update ProxyHeadersMiddleware to preserve port from XFF when present - Update _TrustedHosts.__contains__ to recognize IPs with ports as trusted - Add tests for IPv4:port, [IPv6]:port, and multi-proxy port scenarios
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.
Fixes #2789
Problem
X-Forwarded-Forentries can include port numbers (e.g.1.2.3.4:1024,[::1]:8080), butProxyHeadersMiddlewarewas:port = 0inscope["client"]"10.0.0.1:9999"would failipaddress.ip_address()parsing and never match trusted hosts, so trusted proxies were treated as untrusted clientsThis produced malformed client tuples like
("1.2.3.4:1024", 0).Fix
_parse_host_and_port()helper that handles IPv4 with port (1.2.3.4:8080), bracketed IPv6 with port ([::1]:8080), and bare IPv6 (::1)__call__to use parsed host and port instead of hardcodedport = 0_TrustedHosts.__contains__to strip ports before IP comparison, while preserving literal matching for unix sockets etc.Tests
_parse_host_and_portedge cases (invalid port string, missing brackets, etc.)