The live-sync socket never forgets a closed client, accepts unmasked and unbounded frames, and answers a wrong message with silence #1313

Closed
opened 2026-11-10 01:43:35 +00:00 by jaylouisw · 0 comments
Owner

The only thing any test asserted about server/ws.js was that a broadcast carries verdicts (#1306). Nobody had
ever sent it a frame it did not expect. Four things came out, and the first one is the reason the obvious fix would
not have worked.

1. clients only ever grew — and close is never the moment

client.onClose was never wired and nothing removed a client from the set. Every browser tab that had ever
connected stayed in it: broadcast() wrote to dead sockets for the life of the process and count never came down.

The trap is that deleting on socket.on('close') does not fix it. An upgraded socket has
allowHalfOpen = true, so when the peer goes away it emits end and stops there — close never fires. Measured:

server socket: allowHalfOpen = true
destroying client; count = 1
  server socket event: end
after destroy: count = 1

So end and error have to count as gone as well, or the fix is decorative.

2. An unmasked client frame was accepted

RFC 6455 §5.1 makes masking mandatory for client→server frames. It was accepted, so any plain TCP writer could
speak the protocol. A browser always masks, so refusing costs nothing.

3. There was no frame-size ceiling

parseFrame() waits for the length the client declared and Buffer.concat() grows until it arrives. One
connection could ask the process to hold as much memory as it liked. An 8 MiB text frame was accepted in full.

Related: the 64-bit length form read readUInt32BE(6) and ignored the high word, so a declared length over 4 GiB
became a small frame and the parser then waited for bytes that were never coming.

4. A wrong message was a silence

subscribe {topologyId:"nope"}  -> nothing
{type:"nonsense"}              -> nothing

A client waiting for a board it misspelled could not tell that from an empty board. This is the same rule the HTTP
surface got in #1309: every message gets an answer.

What must be true instead

  • a client that has gone is gone — end, error and close all remove it, and a broadcast reaches only the living;
  • an unmasked frame, or a 64-bit length with a non-zero high word, closes with 1002; a frame over 1 MiB closes with
    1009 — the code says which rule broke;
  • every message gets an answer: E_NO_TOPOLOGY (naming the id asked for), E_UNKNOWN_TYPE (naming the one that
    exists), E_BAD_MESSAGE — and none of them closes the connection;
  • none of the above takes the server down, and a handler that throws does not either.
The only thing any test asserted about `server/ws.js` was that a broadcast carries `verdicts` (#1306). Nobody had ever sent it a frame it did not expect. Four things came out, and the first one is the reason the obvious fix would not have worked. ## 1. `clients` only ever grew — and `close` is never the moment `client.onClose` was never wired and nothing removed a client from the set. Every browser tab that had ever connected stayed in it: `broadcast()` wrote to dead sockets for the life of the process and `count` never came down. The trap is that deleting on `socket.on('close')` **does not fix it**. An upgraded socket has `allowHalfOpen = true`, so when the peer goes away it emits `end` and stops there — `close` never fires. Measured: ``` server socket: allowHalfOpen = true destroying client; count = 1 server socket event: end after destroy: count = 1 ``` So `end` and `error` have to count as gone as well, or the fix is decorative. ## 2. An unmasked client frame was accepted RFC 6455 §5.1 makes masking mandatory for client→server frames. It was accepted, so any plain TCP writer could speak the protocol. A browser always masks, so refusing costs nothing. ## 3. There was no frame-size ceiling `parseFrame()` waits for the length the client declared and `Buffer.concat()` grows until it arrives. One connection could ask the process to hold as much memory as it liked. An 8 MiB text frame was accepted in full. Related: the 64-bit length form read `readUInt32BE(6)` and ignored the high word, so a declared length over 4 GiB became a small frame and the parser then waited for bytes that were never coming. ## 4. A wrong message was a silence ``` subscribe {topologyId:"nope"} -> nothing {type:"nonsense"} -> nothing ``` A client waiting for a board it misspelled could not tell that from an empty board. This is the same rule the HTTP surface got in #1309: every message gets an answer. ## What must be true instead * a client that has gone is gone — `end`, `error` and `close` all remove it, and a broadcast reaches only the living; * an unmasked frame, or a 64-bit length with a non-zero high word, closes with `1002`; a frame over 1 MiB closes with `1009` — the code says which rule broke; * every message gets an answer: `E_NO_TOPOLOGY` (naming the id asked for), `E_UNKNOWN_TYPE` (naming the one that exists), `E_BAD_MESSAGE` — and none of them closes the connection; * none of the above takes the server down, and a handler that throws does not either.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge#1313
No description provided.