Repository navigation
httpx accepts out-of-range port numbers and silently connects to the wrong port (16-bit wraparound) #3830
Unanswered
Khushi6767
asked this question in
Q&A
Replies: 1 comment
|
Hello @Khushi6767! In the HTTPX 0.28.1 source, For an application that accepts untrusted URLs, you could reject an out-of-range port before making the request: url = httpx.URL(raw_url)
if url.port is not None and not 0 <= url.port <= 65535:
raise ValueError("port must be between 0 and 65535")
response = client.get(url)That’s a caller-side guard, not a fix in HTTPX itself. A regression test expecting Hope this helps 👍 |
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.
Summary
httpx.URL(andhttpx.Client/httpx.AsyncClient) accept port numbers far outside the valid 0–65535 range with no validation error. Because the OS socket layer truncates a 32-bit Python int to 16 bits when it's handed toconnect(), a request to an "invalid" port silently succeeds against a different, unintended port (port % 65536), rather than failing loudly. This is a real input-validation gap with a port-confusion / SSRF-allowlist-bypass angle: code that validates a port against an allowlist before handing the URL to httpx would see the out-of-range value and compare it against the allowlist, while httpx itself ends up contacting the wrapped-around (and possibly allowlisted) port.I checked and couldn't find an existing issue/discussion about this, which is why I'm raising it here first per the contributing guide.
Reproduction
With a plain
http.serverinstance bound to127.0.0.1:18234, requesting bothhttp://127.0.0.1:83770/(18234 + 65536) andhttp://127.0.0.1:214842/(18234 + 65536*3) returned HTTP 200 from the server on 18234 - every multiple of 65536 added to a valid port reaches the same server.Comparison with peer libraries
Both
requestsandaiohttpcorrectly reject the same URL outright:urllib3's ownparse_urlalso rejects it directly (LocationParseError). So this looks specific to httpx/httpcore's URL handling rather than an unavoidable consequence of how sockets work.Expected behavior
httpx.URL(in_urlparse.py,normalize_port) should validate that a numeric port falls within 0-65535 and raiseInvalidURLotherwise, matching the behavior ofrequests,aiohttp, andurllib3.Environment
All reactions