Repository navigation
beast: bound what a WebSocket client's handshake reads - #2
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withhttp::error::header_limitorbody_limitafter 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