#893·libhv

WebSocket: a control frame between fragments of a data message corrupts that message

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

A PING (or PONG/CLOSE) frame that arrives between the fragments of a fragmented TEXT/BINARY message is merged into the unfinished data message, and the message's opcode is replaced by the control opcode. The application never receives the data message: the server (or client) instead replies with a PONG that echoes the partial data plus the ping payload, and then echoes the remaining fragments as a second PONG. RFC 6455 explicitly allows control frames in the middle of a fragmented message, so this is triggered by conforming peers, not only by malicious ones.

Inspected and reproduced revision: 62f1292b5735a5750aaa88d0ee70596b5ac73832 (ithewei/libhv, committed 2026-09-26). The same parser is used by the server (HttpHandler::SwitchWebSocket()) and by WebSocketClient.

Details

on_frame_header() stores the opcode of every non-continuation frame into the single per-connection wp->opcode, and only clears the shared wp->message buffer when the previous frame had FIN set:

c
int opcode = parser->flags & WS_OP_MASK;
if (opcode != WS_OP_CONTINUE) {
    wp->opcode = opcode;
}
...
if (wp->state == WS_FRAME_BEGIN ||
    wp->state == WS_FRAME_FIN) {
    wp->message.clear();
}

on_frame_body() appends every frame's payload to that same buffer, and on_frame_end() delivers (wp->opcode, wp->message) whenever a frame has FIN set. There is no separate handling for control frames (opcode & 0x8), so:

  1. TEXT, FIN=0, "Hel" — opcode=TEXT, message = "Hel", state WS_FRAME_END.
  2. PING, FIN=1, "x" — wp->opcode becomes PING; the buffer is not cleared (previous frame had no FIN), message = "Helx"; FIN → onMessage(PING, "Helx").
  3. CONT, FIN=1, "lo" — opcode stays PING; buffer cleared (previous frame had FIN); FIN → onMessage(PING, "lo").

The server dispatcher in HttpHandler::SwitchWebSocket() (and the equivalent lambda in WebSocketClient) answers each PING with send(msg, WS_OPCODE_PONG), so both pieces are echoed back as PONGs and onmessage is never called for "Hello".

Spec: RFC 6455 §5.4: "Control frames MAY be injected in the middle of a fragmented message" and "An endpoint MUST be capable of handling control frames in the middle of a fragmented message." §5.5: control frames themselves MUST NOT be fragmented.

Related consequences of the same single opcode/buffer state (same root cause):

  • A CONTINUATION frame received when no fragmented message is open is not rejected (§5.4 requires a continuation to follow an unfinished data frame). If it is the first frame on the connection, it is delivered with the constructor's default opcode WS_OP_CLOSE, so the peer's bytes are echoed back as a CLOSE and the connection is closed. After a completed TEXT message it is delivered to the application as another TEXT message.
  • A fragmented PING (PING FIN=0 + CONT FIN=1) is accepted and reassembled.

Trigger (client → libhv server, masked as a browser would send)

01 83 11 22 33 44 59 47 5f            TEXT  FIN=0 "Hel"
89 81 11 22 33 44 69                  PING  FIN=1 "x"
80 82 11 22 33 44 7d 4d               CONT  FIN=1 "lo"

Actual (harness, unmodified websocket_parser.c + WebSocketParser.cpp, ASan/UBSan, no sanitizer reports):

onMessage(opcode=9, len=4, "Helx")   -> server sends PONG "Helx"
onMessage(opcode=9, len=2, "lo")     -> server sends PONG "lo"

Expected: onMessage(PING, "x") (answered with PONG "x") followed by onMessage(TEXT, "Hello").

Harness and output: issues/verification/libhv_websocket/ (ws_frames.cpp, build_and_run.sh, result.txt). This is a parser-level harness, not a TCP end-to-end test; the dispatch labels in the output restate the switch in HttpHandler::SwitchWebSocket().

Impact

Data-integrity / interoperability bug: fragmented messages from peers that send keep-alive PINGs while streaming (allowed by the RFC) are silently lost and their contents are reflected back to the sender inside PONG frames. No memory-safety impact was observed. A malicious peer gains nothing beyond what it could already send; the impact is on correctness for conforming peers (for example, a server that pings a libhv WebSocketClient during a large fragmented message).

Suggested fix

Keep control frames out of the data-message state: in on_frame_header(), if opcode & 0x8, require FIN and length <= 125, and collect the payload into a separate buffer/opcode that is delivered and cleared at that frame's end without touching wp->opcode/wp->message. Reject a CONTINUATION when no fragmented message is open, and a new TEXT/BINARY while one is open. Add tests for TEXT(FIN=0) PING CONT(FIN=1).