Skip to content

gh-153679: Raise ZipImportError for an invalid UTF-8 file name in zipimport#153680

Open
tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:zipimport-utf8-name-decode
Open

gh-153679: Raise ZipImportError for an invalid UTF-8 file name in zipimport#153680
tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:zipimport-utf8-name-decode

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

zipimport.zipimporter() is documented to raise ZipImportError for an
invalid archive, but _read_directory() leaked a raw UnicodeDecodeError
when a central directory entry set the UTF-8 file name flag (0x800) but
stored bytes that are not valid UTF-8: the UTF-8 branch called name.decode()
unguarded, while the sibling non-UTF-8 branch already handles
UnicodeDecodeError.

Guard the UTF-8 decode and raise ZipImportError, matching the documented
contract and every other corruption path in the function.

…in zipimport

zipimport.zipimporter() is documented to raise ZipImportError for an invalid
archive, but _read_directory() leaked a raw UnicodeDecodeError when a central
directory entry set the UTF-8 file name flag (0x800) but stored bytes that are
not valid UTF-8: the UTF-8 branch called name.decode() unguarded, while the
sibling non-UTF-8 branch already handles UnicodeDecodeError. Guard the UTF-8
decode and raise ZipImportError instead.
Comment thread Lib/zipimport.py Outdated
Comment on lines +432 to +435
except UnicodeDecodeError:
raise ZipImportError(
f"can't decode file name in Zip file: {archive!r}",
path=archive)

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.

Suggested change
except UnicodeDecodeError:
raise ZipImportError(
f"can't decode file name in Zip file: {archive!r}",
path=archive)
except UnicodeDecodeError as exc:
raise ZipImportError(
f"can't decode file name in Zip file: {archive!r}",
path=archive) from exc

Comment thread Lib/test/test_zipimport.py Outdated
os_helper.unlink(TESTMOD)
with open(TESTMOD, 'wb') as fp:
fp.write(cdh + eocd)
self.assertZipFailure(TESTMOD)

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.

Make sure the UnicodeDecodeError is attached to the exception. That way we know the failure is due to that and not a mistake in writing the malformed zip file.

@bedevere-app

bedevere-app Bot commented Jul 17, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

Done. Chained the UnicodeDecodeError via from exc and added assertIsInstance(cm.exception.__cause__, UnicodeDecodeError) in the test.

I have made the requested changes; please review again.

@bedevere-app

bedevere-app Bot commented Jul 18, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@bedevere-app
bedevere-app Bot requested a review from brettcannon July 18, 2026 00:21
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.

2 participants