fix: do not mistake an EOCD signature inside the comment for the real record#966
Open
spokodev wants to merge 1 commit into
Open
fix: do not mistake an EOCD signature inside the comment for the real record#966spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
… record loadAsync used the last "PK\x05\x06" signature in the file as the end of central directory record, without validating it. When the archive comment itself contains those bytes, the false positive is picked and reading the bogus comment-length field runs past the end of the buffer, throwing "Corrupted zip ... End of data reached" on otherwise valid archives (including JSZip's own generateAsync output, which libarchive reads fine). Per APPNOTE.TXT 4.3.16 the genuine EOCD satisfies `offset + 22 + commentLength === fileLength`. Scan the candidate signatures backward and prefer the one matching this invariant, falling back to the last occurrence so archives with appended/prepended bytes keep working.
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.
Problem
JSZip.loadAsynccannot read a valid ZIP whose archive comment contains theend of central directory signature (
PK\x05\x06). It even fails on JSZip's ownoutput:
libarchive reads the exact same bytes correctly, so this is a JSZip-side
parsing bug rather than a malformed archive:
Root cause
readEndOfCentral()locates the EOCD record withlastIndexOfSignature(CENTRAL_DIRECTORY_END), which returns the occurrenceclosest to EOF, and uses it with no validation. When
PK\x05\x06appears insidethe comment, that false positive sits after the real record. Reading its
"comment length" field then yields garbage (in the repro, 23130), and the
reader walks past the end of the buffer, raising "End of data reached".
For the repro the signature appears at offsets 88 (real) and 110 (inside the
comment); the parser picks 110.
Fix
APPNOTE.TXT 4.3.16 states the genuine EOCD satisfies
offset + 22 + commentLength === fileLength(22 being the fixed record size).findEndOfCentral()scans the candidate signatures backward and returns thefirst one matching this invariant. If none matches (e.g. an archive with
trailing bytes appended after the EOCD), it falls back to the last occurrence,
preserving the previous behavior for the prepended/appended-bytes cases.
This required a small addition to
lastIndexOfSignature: an optionalendIndexargument so the scan can continue backward past a rejectedcandidate.
Tests
Added a
loadtest that round-trips an archive whose comment starts with theEOCD signature. It fails on
main(the "End of data reached" throw) and passeswith the fix. The existing prepended-bytes, appended-bytes and zip64 extra-bytes
load tests continue to pass, as does the rest of the suite.