Skip to content

Fix urlutils parsing of unencoded ? and / in URL credentials (#309) - #578

Closed
inchang-ing wants to merge 2 commits into
mahmoud:masterfrom
inchang-ing:fix-urlutils-unencoded-credentials
Closed

inchang-ing wants to merge 2 commits into
mahmoud:masterfrom
inchang-ing:fix-urlutils-unencoded-credentials

Conversation

@inchang-ing

Copy link
Copy Markdown

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_url then tried to interpret the password as the port and raised URLParseError (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 raised URLParseError where urllib.parse had been silently producing a wrong result, so I dug into urlutils to 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 full tests/test_urlutils.py suite (129 tests) still passes.

Test plan

  • pytest tests/test_urlutils.py — all 129 pass.
  • New tests assert port == 443 / port == 8080 and correct username/password for the reported cases.

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 Saadia751 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@inchang-ing

Copy link
Copy Markdown
Author

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 _is_truncated_userinfo() helper: a truncated userinfo always contains ':' (user:password), while an ordinary authority is either a bare host (no ':') or host:port, whose text after the last ':' is digits. IPv6 literals start with '[' and are excluded explicitly.

Verified:

  • URL('http://example.com?@other.example').host == 'example.com', .path/.query_params keep the '@…' content — same for the '/@other.example' variant.
  • host:port (http://example.com:8080?@other.example) and IPv6 (http://[::1]?@other.example) also keep their host/port, added as regression tests alongside yours.
  • Discrimination: dropping the guard fails those three new tests; with it, tests/test_urlutils.py is 132 passed and the full suite is 530 passed.

One known limit worth naming: a password that is entirely digits (e.g. http://user:123?@host) is indistinguishable from host:port and is no longer recovered. I chose that over breaking ordinary URLs, but say the word if you'd rather flip the trade-off.

@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

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.

@mahmoud mahmoud closed this Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

boltons.urlutils.URL cannot handle some characters in credentials

3 participants