WebsocketServer: cap message size, avoid UB on empty-vector payloads

ReadFrame() accepted a 64-bit client-supplied payload length with only
a top-bit check, and ReadPending() immediately resized a buffer by it
before any data had arrived - a single frame claiming a huge length
(reachable via the WebSocket debugger endpoint) could trigger a
multi-exabyte allocation attempt. Now rejected up front (both the
single frame and the fragmented-message total) against a 64MB cap.

Also replaced &payload[0]/&vector[0] with .data() in the send/receive
paths - operator[] on a possibly-empty vector (e.g. an empty PING) is
UB even when the result is never dereferenced.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L4QAoxV2KY7ek4PcZw3WvY
This commit is contained in:
Henrik RydgårdandClaude Sonnet 5 committed 2026-08-10 11:26:24 +02:00
1 parent 6ee6641fe5
commit 37cea65e0e
1 file changed
+16 -5
+16 -5
View File
@@ -15,6 +15,10 @@
static const char *const WEBSOCKET_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
// Sanity cap on a single message's total size, so a client can't crash us by simply
// claiming an enormous frame length before sending any actual data.
static constexpr uint64_t MAX_WS_MESSAGE_SIZE = 64 * 1024 * 1024;
namespace net {
enum class Opcode {
@@ -125,7 +129,7 @@ void WebSocketServer::Send(const std::vector<uint8_t> &payload) {
_assert_(open_);
_assert_(fragmentOpcode_ == -1);
SendHeader(true, (int)Opcode::BINARY, payload.size());
SendBytes((const char *)&payload[0], payload.size());
SendBytes((const char *)payload.data(), payload.size());
}
void WebSocketServer::AddFragment(bool finish, const std::string &str) {
@@ -154,7 +158,7 @@ void WebSocketServer::AddFragment(bool finish, const std::vector<uint8_t> &paylo
} else {
_assert_(fragmentOpcode_ == (int)Opcode::BINARY || fragmentOpcode_ == -1);
}
SendBytes((const char *)&payload[0], payload.size());
SendBytes((const char *)payload.data(), payload.size());
if (finish) {
fragmentOpcode_ = -1;
}
@@ -164,14 +168,14 @@ void WebSocketServer::Ping(const std::vector<uint8_t> &payload) {
_assert_(open_);
_assert_(payload.size() <= 125);
SendHeader(true, (int)Opcode::PING, payload.size());
SendBytes((const char *)&payload[0], payload.size());
SendBytes((const char *)payload.data(), payload.size());
}
void WebSocketServer::Pong(const std::vector<uint8_t> &payload) {
_assert_(open_);
_assert_(payload.size() <= 125);
SendHeader(true, (int)Opcode::PONG, payload.size());
SendBytes((const char *)&payload[0], payload.size());
SendBytes((const char *)payload.data(), payload.size());
}
void WebSocketServer::Close(WebSocketClose reason) {
@@ -340,6 +344,13 @@ bool WebSocketServer::ReadFrame() {
}
}
// Reject before ReadPending() turns this into a resize() - a client can claim any
// length up front, long before actually sending that much data.
if (sz > MAX_WS_MESSAGE_SIZE || pendingBuf_.size() + sz > MAX_WS_MESSAGE_SIZE) {
Close(WebSocketClose::MESSAGE_TOO_LONG);
return false;
}
if (opcode >= (int)Opcode::CONTROL_MIN) {
// It's safe to overwrite this since we can be between fragmented frames, but not inside a frame.
memcpy(pendingMask_, mask, sizeof(pendingMask_));
@@ -412,7 +423,7 @@ bool WebSocketServer::ReadControlFrame(int opcode, size_t sz) {
std::vector<uint8_t> payload;
payload.resize(sz);
// Just block here to read the payload.
if (!in_->TakeExact((char *)&payload[0], sz)) {
if (!in_->TakeExact((char *)payload.data(), sz)) {
// TODO: Failing on too slow trickle timeout for now.
Close(WebSocketClose::POLICY_VIOLATION);
return false;