Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions getstream/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down Expand Up @@ -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")
)
Expand All @@ -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")
)
Expand Down Expand Up @@ -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")
)
Expand Down
12 changes: 12 additions & 0 deletions getstream/tests/test_webhook.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,7 @@
InvalidWebhookError,
UnknownEvent,
get_event_type,
gunzip_payload,
parse_event,
parse_sns,
parse_sqs,
Expand Down Expand Up @@ -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
Expand Down
34 changes: 24 additions & 10 deletions getstream/webhook.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -658,24 +662,34 @@ 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(
InvalidWebhookError.INVALID_JSON + ": body must be 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:
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"message.new","created_at":1778198400000000000}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fbfb957f04748ba32f10808cc88b47c62c1d795410e430857cbabb7c788d121a

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eyJ0eXBlIjoibWVzc2FnZS5uZXciLCJjcmVhdGVkX2F0IjoxNzc4MTk4NDAwMDAwMDAwMDAwfQ==
Loading