Skip to content

Commit 2f1ecb3

Browse files
gh-134098: Fix handling %-encoded trailing slash in SimpleHTTPRequestHandler (GH-134099)
1 parent fcaf009 commit 2f1ecb3

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
@@ -692,10 +692,19 @@ def test_get(self):
692692
# check for trailing "/" which should return 404. See Issue17324
693693
response = self.request(self.base_url + '/test/')
694694
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
695+
response = self.request(self.base_url + '/test%2f')
696+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
697+
response = self.request(self.base_url + '/test%2F')
698+
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
695699
response = self.request(self.base_url + '/')
696700
self.check_status_and_reason(response, HTTPStatus.OK)
701+
response = self.request(self.base_url + '%2f')
702+
self.check_status_and_reason(response, HTTPStatus.OK)
703+
response = self.request(self.base_url + '%2F')
704+
self.check_status_and_reason(response, HTTPStatus.OK)
697705
response = self.request(self.base_url)
698706
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
707+
self.assertEqual(response.getheader("Location"), self.base_url + "/")
699708
self.assertEqual(response.getheader("Content-Length"), "0")
700709
response = self.request(self.base_url + '/?hi=2')
701710
self.check_status_and_reason(response, HTTPStatus.OK)
@@ -801,6 +810,8 @@ def test_path_without_leading_slash(self):
801810
self.check_status_and_reason(response, HTTPStatus.OK)
802811
response = self.request(self.tempdir_name)
803812
self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
813+
self.assertEqual(response.getheader("Location"),
814+
self.tempdir_name + "/")
804815
response = self.request(self.tempdir_name + '/?hi=2')
805816
self.check_status_and_reason(response, HTTPStatus.OK)
806817
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)