[utils] remove_dot_segments: Fix missing leading slashes (#17937)

Authored by: doe1080
This commit is contained in:
doe1080
2026-11-24 19:07:08 +01:00
committed by GitHub
parent 6f73a8d166
commit da452a569f
2 changed files with 31 additions and 7 deletions
+6
View File
@@ -1056,6 +1056,8 @@ class TestUtil(unittest.TestCase):
self.assertEqual(normalize_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0')
self.assertEqual(normalize_url('http://www.example.com/../a/b/../c/./d.html'), 'http://www.example.com/a/c/d.html')
self.assertEqual(normalize_url('file:///..//dir/media/video.mp4'), 'file:///dir/media/video.mp4')
self.assertEqual(normalize_url('file:////server/share/dir/../video.mp4'), 'file:////server/share/video.mp4')
def test_remove_dot_segments(self):
self.assertEqual(remove_dot_segments('/a/b/c/./../../g'), '/a/g')
@@ -1063,6 +1065,8 @@ class TestUtil(unittest.TestCase):
self.assertEqual(remove_dot_segments('/ad/../cd'), '/cd')
self.assertEqual(remove_dot_segments('/ad/../cd/'), '/cd/')
self.assertEqual(remove_dot_segments('/..'), '/')
self.assertEqual(remove_dot_segments('/../'), '/')
self.assertEqual(remove_dot_segments('/..//a'), '//a')
self.assertEqual(remove_dot_segments('/./'), '/')
self.assertEqual(remove_dot_segments('/./a'), '/a')
self.assertEqual(remove_dot_segments('/abc/./.././d/././e/.././f/./../../ghi'), '/ghi')
@@ -1072,6 +1076,8 @@ class TestUtil(unittest.TestCase):
self.assertEqual(remove_dot_segments(''), '')
self.assertEqual(remove_dot_segments('/../a/b/c'), '/a/b/c')
self.assertEqual(remove_dot_segments('../a'), 'a')
self.assertEqual(remove_dot_segments('a/..'), '/')
self.assertEqual(remove_dot_segments('a/../b'), '/b')
self.assertEqual(remove_dot_segments('./a'), 'a')
self.assertEqual(remove_dot_segments('.'), '')
self.assertEqual(remove_dot_segments('////'), '////')
+25 -7
View File
@@ -206,9 +206,12 @@ def clean_headers(headers: HTTPHeaderDict):
def remove_dot_segments(path):
# Implements RFC3986 5.2.4 remote_dot_segments
# Pseudo-code: https://tools.ietf.org/html/rfc3986#section-5.2.4
# Implements RFC 3986 5.2.4 remove_dot_segments
# https://www.rfc-editor.org/info/rfc3986/#section-5.2.4
# https://github.com/urllib3/urllib3/blob/ba49f5c4e19e6bca6827282feb77a3c9f937e64b/src/urllib3/util/url.py#L263
if '/.' not in path and not path.startswith('.'):
return path
output = []
segments = path.split('/')
for s in segments:
@@ -217,10 +220,10 @@ def remove_dot_segments(path):
elif s == '..':
if output:
output.pop()
if not output:
output.append('')
else:
output.append(s)
if not segments[0] and (not output or output[0]):
output.insert(0, '')
if segments[-1] in ('.', '..'):
output.append('')
return '/'.join(output)
@@ -234,13 +237,28 @@ def escape_rfc3986(s):
def normalize_url(url):
"""Normalize URL as suggested by RFC 3986"""
url_parsed = urllib.parse.urlparse(url)
return url_parsed._replace(
path = remove_dot_segments(url_parsed.path)
if url_parsed.scheme == 'file' and path.startswith('//') and not url_parsed.path.startswith('//'):
path = '/' + path.lstrip('/')
normalized = url_parsed._replace(
netloc=url_parsed.netloc.encode('idna').decode('ascii'),
path=escape_rfc3986(remove_dot_segments(url_parsed.path)),
path=escape_rfc3986(path),
params=escape_rfc3986(url_parsed.params),
query=escape_rfc3986(url_parsed.query),
fragment=escape_rfc3986(url_parsed.fragment),
).geturl()
)
url = normalized.geturl()
# compat: <=3.12: Restore the empty authority delimiter if urlunsplit() omits it
if (
normalized.scheme == 'file'
and not normalized.netloc
and normalized.path.startswith('//')
and not url.startswith(normalized.path, len('file://'))
):
return url.replace('file:', 'file://', 1)
return url
def select_proxy(url, proxies):