validate ZIP central directory and throw on corruption#39
Open
splitbrain wants to merge 4 commits into
Open
Conversation
The readCentralDir() scan for the End-of-Central-Directory signature
silently fell through when it was missing, leaving the file pointer
past EOF and feeding null to unpack(). That surfaced as PHP warnings
("unpack(): Type V: not enough input") rather than a meaningful error.
Detect both the missing EOCD signature and a failed unpack() and raise
ArchiveCorruptedException instead, matching the error handling used
elsewhere in the package. Added tests covering a non-ZIP payload and
a file too short to hold an EOCD record.
Drop the $found flag: after the loop, $bytes can only equal the EOCD signature if we broke out, since the check runs on every iteration.
Three ZIP record signatures were sprinkled through the file as raw byte literals (one as a uint32 in the EOCD scan, three more as "\x50\x4b..." strings on write). Pull them out as SIG_* class constants and convert the EOCD scan to a 4-byte sliding string buffer so reads and writes share a single source of truth.
CI failed only on PHP 8.0 / windows-latest. Replace tempnam() with the sys_get_temp_dir() + md5(uniqid()) pattern the rest of the suite uses, and explicitly close the Zip handle in finally so Windows can unlink the temp file (open handles block deletion).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The readCentralDir() scan for the End-of-Central-Directory signature
silently fell through when it was missing, leaving the file pointer
past EOF and feeding null to unpack(). That surfaced as PHP warnings
("unpack(): Type V: not enough input") rather than a meaningful error.
Detect both the missing EOCD signature and a failed unpack() and raise
ArchiveCorruptedException instead, matching the error handling used
elsewhere in the package. Added tests covering a non-ZIP payload and
a file too short to hold an EOCD record.