From f04ef07ac7aea48e8256fdd55f1a8332743bdb3d Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Tue, 14 Jul 2026 22:32:40 +0800 Subject: [PATCH 1/2] gh-153702: Prevent writing improper extra fields to local file entry --- Lib/test/test_zipfile/test_core.py | 47 +++++++++++++++++++ Lib/zipfile/__init__.py | 17 +++++-- ...-07-14-22-25-00.gh-issue-153702.000000.rst | 3 ++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-14-22-25-00.gh-issue-153702.000000.rst diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 4f20209927e7b3..8da33ab0951580 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -1189,6 +1189,53 @@ def test_generated_valid_zip64_extra(self): self.assertEqual(zinfo.header_offset, expected_header_offset) self.assertEqual(zf.read(zinfo), expected_content) + def test_generated_extra_in_local_entry(self): + """Should not write duplicated or improper fields to local file entry.""" + # zip64 + fh = io.BytesIO() + with zipfile.ZipFile(fh, 'w') as zh: + zinfo = zipfile.ZipInfo('strfile') + zinfo.extra = ( + # zip64 + struct.pack(' ZIP64_LIMIT or compress_size > ZIP64_LIMIT if zip64: fmt = '` +had them. From c69cf30fafc9c2bb638326afc9ec15a3f34e5f33 Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Tue, 14 Jul 2026 22:51:28 +0800 Subject: [PATCH 2/2] Fix encoding error --- Lib/test/test_zipfile/test_core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index 8da33ab0951580..441508518d01d6 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -1191,6 +1191,8 @@ def test_generated_valid_zip64_extra(self): def test_generated_extra_in_local_entry(self): """Should not write duplicated or improper fields to local file entry.""" + comment = '\u4e00'.encode('utf-8') + # zip64 fh = io.BytesIO() with zipfile.ZipFile(fh, 'w') as zh: @@ -1200,7 +1202,7 @@ def test_generated_extra_in_local_entry(self): struct.pack('