Skip to main content
Menu
Advisories

Quadratic WebSocket fragment reassembly causes CPU denial of service

GHSA-3W8Q-H8JV-VVHWPublished Updated GitHub

Description

WebSocketSession._background_receive() and AsyncWebSocketSession._background_receive() reassembled fragmented WebSocket messages by concatenating each incoming continuation frame onto a running buffer with partial_message_buffer += event.data. For binary messages (bytes), every continuation frame reallocates and copies the entire accumulated buffer, making reassembly O(N^2) in the number of frames. A remote WebSocket server, or an on-path attacker on a non-TLS (ws://) connection, can split messages into tiny continuation frames to consume excessive client CPU.

Details

In httpx2/websockets/_api.py, wsproto emits one Message event per incoming continuation frame with message_finished=False. Both the synchronous and asynchronous receive loops accumulated unfinished frames with:

partial_message_buffer += event.data

Because bytes is immutable in Python, reassembling a binary message of N bytes delivered as k-byte continuation frames performs O(N^2 / k) byte copies. Although httpx2 >= 2.10.0 enforces a per-message size cap (max_message_size_bytes, defaulting to 65,536 bytes), that guard only checks cumulative payload size and does not bound the number of frames or the quadratic copy work performed up to the limit. Versions 2.6.0 through 2.9.1 had no per-message size cap at all.

The receive loops start automatically whenever a session is opened with client.websocket() or httpx2.websocket().

Impact

Availability degradation (CPU denial of service) in applications that connect to an untrusted or compromised WebSocket endpoint, or over an unencrypted ws:// link. Reassembly holds the GIL in the synchronous client and runs without yielding to the event loop in the asynchronous client, blocking other coroutines in the process.

With the default 64 KiB max_message_size_bytes cap in >= 2.10.0, per-message quadratic overhead is bounded (tens of milliseconds per maximally fragmented message) and must be repeated across messages or connections to sustain CPU load. Applications running 2.6.0 through 2.9.1, or configuring a larger max_message_size_bytes, face substantially larger single-message CPU amplification. Confidentiality and integrity are not affected.

Mitigation

Upgrade to httpx2 2.18.0 or later, which accumulates continuation-frame payloads in a list (ignoring empty fragments) and joins them once when the final frame arrives, making message reassembly linear in the received bytes.

If upgrading is not immediately possible, keep max_message_size_bytes small and restrict WebSocket connections to trusted wss:// endpoints.

Affected versions

References

GitHub · pydantic/httpx2