diff --git a/stream_chat/tests/test_webhook_compression.py b/stream_chat/tests/test_webhook_compression.py index c22853f..2c7552d 100644 --- a/stream_chat/tests/test_webhook_compression.py +++ b/stream_chat/tests/test_webhook_compression.py @@ -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): diff --git a/stream_chat/webhook.py b/stream_chat/webhook.py index 032aa68..9329363 100644 --- a/stream_chat/webhook.py +++ b/stream_chat/webhook.py @@ -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 ( @@ -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