From 84605e641025d944252bcb53692fb0eb602ddccf Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Fri, 13 Nov 2026 08:04:25 +0000 Subject: [PATCH 1/2] gh-103051: validate EOCD archive-comment-length in zipimport --- Lib/test/test_zipimport.py | 116 ++++++++++++++++++ Lib/zipimport.py | 13 ++ ...-11-13-07-41-43.gh-issue-103051.PeE8Ys.rst | 5 + 3 files changed, 134 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-13-07-41-43.gh-issue-103051.PeE8Ys.rst diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index 021fae1bd298e0..a8a96bf53746e6 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -1182,5 +1182,121 @@ def tearDownModule(): os_helper.unlink(TESTMOD) +class EocdCommentSizeTests(unittest.TestCase): + """gh-103051 -- zipimport must validate the EOCD archive-comment-length + field (bytes 20-21 of the EOCD record). A crafted .pyc whose compiled + bytecode happens to contain a PK\x05\x06 signature must be rejected. + + Invariant for every valid ZIP: + header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size + """ + + def _make_zip(self, files, comment=b"", prepend=b""): + import io as _io + import zipfile as _zf + buf = _io.BytesIO() + with _zf.ZipFile(buf, "w", _zf.ZIP_STORED) as zf: + for name, data in files: + zf.writestr(name, data) + zf.comment = comment + return prepend + buf.getvalue() + + def _write_tmp(self, data, suffix=".zip"): + import tempfile as _tmp + fd, path = _tmp.mkstemp(suffix=suffix) + self.addCleanup(os.unlink, path) + with os.fdopen(fd, "wb") as f: + f.write(data) + return path + + # -- valid ZIPs that must continue to work -------------------------------- + + def test_plain_zip_accepted(self): + """Plain ZIP with comment_size=0 must be accepted.""" + path = self._write_tmp(self._make_zip([("hello.py", b"x = 1\n")])) + zipimport.zipimporter(path) # must not raise + + def test_zip_with_archive_comment_accepted(self): + """ZIP with a non-empty archive comment must be accepted.""" + path = self._write_tmp( + self._make_zip([("hello.py", b"x = 1\n")], comment=b"my comment")) + zipimport.zipimporter(path) + + def test_zip_with_max_comment_accepted(self): + """ZIP with a maximum-length (65535-byte) archive comment must be + accepted -- the comment exactly fills the EOCD comment field.""" + path = self._write_tmp( + self._make_zip([("hello.py", b"x = 1\n")], comment=b"z" * 65535)) + zipimport.zipimporter(path) + + def test_pex_style_zip_accepted(self): + """ZIP with prepended bytes (PEX / zipapp shebang) must be accepted. + arc_offset handles the prefix; the EOCD stays at the true end.""" + path = self._write_tmp( + self._make_zip([("hello.py", b"x = 1\n")], + prepend=b"#!/usr/bin/env python3\n")) + zipimport.zipimporter(path) + + def test_pex_with_archive_comment_accepted(self): + """PEX-style ZIP that also carries an archive comment must work.""" + path = self._write_tmp( + self._make_zip([("hello.py", b"x = 1\n")], + comment=b"pex metadata", + prepend=b"#!/usr/bin/env python3\n")) + zipimport.zipimporter(path) + + # -- attack variants that must be rejected -------------------------------- + + def test_polyglot_pyc_zip_rejected(self): + """gh-103051: file with fake .pyc header + embedded ZIP + trailing + bytecode must be rejected. EOCD is in the middle; comment_size=0 + but bytes follow -> header_position + 22 + 0 != file_size.""" + inner_zip = self._make_zip([("__main__.py", b"print('hacked')\n")]) + fake_pyc_header = b"\x6f\x0d\x0d\x0a" + b"\x00" * 12 + trailing_bytecode = b"\x00" * 50 + path = self._write_tmp( + fake_pyc_header + inner_zip + trailing_bytecode, suffix=".pyc") + with self.assertRaises(zipimport.ZipImportError): + zipimport.zipimporter(path) + + def test_trailing_garbage_after_eocd_rejected(self): + """ZIP with bytes appended after the EOCD must be rejected: + comment_size=0 but file continues past EOCD end.""" + data = self._make_zip([("hello.py", b"x = 1\n")]) + b"\xde\xad" * 20 + path = self._write_tmp(data) + with self.assertRaises(zipimport.ZipImportError): + zipimport.zipimporter(path) + + def test_wrong_comment_size_field_rejected(self): + """ZIP with EOCD comment_size patched to a wrong value must be + rejected.""" + import struct + data = bytearray(self._make_zip([("hello.py", b"x = 1\n")])) + eocd_pos = data.rfind(b"PK\x05\x06") + struct.pack_into(" Date: Fri, 13 Nov 2026 08:18:02 +0000 Subject: [PATCH 2/2] gh-103051: clean up test docstrings --- Lib/test/test_zipimport.py | 36 ++++++++++++------------------------ Lib/zipimport.py | 12 ++---------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index a8a96bf53746e6..ad96034682f94e 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -1183,27 +1183,21 @@ def tearDownModule(): class EocdCommentSizeTests(unittest.TestCase): - """gh-103051 -- zipimport must validate the EOCD archive-comment-length - field (bytes 20-21 of the EOCD record). A crafted .pyc whose compiled - bytecode happens to contain a PK\x05\x06 signature must be rejected. - - Invariant for every valid ZIP: - header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size - """ + """gh-103051: validate the EOCD archive-comment-length field.""" def _make_zip(self, files, comment=b"", prepend=b""): - import io as _io - import zipfile as _zf - buf = _io.BytesIO() - with _zf.ZipFile(buf, "w", _zf.ZIP_STORED) as zf: + import io + import zipfile + buf = io.BytesIO() + with zipfile.ZipFile(buf, "w", zipfile.ZIP_STORED) as zf: for name, data in files: zf.writestr(name, data) zf.comment = comment return prepend + buf.getvalue() def _write_tmp(self, data, suffix=".zip"): - import tempfile as _tmp - fd, path = _tmp.mkstemp(suffix=suffix) + import tempfile + fd, path = tempfile.mkstemp(suffix=suffix) self.addCleanup(os.unlink, path) with os.fdopen(fd, "wb") as f: f.write(data) @@ -1223,15 +1217,13 @@ def test_zip_with_archive_comment_accepted(self): zipimport.zipimporter(path) def test_zip_with_max_comment_accepted(self): - """ZIP with a maximum-length (65535-byte) archive comment must be - accepted -- the comment exactly fills the EOCD comment field.""" + """ZIP with a maximum-length (65535-byte) archive comment must be accepted.""" path = self._write_tmp( self._make_zip([("hello.py", b"x = 1\n")], comment=b"z" * 65535)) zipimport.zipimporter(path) def test_pex_style_zip_accepted(self): - """ZIP with prepended bytes (PEX / zipapp shebang) must be accepted. - arc_offset handles the prefix; the EOCD stays at the true end.""" + """ZIP with prepended bytes (PEX-style shebang) must be accepted.""" path = self._write_tmp( self._make_zip([("hello.py", b"x = 1\n")], prepend=b"#!/usr/bin/env python3\n")) @@ -1248,9 +1240,7 @@ def test_pex_with_archive_comment_accepted(self): # -- attack variants that must be rejected -------------------------------- def test_polyglot_pyc_zip_rejected(self): - """gh-103051: file with fake .pyc header + embedded ZIP + trailing - bytecode must be rejected. EOCD is in the middle; comment_size=0 - but bytes follow -> header_position + 22 + 0 != file_size.""" + """gh-103051: pyc/zip polyglot must be rejected.""" inner_zip = self._make_zip([("__main__.py", b"print('hacked')\n")]) fake_pyc_header = b"\x6f\x0d\x0d\x0a" + b"\x00" * 12 trailing_bytecode = b"\x00" * 50 @@ -1260,8 +1250,7 @@ def test_polyglot_pyc_zip_rejected(self): zipimport.zipimporter(path) def test_trailing_garbage_after_eocd_rejected(self): - """ZIP with bytes appended after the EOCD must be rejected: - comment_size=0 but file continues past EOCD end.""" + """ZIP with bytes appended after the EOCD must be rejected.""" data = self._make_zip([("hello.py", b"x = 1\n")]) + b"\xde\xad" * 20 path = self._write_tmp(data) with self.assertRaises(zipimport.ZipImportError): @@ -1289,8 +1278,7 @@ def test_comment_size_exceeds_file_rejected(self): zipimport.zipimporter(path) def test_junk_after_real_comment_rejected(self): - """ZIP with a valid archive comment AND extra trailing bytes must be - rejected: extra bytes are not covered by comment_size.""" + """ZIP with extra bytes after a real archive comment must be rejected.""" data = self._make_zip([("hello.py", b"x = 1\n")], comment=b"real comment") data += b"\xff" * 30 path = self._write_tmp(data) diff --git a/Lib/zipimport.py b/Lib/zipimport.py index d88f358f9a4b8c..cd31ab49e95bc8 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -352,21 +352,13 @@ def _read_directory(archive): central_directory_position = _unpack_uint32(buffer[16:20]) num_entries = _unpack_uint16(buffer[8:10]) - # gh-103051: Validate that the archive comment length stored in - # the EOCD is consistent with the actual end-of-file position. - # Without this check a crafted .pyc file whose compiled bytecode - # happens to contain a PK\x05\x06 signature can be misidentified - # as a ZIP archive (pyc/zip polyglot attack). For every valid ZIP - # (plain, with archive comment, or with prepended bytes such as PEX - # or self-extracting .exe) the invariant holds exactly: - # header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size + # gh-103051: reject files where the EOCD archive-comment-length + # field doesn't match the actual file size (pyc/zip polyglot). comment_size = _unpack_uint16(buffer[20:22]) if header_position + END_CENTRAL_DIR_SIZE + comment_size != file_size: raise ZipImportError(f'not a Zip file: {archive!r}', path=archive) - # N.b. if someday you want to prefer the standard (non-zip64) EOCD, - # you need to adjust position by 76 for arc to be 0. else: raise ZipImportError(f'not a Zip file: {archive!r}', path=archive)