#896·libhv

WebSocket parser accepts unmasked client frames, RSV bits, reserved opcodes and oversized control frames

Author: K-ANOYCreated Sep 30, 2026Updated Sep 30, 2026

libhv's WebSocket receive path performs no RFC 6455 frame-header validation. A libhv server accepts unmasked frames from clients, frames with RSV1-3 set (no extension is negotiated), reserved opcodes, and control frames longer than 125 bytes; a PING with a large payload is echoed back as a PONG of the same size (fragmented into several PONG frames once it exceeds 65535 bytes).

Details

websocket_parser_execute() keeps only the opcode and FIN from the first header byte, so RSV1-3 are discarded without being checked, and it records the MASK bit without any role check:

c
parser->flags       = (websocket_flags) (CC & WS_OP_MASK);   // RSV bits dropped
if(CC & (1<<7)) {
    parser->flags |= WS_FIN;
}
...
if(CC & 0x80) {
    parser->flags |= WS_HAS_MASK;                            // absent mask accepted
}

on_frame_header() accepts every opcode and payload length (the length is only used as a capacity hint), and on_frame_body() unmasks only when the MASK bit happens to be set. HttpHandler::SwitchWebSocket() then echoes PING payloads of any length with send(msg, WS_OPCODE_PONG); WebSocketChannel::send() splits payloads above 65535 bytes into a PONG with FIN=0 plus CONTINUATION frames, i.e. libhv itself emits a fragmented control frame. Frames with reserved opcodes fall into the default: break; branch and are silently ignored instead of failing the connection.

Requirements (RFC 6455):

  • §5.1: "a client MUST mask all frames that it sends to the server"; "The server MUST close the connection upon receiving a frame that is not masked." A client MUST close the connection if it detects a masked frame from the server.
  • §5.2: RSV1-3 "MUST be 0 unless an extension is negotiated … If a nonzero value is received and none of the negotiated extensions defines the meaning of such a nonzero value, the receiving endpoint MUST Fail the WebSocket Connection." Reserved opcodes likewise MUST fail the connection.
  • §5.5: "All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented."

Trigger / observed result

Parser-level harness with the unmodified http/websocket_parser.c and http/WebSocketParser.cpp (ASan/UBSan; no sanitizer reports). Each input was fully consumed and delivered:

Input (client → server) Observed Expected
81 02 68 69 — TEXT "hi", unmasked onMessage(TEXT,"hi") → application onmessage fail connection (§5.1)
f1 82 … — TEXT "hi", RSV1-3 = 1 onMessage(TEXT,"hi") → application fail connection (§5.2)
83 82 … — opcode 3 onMessage(3,"zz"), silently ignored fail connection (§5.2)
89 fe 00 c8 … — PING, 200-byte payload onMessage(PING, 200 bytes) → echoed as a 200-byte PONG fail connection (§5.5)
09 82 … 80 82 … — fragmented PING reassembled onMessage(PING,"abcd") → PONG fail connection (§5.5)

Harness and output: issues/verification/libhv_websocket/ (ws_frames.cpp, build_and_run.sh, result.txt).

Impact

Protocol-conformance defect. The practical consequences are limited: a non-browser client can skip masking (masking exists to protect intermediaries from cache-poisoning attacks, §10.3), frames that a future extension would mark with RSV bits are misinterpreted as plain data, and a peer can make the server reflect up to message-size PONG payloads (a reflection/amplification-free echo of the peer's own data). No memory-safety issue was found in these paths. I did not assess any application-level consequence.

Related (found during manual review, not by the tool): there is no upper bound on the reassembled message size; MAX_PAYLOAD_LENGTH (16 MiB) only caps the reserve() hint in on_frame_header(), while on_frame_body() appends without limit, so a peer can grow wp->message for as long as it keeps sending one frame with a 64-bit length.

Suggested fix

In websocket_parser_execute(), preserve RSV bits in flags (or reject nonzero RSV there). In on_frame_header() (which can return nonzero to stop the parser, making FeedRecvData() return a short count that both callers already treat as an error): reject reserved opcodes; for control opcodes require FIN and length <= 125; and enforce the mask direction, e.g. by giving WebSocketParser a role flag set by HttpHandler (server: require MASK) and WebSocketClient (client: require no MASK). Consider a configurable maximum message size.