Skip to content

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

Merged
StanFromIreland merged 4 commits into
python:mainfrom
tonghuaroot:zipimport-utf8-name-decode
Oct 24, 2026
Merged

StanFromIreland merged 4 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
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Aug 17, 2026

@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. 👍

@StanFromIreland
StanFromIreland enabled auto-merge (squash) October 24, 2026 18:03
@StanFromIreland
StanFromIreland merged commit 0d606d6 into python:main Oct 24, 2026
141 of 145 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 24, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 24, 2026
@bedevere-app

bedevere-app Bot commented Oct 24, 2026

Copy link
Copy Markdown

GH-160618 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 Oct 24, 2026
StanFromIreland pushed a commit that referenced this pull request Oct 24, 2026
…me in `zipimport` (GH-153680) (#160618)

(cherry picked from commit 0d606d6)

Co-authored-by: tonghuaroot (童话) <[email protected]>
StanFromIreland pushed a commit that referenced this pull request Oct 24, 2026
…me in `zipimport` (GH-153680) (#160617)

(cherry picked from commit 0d606d6)

Co-authored-by: tonghuaroot (童话) <[email protected]>
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.

4 participants