From 0d5f0b51b0f111ea3a21806ba47391837baea1f3 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Thu, 12 Nov 2026 17:01:52 +0100 Subject: [PATCH] feat: add expected_value to custom-property rules and cap gzip webhook bodies --- getstream/models/__init__.py | 12 +++++++ getstream/tests/test_webhook.py | 12 +++++++ getstream/webhook.py | 34 ++++++++++++------ .../_invalid/oversized_compression/body.gz | Bin 0 -> 20551 bytes .../_invalid/oversized_compression/body.json | 1 + .../oversized_compression/expected.json | 1 + .../oversized_compression/signature.txt | 1 + .../sns_notification.txt | 1 + .../oversized_compression/sqs_body.txt | 1 + .../sqs_body_uncompressed.txt | 1 + 10 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/body.gz create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/body.json create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/expected.json create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/signature.txt create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/sns_notification.txt create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/sqs_body.txt create mode 100644 tests/assets/webhooks/_invalid/oversized_compression/sqs_body_uncompressed.txt diff --git a/getstream/models/__init__.py b/getstream/models/__init__.py index 12d28cd4..1fcf9e54 100644 --- a/getstream/models/__init__.py +++ b/getstream/models/__init__.py @@ -3927,6 +3927,9 @@ class CallCreatedEvent(DataClassJsonMixin): @dataclass class CallCustomPropertyParameters(DataClassJsonMixin): + expected_value: object | None = dc_field( + default=None, metadata=dc_config(field_name="expected_value") + ) operator: str | None = dc_field( default=None, metadata=dc_config(field_name="operator") ) @@ -9268,6 +9271,9 @@ class ContentCountRuleParameters(DataClassJsonMixin): @dataclass class ContentCustomPropertyCountParameters(DataClassJsonMixin): + expected_value: object | None = dc_field( + default=None, metadata=dc_config(field_name="expected_value") + ) operator: str | None = dc_field( default=None, metadata=dc_config(field_name="operator") ) @@ -9284,6 +9290,9 @@ class ContentCustomPropertyCountParameters(DataClassJsonMixin): @dataclass class ContentCustomPropertyParameters(DataClassJsonMixin): + expected_value: object | None = dc_field( + default=None, metadata=dc_config(field_name="expected_value") + ) operator: str | None = dc_field( default=None, metadata=dc_config(field_name="operator") ) @@ -30303,6 +30312,9 @@ class UserCustomEventRequest(DataClassJsonMixin): @dataclass class UserCustomPropertyParameters(DataClassJsonMixin): + expected_value: object | None = dc_field( + default=None, metadata=dc_config(field_name="expected_value") + ) operator: str | None = dc_field( default=None, metadata=dc_config(field_name="operator") ) diff --git a/getstream/tests/test_webhook.py b/getstream/tests/test_webhook.py index 1f5c9f03..0d21a034 100644 --- a/getstream/tests/test_webhook.py +++ b/getstream/tests/test_webhook.py @@ -1,6 +1,7 @@ # Code generated by GetStream internal OpenAPI code generator. DO NOT EDIT. import hashlib +import gzip import hmac import warnings from pathlib import Path @@ -11,6 +12,7 @@ InvalidWebhookError, UnknownEvent, get_event_type, + gunzip_payload, parse_event, parse_sns, parse_sqs, @@ -1261,6 +1263,16 @@ def test_bad_compression(self): with pytest.raises(InvalidWebhookError, match=InvalidWebhookError.GZIP_FAILED): verify_and_parse_webhook(body, sig, CANONICAL_TEST_SECRET) + def test_oversized_compression(self): + body = _read_bytes(self._neg("oversized_compression") / "body.gz") + sig = _read_str(self._neg("oversized_compression") / "signature.txt") + with pytest.raises(InvalidWebhookError, match="payload exceeds"): + verify_and_parse_webhook(body, sig, CANONICAL_TEST_SECRET) + + def test_trailing_data_after_gzip(self): + with pytest.raises(InvalidWebhookError, match="trailing data"): + gunzip_payload(gzip.compress(b"{}") + b"XYZ") + def test_bad_base64(self): # Per CHA-3071 wire format: decode_sqs_payload falls back to raw bytes when # base64 decoding fails (uncompressed wire format). For input that is diff --git a/getstream/webhook.py b/getstream/webhook.py index b8c39083..e2f4773c 100644 --- a/getstream/webhook.py +++ b/getstream/webhook.py @@ -1,7 +1,6 @@ # Code generated by GetStream internal OpenAPI code generator. DO NOT EDIT. import base64 -import gzip import hashlib import hmac import json @@ -650,6 +649,11 @@ def verify_webhook_signature(body: bytes | str, signature: str, secret: str) -> _GZIP_MAGIC = b"\x1f\x8b" +# Caps the decompressed size of a gzip webhook body. gunzip_payload runs before +# the signature check, so without a cap an unsigned gzip bomb could exhaust +# memory. Raise it if your events are larger. +MAX_WEBHOOK_PAYLOAD_BYTES = 10 * 1024 * 1024 + def gunzip_payload(body: bytes) -> bytes: """Decompress body if gzip-prefixed, else pass through. @@ -658,13 +662,8 @@ def gunzip_payload(body: bytes) -> bytes: Stream webhook bodies are always JSON, and JSON cannot start with 0x1F. Raises InvalidWebhookError if body has the gzip magic prefix but isn't a - valid gzip stream. gzip.decompress can fail with at least three different - exception types depending on where decoding breaks: - * gzip.BadGzipFile (subclass of OSError) - malformed header. - * zlib.error - corrupt compressed stream. - * EOFError - stream truncated mid-block. - The first is OSError-derived; the latter two are not. Catch all three so - every "looks like gzip but isn't" failure mode surfaces as InvalidWebhookError. + valid gzip stream (corrupt or truncated), or decompresses to more than + MAX_WEBHOOK_PAYLOAD_BYTES. """ if not isinstance(body, (bytes, bytearray)): raise InvalidWebhookError( @@ -672,10 +671,25 @@ def gunzip_payload(body: bytes) -> bytes: ) if len(body) < 2 or bytes(body[:2]) != _GZIP_MAGIC: return bytes(body) + limit = MAX_WEBHOOK_PAYLOAD_BYTES + decompressor = zlib.decompressobj(wbits=31) try: - return gzip.decompress(bytes(body)) - except (OSError, zlib.error, EOFError) as e: + out = decompressor.decompress(bytes(body), limit + 1) + except zlib.error as e: raise InvalidWebhookError(f"{InvalidWebhookError.GZIP_FAILED}: {e}") from e + if len(out) > limit: + raise InvalidWebhookError( + f"{InvalidWebhookError.GZIP_FAILED}: payload exceeds {limit} bytes" + ) + if not decompressor.eof: + raise InvalidWebhookError( + f"{InvalidWebhookError.GZIP_FAILED}: truncated gzip stream" + ) + if decompressor.unused_data: + raise InvalidWebhookError( + f"{InvalidWebhookError.GZIP_FAILED}: trailing data after gzip stream" + ) + return out def decode_sqs_payload(message_body: str) -> bytes: diff --git a/tests/assets/webhooks/_invalid/oversized_compression/body.gz b/tests/assets/webhooks/_invalid/oversized_compression/body.gz new file mode 100644 index 0000000000000000000000000000000000000000..78386cbfeae45c22abd71d8c66f55f610a1d0c22 GIT binary patch literal 20551 zcmeIzp-O`R7zNOOS_B7TT~V7h?WIH@BxO153s7qqG7>cFo-(z4K5c9 zo`o?6_rk{g{-hcoVsXDSTE=g$KQA}$-XD+u z^tF4n9{-d|@M&79iA%OmSL19Y=nt@f%8=p`C*T?2AsC-eB}f4ZRE89nP=Epyh|i}I zqyPmfLyAi%KmiKG=Tiw%fC7~v#U&J=00rXnsRSuNfy$8L5(-d&0`d7&f)t=YWk_)e z1t>s)_*Cm1Svp)%8=p` X3Q*wRD{we@8_$N<$-&KNI3K