Skip to content

Commit bc0dc9d

Browse files
[3.13] gh-134098: Fix handling %-encoded trailing slash in SimpleHTTPRequestHandler (GH-134099) (GH-134124)
(cherry picked from commit 2f1ecb3) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6dae082 commit bc0dc9d

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Lib/http/server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ def send_head(self):
701701
f = None
702702
if os.path.isdir(path):
703703
parts = urllib.parse.urlsplit(self.path)
704-
if not parts.path.endswith('/'):
704+
if not parts.path.endswith(('/', '%2f', '%2F')):
705705
# redirect browser - doing basically what apache does
706706
self.send_response(HTTPStatus.MOVED_PERMANENTLY)
707707
new_parts = (parts[0], parts[1], parts[2] + '/',
@@ -840,14 +840,14 @@ def translate_path(self, path):
840840
841841
"""
842842
# abandon query parameters
843-
path = path.split('?',1)[0]
844-
path = path.split('#',1)[0]
843+
path = path.split('#', 1)[0]
844+
path = path.split('?', 1)[0]
845845
# Don't forget explicit trailing slash when normalizing. Issue17324
846-
trailing_slash = path.rstrip().endswith('/')
847846
try:
848847
path = urllib.parse.unquote(path, errors='surrogatepass')
849848
except UnicodeDecodeError:
850849
path = urllib.parse.unquote(path)
850+
trailing_slash = path.endswith('/')
851851
path = posixpath.normpath(path)
852852
words = path.split('/')
853853
words = filter(None, words)

Lib/test/test_httpservers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,19 @@ def test_get(self):
581581
# check for trailing "/" which should return 404. See Issue17324
582582
response = self.request(self.base_url + '/test/')
583583
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
584+
response = self.request(self.base_url + '/test%2f')
585+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
586+
response = self.request(self.base_url + '/test%2F')
587+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
584588
response = self.request(self.base_url + '/')
585589
self.check_status_and_reason(response, HTTPStatus.OK)
590+
response = self.request(self.base_url + '%2f')
591+
self.check_status_and_reason(response, HTTPStatus.OK)
592+
response = self.request(self.base_url + '%2F')
593+
self.check_status_and_reason(response, HTTPStatus.OK)
586594
response = self.request(self.base_url)
587595
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
596+
self.assertEqual(response.getheader("Location"), self.base_url + "/")
588597
self.assertEqual(response.getheader("Content-Length"), "0")
589598
response = self.request(self.base_url + '/?hi=2')
590599
self.check_status_and_reason(response, HTTPStatus.OK)
@@ -690,6 +699,8 @@ def test_path_without_leading_slash(self):
690699
self.check_status_and_reason(response, HTTPStatus.OK)
691700
response = self.request(self.tempdir_name)
692701
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
702+
self.assertEqual(response.getheader("Location"),
703+
self.tempdir_name + "/")
693704
response = self.request(self.tempdir_name + '/?hi=2')
694705
self.check_status_and_reason(response, HTTPStatus.OK)
695706
response = self.request(self.tempdir_name + '?hi=1')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix handling paths that end with a percent-encoded slash (``%2f`` or
2+
``%2F``) in :class:`http.server.SimpleHTTPRequestHandler`.

0 commit comments

Comments
 (0)