Skip to content

Commit fbc265f

Browse files
committed
gh-103051: clean up test docstrings
1 parent 84605e6 commit fbc265f

2 files changed

Lines changed: 14 additions & 34 deletions

File tree

‎Lib/test/test_zipimport.py‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,27 +1183,21 @@ def tearDownModule():
11831183

11841184

11851185
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-
"""
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)

‎Lib/zipimport.py‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,13 @@ 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
355+
# gh-103051: reject files where the EOCD archive-comment-length
356+
# field doesn't match the actual file size (pyc/zip polyglot).
363357
comment_size = _unpack_uint16(buffer[20:22])
364358
if header_position + END_CENTRAL_DIR_SIZE + comment_size != file_size:
365359
raise ZipImportError(f'not a Zip file: {archive!r}',
366360
path=archive)
367361

368-
# N.b. if someday you want to prefer the standard (non-zip64) EOCD,
369-
# you need to adjust position by 76 for arc to be 0.
370362
else:
371363
raise ZipImportError(f'not a Zip file: {archive!r}',
372364
path=archive)

0 commit comments

Comments
 (0)