[CHA-3071] feat: decode gzip-compressed webhook bodies#169
Open
nijeesh-stream wants to merge 2 commits intomainfrom
Open
[CHA-3071] feat: decode gzip-compressed webhook bodies#169nijeesh-stream wants to merge 2 commits intomainfrom
nijeesh-stream wants to merge 2 commits intomainfrom
Conversation
Adds Client::decompressWebhookBody and Client::verifyAndDecodeWebhook so handlers can accept the new outbound webhook compression (GetStream/chat#13222) without changing how X-Signature is verified. decompressWebhookBody runs gzdecode when the Content-Encoding header is gzip, returns the body unchanged when the header is null or empty, and throws StreamException for any other value with a message that points the operator at the app's webhook_compression_algorithm setting. verifyAndDecodeWebhook chains decompression with the existing HMAC check and returns the raw JSON when the signature matches. The signature is always computed over the uncompressed bytes, matching the server. verifyWebhook switches to hash_equals so the comparison is constant-time. Tests cover gzip round-trip, null/empty/whitespace passthrough, case- insensitive Content-Encoding, invalid gzip bytes, every non-gzip encoding being rejected with a clear message, signature mismatch, and the regression case where the signature was computed over the compressed bytes. Co-authored-by: Cursor <cursoragent@cursor.com>
Extends `decompressWebhookBody` and `verifyAndDecodeWebhook` with an optional `$payloadEncoding` argument. When set to "base64" (the wrapper Stream applies for SQS / SNS firehose so the message stays valid UTF-8 over the queue), the body is base64-decoded before gzip decompression. The HMAC signature continues to be computed over the innermost (uncompressed, base64-decoded) JSON, so the verification rule is invariant across HTTP webhooks and SQS / SNS. `null` / `""` for payloadEncoding is a no-op, so the HTTP webhook path is byte-identical to before this change. Default value of `null` preserves backward compatibility with the previous 3-argument call. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Summary
Adds SDK-side support for the gzip webhook compression that the Stream chat backend can now apply to outbound webhook payloads. Linear: CHA-3071.
The same helper covers HTTP webhooks and SQS / SNS firehose payloads, so customers get one entry point regardless of transport.
What's new
Two new public methods on
Client:decompressWebhookBody($body, ?$contentEncoding, ?$payloadEncoding = null): string— primitive that undoes the encoding wrappers Stream applies.gzipfor compression,base64for the SQS / SNS transport wrapper. Anything else throwsStreamExceptionwith a clear message.verifyAndDecodeWebhook($body, $signature, ?$contentEncoding, ?$payloadEncoding = null): string— convenience: decode + verify in one call. ThrowsStreamException("invalid webhook signature")on signature mismatch. The HMAC is always validated over the innermost (uncompressed, base64-decoded) JSON, so the verification rule is invariant across HTTP and SQS / SNS.verifyWebhook($requestBody, $XSignature)is unchanged on its public signature; internally it now useshash_equalsfor constant-time comparison.Cross-SDK contract
Same surface is shipping in every SDK so customers see a uniform API:
$client->verifyAndDecodeWebhook($body, $signature, $contentEncoding, $payloadEncoding)App.verifyAndDecodeWebhook(rawBody, signature, contentEncoding, payloadEncoding)client.VerifyAndDecodeWebhook(body, signature, contentEncoding, payloadEncoding)$payloadEncodingisnullfor HTTP webhooks today; it's the slot the SQS / SNS firehose path uses to flag a base64 wrapper.Tests
tests/unit/WebhookCompressionTest.phpcovers:Content-EncodingContent-Encoding(br,brotli,zstd,deflate,compress,lz4) rejectedpayload_encoding(hex,url,binary) rejectedStreamExceptionverifyWebhookconstant-time compareverifyAndDecodeWebhook(plain, gzip, base64+gzip)StreamException24 tests / 44 assertions, all green.
Docs
Updates
docs/webhooks/webhooks_overview/webhooks_overview.mdwith the public-facing copy from the Linear ticket, aContent-Encodingrow in the headers table, and PHP usage examples for both HTTP webhooks and SQS / SNS.Notes for review
gzipis the only compression algorithm we support today, despite the server's validation tag.gzdecode,base64_decode,hash_hmac,hash_equalsare all built-in.Verification
All green.