[CHA-3071] feat: decode gzip-compressed webhook bodies#169
Open
nijeesh-stream wants to merge 1 commit intomainfrom
Open
[CHA-3071] feat: decode gzip-compressed webhook bodies#169nijeesh-stream wants to merge 1 commit intomainfrom
nijeesh-stream wants to merge 1 commit 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>
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.
Ticket
Summary
Server-side webhook compression is landing in GetStream/chat#13222. When an app opts in (
webhook_compression_algorithm = "gzip") every outbound webhook body is gzipped and aContent-Encoding: gzipheader is added.X-Signatureis still computed over the uncompressed JSON, so handlers must decompress before verifying.This PR adds the customer-side primitives so handlers built on this SDK keep working when their app gets flipped on.
What's new
Client::decompressWebhookBody(string $body, ?string $contentEncoding): stringnull/''/ whitespace → returns body unchanged.gzip(case-insensitive, trimmed) →gzdecode.StreamExceptionwith a message that names the offending encoding and points the operator back atwebhook_compression_algorithm.Client::verifyAndDecodeWebhook(string $body, string $signature, ?string $contentEncoding): stringStreamExceptionon signature mismatch.Client::verifyWebhooknow useshash_equalsinstead of===so the signature comparison is constant-time. Public surface is unchanged.The SDK supports gzip only. Any other
Content-Encoding(br,zstd,deflate, …) raises a clear error rather than silently dropping the body.Tests
tests/unit/WebhookCompressionTest.php(16 cases under the unit suite, 20 total in the suite):null, empty, whitespacebr,brotli,zstd,deflate,compress,lz4with the expected hintfailed to gzip-decodeverifyWebhookconstant-time true / falseverifyAndDecodeWebhookhappy paths (gzip + passthrough), bad signature, and the regression case where the signature was computed over the compressed bytes (must reject).vendor/bin/php-cs-fixer fix --dry-run --diffclean on the touched files.Backward compatibility
verifyWebhook($body, $signature)keeps the same signature and behavior; the only difference is the comparison is now constant-time.zlibextension.Made with Cursor