From 62efd2c49fe0489176975a6e38360d129220a215 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Mon, 13 Jul 2026 10:37:03 +0800 Subject: [PATCH 1/2] gh-153679: Raise ZipImportError for an invalid UTF-8 file name 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. --- Lib/test/test_zipimport.py | 18 ++++++++++++++++++ Lib/zipimport.py | 7 ++++++- ...6-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst | 4 ++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 76cd85709a63af..50d0440c4fa563 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -1074,6 +1074,24 @@ def testNotZipFile(self): fp.close() self.assertZipFailure(TESTMOD) + def testInvalidUTF8FileName(self): + # A central directory entry that sets the UTF-8 file name flag (0x800) + # but stores bytes that are not valid UTF-8 must raise ZipImportError + # rather than leaking a UnicodeDecodeError. + UTF8_FLAG = 0x800 + name = b'\xff\xfe\xff' + cdh = b'PK\x01\x02' + struct.pack( + ' Date: Tue, 14 Jul 2026 10:30:16 +0800 Subject: [PATCH 2/2] gh-153679: Trim the test comment and NEWS wording --- Lib/test/test_zipimport.py | 4 +--- .../Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 50d0440c4fa563..c3c43f39a58e85 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -1075,9 +1075,7 @@ def testNotZipFile(self): self.assertZipFailure(TESTMOD) def testInvalidUTF8FileName(self): - # A central directory entry that sets the UTF-8 file name flag (0x800) - # but stores bytes that are not valid UTF-8 must raise ZipImportError - # rather than leaking a UnicodeDecodeError. + # A UTF-8-flagged file name that is not valid UTF-8 raises ZipImportError. UTF8_FLAG = 0x800 name = b'\xff\xfe\xff' cdh = b'PK\x01\x02' + struct.pack( diff --git a/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst b/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst index f1da233ab9d79d..d612903f225021 100644 --- a/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst +++ b/Misc/NEWS.d/next/Library/2026-07-13-00-10-00.gh-issue-153679.RJ2WGB.rst @@ -1,4 +1,3 @@ :class:`zipimport.zipimporter` now raises :exc:`zipimport.ZipImportError` -instead of :exc:`UnicodeDecodeError` when a central directory entry sets the -UTF-8 file name flag but stores a file name that is not valid UTF-8. -Patch by tonghuaroot. +instead of :exc:`UnicodeDecodeError` for a UTF-8-flagged file name that is not +valid UTF-8. Patch by tonghuaroot.