Skip to content

beast: bound what a WebSocket client's handshake reads - #2

Merged
pinhopro merged 3 commits into
mainfrom
handshake-limit
Nov 15, 2026
Merged

pinhopro merged 3 commits into
mainfrom
handshake-limit

Conversation

@pinhopro

Copy link
Copy Markdown
Contributor

Summary

A WebSocket client can now bound the HTTP response its handshake reads, with websocket::bounded_handshake_stream, a next layer between the stream and its transport (<webcpp/beast/websocket/bounded_handshake_stream.hpp>).

Natively the WebSocket stream is Boost.Beast's own, whose handshake parses the response with the default limits (8 KiB of header, up to 8 MiB of a refusal's body, outside the caller's allocator) and offers no option to lower them. The layer parses the response's header with Beast's own parser under response_limits (by default 8 KiB of header and 1 KiB of a refusal's body), fails the handshake with http::error::header_limit or body_limit after reading at most both bounds, and lets a 101 through unchanged. The plain stream is unchanged. webcpp::trystero's relays use it with no byte of a refusal's body read (webcpporg/webcpp#17): a 4 MiB refusal went from 4,199,353 bytes allocated to 5,144.

Verification

Native, wasip2, wasip3 and emscripten lanes, the TLS lane over TLS 1.2 and 1.3, without exceptions, the browser lanes, the page; tests written first (16 failures against a pass-through stand-in), and the lint clean.

🤖 Generated with Claude Code

R added 2 commits November 14, 2026 23:22
A client's handshake reads the server's HTTP response with Beast's default
parser limits, 8 KiB of header and 8 MiB of a refused upgrade's body, outside
the caller's allocator. Natively that is Boost.Beast's own code and on Wasm the
shipped copy, neither of which this library corrects, so the plain stream keeps
those limits.

websocket::bounded_handshake_stream is a next layer between the WebSocket stream
and its transport. It parses the response's header with Beast's own parser under
websocket::response_limits (8 KiB of header, 1 KiB after a refusal's header by
default) and fails the handshake with http::error::header_limit or body_limit
after reading at most both bounds; a 101 passes through unchanged. It starts its
transport's operations through function pointers, so that misc-no-recursion sees
no cycle through it in a consumer's lint. The WASI facade now passes an
allocator-tagged construction on to an allocator-using next layer, as Boost does
natively.

handshake_test.cpp refuses upgrades with declared, streamed and chunked megabyte
bodies and answers one with a long header, against local stand-ins, on native,
WASIp2 and WASIp3; the TLS protocol test repeats the refusal and an echo whose
shutdown passes through the layer.
…uent chain

The lint's fluent-chains rule refused two calls of async_handshake on
next_layer().next_layer(): in the example of bounded_handshake_stream's
Doc Comment and in the TLS protocol test's bounded exchange. Both now
bind the TLS stream to a reference first, as the rule asks.

The example also starts the WebSocket handshake from the TLS handshake's
completion handler, which hands a TLS error to on_upgrade, where it
started both handshakes one after the other. It compiles as written
natively, and the reference page shows it so.
… library does

MSVC's C library has no aligned_alloc, so the test's counting operator new failed to compile on both MSVC lanes; it takes _aligned_malloc and gives back with _aligned_free there, as asio's and document's allocation tests do.
@pinhopro
pinhopro merged commit 6f7c58a into main Nov 15, 2026
66 checks passed
@pinhopro
pinhopro deleted the handshake-limit branch November 17, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant