Repository navigation
Fix urlutils parsing of unencoded ? and / in URL credentials (#309) - #578
inchang-ing wants to merge 2 commits into
Conversation
Resolves mahmoud#309. When a URL's password contained an unencoded '?' or '/', the authority regex ([^/?#]*) stopped early at the special character and the credential '@' separator ended up inside the query or path. parse_url then tried to interpret the password as the port and raised URLParseError. Now, if the authority has no '@' but the query starts with '@' (unencoded '?' in the password) or the path starts with '/@' (unencoded '/' in the password), reconstruct the userinfo/hostinfo from there. This does not affect ordinary URLs, whose query/path '@' is never at the start. Adds regression tests test_userinfo_unencoded_qmark_in_password and test_userinfo_unencoded_slash_in_password.
Saadia751
left a comment
There was a problem hiding this comment.
I found an authority-parsing regression with ordinary URLs whose path or query starts with @:
from boltons.urlutils import URL
URL("http://example.com?@other.example").host # "other.example"
URL("http://example.com/@other.example").host # "other.example"Both hosts are example.com on the PR base. The new fallback moves valid query/path content into the authority and renders the original host as a username. Could the recovery logic avoid reinterpreting ordinary host-only URLs? I reproduced this on base and head.
Checked b4bc26ec1fb7 on Python 3.12.14; the six host/credential assertions fail on the head. I haven't run the full upstream suite.
The fallback for an unencoded '?' or '/' in the password fired for any URL whose query or path starts with '@', so an ordinary host-only URL like 'http://example.com?@other.example' lost its host to the 'userinfo' and picked up 'other.example' instead. Guard both branches with _is_truncated_userinfo(): a truncated userinfo always carries ':' (user:password), while an ordinary authority is either a bare host or host:port, with digits after the last ':'. IPv6 literals start with '[' and are excluded as well. Adds regression tests for the host-only, host:port and IPv6 cases.
|
Thanks for the sharp catch — both of those URLs do regress on head, and your repro is exactly the case I failed to consider. Fixed in 115d408. The fallback fired whenever the query or path started with '@', without checking whether the authority could actually be a truncated userinfo. Both recovery branches are now guarded by a new Verified:
One known limit worth naming: a password that is entirely digits (e.g. |
|
Userinfo must be percent-encoded per RFC 3986 section 3.2.1; heuristic recovery of unencoded ? and / adds a second parser path I'd rather not maintain (see #309). Closing. |
The change
Fixes #309.
When a URL's password contained an unencoded
?or/, the authority regex[^/?#]*stopped early at the special character and the credential@separator ended up inside the query or path.parse_urlthen tried to interpret the password as the port and raisedURLParseError(and for the/-in-username variant it silently produced a wrong host/port).The fix (in
parse_url): if the authority contains no@but the query starts with@(unencoded?in the password) or the path starts with/@(unencoded/in the password), reconstruct the userinfo/hostinfo from there. Ordinary URLs are unaffected — their query/path@is never at the very start, so the new branches never fire for them.This is the same safe-recovery scope as the previously closed PR #417, minus the parts that risk mis-parsing valid URLs whose query/path legitimately contains
@.The backstory
I hit this while parsing proxy URLs supplied by a third-party tool that emitted credentials with unencoded special characters (e.g.
http://username:password?@www.proxy.com:443). boltons raisedURLParseErrorwhereurllib.parsehad been silently producing a wrong result, so I dug intourlutilsto make the parse tolerant without breaking well-formed URLs. Added two regression tests (test_userinfo_unencoded_qmark_in_password,test_userinfo_unencoded_slash_in_password); the fulltests/test_urlutils.pysuite (129 tests) still passes.Test plan
pytest tests/test_urlutils.py— all 129 pass.port == 443/port == 8080and correctusername/passwordfor the reported cases.