Skip to content

Commit 84605e6

Browse files
committed
gh-103051: validate EOCD archive-comment-length in zipimport
1 parent 230917f commit 84605e6

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎Lib/test/test_zipimport.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,5 +1182,121 @@ def tearDownModule():
11821182
os_helper.unlink(TESTMOD)
11831183

11841184

1185+
class EocdCommentSizeTests(unittest.TestCase):
1186+
"""gh-103051 -- zipimport must validate the EOCD archive-comment-length
1187+
field (bytes 20-21 of the EOCD record). A crafted .pyc whose compiled
1188+
bytecode happens to contain a PK\x05\x06 signature must be rejected.
1189+
1190+
Invariant for every valid ZIP:
1191+
header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size
1192+
"""
1193+
1194+
def _make_zip(self, files, comment=b"", prepend=b""):
1195+
import io as _io
1196+
import zipfile as _zf
1197+
buf = _io.BytesIO()
1198+
with _zf.ZipFile(buf, "w", _zf.ZIP_STORED) as zf:
1199+
for name, data in files:
1200+
zf.writestr(name, data)
1201+
zf.comment = comment
1202+
return prepend + buf.getvalue()
1203+
1204+
def _write_tmp(self, data, suffix=".zip"):
1205+
import tempfile as _tmp
1206+
fd, path = _tmp.mkstemp(suffix=suffix)
1207+
self.addCleanup(os.unlink, path)
1208+
with os.fdopen(fd, "wb") as f:
1209+
f.write(data)
1210+
return path
1211+
1212+
# -- valid ZIPs that must continue to work --------------------------------
1213+
1214+
def test_plain_zip_accepted(self):
1215+
"""Plain ZIP with comment_size=0 must be accepted."""
1216+
path = self._write_tmp(self._make_zip([("hello.py", b"x = 1\n")]))
1217+
zipimport.zipimporter(path) # must not raise
1218+
1219+
def test_zip_with_archive_comment_accepted(self):
1220+
"""ZIP with a non-empty archive comment must be accepted."""
1221+
path = self._write_tmp(
1222+
self._make_zip([("hello.py", b"x = 1\n")], comment=b"my comment"))
1223+
zipimport.zipimporter(path)
1224+
1225+
def test_zip_with_max_comment_accepted(self):
1226+
"""ZIP with a maximum-length (65535-byte) archive comment must be
1227+
accepted -- the comment exactly fills the EOCD comment field."""
1228+
path = self._write_tmp(
1229+
self._make_zip([("hello.py", b"x = 1\n")], comment=b"z" * 65535))
1230+
zipimport.zipimporter(path)
1231+
1232+
def test_pex_style_zip_accepted(self):
1233+
"""ZIP with prepended bytes (PEX / zipapp shebang) must be accepted.
1234+
arc_offset handles the prefix; the EOCD stays at the true end."""
1235+
path = self._write_tmp(
1236+
self._make_zip([("hello.py", b"x = 1\n")],
1237+
prepend=b"#!/usr/bin/env python3\n"))
1238+
zipimport.zipimporter(path)
1239+
1240+
def test_pex_with_archive_comment_accepted(self):
1241+
"""PEX-style ZIP that also carries an archive comment must work."""
1242+
path = self._write_tmp(
1243+
self._make_zip([("hello.py", b"x = 1\n")],
1244+
comment=b"pex metadata",
1245+
prepend=b"#!/usr/bin/env python3\n"))
1246+
zipimport.zipimporter(path)
1247+
1248+
# -- attack variants that must be rejected --------------------------------
1249+
1250+
def test_polyglot_pyc_zip_rejected(self):
1251+
"""gh-103051: file with fake .pyc header + embedded ZIP + trailing
1252+
bytecode must be rejected. EOCD is in the middle; comment_size=0
1253+
but bytes follow -> header_position + 22 + 0 != file_size."""
1254+
inner_zip = self._make_zip([("__main__.py", b"print('hacked')\n")])
1255+
fake_pyc_header = b"\x6f\x0d\x0d\x0a" + b"\x00" * 12
1256+
trailing_bytecode = b"\x00" * 50
1257+
path = self._write_tmp(
1258+
fake_pyc_header + inner_zip + trailing_bytecode, suffix=".pyc")
1259+
with self.assertRaises(zipimport.ZipImportError):
1260+
zipimport.zipimporter(path)
1261+
1262+
def test_trailing_garbage_after_eocd_rejected(self):
1263+
"""ZIP with bytes appended after the EOCD must be rejected:
1264+
comment_size=0 but file continues past EOCD end."""
1265+
data = self._make_zip([("hello.py", b"x = 1\n")]) + b"\xde\xad" * 20
1266+
path = self._write_tmp(data)
1267+
with self.assertRaises(zipimport.ZipImportError):
1268+
zipimport.zipimporter(path)
1269+
1270+
def test_wrong_comment_size_field_rejected(self):
1271+
"""ZIP with EOCD comment_size patched to a wrong value must be
1272+
rejected."""
1273+
import struct
1274+
data = bytearray(self._make_zip([("hello.py", b"x = 1\n")]))
1275+
eocd_pos = data.rfind(b"PK\x05\x06")
1276+
struct.pack_into("<H", data, eocd_pos + 20, 10)
1277+
path = self._write_tmp(bytes(data))
1278+
with self.assertRaises(zipimport.ZipImportError):
1279+
zipimport.zipimporter(path)
1280+
1281+
def test_comment_size_exceeds_file_rejected(self):
1282+
"""ZIP with comment_size that extends past EOF must be rejected."""
1283+
import struct
1284+
data = bytearray(self._make_zip([("hello.py", b"x = 1\n")]))
1285+
eocd_pos = data.rfind(b"PK\x05\x06")
1286+
struct.pack_into("<H", data, eocd_pos + 20, 999)
1287+
path = self._write_tmp(bytes(data))
1288+
with self.assertRaises(zipimport.ZipImportError):
1289+
zipimport.zipimporter(path)
1290+
1291+
def test_junk_after_real_comment_rejected(self):
1292+
"""ZIP with a valid archive comment AND extra trailing bytes must be
1293+
rejected: extra bytes are not covered by comment_size."""
1294+
data = self._make_zip([("hello.py", b"x = 1\n")], comment=b"real comment")
1295+
data += b"\xff" * 30
1296+
path = self._write_tmp(data)
1297+
with self.assertRaises(zipimport.ZipImportError):
1298+
zipimport.zipimporter(path)
1299+
1300+
11851301
if __name__ == "__main__":
11861302
unittest.main()

‎Lib/zipimport.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,19 @@ def _read_directory(archive):
352352
central_directory_position = _unpack_uint32(buffer[16:20])
353353
num_entries = _unpack_uint16(buffer[8:10])
354354

355+
# gh-103051: Validate that the archive comment length stored in
356+
# the EOCD is consistent with the actual end-of-file position.
357+
# Without this check a crafted .pyc file whose compiled bytecode
358+
# happens to contain a PK\x05\x06 signature can be misidentified
359+
# as a ZIP archive (pyc/zip polyglot attack). For every valid ZIP
360+
# (plain, with archive comment, or with prepended bytes such as PEX
361+
# or self-extracting .exe) the invariant holds exactly:
362+
# header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size
363+
comment_size = _unpack_uint16(buffer[20:22])
364+
if header_position + END_CENTRAL_DIR_SIZE + comment_size != file_size:
365+
raise ZipImportError(f'not a Zip file: {archive!r}',
366+
path=archive)
367+
355368
# N.b. if someday you want to prefer the standard (non-zip64) EOCD,
356369
# you need to adjust position by 76 for arc to be 0.
357370
else:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`zipimport` now validates that the archive-comment-length field in the
2+
ZIP End-of-Central-Directory record is consistent with the actual file size.
3+
Previously the field was silently ignored, allowing a crafted ``.pyc`` file
4+
whose compiled bytecode happened to contain a ``PK\\x05\\x06`` signature to
5+
be misidentified as a ZIP archive (pyc/zip polyglot attack).

0 commit comments

Comments
 (0)