Skip to content

Commit 56b5bfb

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-134098: Fix handling %-encoded trailing slash in SimpleHTTPRequestHandler (GH-134099)
(cherry picked from commit 2f1ecb3) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent f893232 commit 56b5bfb

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
@@ -750,7 +750,7 @@ def send_head(self):
750750
f = None
751751
if os.path.isdir(path):
752752
parts = urllib.parse.urlsplit(self.path)
753-
if not parts.path.endswith('/'):
753+
if not parts.path.endswith(('/', '%2f', '%2F')):
754754
# redirect browser - doing basically what apache does
755755
self.send_response(HTTPStatus.MOVED_PERMANENTLY)
756756
new_parts = (parts[0], parts[1], parts[2] + '/',
@@ -890,14 +890,14 @@ def translate_path(self, path):
890890
891891
"""
892892
# abandon query parameters
893-
path = path.split('?',1)[0]
894-
path = path.split('#',1)[0]
893+
path = path.split('#', 1)[0]
894+
path = path.split('?', 1)[0]
895895
# Don't forget explicit trailing slash when normalizing. Issue17324
896-
trailing_slash = path.rstrip().endswith('/')
897896
try:
898897
path = urllib.parse.unquote(path, errors='surrogatepass')
899898
except UnicodeDecodeError:
900899
path = urllib.parse.unquote(path)
900+
trailing_slash = path.endswith('/')
901901
path = posixpath.normpath(path)
902902
words = path.split('/')
903903
words = filter(None, words)

Lib/test/test_httpservers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,19 @@ def test_get(self):
615615
# check for trailing "/" which should return 404. See Issue17324
616616
response = self.request(self.base_url + '/test/')
617617
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
618+
response = self.request(self.base_url + '/test%2f')
619+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
620+
response = self.request(self.base_url + '/test%2F')
621+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
618622
response = self.request(self.base_url + '/')
619623
self.check_status_and_reason(response, HTTPStatus.OK)
624+
response = self.request(self.base_url + '%2f')
625+
self.check_status_and_reason(response, HTTPStatus.OK)
626+
response = self.request(self.base_url + '%2F')
627+
self.check_status_and_reason(response, HTTPStatus.OK)
620628
response = self.request(self.base_url)
621629
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
630+
self.assertEqual(response.getheader("Location"), self.base_url + "/")
622631
self.assertEqual(response.getheader("Content-Length"), "0")
623632
response = self.request(self.base_url + '/?hi=2')
624633
self.check_status_and_reason(response, HTTPStatus.OK)
@@ -724,6 +733,8 @@ def test_path_without_leading_slash(self):
724733
self.check_status_and_reason(response, HTTPStatus.OK)
725734
response = self.request(self.tempdir_name)
726735
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
736+
self.assertEqual(response.getheader("Location"),
737+
self.tempdir_name + "/")
727738
response = self.request(self.tempdir_name + '/?hi=2')
728739
self.check_status_and_reason(response, HTTPStatus.OK)
729740
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)