Bound mcpb unpack against decompression bombs and malformed central directories#273
Open
aosmcleod wants to merge 1 commit into
Open
Bound mcpb unpack against decompression bombs and malformed central directories#273aosmcleod wants to merge 1 commit into
aosmcleod wants to merge 1 commit into
Conversation
…ories unpackExtension inflated the whole archive with unzipSync and no size limit, and trusted the central-directory entry count — a small, highly compressible .mcpb could exhaust memory. The central-directory parse also read at offsets derived from untrusted ZIP fields with no bounds checks. Add configurable maxUncompressedBytes (default 1 GiB) and maxEntries (default 100k) caps, enforced from the central directory before decompression. Make the central-directory pass run on all platforms with bounds-checked reads so a truncated/malformed directory can't throw a RangeError (Unix attribute extraction is still gated to Unix). Adds tests for normal unpack and for size/entry-limit rejection. Note: the size cap relies on the archive's declared uncompressed sizes; fully bounding a deliberately under-declaring archive needs streaming decompression with a running byte cap, which would be a larger change (follow-up). Fixes modelcontextprotocol#266 (central-directory bounds is item 4 of modelcontextprotocol#267)
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
unpackExtensiondecompresses the whole archive withunzipSyncand no size limit, and trusts the central-directory entry count without bound — a small, highly compressible.mcpb(zip bomb) can exhaust memory and crash the process (#266). Separately, the central-directory pass reads at offsets derived from untrusted ZIP fields with no bounds validation, so a malformed/truncated directory throws aRangeError(item 4 of #267).Fix
maxUncompressedBytes(default 1 GiB) andmaxEntries(default 100k), enforced from the central directory before decompression.Limitation (called out honestly)
The size cap relies on the archive's declared uncompressed sizes. Fully bounding an archive that deliberately under-declares its sizes requires streaming decompression with a running byte cap (fflate
Unzip+ondata), which is a larger rewrite — flagged as a follow-up rather than done here.Test
Adds
test/unpack-limits.test.ts: a normal archive unpacks within defaults; archives exceedingmaxUncompressedBytesormaxEntriesare rejected without extracting.yarn lint+yarn test(this suite) green.Fixes #266.