From 60389df6d69ce833164696dcf36cbb43336d3426 Mon Sep 17 00:00:00 2001 From: Ian Stapleton Cordasco Date: Wed, 21 Feb 2024 19:09:48 -0600 Subject: [PATCH] Trim excess leading path separators A URL with excess leading / (path-separator)s would cause urllib3 to attempt to reparse the request-uri as a full URI with a host and port. This bypasses that logic in ConnectionPool.urlopen by replacing these leading /s with just a single /. Closes #6643 --- src/requests/adapters.py | 3 +++ tests/test_adapters.py | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/test_adapters.py diff --git a/src/requests/adapters.py b/src/requests/adapters.py index eb240fa954..fc5606bdcb 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -390,6 +390,9 @@ def request_url(self, request, proxies): using_socks_proxy = proxy_scheme.startswith("socks") url = request.path_url + if url.startswith("//"): # Don't confuse urllib3 + url = f"/{url.lstrip('/')}" + if is_proxied_http_request and not using_socks_proxy: url = urldefragauth(request.url) diff --git a/tests/test_adapters.py b/tests/test_adapters.py new file mode 100644 index 0000000000..6c55d5a130 --- /dev/null +++ b/tests/test_adapters.py @@ -0,0 +1,8 @@ +import requests.adapters + + +def test_request_url_trims_leading_path_separators(): + """See also https://github.com/psf/requests/issues/6643.""" + a = requests.adapters.HTTPAdapter() + p = requests.Request(method="GET", url="http://127.0.0.1:10000//v:h").prepare() + assert "/v:h" == a.request_url(p, {})