Skip to content

Cap each WebSocket action frame at one legal action - #39

Merged
JacobStephens2 merged 5 commits into
masterfrom
issue-38
Nov 29, 2026
Merged

JacobStephens2 merged 5 commits into
masterfrom
issue-38

Conversation

@JacobStephens2

@JacobStephens2 JacobStephens2 commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

upgrade /ws
  authenticate seat
  WebSocketServer maxPayload = one compact action (181 bytes)
    message
      parse action
      rooms.action
      broadcast snapshot
    frame larger than maxPayload
      ws closes 1009
      message handler does not run
    malformed frame (bad UTF-8, declared length over 2^53 - 1, ...)
      ws closes that socket (1007 / 1009)
      error listener logs it, never throws
      server keeps serving every other room

A seated player's socket accepted frames up to the ws 8.21.1 default of 100 MiB and parsed them in the message handler. The socket code moved from packages/server/src/index.ts to packages/server/src/sockets.ts (attachGameSockets), which sets maxPayload to the compact JSON size of the longest action the engine can mint: PLAY_OPEN with two follow-<safe integer> instance ids and a safe-integer sequence (181 bytes).

Review then caught that the socket error listener rethrew every error except the maxPayload one. ws starts closing a socket before it emits error, so throwing skipped the close cleanup and killed the process: one invalid-UTF-8 frame (WS_ERR_INVALID_UTF8) or a 14-byte header declaring a length over 2^53 - 1 (WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH) from one seat took down every room. The listener now never throws: both length closes stay quiet, other errors are logged with the room code, and ws finishes closing only the offending socket.

The cap is now guarded in tests instead of only measured once: every GameAction variant at its largest legal size must fit the cap, checked exhaustively against GameActionSchema.options, so a new or longer variant fails CI rather than 1009-disconnecting legal play.

Evidence

Execution evidence

  • Before: new WebSocketServer({ noServer: true }) left maxPayload at 104857600 (installed ws default, websocket-server.js). After the first commit, reviewers' failing cases, run as written: cap mutated to 100 bytes still gave Tests 2 passed (test could not detect a cap too small for legal PLAY_OPEN); an invalid-UTF-8 text frame gave Uncaught Exception ... WS_ERR_INVALID_UTF8, Errors 1 error; a crafted 2^64 - 1 declared-length header gave Uncaught Exception RangeError ... WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH, Errors 1 error.
    After: npx vitest run packages/server/tests/action-frame.test.ts - 6 passed, no errors: legal CONFIRM_HAND applies (sequence 0 → 1), a frame at exactly 181 bytes applies, a frame at 182 bytes closes 1009 with the sequence still 0, invalid UTF-8 closes only that socket with 1007, the crafted over-length header closes 1009 and a second connection to the same server still gets its snapshot, and all 14 variants at maximal size fit the cap. The same cap mutation to 100 bytes now gives Tests 1 failed | 5 passed. npx vitest run packages/server/tests/ - 29 passed. npm run typecheck passed for all workspaces; eslint and prettier --check clean on both changed files. Full npm test on this loaded machine (load average 6.2 on 4 cores): 122 passed, 1 failed - packages/engine/tests/properties.test.ts timed out at its 5s default in code this branch does not touch; rerun with --testTimeout=120000 gave 2 passed, and CI verify passed npm test on the previous head.

Visual evidence

None: this branch changes no rendered UI files (packages/server/src/index.ts, packages/server/src/sockets.ts, packages/server/tests/action-frame.test.ts only).

Merge Danger

Door: two-way

Revert either commit independently; no data migration.

Blast Radius: online seat sockets

One oversized or malformed frame now closes only that socket instead of reaching JSON.parse or crashing the process. Risk is the cap rejecting a legal action: the client sends compact JSON.stringify (packages/client/src/store.ts), the longest legal variant was computed field by field over all 14 GameActionSchema variants, and the exhaustive test above keeps that true.

Unaddressed findings

Standards

None. Both Standards findings were addressed: the throwing error listener (packages/server/src/sockets.ts) no longer throws, and the cap's duplication of engine facts (`${role}-${counter}` ids from packages/engine/src/game.ts, PLAY_OPEN longest in packages/engine/src/schemas.ts) is now guarded by the exhaustive variant test rather than moved into the engine - the engine still does not own WebSocket frame bytes.

Spec

None. Both Spec findings were addressed: the regression test now fails when the cap falls below a legal action (mutation evidence above), and the sibling length error WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH no longer takes down the process. The fix follows the Standards finding slightly further than the Spec finding's minimum: all per-socket errors are logged instead of rethrown, because ws has already started closing that socket before emitting any of them.

Security

None unaddressed. One introduced finding was fixed on this branch (9ad0ab6): the seat socket error log line in packages/server/src/sockets.ts interpolated the stored room code, which the upgrade handler uppercased but did not trim while RoomManager.authenticate trims before lookup - so a seat holder connecting with a newline padded onto their real code split the log line ([clave] seat socket error in room <code> then : Invalid WebSocket frame: invalid UTF-8 sequence on the next line, captured by the regression test's console spy before the fix). The upgrade handler now trims the code to match authenticate, and keeps a whitespace-padded room code out of the socket error log line in packages/server/tests/action-frame.test.ts passes with the exact single-line message (7 passed in that file, 30 in packages/server/tests/). A fragmentation bypass of the new cap was also probed - two sub-cap fragments reassembling over 181 bytes - and held: ws accumulates fragment lengths against maxPayload, the socket closed 1009, and the action did not apply.

Security review outcome

No unaddressed introduced findings.

Review coverage

Both axes read every changed file against refs/remotes/origin/master: packages/server/src/index.ts, packages/server/src/sockets.ts, and packages/server/tests/action-frame.test.ts. No changed file was left unread, and no generated output was excluded.

Closes #38

Built with muse Muse Code 1.4.25 (1.4.25-R9361.1) · muse-wonder-full-1.0 · max

A seated player's socket accepted frames up to the ws default of 100 MiB and parsed them in the message handler. The server now sets maxPayload to the compact JSON size of the longest action the engine can mint: PLAY_OPEN with two follow-<safe integer> instance ids and a safe-integer sequence (181 bytes).

ws closes a larger frame with 1009 before it emits message, so trailing whitespace after a CONFIRM_HAND does not apply that action. A compact CONFIRM_HAND still applies and is broadcast. The payload error is ignored so the close does not exit the process.

The socket wiring now lives in packages/server/src/sockets.ts. Tests: packages/server/tests/action-frame.test.ts.
The seat socket error listener rethrew every error except the maxPayload length error. ws starts closing a socket before it emits error, so throwing skipped close cleanup and killed the process: an invalid-UTF-8 frame (WS_ERR_INVALID_UTF8) or a header declaring a length over 2^53 - 1 (WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH) from one seat took down every room. The listener now never throws; length closes stay quiet and other errors are logged with the room code.

Tests: keep the reviewers cases - invalid UTF-8 closes only the offending socket with 1007, the crafted over-length header closes 1009 and the server keeps serving, a legal action padded to exactly the cap applies, and every GameAction variant at its largest legal size fits the cap (checked exhaustively against GameActionSchema so a new variant cannot silently outgrow it).
The upgrade handler stored the room code uppercased but untrimmed, while RoomManager.authenticate trims before lookup, so a seat holder could connect with a newline padded onto their real code. That newline then split the socket error log line added with the frame cap. Trim the code the same way at upgrade so the stored code is the canonical room code, with a regression test that connects with a padded code and checks the logged line.
Master's call work added ADJUST and COVER_CALL to GameActionSchema and gave END_DANCE a callId, so the cap test's maximal entries no longer covered every variant and its exhaustiveness assertion failed on both CI legs. Add both actions at the largest minted instance id, end the dance with call_999 instead of omitting the call, and record the call-id length beside the card-id one. PLAY_OPEN stays the longest legal frame, so the cap itself does not move.
@JacobStephens2
JacobStephens2 merged commit dc93688 into master Nov 29, 2026
6 checks passed
@JacobStephens2
JacobStephens2 deleted the issue-38 branch November 29, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cap the server's WebSocket action frames

1 participant