]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133889: Only show the path of the URL in the SimpleHTTPRequestHandler page (GH...
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 18 May 2025 18:09:51 +0000 (21:09 +0300)
committerGitHub <noreply@github.com>
Sun, 18 May 2025 18:09:51 +0000 (18:09 +0000)
The query and fragment are ambiguous and not used.

Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS.d/next/Library/2025-05-17-12-40-12.gh-issue-133889.Eh-zO4.rst [new file with mode: 0644]

index f42e9a375e479adb524bf0f0b30aaa8b094d23e5..abf9f87a1fc711e79d3de9fe60cbe93a4e44f246 100644 (file)
@@ -818,11 +818,14 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
             return None
         list.sort(key=lambda a: a.lower())
         r = []
+        displaypath = self.path
+        displaypath = displaypath.split('#', 1)[0]
+        displaypath = displaypath.split('?', 1)[0]
         try:
-            displaypath = urllib.parse.unquote(self.path,
+            displaypath = urllib.parse.unquote(displaypath,
                                                errors='surrogatepass')
         except UnicodeDecodeError:
-            displaypath = urllib.parse.unquote(self.path)
+            displaypath = urllib.parse.unquote(displaypath)
         displaypath = html.escape(displaypath, quote=False)
         enc = sys.getfilesystemencoding()
         title = f'Directory listing for {displaypath}'
index 11c74a02bf29036d481809752dba62e0f7b788d0..df5d2a7bedc4b2fb75cdaec96e092cce8fa98823 100644 (file)
@@ -627,13 +627,14 @@ class SimpleHTTPServerTestCase(BaseTestCase):
                 self.check_list_dir_filename(filename)
                 os_helper.unlink(os.path.join(self.tempdir, filename))
 
-    def test_undecodable_parameter(self):
-        # sanity check using a valid parameter
+    def test_list_dir_with_query_and_fragment(self):
+        prefix = f'listing for {self.base_url}/</'.encode('latin1')
+        response = self.request(self.base_url + '/#123').read()
+        self.assertIn(prefix + b'title>', response)
+        self.assertIn(prefix + b'h1>', response)
         response = self.request(self.base_url + '/?x=123').read()
-        self.assertRegex(response, rf'listing for {self.base_url}/\?x=123'.encode('latin1'))
-        # now the bogus encoding
-        response = self.request(self.base_url + '/?x=%bb').read()
-        self.assertRegex(response, rf'listing for {self.base_url}/\?x=\xef\xbf\xbd'.encode('latin1'))
+        self.assertIn(prefix + b'title>', response)
+        self.assertIn(prefix + b'h1>', response)
 
     def test_get_dir_redirect_location_domain_injection_bug(self):
         """Ensure //evil.co/..%2f../../X does not put //evil.co/ in Location.
diff --git a/Misc/NEWS.d/next/Library/2025-05-17-12-40-12.gh-issue-133889.Eh-zO4.rst b/Misc/NEWS.d/next/Library/2025-05-17-12-40-12.gh-issue-133889.Eh-zO4.rst
new file mode 100644 (file)
index 0000000..58b213e
--- /dev/null
@@ -0,0 +1,3 @@
+The generated directory listing page in
+:class:`http.server.SimpleHTTPRequestHandler` now only shows the decoded
+path component of the requested URL, and not the query and fragment.