Repository navigation
Validate declared uncompressed length before allocating - #166
Closed
brantburnett wants to merge 1 commit into
Closed
brantburnett wants to merge 1 commit into
brantburnett wants to merge 1 commit into
Conversation
Decompression allocated an output buffer of the block's declared uncompressed length before reading any tag data, so a few bytes of input could cause an allocation of up to 2 GB. Declared lengths of 2^31 or more became negative and the block decoded as empty. - Reject declared lengths above int.MaxValue with InvalidDataException. - Where the whole block is known, reject declared lengths above the most the block's tag data can produce (64 bytes per 3-byte copy tag). - Cap each block in a framed stream at 65,536 bytes, as the framing format requires, and bound it by its chunk size. - Add SnappyDecompressionOptions with MaxUncompressedLength, accepted by DecompressToArray, DecompressToMemory and Decompress with an IBufferWriter<byte>. - Throw InvalidDataException rather than InvalidOperationException for a malformed length varint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
|
Superseded by #167. |
brantburnett
deleted the
claude/snappier-memory-allocation-cve-8dcc1b
branch
November 23, 2026 16:52
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.
What changed
SnappyDecompressorchecks a block's declared uncompressed length before allocating an output buffer for it.int.MaxValuethrowsInvalidDataException. Previously it became negative and the block decoded as empty.InvalidDataException. The densest element is a 3-byte copy tag producing 64 bytes, so the bound is 64 bytes per 3 bytes of tag data. This applies to everySnappyblock API and toGetUncompressedLength.SnappyStreamis capped at 65,536 bytes, as the framing format requires, and is bounded by its chunk size.SnappyDecompressionOptions: a newreadonly structwithint? MaxUncompressedLength. New overloads ofDecompressToArray,DecompressToMemory(span and sequence) andDecompresswith anIBufferWriter<byte>accept it. A block declaring more than the maximum throwsInvalidDataExceptionbefore any buffer is allocated or any memory is requested from the writer. The default value applies no maximum, matching the existing overloads.InvalidDataExceptioninstead ofInvalidOperationExceptionfromDecompressToMemory,TryDecompress,DecompressandSnappyStream.docs/block.mdhas a new section on decompressing untrusted data.Why
Decompression allocated a buffer of the declared uncompressed length before reading any tag data. A 5-byte block declaring 1.5 GB allocated 1.5 GB through
DecompressToMemoryandTryDecompress, and 3 GB throughDecompressToArray. A 23-byte framed stream allocated 1.5 GB throughSnappyStream. With these changes, each of those inputs throwsInvalidDataExceptionwith no allocation.Reviewer notes
GetUncompressedLengthnow throws when the declared length is larger than the input could produce. Its documentation already required a whole block, but a caller passing only the first few bytes will now getInvalidDataException.SnappyStreamrejects compressed chunks declaring more than 65,536 bytes. Conforming producers, including Snappier's compressor, stay within this limit.InvalidOperationExceptiontoInvalidDataExceptionchange above.SnappyDecompressionOptionsstorage: a stored0means no maximum, sodefaultapplies none. Setting a value of zero or less throwsArgumentOutOfRangeException.TryDecompress,Decompress(ReadOnlySpan<byte>, Span<byte>)andSnappyStreamtake no options. The expansion bound and, for streams, the 64 KiB cap apply to them.🤖 Generated with Claude Code