Skip to content

gh-103051: validate EOCD archive-comment-length in zipimport - #161929

Open
BHUVANSH855 wants to merge 2 commits into
python:mainfrom
BHUVANSH855:gh-103051-zipimport-eocd-validation
Open

BHUVANSH855 wants to merge 2 commits into
python:mainfrom
BHUVANSH855:gh-103051-zipimport-eocd-validation

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

zipimport._read_directory() located the EOCD record using rfind(b'PK\x05\x06')
but never validated that the archive-comment-length field (bytes 20-21) was
consistent with the actual file size. A crafted .pyc whose bytecode contains
the EOCD signature could be misidentified as a ZIP archive (pyc/zip polyglot).

Add the invariant check:
header_position + END_CENTRAL_DIR_SIZE + comment_size == file_size

This holds for all valid ZIPs (plain, with archive comment, with prepended
bytes such as PEX or self-extracting .exe). Files where the signature is
spurious fail the check and raise ZipImportError.

Closes issue #103051

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant