Skip to content

Commit

Permalink
pythongh-100474: Fix handling of dirs named index.html in http.server (
Browse files Browse the repository at this point in the history
…pythonGH-100475)

If you had a directory called index.html or index.htm within a directory, it would cause http.server to return a 404 Not Found error instead of the directory listing. This came about due to not checking that the index was a regular file.

I have also added a test case for this situation.
(cherry picked from commit 46e6a28)

Co-authored-by: James Frost <git@frost.cx>
Automerge-Triggered-By: GH:merwok
  • Loading branch information
Fraetor authored and miss-islington committed Dec 24, 2022
1 parent a7eee89 commit b2eea68
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ def send_head(self):
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
if os.path.isfile(index):
path = index
break
else:
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_httpservers.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ def test_get(self):
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
response = self.request('/' + 'ThisDoesNotExist' + '/')
self.check_status_and_reason(response, HTTPStatus.NOT_FOUND)
os.makedirs(os.path.join(self.tempdir, 'spam', 'index.html'))
response = self.request(self.base_url + '/spam/')
self.check_status_and_reason(response, HTTPStatus.OK)

data = b"Dummy index file\r\n"
with open(os.path.join(self.tempdir_name, 'index.html'), 'wb') as f:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`http.server` now checks that an index page is actually a regular file before trying
to serve it. This avoids issues with directories named ``index.html``.

0 comments on commit b2eea68

Please sign in to comment.