URL netloc overrides reject bracketed IPv6 addresses #3795
Replies: 2 comments 1 reply
|
$ python - <<'PY'
import httpx
u = httpx.URL('http://[::1]:8080/')
print(httpx.__version__, repr(u.netloc))
try:
print(u.copy_with(netloc=u.netloc))
except httpx.InvalidURL as exc:
print(type(exc).__name__, str(exc))
print(u.copy_with(host=u.host, port=u.port))
PY
0.28.1 b'[::1]:8080'
InvalidURL Invalid port: ':1]:8080'
http://[::1]:8080/For code that needs a new authority, pass |
|
Thanks for confirming. The proposed patch covers both |
Uh oh!
There was an error while loading. Please reload this page.
URL.netlocreturns bytes for a bracketed IPv6 address, but passing those bytes back to the public constructor orcopy_withraisesInvalidURL.Minimal offline reproduction on current master
b5addb64f0161ff6bfe94c124ef76f6a1fba5254:I expected
url.copy_with(netloc=url.netloc)to preserve the host and port, as it does for domain names and IPv4. A plain IPv6 URL string already parses correctly; no connection is made in this reproduction.The
netlocoverride in_urlparse.pysplits at the first:, including colons inside IPv6 brackets. I tested a small change that splits bracketed hosts at]:and leaves the non-bracketed path unchanged. Constructor/copy tests cover IPv6 with and without a port, default-port normalization, IPv4-mapped IPv6, domain/IPv4 controls, preserved userinfo and malformed inputs. With the same regression tests, the baseline has 14 failing IPv6 cases; the change passes all 122 URL tests.Environment: Linux container, Python 3.10.19, HTTPX 0.28.1 at the commit above; pytest 8.4.1, anyio 4.15.1, httpcore 1.0.9, idna 3.20, Trio 0.31.0.
ruff format --diff, mypy and Ruff checks pass. The full offline native suite reports 1,444 passed / 1 failed / 5 network cases deselected after the change. The remainingtest_write_timeout[trio]async-generator cleanup warning is also present in the baseline and is not changed here. I have not tested external networking or claimed a complete CI pass.I searched the existing Issues/PRs and Discussions for
netloc/ IPv6 copying but may have missed related work. Would this be suitable for a focused PR?All reactions