Skip to content

Commit 0853259

Browse files
serhiy-storchakamiss-islington
authored andcommitted
pythongh-134098: Fix handling %-encoded trailing slash in SimpleHTTPRequestHandler (pythonGH-134099)
(cherry picked from commit 2f1ecb3) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 510e28e commit 0853259

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
@@ -504,10 +504,19 @@ def test_get(self):
504504
# check for trailing "/" which should return 404. See Issue17324
505505
response = self.request(self.base_url + '/test/')
506506
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
507+
response = self.request(self.base_url + '/test%2f')
508+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
509+
response = self.request(self.base_url + '/test%2F')
510+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
507511
response = self.request(self.base_url + '/')
508512
self.check_status_and_reason(response, HTTPStatus.OK)
513+
response = self.request(self.base_url + '%2f')
514+
self.check_status_and_reason(response, HTTPStatus.OK)
515+
response = self.request(self.base_url + '%2F')
516+
self.check_status_and_reason(response, HTTPStatus.OK)
509517
response = self.request(self.base_url)
510518
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
519+
self.assertEqual(response.getheader("Location"), self.base_url + "/")
511520
self.assertEqual(response.getheader("Content-Length"), "0")
512521
response = self.request(self.base_url + '/?hi=2')
513522
self.check_status_and_reason(response, HTTPStatus.OK)
@@ -613,6 +622,8 @@ def test_path_without_leading_slash(self):
613622
self.check_status_and_reason(response, HTTPStatus.OK)
614623
response = self.request(self.tempdir_name)
615624
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
625+
self.assertEqual(response.getheader("Location"),
626+
self.tempdir_name + "/")
616627
response = self.request(self.tempdir_name + '/?hi=2')
617628
self.check_status_and_reason(response, HTTPStatus.OK)
618629
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)