Repository navigation
Reject oversized zip entries instead of silently truncating them - #4027
Open
m86hussein wants to merge 1 commit into
Open
m86hussein wants to merge 1 commit into
m86hussein wants to merge 1 commit into
Conversation
ZipUtils.unzipFile guards against zip bombs (CERT IDS04-J) by capping the total unzipped size. The read loop condition was `total + BUFFER_SIZE <= MAX_UNZIPPED_SIZE`, which stops reading an entry once `total` gets within one buffer of the limit, leaving `total` below MAX_UNZIPPED_SIZE. The post-loop guard `total >= MAX_UNZIPPED_SIZE` then never fired, so an oversized entry was silently written out truncated rather than rejected. Move the size check inside the loop so the archive is rejected as soon as the limit is crossed. Added a test with a 10KB entry against a 1KB (non-buffer-aligned) limit that reproduces the silent truncation before the fix.
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.
Summary
ZipUtils.unzipFileprotects against zip bombs (CERT IDS04-J) by rejecting archives whose total unzipped size exceedsMAX_UNZIPPED_SIZE. However, the read loop could silently truncate an oversized entry instead of rejecting the archive.The bug
The inner read loop was:
The loop stops reading an entry as soon as
totalgets within oneBUFFER_SIZEof the limit, which leavestotalstrictly belowMAX_UNZIPPED_SIZE. The post-loop guardtotal >= MAX_UNZIPPED_SIZEtherefore never fires, and the oversized entry is written out in truncated form while extraction continues as if nothing went wrong.The existing
testFileWithSmallMaxUnzipSizeonly passes by coincidence: its 5MB limit is a multiple of the 512-byte buffer, so the 512-byte reads landtotalexactly on the limit and trip the post-check. With a limit that isn't buffer-aligned, the archive is silently truncated.The fix
Move the size check inside the loop so the archive is rejected the moment the limit is crossed:
The now-unreachable
total >= MAX_UNZIPPED_SIZEpost-check is removed.Testing
Added
testEntryExceedingLimitIsRejectedNotTruncated, which builds a zip with a single 10KB entry and a 1KB (non-buffer-aligned) limit. It fails against the old code (the entry is truncated, no exception) and passes with the fix. All existingZipUtilsTesttests still pass.