[Python-checkins] [3.10] gh-100474: Fix handling of dirs named index.html in http.server (GH-100504)

merwok webhook-mailer at python.org
Sat Dec 24 15:29:27 EST 2022


https://github.com/python/cpython/commit/ecbf136702267857b261a81b64f234965b9de913
commit: ecbf136702267857b261a81b64f234965b9de913
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: merwok <merwok at netwok.org>
date: 2022-12-24T15:29:21-05:00
summary:

[3.10] gh-100474: Fix handling of dirs named index.html in http.server (GH-100504)

Co-authored-by: James Frost <git at frost.cx>

files:
A Misc/NEWS.d/next/Library/2022-12-23-21-02-43.gh-issue-100474.gppA4U.rst
M Lib/http/server.py
M Lib/test/test_httpservers.py

diff --git a/Lib/http/server.py b/Lib/http/server.py
index 03dbaa51b798..9c218d06acf5 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -709,7 +709,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:
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
index ac8da494e9bb..a5f787ff48c9 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -488,6 +488,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:
diff --git a/Misc/NEWS.d/next/Library/2022-12-23-21-02-43.gh-issue-100474.gppA4U.rst b/Misc/NEWS.d/next/Library/2022-12-23-21-02-43.gh-issue-100474.gppA4U.rst
new file mode 100644
index 000000000000..31abfb8b87fb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-23-21-02-43.gh-issue-100474.gppA4U.rst
@@ -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``.



More information about the Python-checkins mailing list