Skip to content

2.7.18.16: mimetypes reads C:\etc\mime.types etc. on Windows (CVE-2024-3220) - #98

Merged
icanhasmath merged 1 commit into
2.7.18.16from
2.7.18.16-mimetypes-knownfiles
Oct 28, 2026
Merged

icanhasmath merged 1 commit into
2.7.18.16from
2.7.18.16-mimetypes-knownfiles

Conversation

@icanhasmath

Copy link
Copy Markdown

Backport of python/cpython pythongh-159428 (CVE-2024-3220, PSF-2025-2) to 2.7.18.x. This is the same fix as in 3.10.22.1 (#87) and 3.9.25.1 (#88).

Problem: on Windows, mimetypes.init() reads the registry and then every path in knownfiles. Those are Unix paths such as /etc/mime.types, which on Windows resolve to the root of the current drive (C:\etc\mime.types). Unprivileged users can usually create files there, so one user could change MIME types for every Python process on the machine.

Fix: knownfiles is empty on Windows, so only the registry is read by default. Explicit init(files) calls still work.

Tests:

  • Win32MimeTypesTestCase.test_default_knownfiles runs on Windows only.
  • KnownfilesTestCase.test_default_knownfiles runs on other platforms.

On Linux, test_mimetypes, test_urllib, test_urllib2, test_SimpleHTTPServer and test_httpservers pass. Forcing sys.platform = 'win32' before import, knownfiles goes from 9 Unix paths to []. The Windows-only test needs the Windows build of the rc.

Part of 2.7.18.16. It targets the 2.7.18.16 integration branch.

CVE-2024-3220)

Backport of python/cpython pythongh-159428 (pythonGH-159445; the 3.10.22.1 port is
f3c1882) to 2.7.18.x.

mimetypes.init() reads the registry on Windows and then every file in
knownfiles.  Those are Unix paths ("/etc/mime.types", ...), which on
Windows resolve to the root of the current drive (C:\etc\mime.types).
Unprivileged users can usually create such files, and so could
register arbitrary MIME types for every Python process on the machine,
e.g. to change what SimpleHTTPServer serves a file as.  knownfiles is now
empty on Windows; only the registry is used by default.  Callers can
still pass files to init() or add to knownfiles themselves.

2.7.18.x: test_mimetypes empties mimetypes.knownfiles at import for
isolation, so the test keeps a reference to the default list taken
before that line; the non-Windows check lives in a new
KnownfilesTestCase registered in test_main().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ezequielp-activestate ezequielp-activestate left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers. The change is upstream's pythongh-159428 fix (04a29bf), line for line, and nothing has changed in Lib/mimetypes.py upstream since.

  • Repro: importing mimetypes with sys.platform forced to win32 gives the 9 Unix paths in knownfiles on the base and [] on this branch. 2.7's init() still reads the registry first through _winreg, so the default Windows behaviour is registry-only, as upstream.
  • Tests: with this branch's Lib/, test_mimetypes, test_urllib, test_urllib2, test_SimpleHTTPServer and test_httpservers pass on Linux.

Not assessed: the Windows-only test, which needs a Windows build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants