Skip to content
Open
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
9 changes: 9 additions & 0 deletions stream_chat/tests/test_webhook_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def test_truncated_gzip_with_magic_raises(self):
gunzip_payload(bad)
assert str(exc_info.value) == INVALID_WEBHOOK_GZIP_FAILED

def test_corrupt_deflate_stream_raises(self):
# A valid gzip header followed by a damaged deflate stream makes
# gzip.decompress raise zlib.error, which is not an OSError.
corrupt = bytearray(_gzip(JSON_BODY))
corrupt[10] = 0xFF
with pytest.raises(InvalidWebhookError) as exc_info:
gunzip_payload(bytes(corrupt))
assert str(exc_info.value) == INVALID_WEBHOOK_GZIP_FAILED


class TestDecodeSqsPayload:
def test_base64_only_no_compression(self):
Expand Down
3 changes: 2 additions & 1 deletion stream_chat/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import hashlib
import hmac
import json
import zlib
from typing import Any, Dict, Optional, Union

from stream_chat.base.exceptions import (
Expand Down Expand Up @@ -54,7 +55,7 @@ def gunzip_payload(body: _BytesLike) -> bytes:
return raw
try:
return gzip.decompress(raw)
except (gzip.BadGzipFile, OSError, EOFError) as exc:
except (gzip.BadGzipFile, OSError, EOFError, zlib.error) as exc:
raise InvalidWebhookError(INVALID_WEBHOOK_GZIP_FAILED) from exc


Expand Down