Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ namespace Microsoft.AspNetCore.RequestDecompression;
/// </summary>
internal sealed class ZstandardDecompressionProvider : IDecompressionProvider
{
// Cap the decompression window at 8 MB (2^23). RFC 9659 requires the "zstd" HTTP content
// coding to use a window no larger than 8 MB, and browsers such as Chromium reject larger
// windows for the same reason. Payloads produced with a larger window (for example,
// "zstd --long") will fail to decompress; default streaming compression uses a window of at
// most 2 MB and is unaffected.
private static readonly ZstandardDecompressionOptions s_decompressionOptions = new()
{
MaxWindowLog = 23,
};

/// <inheritdoc />
public Stream GetDecompressionStream(Stream stream)
{
return new ZstandardStream(stream, CompressionMode.Decompress, leaveOpen: true);
return new ZstandardStream(stream, s_decompressionOptions, leaveOpen: true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ static Stream compressorDelegate(Stream compressedContent) =>
return await GetCompressedContent(compressorDelegate, uncompressedBytes);
}

private static async Task<byte[]> GetZstdCompressedContentWithWindowLog(byte[] uncompressedBytes, int windowLog)
{
Stream compressorDelegate(Stream compressedContent) =>
new ZstandardStream(compressedContent, new ZstandardCompressionOptions { WindowLog = windowLog });

return await GetCompressedContent(compressorDelegate, uncompressedBytes);
}

[Fact]
public async Task Request_ContentEncodingBrotli_Decompressed()
{
Expand Down Expand Up @@ -163,6 +171,21 @@ public async Task Request_ContentEncodingZstd_Decompressed()
Assert.Equal(uncompressedBytes, decompressedBytes);
}

[Fact]
public async Task Request_ContentEncodingZstd_WindowExceedsMaxWindowLog_Throws()
{
// The middleware caps the Zstandard decompression window at 8 MB (windowLog 23) as
// required by RFC 9659, so a payload advertising a larger window must be rejected.

// Arrange
var contentEncoding = "zstd";
var uncompressedBytes = GetUncompressedContent();
var compressedBytes = await GetZstdCompressedContentWithWindowLog(uncompressedBytes, windowLog: 24);

// Act/Assert
await Assert.ThrowsAsync<IOException>(async () => await InvokeMiddleware(compressedBytes, new[] { contentEncoding }));
}

[Fact]
public async Task Request_NoContentEncoding_NotDecompressed()
{
Expand Down
Loading