Fix canonicalize_url IDNA-encoding the port and userinfo of a non-ASCII host#270
Open
patchwright wants to merge 1 commit into
Open
Fix canonicalize_url IDNA-encoding the port and userinfo of a non-ASCII host#270patchwright wants to merge 1 commit into
patchwright wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #222.
Problem
canonicalize_urlruns IDNA encoding on the entire netloc, so when a non-ASCII host has a port (or userinfo), those parts get folded into the last IDNA label and mangled:>>> canonicalize_url('https://тест.тест:33') 'https://xn--e1aybc.xn--:33-qdd4dec/' # the :33 is corruptedExpected:
safe_url_stringalready handles host and port separately; only thecanonicalize_urlpath (via_safe_ParseResult) was affected.Fix
Add
_idna_netloc, which applies IDNA to the host substring only and leaves anyuserinfo@prefix and:portsuffix verbatim. IPv6 literals are ASCII (IDNA is a no-op) so the bracketed host is returned untouched. Because IDNA is a no-op on ASCII, no existing ASCII-netloc behaviour changes. The existingUnicodeErrorfallback (too-long / missing labels) is preserved.Tests
Added
test_canonicalize_idns_with_portcovering host+port, userinfo+port (userinfo stays case-sensitive), and the no-port case. Proven to fail on unpatched source (xn--33-mlc2cdc) and pass with the fix. Full suite: 359 passed, 0 failures; mypy + ruff clean.Note: the trailing-multiple-dots addendum in #222 is left out of scope, as the reporter noted it may be standards-correct behaviour.