@@ -1183,27 +1183,21 @@ def tearDownModule():
11831183
11841184
11851185class 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- """
1186+ """gh-103051: validate the EOCD archive-comment-length field."""
11931187
11941188 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 :
1189+ import io
1190+ import zipfile
1191+ buf = io .BytesIO ()
1192+ with zipfile .ZipFile (buf , "w" , zipfile .ZIP_STORED ) as zf :
11991193 for name , data in files :
12001194 zf .writestr (name , data )
12011195 zf .comment = comment
12021196 return prepend + buf .getvalue ()
12031197
12041198 def _write_tmp (self , data , suffix = ".zip" ):
1205- import tempfile as _tmp
1206- fd , path = _tmp .mkstemp (suffix = suffix )
1199+ import tempfile
1200+ fd , path = tempfile .mkstemp (suffix = suffix )
12071201 self .addCleanup (os .unlink , path )
12081202 with os .fdopen (fd , "wb" ) as f :
12091203 f .write (data )
@@ -1223,15 +1217,13 @@ def test_zip_with_archive_comment_accepted(self):
12231217 zipimport .zipimporter (path )
12241218
12251219 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."""
1220+ """ZIP with a maximum-length (65535-byte) archive comment must be accepted."""
12281221 path = self ._write_tmp (
12291222 self ._make_zip ([("hello.py" , b"x = 1\n " )], comment = b"z" * 65535 ))
12301223 zipimport .zipimporter (path )
12311224
12321225 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."""
1226+ """ZIP with prepended bytes (PEX-style shebang) must be accepted."""
12351227 path = self ._write_tmp (
12361228 self ._make_zip ([("hello.py" , b"x = 1\n " )],
12371229 prepend = b"#!/usr/bin/env python3\n " ))
@@ -1248,9 +1240,7 @@ def test_pex_with_archive_comment_accepted(self):
12481240 # -- attack variants that must be rejected --------------------------------
12491241
12501242 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."""
1243+ """gh-103051: pyc/zip polyglot must be rejected."""
12541244 inner_zip = self ._make_zip ([("__main__.py" , b"print('hacked')\n " )])
12551245 fake_pyc_header = b"\x6f \x0d \x0d \x0a " + b"\x00 " * 12
12561246 trailing_bytecode = b"\x00 " * 50
@@ -1260,8 +1250,7 @@ def test_polyglot_pyc_zip_rejected(self):
12601250 zipimport .zipimporter (path )
12611251
12621252 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."""
1253+ """ZIP with bytes appended after the EOCD must be rejected."""
12651254 data = self ._make_zip ([("hello.py" , b"x = 1\n " )]) + b"\xde \xad " * 20
12661255 path = self ._write_tmp (data )
12671256 with self .assertRaises (zipimport .ZipImportError ):
@@ -1289,8 +1278,7 @@ def test_comment_size_exceeds_file_rejected(self):
12891278 zipimport .zipimporter (path )
12901279
12911280 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."""
1281+ """ZIP with extra bytes after a real archive comment must be rejected."""
12941282 data = self ._make_zip ([("hello.py" , b"x = 1\n " )], comment = b"real comment" )
12951283 data += b"\xff " * 30
12961284 path = self ._write_tmp (data )
0 commit comments