Skip to content

Trust central directory sizes, stop at truncated tar data, add extraction limits - #40

Merged
splitbrain merged 7 commits into
splitbrain:masterfrom
splitbrain-bot:verify-entry-sizes
Nov 24, 2026
Merged

splitbrain merged 7 commits into
splitbrain:masterfrom
splitbrain-bot:verify-entry-sizes

Conversation

@splitbrain-bot

@splitbrain-bot splitbrain-bot commented Nov 24, 2026 •

Copy link
Copy Markdown
  • Zip uses the sizes and the CRC from the central directory only. Local header sizes could make extract() write more than contents() declared.
  • Tar stops at missing entry data instead of looping until the declared size. Skipping in bzip2 archives stops at the end of the file.
  • New setExtractionSizeLimit() and setExtractionFileLimit() let callers limit what extract() writes. An exceeded limit throws ArchiveLimitException.
  • extract() closes the archive also when it throws, and both classes check that they can create the target directories.
  • Zip decompresses entries with inflate_add() instead of a temporary .gz file, and checks the output size and the CRC.
  • A failed fwrite() during extraction throws ArchiveIOException instead of being ignored.

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
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.
@splitbrain
splitbrain merged commit 6c82fad into splitbrain:master Nov 24, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants