Wrap urllib3 LocationParseError from create_connection in InvalidURL (#5744)#7471
Open
jbbqqf wants to merge 1 commit into
Open
Wrap urllib3 LocationParseError from create_connection in InvalidURL (#5744)#7471jbbqqf wants to merge 1 commit into
jbbqqf wants to merge 1 commit into
Conversation
…lidURL When a hostname has labels that fail validation (e.g. a label longer than 63 characters, as constrained by RFC 1035), prepare_url's existing handler catches LocationParseError at parse time and surfaces it as InvalidURL. The same exception can also be raised later from urllib3's util.connection.create_connection, where it bubbles up through HTTPAdapter.send. The (_SSLError, _HTTPError) except clause in send catches it (LocationParseError is a urllib3.HTTPError) but the final `else: raise` re-raises it bare, so callers see a raw urllib3 exception instead of a requests-native one. Add an explicit branch that converts LocationValueError (parent of LocationParseError) to InvalidURL, mirroring the behaviour of get_connection_with_tls_context earlier in send. Regression test: parametrize test_errors with a 64-character DNS label and assert that requests.exceptions.InvalidURL is raised. Fixes psf#5744
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.
Summary
Wrap
urllib3.exceptions.LocationParseErrorraised from insideurllib3.util.connection.create_connectioninrequests.exceptions.InvalidURL,so callers see a requests-native exception instead of a raw urllib3 one. Fixes #5744.
PreparedRequest.prepare_urlalready catchesLocationParseErrorat parse timeand converts it to
InvalidURL. The same exception class can also be raisedlater from urllib3's
create_connectionwhen the hostname survivesparse_urlbut contains a label that fails
host.encode("idna")(e.g. a DNS label longerthan 63 characters per RFC 1035). The
(_SSLError, _HTTPError)exceptclausein
HTTPAdapter.senddoes catch it —LocationParseErroris aurllib3.exceptions.HTTPError— but falls through to the trailingelse: raiseand re-raises it bare. The fix adds an explicit
isinstance(e, LocationValueError)branch that wraps it in
InvalidURL, mirroring the behaviour ofget_connection_with_tls_contextearlier in the same method.Reproduce BEFORE/AFTER yourself (copy-paste)
What I ran locally
The single failure is
test_proxy_error, which is environment-dependent(it relies on a network resolution behaviour) and reproduces unchanged on
origin/mainwith the same setup — unrelated to this PR.The new parametrize row in
test_errors:And the same test on
origin/main(regression):Edge cases
http://aaa...64a.example.com(label > 63 chars)urllib3.exceptions.LocationParseErrorleaksrequests.exceptions.InvalidURLhost.encode("idna")increate_connectionraisesLocationParseErrorhttp://*.example.comInvalidURL(caught inprepare_url)InvalidURL(unchanged)test_preparing_bad_urlhttp://fe80::5054:ff:fe5a:fc0(zone-id-less IPv6)InvalidURLInvalidURL(unchanged)test_errorsConnectTimeoutErrorConnectTimeoutConnectTimeout(unchanged)MaxRetryErrorbranch still catchesProtocolErrorConnectionErrorConnectionError(unchanged)(ProtocolError, OSError)branch still catches_InvalidHeaderInvalidHeaderInvalidHeader(unchanged)PR drafted with assistance from Claude Code (Anthropic). The change was reviewed manually against requests' source. The reproducer block above is the one I used during development; reviewers can paste it verbatim.