-
Notifications
You must be signed in to change notification settings - Fork 9
feat(webhooks): verifyAndParse* API for compressed payloads (CHA-3071) #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nijeesh-stream
merged 13 commits into
main
from
nijeeshjoshy/cha-3071-compress-webhook-payloads
May 13, 2026
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d983247
[CHA-3071] feat: decode gzip-compressed webhook bodies
nijeesh-stream 401d209
[CHA-3071] feat: support base64 payload_encoding for SQS / SNS
nijeesh-stream 5561cb6
refactor(webhooks): switch to verifyAndParse* API (CHA-3071)
nijeesh-stream 49f5417
refactor(webhooks): use 2-byte gzip magic per RFC 1952 (CHA-3071)
nijeesh-stream f987cfc
Add Webhook static class for cross-SDK 3-arg contract
nijeesh-stream b6870f2
docs(webhooks): align compression docs with shipped API (CHA-3071)
nijeesh-stream 238c5b8
refactor(webhooks): make Webhook the canonical home of the contract (…
nijeesh-stream 5d92a2b
fix(webhooks): unwrap SNS notification envelope in decodeSnsPayload
nijeesh-stream e6cb426
refactor(webhooks): rename ungzipPayload to gunzipPayload + add golde…
nijeesh-stream 619e75c
refactor(webhooks): unify webhook errors under InvalidWebhookExceptio…
nijeesh-stream 703e939
feat(webhooks): make signature optional on verifyAndParseSqs/Sns (CHA…
nijeesh-stream 7399515
fix(webhooks): parseSqs/ParseSns decode-only; HTTP verify via verifyA…
nijeesh-stream 8fb87c4
feat(webhooks): align cross-SDK contract — InvalidWebhookError + gunz…
nijeesh-stream File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=0); | ||
|
|
||
| namespace GetStream\StreamChat; | ||
|
|
||
| /** | ||
| * Stateless helpers implementing the cross-SDK webhook contract documented at | ||
| * https://getstream.io/chat/docs/node/webhooks_overview/. | ||
| * | ||
| * The composite functions (`verifyAndParseWebhook`, `verifyAndParseSqs`, | ||
| * `verifyAndParseSns`) are the recommended entry points. The primitives they | ||
| * compose (`ungzipPayload`, `decodeSqsPayload`, `decodeSnsPayload`, | ||
| * `verifySignature`, `parseEvent`) are exposed so callers can build custom | ||
| * flows or run individual steps in isolation. | ||
| * | ||
| * The PHP SDK currently returns the parsed JSON as an associative array; typed | ||
| * event classes will land in a future release. | ||
| */ | ||
| class Webhook | ||
| { | ||
| /** Constant-time HMAC-SHA256 verification of `$signature` against the digest of | ||
| * `$body` using `$secret` as the key. | ||
| * | ||
| * The signature is always computed over the **uncompressed** JSON bytes, so | ||
| * callers that decoded a gzipped or base64-wrapped payload must pass the | ||
| * inflated bytes here. | ||
| */ | ||
| public static function verifySignature(string $body, string $signature, string $secret): bool | ||
| { | ||
| return hash_equals(hash_hmac('sha256', $body, $secret), $signature); | ||
| } | ||
|
|
||
| /** Returns `$body` unchanged unless it starts with the gzip magic | ||
| * (`1f 8b`, per RFC 1952), in which case the gzip stream is inflated and | ||
| * the decompressed bytes are returned. | ||
| * | ||
| * Magic-byte detection (rather than relying on a header) keeps the same | ||
| * handler correct when middleware auto-decompresses the request before your | ||
| * code sees it. | ||
| * | ||
| * @throws StreamException when the body has the gzip magic but cannot be | ||
| * inflated. | ||
| */ | ||
| public static function ungzipPayload(string $body): string | ||
| { | ||
| if (substr($body, 0, 2) !== "\x1f\x8b") { | ||
| return $body; | ||
| } | ||
| $decoded = @gzdecode($body); | ||
| if ($decoded === false) { | ||
| throw new StreamException('failed to decompress gzip payload'); | ||
| } | ||
| return $decoded; | ||
| } | ||
|
|
||
| /** Reverses the SQS firehose envelope: the message `Body` is base64-decoded | ||
| * and, when the result begins with the gzip magic, gzip-decompressed. The | ||
| * same call works whether or not Stream is currently compressing payloads. | ||
| * | ||
| * @throws StreamException when the input is not valid base64 or the inner | ||
| * gzip stream cannot be inflated. | ||
| */ | ||
| public static function decodeSqsPayload(string $body): string | ||
| { | ||
| $decoded = base64_decode($body, true); | ||
| if ($decoded === false) { | ||
| throw new StreamException('failed to base64-decode payload'); | ||
| } | ||
| return self::ungzipPayload($decoded); | ||
| } | ||
|
|
||
| /** Identical to {@see decodeSqsPayload()}; exposed under both names so call | ||
| * sites read intent. | ||
| * | ||
| * @throws StreamException | ||
| */ | ||
| public static function decodeSnsPayload(string $message): string | ||
| { | ||
| return self::decodeSqsPayload($message); | ||
| } | ||
|
|
||
| /** Parse a JSON-encoded webhook event into an associative array. | ||
| * | ||
| * The PHP SDK currently returns the parsed JSON as an array; typed event | ||
| * classes will land in a future release. The function name matches the | ||
| * documented primitive so callers can swap in a typed parser later without | ||
| * changing call sites. | ||
| * | ||
| * @return array<string, mixed> | ||
| * @throws StreamException when the bytes are not valid JSON. | ||
| */ | ||
| public static function parseEvent(string $payload): array | ||
| { | ||
| try { | ||
| $event = json_decode($payload, true, 512, JSON_THROW_ON_ERROR); | ||
| } catch (\JsonException $e) { | ||
| throw new StreamException('failed to parse webhook event: ' . $e->getMessage()); | ||
| } | ||
| if (!is_array($event)) { | ||
| throw new StreamException('failed to parse webhook event: top-level value is not an object'); | ||
| } | ||
| return $event; | ||
| } | ||
|
|
||
| /** Decompress `$body` when gzipped, verify the HMAC `$signature`, and return | ||
| * the parsed event. | ||
| * | ||
| * @return array<string, mixed> | ||
| * @throws StreamException when the signature does not match or the gzip | ||
| * envelope is malformed. | ||
| */ | ||
| public static function verifyAndParseWebhook(string $body, string $signature, string $secret): array | ||
| { | ||
| $inflated = self::ungzipPayload($body); | ||
| if (!self::verifySignature($inflated, $signature, $secret)) { | ||
| throw new StreamException('invalid webhook signature'); | ||
| } | ||
| return self::parseEvent($inflated); | ||
| } | ||
|
|
||
| /** Decode the SQS `Body` (base64, then gzip-if-magic), verify the HMAC | ||
| * `$signature` from the `X-Signature` message attribute, and return the | ||
| * parsed event. | ||
| * | ||
| * @return array<string, mixed> | ||
| * @throws StreamException | ||
| */ | ||
| public static function verifyAndParseSqs(string $messageBody, string $signature, string $secret): array | ||
| { | ||
| $inflated = self::decodeSqsPayload($messageBody); | ||
| if (!self::verifySignature($inflated, $signature, $secret)) { | ||
| throw new StreamException('invalid webhook signature'); | ||
| } | ||
| return self::parseEvent($inflated); | ||
| } | ||
|
|
||
| /** Decode the SNS notification `Message` (identical to SQS handling), verify | ||
| * the HMAC `$signature` from the `X-Signature` message attribute, and return | ||
| * the parsed event. | ||
| * | ||
| * @return array<string, mixed> | ||
| * @throws StreamException | ||
| */ | ||
| public static function verifyAndParseSns(string $message, string $signature, string $secret): array | ||
| { | ||
| $inflated = self::decodeSnsPayload($message); | ||
| if (!self::verifySignature($inflated, $signature, $secret)) { | ||
| throw new StreamException('invalid webhook signature'); | ||
| } | ||
| return self::parseEvent($inflated); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.