Skip to content

gh-117807: Handle invalid UTF-8 in mimetypes map files - #151216

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
harjothkhara:gh-117807-mimetypes-invalid-utf8
Aug 4, 2026
Merged

gh-117807: Handle invalid UTF-8 in mimetypes map files#151216
serhiy-storchaka merged 1 commit into
python:mainfrom
harjothkhara:gh-117807-mimetypes-invalid-utf8

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Reading a MIME map file that contains invalid UTF-8 bytes in a comment raised UnicodeDecodeError, because mimetypes opens these files with encoding='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 both MimeTypes.read() and read_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.

@python-cla-bot

python-cla-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Jun 10, 2026

Copy link
Copy Markdown

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 skip news label instead.

@harjothkhara
harjothkhara force-pushed the gh-117807-mimetypes-invalid-utf8 branch from 2bf1045 to 8aa7d8f Compare June 10, 2026 01:27
@harjothkhara
harjothkhara marked this pull request as ready for review June 10, 2026 02:10
@harjothkhara
harjothkhara requested a review from a team as a code owner June 10, 2026 02:10
@harjothkhara

Copy link
Copy Markdown
Contributor Author

@sobolevn I saw that you’ve worked on mimetypes recently, could you please take a look at this fix?

I used errors='replace' so one bad byte in a comment doesn’t break the whole map file. I can switch to surrogateescape or skip the bad line if you prefer.

@harjothkhara

harjothkhara commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka on gh-117807 you suggested adding errors='replace' (or surrogateescape). This PR implements errors='replace' across the three read paths, with a regression test and NEWS. Can you please review when you have some time?

@kulikjak kulikjak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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".

@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 4, 2026
@harjothkhara
harjothkhara force-pushed the gh-117807-mimetypes-invalid-utf8 branch from 8aa7d8f to 22bc27a Compare August 4, 2026 15:45
@harjothkhara harjothkhara changed the title gh-117807: Handle invalid UTF-8 in mimetypes comments gh-117807: Handle invalid UTF-8 in mimetypes map files Aug 4, 2026
@harjothkhara

Copy link
Copy Markdown
Contributor Author

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.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit 30ac23e into python:main Aug 4, 2026
54 checks passed
@miss-islington-app

Copy link
Copy Markdown

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.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Aug 4, 2026

Copy link
Copy Markdown

GH-155183 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 4, 2026
@bedevere-app

bedevere-app Bot commented Aug 4, 2026

Copy link
Copy Markdown

GH-155184 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 4, 2026
@bedevere-app

bedevere-app Bot commented Aug 4, 2026

Copy link
Copy Markdown

GH-155185 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Aug 4, 2026
serhiy-storchaka pushed a commit that referenced this pull request Aug 4, 2026
) (GH-155185)

(cherry picked from commit 30ac23e)

Co-authored-by: Harjoth Khara <harjoth.khara@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Aug 4, 2026
) (GH-155184)

(cherry picked from commit 30ac23e)

Co-authored-by: Harjoth Khara <harjoth.khara@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants