Repository navigation
httpx accepts out-of-range port numbers and silently connects to the wrong port (16-bit wraparound) #3842
Replies: 1 comment
|
Given #3840 we could possibly consider a fix for this in |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Bug description
httpxdoes not validate that a port number is within the valid range (0–65535). When a port number outside this range is passed (e.g., 80000 or negative values), httpx silently accepts it. Because the OS socket layer truncates the port to 16 bits onconnect(), the request is sent toport % 65536— a completely different port than intended.This can lead to requests being silently misdirected to unintended services, which is a potential security concern.
Reproduction
Expected behavior
httpx should raise
httpx.InvalidURLfor any port outside the 0–65535 range, similar to how other HTTP libraries handle this:InvalidURL: Invalid port: 80000InvalidUrlClientErrorfor out-of-range portsLocationParseError: Failed to parse: ..., invalid portRoot cause
In
httpx/_urlparse.py, thenormalize_portfunction converts the port string to an integer but only checks whether it matches the default port for the scheme. It never validates the range:Impact
port % 65536)Environment
All reactions