aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/servers/wsframecodec.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/zenhttp/servers/wsframecodec.cpp')
-rw-r--r--src/zenhttp/servers/wsframecodec.cpp63
1 files changed, 55 insertions, 8 deletions
diff --git a/src/zenhttp/servers/wsframecodec.cpp b/src/zenhttp/servers/wsframecodec.cpp
index e452141fe..781f04c5e 100644
--- a/src/zenhttp/servers/wsframecodec.cpp
+++ b/src/zenhttp/servers/wsframecodec.cpp
@@ -16,7 +16,7 @@ namespace zen {
//
WsFrameParseResult
-WsFrameCodec::TryParseFrame(const uint8_t* Data, size_t Size)
+WsFrameCodec::TryParseFrame(const uint8_t* Data, size_t Size, bool RequireMask)
{
// Minimum frame: 2 bytes header (unmasked server frames) or 6 bytes (masked client frames)
if (Size < 2)
@@ -24,10 +24,48 @@ WsFrameCodec::TryParseFrame(const uint8_t* Data, size_t Size)
return {};
}
- const bool Fin = (Data[0] & 0x80) != 0;
- const uint8_t OpcodeRaw = Data[0] & 0x0F;
- const bool Masked = (Data[1] & 0x80) != 0;
- uint64_t PayloadLen = Data[1] & 0x7F;
+ const bool Fin = (Data[0] & 0x80) != 0;
+ const uint8_t RsvBits = Data[0] & 0x70;
+ const uint8_t OpcodeRaw = Data[0] & 0x0F;
+ const bool Masked = (Data[1] & 0x80) != 0;
+ const uint8_t ShortLength = Data[1] & 0x7F;
+ uint64_t PayloadLen = ShortLength;
+
+ const bool IsControlFrame = (OpcodeRaw & 0x08) != 0;
+
+ // RFC 6455 section 5.2: RSV1/2/3 must be zero unless a negotiated extension
+ // defines them. We do not negotiate any extensions, so any non-zero RSV bit
+ // is a protocol violation.
+ if (RsvBits != 0)
+ {
+ WsFrameParseResult Error;
+ Error.Status = WsFrameParseStatus::kProtocolError;
+ return Error;
+ }
+
+ // RFC 6455 section 5.5: control frames (Close / Ping / Pong and any opcode
+ // in 0x8..0xF) MUST NOT be fragmented and MUST have a payload of 125 bytes
+ // or less. Rejecting fragmented or oversized control frames prevents a
+ // peer from tying up unbounded memory inside an auto-pong, and closes off
+ // a class of smuggling tricks where handlers might observe partial control
+ // payloads.
+ if (IsControlFrame && (!Fin || ShortLength > 125))
+ {
+ WsFrameParseResult Error;
+ Error.Status = WsFrameParseStatus::kProtocolError;
+ return Error;
+ }
+
+ // RFC 6455 section 5.1: a server MUST close the connection upon receiving an
+ // unmasked client frame. Signal this distinctly from "need more data" so the
+ // server close path can trigger a 1002 close rather than stalling for bytes
+ // that will never satisfy the parse.
+ if (RequireMask && !Masked)
+ {
+ WsFrameParseResult Error;
+ Error.Status = WsFrameParseStatus::kProtocolError;
+ return Error;
+ }
size_t HeaderSize = 2;
@@ -51,11 +89,19 @@ WsFrameCodec::TryParseFrame(const uint8_t* Data, size_t Size)
HeaderSize = 10;
}
- // Reject frames with unreasonable payload sizes to prevent OOM
- static constexpr uint64_t kMaxPayloadSize = 256 * 1024 * 1024; // 256 MB
+ // Reject frames with unreasonable payload sizes to bound per-connection
+ // memory. Parsers accumulate the whole frame before dispatch (see the
+ // read loops in wsasio.cpp / wshttpsys.cpp), so this cap also bounds the
+ // accumulator: a peer that advertises a large frame and streams bytes
+ // slowly cannot grow buffers past this limit. 4 MB is well above anything
+ // the monitoring / stats endpoints produce; raise it if a legitimate use
+ // case emerges.
+ static constexpr uint64_t kMaxPayloadSize = 4 * 1024 * 1024; // 4 MB
if (PayloadLen > kMaxPayloadSize)
{
- return {};
+ WsFrameParseResult Error;
+ Error.Status = WsFrameParseStatus::kProtocolError;
+ return Error;
}
const size_t MaskSize = Masked ? 4 : 0;
@@ -70,6 +116,7 @@ WsFrameCodec::TryParseFrame(const uint8_t* Data, size_t Size)
const uint8_t* PayloadData = Data + HeaderSize + MaskSize;
WsFrameParseResult Result;
+ Result.Status = WsFrameParseStatus::kValid;
Result.IsValid = true;
Result.BytesConsumed = TotalFrame;
Result.Opcode = static_cast<WebSocketOpcode>(OpcodeRaw);