Skip to content

Validate declared uncompressed length before allocating - #167

Merged
brantburnett merged 2 commits into
mainfrom
claude/validate-uncompressed-length
Nov 23, 2026
Merged

brantburnett merged 2 commits into
mainfrom
claude/validate-uncompressed-length

Conversation

@brantburnett

Copy link
Copy Markdown
Owner

What changed

  • Declared length validation: SnappyDecompressor checks a block's declared uncompressed length before allocating an output buffer for it.
    • A length above int.MaxValue throws InvalidDataException. Previously it became negative and the block decoded as empty.
    • When the whole block is known, a length above what its tag data could produce throws 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 every Snappy block API and to GetUncompressedLength.
  • Framed streams: each compressed block in a SnappyStream is capped at 65,536 bytes, as the framing format requires, and is bounded by its chunk size.
  • SnappyDecompressionOptions: a new readonly struct with int? MaxUncompressedLength. New overloads of DecompressToArray, DecompressToMemory (span and sequence) and Decompress with an IBufferWriter<byte> accept it. A block declaring more than the maximum throws InvalidDataException before any buffer is allocated or any memory is requested from the writer. The default value applies no maximum, matching the existing overloads.
  • Exception type: a malformed length varint throws InvalidDataException instead of InvalidOperationException from DecompressToMemory, TryDecompress, Decompress and SnappyStream.
  • Docs: docs/block.md has 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 DecompressToMemory and TryDecompress, and 3 GB through DecompressToArray. A 23-byte framed stream allocated 1.5 GB through SnappyStream. With these changes, each of those inputs throws InvalidDataException with no allocation.

Reviewer notes

  • Behavior changes for existing callers:
    • GetUncompressedLength now 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 get InvalidDataException.
    • SnappyStream rejects compressed chunks declaring more than 65,536 bytes. Conforming producers, including Snappier's compressor, stay within this limit.
    • The InvalidOperationException to InvalidDataException change above.
  • SnappyDecompressionOptions storage: a stored 0 means no maximum, so default applies none. Setting a value of zero or less throws ArgumentOutOfRangeException.
  • TryDecompress, Decompress(ReadOnlySpan<byte>, Span<byte>) and SnappyStream take no options. The expansion bound and, for streams, the 64 KiB cap apply to them.
  • All 1,368 tests pass on net8.0, net10.0, net11.0 and net481. Package validation against the 1.2.0 baseline passes when packed as 1.3.3. Benchmarks have not been run; the check runs once per block.

🤖 Generated with Claude Code

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread docs/block.md Resolved
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>
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