gh-117807: Handle invalid UTF-8 in mimetypes map files - #151216
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
2bf1045 to
8aa7d8f
Compare
|
@sobolevn I saw that you’ve worked on I used |
|
@serhiy-storchaka on gh-117807 you suggested adding |
kulikjak
left a comment
There was a problem hiding this comment.
Hi, I don't have the explicit approval rights, but I still wanted to mention that I tested your change and it works as expected - the original issue we had is gone. Thanks!
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I think it is safer to use 'surrogateescape'. For comments, there is no difference, but if non-UTF-8 sequence occurs in MIME type or extension, 'replace' will be lossy.
Also, the scope is wider than the title/NEWS suggest. The fix tolerates invalid bytes everywhere, not just in comments. This is fine, but please update the title/NEWS -- they should say "in MIME map files".
8aa7d8f to
22bc27a
Compare
|
Thanks, done. Switched both reads to surrogateescape, added a test for the type/extension case, and updated the title and NEWS to say map files instead of comments. |
|
Thanks @harjothkhara for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
GH-155183 is a backport of this pull request to the 3.15 branch. |
|
GH-155184 is a backport of this pull request to the 3.14 branch. |
|
GH-155185 is a backport of this pull request to the 3.13 branch. |
Reading a MIME map file that contains invalid UTF-8 bytes in a comment raised
UnicodeDecodeError, becausemimetypesopens these files withencoding='utf-8'and strict error handling. A stray non-UTF-8 byte in a comment line shouldn't make the whole file unreadable.Fix: open MIME map files with
errors='replace'in bothMimeTypes.read()andread_mime_types(), so invalid bytes are tolerated and the valid type mappings still load.Added a regression test that reads a file with a non-UTF-8 byte in a comment and checks the following mapping parses correctly. Includes a NEWS entry.
Disclosure: AI-assisted; I've reviewed the change.