Skip to content

Commit

Permalink
Trim excess leading path separators
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sigmavirus24 committed Feb 22, 2024
1 parent 7a13c04 commit 57e8c3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/requests/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import os.path
import re
import socket # noqa: F401

from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
Expand Down Expand Up @@ -389,7 +390,7 @@ def request_url(self, request, proxies):
proxy_scheme = urlparse(proxy).scheme.lower()
using_socks_proxy = proxy_scheme.startswith("socks")

url = request.path_url
url = re.sub("^/+", "/", request.path_url)
if is_proxied_http_request and not using_socks_proxy:
url = urldefragauth(request.url)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -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, {})

0 comments on commit 57e8c3b

Please sign in to comment.