Skip to content

Fix Unicode URL authority encoding with ports and userinfo - #841

Open
student-shivamm wants to merge 1 commit into
getsentry:masterfrom
student-shivamm:codex/unicode-authority
Open

student-shivamm wants to merge 1 commit into
getsentry:masterfrom
student-shivamm:codex/unicode-authority

Conversation

@student-shivamm

Copy link
Copy Markdown

What type of PR is this?

  • Bug Fix

Description

Registering a Unicode hostname with an explicit port can raise
urllib3.exceptions.LocationParseError instead of matching the same URL passed
to Requests. _clean_unicode() currently treats the full authority as DNS
labels, so example.рф:8443 becomes example.xn--:8443-uye2a. Userinfo can
likewise become part of an encoded hostname label.

Separate userinfo and the port before applying the existing Punycode conversion
to hostname labels. This also fixes Unicode add_passthru() prefixes while
preserving the significance of credentials, ports and paths.

Minimal reproduction on master:

import requests
import responses

with responses.RequestsMock(assert_all_requests_are_fired=False) as mock:
    url = "https://example.рф:8443/path"
    mock.get(url, body="matched")
    assert requests.get(url).text == "matched"

No new dependency or public API is added.

Related Issues

No existing matching issue found.

PR checklist

  • Read the contributing guidelines.
  • Ran complete tox and all-files pre-commit checks locally.
    Applicable configured pre-commit hooks pass for all three changed files.
    Full Python 3.12 test runs pass with Requests 2.34.2 / urllib3 2.8.0 and with
    Requests 2.30.0 / urllib3 1.26.20: 278 passed in each environment.
    Other Python versions/full tox matrix were not run.
    Mypy 2.4.0 reports the same three existing errors on pinned master and this
    patch, in unchanged matchers.py and test_recorder.py; it is not claimed to pass.
  • Added the change to CHANGES.

Added/updated tests?

  • Yes: 17 parameterized cases cover matching, stubbed passthrough and
    significant differences in authority/path components. The identical suite
    on master yields 15 failed / 263 passed; the patch yields 278 passed.
    All changed executable encoding lines are covered.

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.

1 participant