Skip to content

gh-153426: Raise BadZipFile for an invalid UTF-8 filename in zipfile#153428

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:zipfile-utf8-badzip
Open

gh-153426: Raise BadZipFile for an invalid UTF-8 filename in zipfile#153428
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:zipfile-utf8-badzip

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

When a ZIP entry sets general purpose bit 11, zipfile decodes the member name as
UTF-8, but the decode is unguarded. A member name that is flagged as UTF-8 yet holds
bytes that are not valid UTF-8 makes ZipFile() (central directory) and
ZipFile.open() / .read() (local file header) raise UnicodeDecodeError instead of
zipfile.BadZipFile.

This wraps both UTF-8 filename decodes so the failure is re-raised as BadZipFile, the
module's own error type, mirroring the sibling extra-field decoder _decodeExtra.
Callers that already handle BadZipFile for a corrupt archive no longer also have to
catch UnicodeDecodeError. Valid archives are unchanged.

…pfile

When a central directory or local file header entry sets the UTF-8 filename
flag (general purpose bit 11) but stores bytes that are not valid UTF-8,
opening the archive raised a raw UnicodeDecodeError instead of the
BadZipFile that signals a corrupt archive. Wrap the bit-11 filename decode
in both _RealGetContents and ZipFile.open to raise BadZipFile, mirroring
the handling of the unicode path extra field, while leaving the
metadata_encoding decode path unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant