[security] Webhook helpers decompress gzip bodies with no size limit before checking the signature (gzip bomb)

未关闭
#244 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend, security

调研方向

Start with stream_chat/webhook.py, especially gunzip_payload and verify_and_parse_webhook, then inspect the webhook tests and the linked implementations for stream-py and stream-chat-js. Reproduce the oversized decompression case and trace how failures are reported. Done means decompression has a configurable output bound and oversized or invalid streams are rejected; the issue gives no test file or limit value.

由索引模型根据 Issue 内容生成。

描述

I'm Claude Opus 5.5, AI can make mistakes. Written for a human user, who asked for this report.

Filed as a public issue tagged security, which SECURITY.md allows as an alternative to emailing [email protected].

Summary

verify_and_parse_webhook decompresses a gzip body completely before it checks X-Signature. The order is forced: the HMAC covers the uncompressed JSON. But gunzip_payload has no limit on how much it decompresses, so anyone who can reach a webhook endpoint can make it use hundreds of MB to GBs of memory with a small unsigned request, a classic "gzip bomb". A webhook endpoint has to be public for Stream to reach it, and the signature check only fails after the memory is already used.

Affected code

The SQS and SNS parsers (parse_sqs / parse_sns, parseSqs / parseSns) use the same helper. Their input normally comes from a queue we control, so the exposure there is lower.

Reproduction (stream-chat 4.32.0, Python 3.14)

import tracemalloc, zlib
from stream_chat.webhook import verify_and_parse_webhook
from stream_chat.base.exceptions import InvalidWebhookError

def bomb(size: int) -> bytes:  # gzip of `size` zero bytes, built without holding `size` bytes
    d = zlib.compressobj(9, zlib.DEFLATED, 31)
    chunk = bytes(1 << 20)
    return b"".join([d.compress(chunk) for _ in range(size >> 20)] + [d.flush()])

body = bomb(256 << 20)   # 260,934 bytes on the wire
tracemalloc.start()
try:
    verify_and_parse_webhook(body, "not-a-signature", "secret")
except InvalidWebhookError as e:
    print(e)             # "signature mismatch", raised only after inflating
print(tracemalloc.get_traced_memory()[1] >> 20, "MB peak")   # 557 MB

A 255 KB unsigned request drives peak memory to ~558 MB before the signature is rejected. A ~1 MB request inflates to ~1 GB, and a handful of concurrent requests will run a typical container out of memory.

Suggested fix

Bound the output of decompression, and reject anything over the limit as an invalid webhook, so it can never succeed and costs at most the limit in memory:

  • Python: zlib.decompressobj(wbits=16 + zlib.MAX_WBITS).decompress(raw, max_length=LIMIT + 1). Treat output longer than LIMIT as an error, and check .eof (truncated stream) and .unused_data (trailing data).
  • Node: zlib.gunzipSync(body, { maxOutputLength: LIMIT }), which throws RangeError past the limit.
  • The limit: a default sized well above any real event, overridable by callers. Stream already caps SQS messages at 256 KB, which may be a natural default. The recorded real chat webhook events we hold are 7–18 KB.

Documenting the limit in the webhook docs' compression section would also help people who decompress in their own middleware.

Context

We hit this while adding gzip support to our webhook receiver. We decompress through a capped zlib.decompressobj there instead of the SDK helper, and would happily switch back once the helper is bounded.

主要语言
Python
星标
73
派生
20
平均合并
11 小时 34 分钟
30 天内合并 PR
7

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

GetStream/stream-chat-python 的其他 Issue

查看 GetStream/stream-chat-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。