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
2 changes: 2 additions & 0 deletions getstream/common/async_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async def update_app(
user_search_disallowed_roles: Optional[List[str]] = None,
webhook_events: Optional[List[str]] = None,
activity_metrics_config: Optional[Dict[str, int]] = None,
agent_review: Optional[AgentReviewSettingsPayload] = None,
apn_config: Optional[APNConfig] = None,
async_moderation_config: Optional[AsyncModerationConfiguration] = None,
datadog_info: Optional[DataDogInfo] = None,
Expand Down Expand Up @@ -153,6 +154,7 @@ async def update_app(
user_search_disallowed_roles=user_search_disallowed_roles,
webhook_events=webhook_events,
activity_metrics_config=activity_metrics_config,
agent_review=agent_review,
apn_config=apn_config,
async_moderation_config=async_moderation_config,
datadog_info=datadog_info,
Expand Down
2 changes: 2 additions & 0 deletions getstream/common/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def update_app(
user_search_disallowed_roles: Optional[List[str]] = None,
webhook_events: Optional[List[str]] = None,
activity_metrics_config: Optional[Dict[str, int]] = None,
agent_review: Optional[AgentReviewSettingsPayload] = None,
apn_config: Optional[APNConfig] = None,
async_moderation_config: Optional[AsyncModerationConfiguration] = None,
datadog_info: Optional[DataDogInfo] = None,
Expand Down Expand Up @@ -153,6 +154,7 @@ def update_app(
user_search_disallowed_roles=user_search_disallowed_roles,
webhook_events=webhook_events,
activity_metrics_config=activity_metrics_config,
agent_review=agent_review,
apn_config=apn_config,
async_moderation_config=async_moderation_config,
datadog_info=datadog_info,
Expand Down
399 changes: 392 additions & 7 deletions getstream/models/__init__.py

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions getstream/moderation/async_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,47 @@ async def insert_action_log(
"/api/v2/moderation/action_logs", InsertActionLogResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.list_agent_feedback")
async def list_agent_feedback(
self, limit: Optional[int] = None, next: Optional[str] = None
) -> StreamResponse[ListAgentFeedbackResponse]:
query_params = build_query_param(**{"limit": limit, "next": next})
return await self.get(
"/api/v2/moderation/agent_feedback",
ListAgentFeedbackResponse,
query_params=query_params,
)

@telemetry.operation_name("getstream.api.moderation.submit_agent_feedback")
async def submit_agent_feedback(
self,
item_id: str,
moderator_action: str,
improvement: Optional[str] = None,
reason: Optional[str] = None,
user_id: Optional[str] = None,
user: Optional[UserRequest] = None,
) -> StreamResponse[SubmitAgentFeedbackResponse]:
json = SubmitAgentFeedbackRequest(
item_id=item_id,
moderator_action=moderator_action,
improvement=improvement,
reason=reason,
user_id=user_id,
user=user,
).to_dict()
return await self.post(
"/api/v2/moderation/agent_feedback", SubmitAgentFeedbackResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.get_agent_review_status")
async def get_agent_review_status(
self,
) -> StreamResponse[GetAgentReviewStatusResponse]:
return await self.get(
"/api/v2/moderation/agent_review_status", GetAgentReviewStatusResponse
)

@telemetry.operation_name("getstream.api.moderation.analyze")
async def analyze(
self,
Expand Down Expand Up @@ -1125,6 +1166,20 @@ async def query_review_queue(
"/api/v2/moderation/review_queue", QueryReviewQueueResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.count_review_queue")
async def count_review_queue(
self,
user_id: Optional[str] = None,
filter: Optional[Dict[str, object]] = None,
user: Optional[UserRequest] = None,
) -> StreamResponse[CountReviewQueueResponse]:
json = CountReviewQueueRequest(
user_id=user_id, filter=filter, user=user
).to_dict()
return await self.post(
"/api/v2/moderation/review_queue/count", CountReviewQueueResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.get_review_queue_item")
async def get_review_queue_item(
self, id: str
Expand Down Expand Up @@ -1163,6 +1218,7 @@ async def submit_action(
appeal_id: Optional[str] = None,
item_id: Optional[str] = None,
user_id: Optional[str] = None,
agent_recommendation: Optional[AgentRecommendationPayload] = None,
ban: Optional[BanActionRequestPayload] = None,
block: Optional[BlockActionRequestPayload] = None,
bypass: Optional[BypassActionRequest] = None,
Expand Down Expand Up @@ -1191,6 +1247,7 @@ async def submit_action(
appeal_id=appeal_id,
item_id=item_id,
user_id=user_id,
agent_recommendation=agent_recommendation,
ban=ban,
block=block,
bypass=bypass,
Expand Down
55 changes: 55 additions & 0 deletions getstream/moderation/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,45 @@ def insert_action_log(
"/api/v2/moderation/action_logs", InsertActionLogResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.list_agent_feedback")
def list_agent_feedback(
self, limit: Optional[int] = None, next: Optional[str] = None
) -> StreamResponse[ListAgentFeedbackResponse]:
query_params = build_query_param(**{"limit": limit, "next": next})
return self.get(
"/api/v2/moderation/agent_feedback",
ListAgentFeedbackResponse,
query_params=query_params,
)

@telemetry.operation_name("getstream.api.moderation.submit_agent_feedback")
def submit_agent_feedback(
self,
item_id: str,
moderator_action: str,
improvement: Optional[str] = None,
reason: Optional[str] = None,
user_id: Optional[str] = None,
user: Optional[UserRequest] = None,
) -> StreamResponse[SubmitAgentFeedbackResponse]:
json = SubmitAgentFeedbackRequest(
item_id=item_id,
moderator_action=moderator_action,
improvement=improvement,
reason=reason,
user_id=user_id,
user=user,
).to_dict()
return self.post(
"/api/v2/moderation/agent_feedback", SubmitAgentFeedbackResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.get_agent_review_status")
def get_agent_review_status(self) -> StreamResponse[GetAgentReviewStatusResponse]:
return self.get(
"/api/v2/moderation/agent_review_status", GetAgentReviewStatusResponse
)

@telemetry.operation_name("getstream.api.moderation.analyze")
def analyze(
self,
Expand Down Expand Up @@ -1111,6 +1150,20 @@ def query_review_queue(
"/api/v2/moderation/review_queue", QueryReviewQueueResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.count_review_queue")
def count_review_queue(
self,
user_id: Optional[str] = None,
filter: Optional[Dict[str, object]] = None,
user: Optional[UserRequest] = None,
) -> StreamResponse[CountReviewQueueResponse]:
json = CountReviewQueueRequest(
user_id=user_id, filter=filter, user=user
).to_dict()
return self.post(
"/api/v2/moderation/review_queue/count", CountReviewQueueResponse, json=json
)

@telemetry.operation_name("getstream.api.moderation.get_review_queue_item")
def get_review_queue_item(
self, id: str
Expand Down Expand Up @@ -1149,6 +1202,7 @@ def submit_action(
appeal_id: Optional[str] = None,
item_id: Optional[str] = None,
user_id: Optional[str] = None,
agent_recommendation: Optional[AgentRecommendationPayload] = None,
ban: Optional[BanActionRequestPayload] = None,
block: Optional[BlockActionRequestPayload] = None,
bypass: Optional[BypassActionRequest] = None,
Expand Down Expand Up @@ -1177,6 +1231,7 @@ def submit_action(
appeal_id=appeal_id,
item_id=item_id,
user_id=user_id,
agent_recommendation=agent_recommendation,
ban=ban,
block=block,
bypass=bypass,
Expand Down
12 changes: 12 additions & 0 deletions getstream/tests/test_webhook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Code generated by GetStream internal OpenAPI code generator. DO NOT EDIT.

import gzip
import hmac
import hashlib

Expand All @@ -16,6 +17,7 @@
verify_signature,
parse_event,
verify_and_parse_webhook,
gunzip_payload,
parse_sqs,
parse_sns,
UnknownEvent,
Expand Down Expand Up @@ -1377,6 +1379,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 hmac
import hashlib
import json
Expand Down Expand Up @@ -707,6 +706,11 @@ def verify_webhook_signature(

_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 @@ -715,24 +719,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