https://github.com/python/cpython/commit/76f4555c4ff10f5dda58d6528c17787df41e256c
commit: 76f4555c4ff10f5dda58d6528c17787df41e256c
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: zooba <[email protected]>
date: 2026-10-16T11:57:19+01:00
summary:

[3.14] gh-159428: Don't read mimetypes.knownfiles by default on Windows 
(GH-159445)

Make knownfiles empty on Windows so that only the registry is used by default. 
Files passed to init() or assigned to knownfiles explicitly are still read.
(cherry picked from commit 04a29bf8f9ff8526cd96cb7368400173303a390a)

Co-authored-by: Cary Hawkins <[email protected]>

files:
A Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst
M Lib/mimetypes.py
M Lib/test/test_mimetypes.py

diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index e7581d2ae0ace41..ba942c6f3baf2c7 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -23,6 +23,8 @@
 read_mime_types(file) -- parse one file, return a dictionary or None
 """
 
+import sys
+
 try:
     from _winapi import _mimetypes_read_windows_registry
 except ImportError:
@@ -40,17 +42,23 @@
     "suffix_map", "encodings_map", "types_map", "common_types"
 ]
 
-knownfiles = [
-    "/etc/mime.types",
-    "/etc/httpd/mime.types",                    # Mac OS X
-    "/etc/httpd/conf/mime.types",               # Apache
-    "/etc/apache/mime.types",                   # Apache 1
-    "/etc/apache2/mime.types",                  # Apache 2
-    "/usr/local/etc/httpd/conf/mime.types",
-    "/usr/local/lib/netscape/mime.types",
-    "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
-    "/usr/local/etc/mime.types",                # Apache 1.3
-    ]
+if sys.platform == "win32":
+    # The default paths would resolve to the root of the current drive on 
Windows
+    # (e.g. C:\etc\mime.types), which unprivileged users can usually create.
+    # Only the registry is used by default.
+    knownfiles = []
+else:
+    knownfiles = [
+        "/etc/mime.types",
+        "/etc/httpd/mime.types",                    # Mac OS X
+        "/etc/httpd/conf/mime.types",               # Apache
+        "/etc/apache/mime.types",                   # Apache 1
+        "/etc/apache2/mime.types",                  # Apache 2
+        "/usr/local/etc/httpd/conf/mime.types",
+        "/usr/local/lib/netscape/mime.types",
+        "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
+        "/usr/local/etc/mime.types",                # Apache 1.3
+        ]
 
 inited = False
 _db = None
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index a7d1d724ff0e18b..8704a7353cfcbf1 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -265,6 +265,10 @@ def test_init_knownfiles(self):
         mimetypes.init()
         self.assertEqual(guess_file_type("file.test2")[0], "testing/test2")
 
+    @unittest.skipIf(sys.platform == "win32", "non-Windows only")
+    def test_default_knownfiles(self):
+        self.assertIn("/etc/mime.types", knownfiles)
+
     def test_added_types_are_used(self):
         mimetypes.add_type('testing/default-type', '')
         mime_type, _ = mimetypes.guess_type('')
@@ -505,6 +509,9 @@ def test_registry_parsing(self):
         eq(self.db.guess_type("image.jpg"), ("image/jpeg", None))
         eq(self.db.guess_type("image.png"), ("image/png", None))
 
+    def test_default_knownfiles(self):
+        self.assertEqual(knownfiles, [])
+
     @unittest.skipIf(not hasattr(_winapi, "_mimetypes_read_windows_registry"),
                      "read_windows_registry accelerator unavailable")
     def test_registry_accelerator(self):
diff --git 
a/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst 
b/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst
new file mode 100644
index 000000000000000..d481ed2b74a20ea
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2026-10-15-03-47-08.gh-issue-159428.XHRKwY.rst
@@ -0,0 +1,2 @@
+:mod:`mimetypes` no longer reads the files listed in 
:data:`mimetypes.knownfiles`
+by default on Windows, and uses only the registry.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to