[rh:urllib] Fix relative redirects with dot segments (#17712)

Authored by: doe1080
This commit is contained in:
doe1080
2026-09-27 19:55:49 +02:00
committed by GitHub
parent f9eee6ba62
commit c2901ed367
2 changed files with 7 additions and 11 deletions
+6
View File
@@ -180,6 +180,11 @@ class HTTPTestRequestHandler(http.server.BaseHTTPRequestHandler):
self.send_header('Location', '/a/b/./../../headers')
self.send_header('Content-Length', '0')
self.end_headers()
elif self.path == '/a/b/redirect_relative_dotsegments':
self.send_response(301)
self.send_header('Location', '../../headers')
self.send_header('Content-Length', '0')
self.end_headers()
elif self.path == '/redirect_dotsegments_absolute':
self.send_response(301)
# redirect to /headers but with dot segments before - absolute url
@@ -412,6 +417,7 @@ class TestHTTPRequestHandler(TestRequestHandlerBase):
@pytest.mark.parametrize('path', [
'/a/b/./../../headers',
'/redirect_dotsegments',
'/a/b/redirect_relative_dotsegments',
# https://github.com/yt-dlp/yt-dlp/issues/9020
'/redirect_dotsegments_absolute',
])
+1 -11
View File
@@ -161,17 +161,7 @@ class HTTPHandler(urllib.request.AbstractHTTPHandler):
if decoded_response is not None:
resp = urllib.request.addinfourl(io.BytesIO(decoded_response), old_resp.headers, old_resp.url, old_resp.code)
resp.msg = old_resp.msg
# Percent-encode redirect URL of Location HTTP header to satisfy RFC 3986 (see
# https://github.com/ytdl-org/youtube-dl/issues/6457).
if 300 <= resp.code < 400:
location = resp.headers.get('Location')
if location:
# As of RFC 2616 default charset is iso-8859-1 that is respected by Python 3
location = location.encode('iso-8859-1').decode()
location_escaped = normalize_url(location)
if location != location_escaped:
del resp.headers['Location']
resp.headers['Location'] = location_escaped
return resp
https_request = http_request