Repository navigation
Trust central directory sizes, stop at truncated tar data, add extraction limits - #40
Merged
Merged
Conversation
Zip::extract() replaced the sizes and the CRC from the central directory with the values from the local file header. A local header could declare a much larger size than the central directory, so a size check based on contents() did not limit what extract() wrote. The central directory now supplies these values. The ZIP specification allows local values only to be zero or masked. Tar::extract() continued its copy loop after the end of the archive. A 10 KB tar with a pax size of 99999999999999 kept the process busy without end. Skipping an excluded entry in a bzip2 archive had the same problem. Extraction now throws an ArchiveCorruptedException on missing data and removes the partial file, and skipping stops at the end of the file.
Callers that extract untrusted archives had no way to stop an archive that expands to a very large size or to very many files. setExtractionSizeLimit() and setExtractionFileLimit() set a maximum for the total bytes and the number of entries that extract() writes. Before each entry, extract() compares the declared size with the remaining limit and throws an ArchiveLimitException when the entry would exceed it. Files extracted up to that point stay on disk. Entries skipped by the include and exclude filters do not count. Both limits default to 0, which means no limit.
Zip::extract() and Tar::extract() closed the archive only after a successful extraction. After an exception, the file handle stayed open, and Windows refused to delete the archive. This made the extraction limit tests of 52eeb10 fail on Windows. The code that writes a single entry moved into extractEntry(), so extract() can close the archive in a finally block. Zip::extract() did not check that it could create the target directory. The first file then failed with an unclear error, and an empty archive gave no error at all. Both classes now use the same directory check for the target directory and for the directories of the entries, which throws an ArchiveIOException.
Zip::extract() wrapped deflated data in a gzip header, wrote it to a temporary .gz file next to the output and decompressed that file with gzread(). This doubled the disk use, and a fatal error left the .gz file behind. The output size was only checked for a short result, and a wrong CRC was not detected. Deflated data is now decompressed in chunks with inflate_add() directly from the archive. Extraction fails when the output is larger or smaller than the declared size or when the CRC does not match. The data checks and the copying moved into checkEntryData() and copyEntryData().
Zip::extract() and Tar::extract() ignored the result of fwrite(). When the disk was full, the extraction reported success and left truncated files. A failed write now throws an ArchiveIOException and removes the partial file.
splitbrain
marked this pull request as ready for review
November 24, 2026 18:16
The size limit of Tar::extract() counted only the extracted files. Skipped entries, empty blocks and pax metadata were still decompressed in full. An 854 byte .tar.bz2 with a skipped 1 GB entry took 2.3 s, and a 785 byte .tar.bz2 of zero blocks took 3.4 s, without reaching any limit. Tar::extract() now applies the size limit to all data it reads from the uncompressed archive. Skipping an entry beyond the limit fails before the data is decompressed. Tar::skipbytes() did not advance the read position for bzip2 archives, because it counted the skipped bytes down to zero before it added them.
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.
Uh oh!
There was an error while loading. Please reload this page.