Skip to content

fix: limit decompressed webhook payloads to 10 MiB - #288

Merged
mogita merged 2 commits into
mainfrom
fix/webhook-gzip-limit
Nov 17, 2026
Merged

mogita merged 2 commits into
mainfrom
fix/webhook-gzip-limit

Conversation

@mogita

@mogita mogita commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Submit a pull request

CLA

  • I have signed the Stream CLA (required).
  • The code changes follow best practices
  • Code changes are tested (add some information if not applicable)

Description of the pull request

Problem

App.gunzipPayload inflated a gzip body with no size limit before the signature check, so an unsigned request of a few hundred KB could expand to hundreds of MB and exhaust memory on a webhook endpoint.

Solution

Decompression now stops one byte past 10 MiB and throws InvalidWebhookError ("gzip decompression failed: payload exceeds 10485760 bytes"). The same helper backs verifyAndParseWebhook, parseSqs and parseSns. Raise the cap if your events are larger:

App.maxWebhookPayloadBytes = 50L * 1024 * 1024;

Uncompressed bodies are not limited, and corrupt gzip still throws the existing "gzip decompression failed" error. This matches the default and error message of the other server SDKs.

How to verify

  1. ./gradlew test --tests 'io.getstream.chat.java.WebhookCompressionTest' (runs offline, no STREAM_KEY needed).
  2. The new tests inflate a body of exactly 10 MiB, reject 10 MiB + 1 byte through verifyAndParseWebhook and parseSqs, honour a custom limit, and keep corrupt gzip on the existing error.

Note

Medium Risk
Changes shared webhook/SQS/SNS decompression behavior; legitimate gzip payloads above 10 MiB will fail unless maxWebhookPayloadBytes is raised.

Overview
Webhook gzip decompression is now bounded so unsigned small compressed bodies cannot expand into huge allocations before signature verification.

App.gunzipPayload no longer reads an entire gzip stream unbounded; it adds a configurable App.maxWebhookPayloadBytes default (10 MiB), reads only one byte past that cap, and throws InvalidWebhookError when decompressed size exceeds the limit. Uncompressed bodies remain unchanged. The unbounded readAll helper is removed in favor of readNBytes.

Because verifyAndParseWebhook, decodeSqsPayload, and the SNS decode path all call gunzipPayload, the same protection applies to direct webhooks, SQS, and SNS ingestion. WebhookCompressionTest gains coverage for at-limit payloads, gzip bombs (including before signature check), custom limits, corrupt deflate data, and early stop past the limit.

Reviewed by Cursor Bugbot for commit 161bf84. Bugbot is set up for automated code reviews on this repo. Configure here.

@DisplayName("gzip bomb is rejected before the signature check and on the SQS path")
void gunzipPayload_rejectsBomb() throws Exception {
long limit = App.maxWebhookPayloadBytes;
byte[] bomb = gzip(new byte[(int) limit + 1]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These tests do not prove the bounded read. Swapping the readNBytes(...) call for in.readAllBytes() (inflate everything, then compare the length) keeps all 34 tests in this class green, so a later refactor could bring the gzip bomb back unnoticed.

A cheap test that fails on that regression: corrupt the trailer CRC and use a small cap. Only a read that stops at limit + 1 returns the size error; an unbounded read reaches the CRC check first and throws plain gzip decompression failed.

@Test
void gunzipPayload_stopsReadingPastLimit() throws Exception {
  long original = App.maxWebhookPayloadBytes;
  try {
    byte[] body = gzip(new byte[100]);
    body[body.length - 8] ^= 0x01; // corrupt CRC32 in the trailer
    App.maxWebhookPayloadBytes = 10;
    InvalidWebhookError ex =
        Assertions.assertThrows(InvalidWebhookError.class, () -> App.gunzipPayload(body));
    Assertions.assertEquals(
        InvalidWebhookError.GZIP_FAILED + ": payload exceeds 10 bytes", ex.getMessage());
  } finally {
    App.maxWebhookPayloadBytes = original;
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in 161bf84.

@mogita
mogita merged commit dd27223 into main Nov 17, 2026
6 of 9 checks passed
@mogita
mogita deleted the fix/webhook-gzip-limit branch November 17, 2026 16:11
@mogita mogita mentioned this pull request Nov 17, 2026
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.

1 participant