From 96ef1dec123e8ecef55e77fad36f783d99d37bdc Mon Sep 17 00:00:00 2001 From: brian Date: Tue, 31 Mar 2026 18:32:38 -0400 Subject: [PATCH] Preserve leading slashes in request path_url Fixes issue where S3 presigned URLs with keys starting with '/' were incorrectly modified, breaking URL signatures. URLs like 'https://bucket.s3.amazonaws.com//key_name' now correctly preserve '//key_name' in the path_url. Closes #6711 --- src/requests/adapters.py | 2 -- tests/test_adapters.py | 14 ++++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/requests/adapters.py b/src/requests/adapters.py index 98f74465f2..279194688e 100644 --- a/src/requests/adapters.py +++ b/src/requests/adapters.py @@ -545,8 +545,6 @@ 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 index 6c55d5a130..aaf281c761 100644 --- a/tests/test_adapters.py +++ b/tests/test_adapters.py @@ -1,8 +1,14 @@ import requests.adapters -def test_request_url_trims_leading_path_separators(): - """See also https://github.com/psf/requests/issues/6643.""" +def test_request_url_preserves_leading_path_separators(): + """See also https://github.com/psf/requests/issues/6711. + + S3 presigned URLs with keys starting with '/' produce paths like + '//key_name'. We should preserve leading slashes to avoid breaking signatures. + """ 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, {}) + p = requests.Request( + method="GET", url="https://bucket.s3.amazonaws.com//key_with_leading_slash.txt" + ).prepare() + assert "//key_with_leading_slash.txt" == a.request_url(p, {})