Skip to content

Commit 93142f0

Browse files
lesnik512claude
andcommitted
fix(errors): handle IPv6 hostnames in _strip_userinfo + close branch coverage gaps
Wrap IPv6 hostnames in brackets after urlsplit strips them, preventing malformed URLs like https://::1:8080/x. Add four tests covering: port preservation, username-only userinfo, at-sign in query string only, and IPv6 host with credentials. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 974d84c commit 93142f0

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎src/httpware/errors.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def _strip_userinfo(url: str) -> str:
2323
parts = urlsplit(url)
2424
if parts.username is None and parts.password is None:
2525
return url
26-
netloc = parts.hostname or ""
26+
hostname = parts.hostname or ""
27+
if ":" in hostname: # IPv6 literal — re-wrap in brackets
28+
hostname = f"[{hostname}]"
29+
netloc = hostname
2730
if parts.port is not None:
2831
netloc = f"{netloc}:{parts.port}"
2932
return urlunsplit((parts.scheme, netloc, parts.path, parts.query, parts.fragment))

‎tests/test_errors.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ def test_per_status_subclasses_construct(status: int, expected: type[StatusError
122122
assert exc.response.status_code == status
123123

124124

125+
def test_status_error_strips_userinfo_with_username_only() -> None:
126+
exc = NotFoundError(_make_response(404, url="https://user@example.test/x"))
127+
assert "user" not in str(exc)
128+
assert str(exc) == "404 GET https://example.test/x"
129+
130+
131+
def test_status_error_summary_preserves_port() -> None:
132+
exc = NotFoundError(_make_response(404, url="https://user:pass@example.test:8080/x"))
133+
assert "user" not in str(exc)
134+
assert "pass" not in str(exc)
135+
assert str(exc) == "404 GET https://example.test:8080/x"
136+
137+
138+
def test_status_error_summary_passthrough_when_at_in_query_only() -> None:
139+
# `@` in query-string with no userinfo — should fall through after urlsplit returns no user/pass.
140+
exc = NotFoundError(_make_response(404, url="https://example.test/x?email=foo@bar.com"))
141+
assert str(exc) == "404 GET https://example.test/x?email=foo@bar.com"
142+
143+
144+
def test_status_error_strips_userinfo_with_ipv6_host() -> None:
145+
exc = NotFoundError(_make_response(404, url="https://user:pass@[::1]:8080/x"))
146+
assert "user" not in str(exc)
147+
assert "pass" not in str(exc)
148+
assert str(exc) == "404 GET https://[::1]:8080/x"
149+
150+
125151
def test_timeout_error_is_builtin_timeout_error() -> None:
126152
exc = TimeoutError("timed out")
127153
assert isinstance(exc, builtins.TimeoutError)

0 commit comments

Comments
 (0)