Repository navigation
Validate declared uncompressed length before allocating - #167
Merged
Merged
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>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The untrusted-data guidance misleadingly says SnappyStream needs no limit, although the 64 KiB check covers only compressed chunks and callers still need a separate total-output limit.
1 open issue
What changed in this PR
This PR hardens Snappier’s decompression APIs by validating declared output lengths before allocating buffers and adding optional application-level size limits.
Changes:
- Reject invalid declared lengths using integer and compressed-size bounds.
- Add block decompression options and a 64 KiB limit for compressed stream chunks.
- Add regression tests and guidance for decompressing untrusted data.
| File | Description |
|---|---|
| Snappier/SnappyDecompressionOptions.cs | Defines the optional maximum uncompressed length. |
| Snappier/Snappy.cs | Adds options overloads and supplies block lengths for validation. |
| Snappier/Internal/ThrowHelper.cs | Adds the size-limit exception helper and removes the obsolete helper. |
| Snappier/Internal/SnappyStreamDecompressor.cs | Applies compressed-chunk size and expansion limits. |
| Snappier/Internal/SnappyDecompressor.cs | Validates declared lengths before buffer allocation. |
| Snappier/Internal/Helpers.cs | Calculates the maximum expansion bound. |
| Snappier.Tests/SnappyTests.cs | Tests invalid lengths, expansion bounds, and options. |
| Snappier.Tests/SnappyStreamTests.cs | Tests compressed-chunk limits and full-size round trips. |
| Snappier.Tests/Internal/SnappyDecompressorTests.cs | Verifies reset clears block length while preserving options. |
| docs/block.md | Documents size limits and untrusted-data handling. |
Review effort: Balanced
The 64 KiB cap applies to compressed chunks only, and a stream may contain any number of chunks, so callers still need to limit total output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
brantburnett
marked this pull request as ready for review
November 23, 2026 18:14
This was referenced Nov 23, 2026
Merged
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