A closed client is forgotten, a bad frame is refused with a code, and a wrong message is answered (#1313) #1314
No reviewers
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
jaylouisw/packetforge!1314
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/websocket-surface"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
A closed client is forgotten, a bad frame is refused with a code, and a wrong message is answered.
Fixes #1313.
The only thing any test asserted about
server/ws.jswas that a broadcast carriesverdicts(#1306). Nobody hadever sent it a frame it did not expect. Four things came out — and the first is why the obvious fix would not have
worked.
1.
clientsonly ever grew — andcloseis never the momentEvery browser tab that had ever connected stayed in the set:
broadcast()wrote to dead sockets for the life of theprocess and
countnever came down.The trap: deleting on
socket.on('close')does not fix it. An upgraded socket hasallowHalfOpen = true, sowhen the peer goes away it emits
endand stops there —closenever fires. Measured:2. An unmasked client frame was accepted
RFC 6455 §5.1 makes masking mandatory client→server; a browser always masks, so refusing costs nothing and a plain
TCP writer can no longer speak the protocol.
3. No frame-size ceiling
parseFrame()waits for the declared length andBuffer.concat()grows until it arrives — one connection could askthe process to hold what it liked (8 MiB was accepted in full). The 64-bit length form also read only
readUInt32BE(6), so a length over 4 GiB became a small frame and the parser waited for bytes that were nevercoming.
4. A wrong message was a silence
subscribe{topologyId:"nope"}and{type:"nonsense"}both vanished — indistinguishable from an empty board. Samerule the HTTP surface got in #1309.
Fixes
forget()onend/error/close; unmasked or absurd 64-bit length → close 1002, over 1 MiB → close1009 (the code names the broken rule); every message answered —
E_NO_TOPOLOGYnaming the id asked for,E_UNKNOWN_TYPEnaming the one that exists,E_BAD_MESSAGE— and none of those closes the connection; a throwingonMessageis caught, since it ran inside a socketdatalistener where a throw is an uncaught exception thattakes every other client with it; the connect frame is only sent when the board exists.
Evidence
test/websocket-surface.test.js— 21 checks: half a unit test ofWebSocketServerover a real socket pair(count comes down when a client leaves, a close frame empties the set, a broadcast after a departure throws nothing
and still arrives), half against the running server (a new tab is given the board with its verdicts; five kinds of
message each get their answer and the connection survives; unmasked → 1002; over the ceiling → 1009; absurd 64-bit
length → 1002; ping → pong; and the server is still answering HTTP afterwards).
With
server/ws.jsandserver/index.jsreverted, 9 of the 21 fail — count stuck at 2 after a client leaves,silence for an unknown board and an unknown type, and no close code for any of the three bad frames.
docs/ARCHITECTURE.md: "The live-sync socket" — the answer table, the close codes, and whyendis the moment.Suite: 172 suites, 6,918 assertions, 0 failed.