Skip to content

Commit 2062a20

Browse files
aixtoolsncoghlan
authored andcommitted
bpo-34711: Return HTTPStatus.NOT_FOUND if path.endswith('/') and not a directory (GH-9687)
AIX allows a trailing slash on local file system paths, which isn't what we want in http.server. Accordingly, check explicitly for this case in the server code, rather than relying on the OS raising an exception. Patch by Michael Felt.
1 parent 22462da commit 2062a20

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

Lib/http/server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,14 @@ def send_head(self):
692692
else:
693693
return self.list_directory(path)
694694
ctype = self.guess_type(path)
695+
# check for trailing "/" which should return 404. See Issue17324
696+
# The test for this was added in test_httpserver.py
697+
# However, some OS platforms accept a trailingSlash as a filename
698+
# See discussion on python-dev and Issue34711 regarding
699+
# parseing and rejection of filenames with a trailing slash
700+
if path.endswith("/"):
701+
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
702+
return None
695703
try:
696704
f = open(path, 'rb')
697705
except OSError:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
http.server ensures it reports HTTPStatus.NOT_FOUND when the local path ends with "/"
2+
and is not a directory, even if the underlying OS (e.g. AIX) accepts such paths as a
3+
valid file reference. Patch by Michael Felt.

0 commit comments

Comments
 (0)