Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 4, 2025

Problem

The DeflateCompressionProvider.Decompress method was incorrectly throwing IDX10816 errors for large but valid JWE payloads during deflation. This occurred because StreamReader.Read() is not guaranteed to return the maximum number of characters requested, even when more data is available.

The original code performed a single read operation:

int bytesRead = reader.Read(chars, 0, MaximumDeflateSize);
if (reader.Peek() != -1) // This would incorrectly trigger for large payloads
{
    throw new SecurityTokenDecompressionFailedException(IDX10816);
}

When decompressing large payloads, the single Read() call would often return fewer characters than the available data, causing reader.Peek() to detect remaining data and incorrectly conclude the payload exceeded the size limit.

Solution

Modified the decompression logic to read from the stream in a loop until all data is consumed or the maximum size is reached:

int totalCharsRead = 0;
int charsRead;

// Read from the stream until all data is consumed or max size is reached
while (totalCharsRead <= MaximumDeflateSize && (charsRead = reader.Read(chars, totalCharsRead, MaximumDeflateSize - totalCharsRead)) > 0)
{
    totalCharsRead += charsRead;
}

// Only throw error if there's actually more data after reaching the limit
if (reader.Peek() != -1)
{
    throw new SecurityTokenDecompressionFailedException(IDX10816);
}

Testing

Added comprehensive unit tests in DeflateCompressionProviderTests.cs:

The fix ensures that:

  • Large valid payloads decompress successfully
  • The size limit is still properly enforced for truly oversized payloads
  • No regression for smaller payloads

Fixes #2516.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] [Bug] Decryption of larger payload JWE fails with IDX10816 during deflation in DeflateCompressionProvider.cs Fix IDX10816 decompression failure for large JWE payloads in DeflateCompressionProvider Aug 4, 2025
@Copilot Copilot AI requested a review from pmaytak August 4, 2025 20:59
Copilot finished work on behalf of pmaytak August 4, 2025 20:59
@Copilot Copilot AI requested a review from pmaytak August 5, 2025 19:54
Copilot finished work on behalf of pmaytak August 5, 2025 19:54
Copilot finished work on behalf of pmaytak August 5, 2025 20:00
@Copilot Copilot AI requested a review from pmaytak August 5, 2025 20:14
Copilot finished work on behalf of pmaytak August 5, 2025 20:14
@Copilot Copilot AI requested a review from pmaytak August 5, 2025 22:47
Copilot finished work on behalf of pmaytak August 5, 2025 22:47
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.

[Bug] Decryption of larger payload JWE fails with IDX10816 during deflation in DeflateCompressionProvider.cs

5 participants