@@ -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+
11851301if __name__ == "__main__" :
11861302 unittest .main ()
0 commit comments