WebSocket receiveFrame accepts malformed frames after a normal HTTP Upgrade
Summary
WebSocket::receiveFrame() returns payloads from several frames that RFC 6455 requires a WebSocket endpoint to reject: an unmasked client frame, a masked server frame, a reserved opcode, an RSV1 bit without a negotiated extension, a fragmented PING, and a PING with a 126-byte payload. These cases were reproduced through real loopback HTTP Upgrade/TCP connections using the unmodified Poco Net library. On the server-side tests, a subsequent valid TEXT frame was also read on the same connection. The demonstrated impact is protocol noncompliance, not a crash, memory-safety issue, or access-control bypass.
Details
Files: Net/src/WebSocketImpl.cpp, Net/src/WebSocket.cpp, and Net/include/Poco/Net/WebSocket.h. In WebSocketImpl::peekHeader(), the first header byte is stored as frameFlags, and the mask bit controls payload unmasking. The parser checks the general _maxPayloadSize, but does not validate the opcode, RSV bits, FIN on control frames, the 125-byte control-frame limit, or masking direction. WebSocket::receiveFrame() returns the payload length and saved flags directly. The server/client role is known through _mustMaskPayload, but that field is used for sending and is not checked when receiving these frames.
The receiveFrame() documentation says that a WebSocketException is thrown for a malformed frame. RFC 6455 requires a server to reject unmasked client frames and a client to reject masked server frames (§5.1), requires an endpoint to fail on reserved opcodes or nonzero RSV bits without an applicable extension (§5.2), and requires control frames to be unfragmented and at most 125 bytes (§5.5).
Reproduction
From the ProtoHound project root, build the unmodified Poco checkout and run the loopback harness with its driver:
cmake -S repo/poco -B /tmp/protohound-poco-build -G Ninja -DPOCO_MINIMAL_BUILD=ON -DENABLE_NET=ON -DENABLE_TESTS=OFF -DENABLE_SAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug
cmake --build /tmp/protohound-poco-build --target Net -j 4
g++ -std=c++20 -Irepo/poco/Net/include -Irepo/poco/Foundation/include issues/verification/poco_websocket_frames.cpp -L/tmp/protohound-poco-build/lib -Wl,-rpath,/tmp/protohound-poco-build/lib -lPocoNetd -lPocoFoundationd -pthread -o /tmp/protohound-poco-websocket-frames
python3 issues/verification/poco_websocket_frames.py /tmp/protohound-poco-websocket-framesThe driver performs a normal HTTP Upgrade without negotiating extensions. For each server-side case, it sends one frame, waits for receiveFrame() to return, and then sends a valid masked TEXT frame containing A. The masking key is 01 02 03 04, so a masked A is encoded as 40. The observed results are:
| First frame sent to Poco server | First receiveFrame() result |
Subsequent valid TEXT |
|---|---|---|
Valid masked TEXT 81 81 01 02 03 04 40 |
1 byte, flags 0x81 |
Returned normally |
Unmasked TEXT 81 01 41 |
1 byte, flags 0x81 |
Returned normally |
Reserved opcode 3 83 81 01 02 03 04 40 |
1 byte, flags 0x83 |
Returned normally |
RSV1 without extension c1 81 01 02 03 04 40 |
1 byte, flags 0xc1 |
Returned normally |
Fragmented PING 09 81 01 02 03 04 40 |
1 byte, flags 0x09 |
Returned normally |
Oversized PING 89 fe 00 7e 01 02 03 04 followed by 126 masked A bytes |
126 bytes, flags 0x89 |
Returned normally |
For the opposite masking direction, the driver also completes an HTTP Upgrade with a Poco WebSocket client, then sends it a masked server TEXT frame 81 81 01 02 03 04 40 followed by a valid unmasked TEXT frame 81 01 41. Both calls return one byte and flags 0x81; neither throws. The first receive result is therefore not limited to a server-side path.
Expected behavior: fail the WebSocket connection on each malformed frame before returning it as an ordinary application frame. A WebSocketException is consistent with the documented API; a protocol-error Close frame with code 1002 may be sent when possible.
Impact and suggested fix
The library leaves PING/PONG/CLOSE handling to applications, and an application can implement some validation itself. However, receiveFrame() currently treats these malformed frame headers as successfully received frames despite its documented malformed-frame behavior. The test did not demonstrate memory corruption, a crash, or denial of service.
Validate RSV/opcode values, control-frame FIN and payload length, and mask direction before returning the frame. Any extension-aware RSV handling should depend on a negotiated extension. Add receive tests for both client and server roles, including the 125/126-byte control-frame boundary.
Source: pocoproject/poco