Files
FFF_vtt_frozen/docs/research/m3-test-tools.md
T
2026-11-10 16:52:29 +01:00

115 lines
11 KiB
Markdown

# M3: test harness WebSocket options (#8 "Test tools check")
Date: 2026-11-10. Scope: facts and tradeoffs only; no decision, no repo code changed. Part of #2 (map). Question: what can the test harnesses use to speak WebSocket with control of Cookie + Origin headers, raw oversized frames, and floods, inside devbox.
Pins used below: treeform/ws `c9c0c7b` (v0.6.0), guzba/whisky `494fb10`, status-im/nim-websock `703f8f4` (v0.5.0), guzba/mummy `01deb1f` (repo pin). Local Nim 2.2.4 (system), devbox lock nim 2.2.12.
## 1. Summary
1. Current prototype WS harnesses are plain Node `.mjs` using only Node built-ins plus the global `WebSocket`. No npm `ws` package, no `package.json` for tools. 302 lines of WS-touching code.
2. Node global `WebSocket` accepts a non-standard `{headers}` 2nd arg. Verified locally: `Cookie` and `Origin` both reach the wire (node v24.22.1). Cannot send raw/malformed frames.
3. Main repo `devbox.json` has no node. Prototype `devbox.json` uses `nodejs: latest`, which the lock resolves to nodejs_26 26.11.1. `lib.mjs` comment says "Node 22" (stale vs lock). Node 26 not tested.
4. Nim 2.2.4 stdlib has no WebSocket module. `httpclient.getSocket` exists, so a hand-rolled handshake is possible.
5. treeform/ws (Nim 2.x compatible, active): no header injection API; `WebSocket.tcpSocket*` is exported, so raw bytes can be written after handshake. Type-checks on Nim 2.2.4.
6. nim-websock (active): header control via `Hook` (Cookie/Origin possible); 20 MB frame cap; but internal write procs are not exported (no raw frames) and it needs nim >= 2.2.14, above both pinned Nim versions.
7. whisky: archived, blocking, no header API. Not a fit for floods or header control.
8. Raw `node:net` gives full control (any bytes, any header) with zero deps. Nim rewrite = hand-rolled handshake + frame codec (roughly 150-250 lines, estimate).
9. Server side: mummy default `maxMessageLen` = 64 KB (server.nim does not override), so oversized-frame tests have a concrete server limit to probe.
## 2. Prototype harnesses (a)
| file | lines | WS? | deps |
|---|---|---|---|
| tools/leak.mjs | 65 | yes (via lib.mjs) | node:fs, lib.mjs |
| tools/lib.mjs | 17 | yes: `new WebSocket(url, {headers:{Cookie}})` | node:fs; global fetch/WebSocket |
| tools/smoke.mjs | 53 | yes: `{headers: {Cookie}}`, bad/absent cookie checks | node:fs |
| tools/restart.mjs | 85 | yes (via lib.mjs) | node:fs, node:child_process |
| tools/concurrency.mjs | 82 | yes (via lib.mjs) | lib.mjs |
| tools/genmap.mjs | 106 | no (map gen) | node:fs, node:zlib |
| tools/leakcheck.nim | 154 | no: replays JSONL transcript | std/[json,tables,sets,strutils,os,math], geometry |
| tools/faulttest.sh | 36 | no: shell; runs leak.mjs under withserver.sh | sh, nim, node |
| tools/withserver.sh / leaktest.sh | 17 / 11 | no: shell orchestration | sh, nginx |
- lib.mjs comment: "Node 22 WebSocket (undici) accepts {headers}" [L1].
- Only `package.json` in prototype is `client/package.json` (pixi.js, vite, typescript, @types/node). Tools do not import it.
- `ws` npm package: not used and not evaluated.
Sources: [P1] `/home/davide/Projects/GDR/FFF_vtt/prototype-server-vision/tools/lib.mjs`; [P2] `.../tools/smoke.mjs` L9; [P3] `.../tools/leak.mjs`, `.../tools/leakcheck.nim` (under `prototype-server-vision/tools/`); [P4] `prototype-server-vision/devbox.json`; [P5] `prototype-server-vision/client/package.json`.
## 3. Node availability and deps (b)
- Main `devbox.json` packages: nim 2, nimble, coreutils, gnused, diffutils, gnugrep. No node, no nginx. [M1] `/home/davide/Projects/GDR/FFF_vtt/devbox.json`
- Prototype `devbox.json`: `nodejs: "latest"`, `nginx`, `sqlite`. [P4]
- Prototype `devbox.lock` L300-303: `nodejs@latest` resolved to `nixpkgs 8edc0c7#nodejs_26`, version 26.11.1. [P6] `prototype-server-vision/devbox.lock`
- System node (outside devbox): v24.22.1, `/run/current-system/sw/bin/node`. Not the devbox version.
- Node `WebSocket` header option: tested by capturing raw upgrade on a local `net` server. Wire request contained `Cookie: sid=abc` and `Origin: https://evil.example`. Test script: `<scratchpad>/nodetest/hdr.mjs`. Node docs for the `headers` option were not checked; behavior is observed, not documented.
- Browser-standard WebSocket has no headers arg; this path depends on Node/undici.
- Deps reproducibility: tools use only built-ins, so no `npm install` step. Pin comes from devbox.lock.
## 4. Nim WebSocket client options (c)
Nim 2.2.4 stdlib: no WebSocket module (grep of `lib/pure` for "websocket" is empty). `httpclient`/`asynchttpclient` expose `getSocket` [S-httpclient]. treeform/ws uses that exact trick for the handshake [T-ws-connect].
| lib | commit / date | async | header control (Cookie/Origin) | raw/malformed frames | oversize | Nim 2.2.4 |
|---|---|---|---|---|---|---|
| treeform/ws | c9c0c7b, v0.6.0, pushed 2026-05-25, MIT | async only (asyncdispatch) | no: `newWebSocket(url, protocols)`; fixed headers | `tcpSocket*` exported, so raw writes possible after handshake; `send` always masks on client, no rsv/len override | no client-side cap; `receivePacket` accumulates fragments unbounded; 64-bit len reads only bytes 4-7 (ok < 4 GB) | `nim check` rc=0 (warnings only) |
| status-im/nim-websock | 703f8f4, v0.5.0, pushed 2026-10-29, Apache/MIT | chronos async | yes: `connect(..., hooks)`; `Hook.append(headers var HttpTable)` | no: `writeMessage`/`writeControl` not exported | `WSMaxMessageSize` 20 MB hard cap (frame.nim); `frameSize` 1 MB chunking | not compatible: `requires nim >= 2.2.14`, plus bearssl, chronicles, chronos 4.4-4.6, httputils, nimcrypto, results, stew, zlib |
| guzba/whisky | 494fb10, archived per GitHub API | blocking only | no: `newWebSocket(url)` | not reviewed beyond framing | no cap seen in `receiveFrame` | `nim check` rc=0 |
| niv/websocket.nim | last push 2022-12-04, license NOASSERTION | - | - | - | - | not evaluated (stale; treeform derives from it) |
| mummy | 01deb1f (repo pin) | server only | - | - | - | n/a (server, not client) |
Key source facts:
- treeform/ws: `newWebSocket` builds headers only from WS fields plus optional `Sec-WebSocket-Protocol` [T-ws-connect]. `WebSocket` object has `tcpSocket*`, `masked*` fields [T-ws-type]. Receive path rejects rsv bits and mask mismatch [T-ws-recv].
- nim-websock: `connect` takes `hooks: seq[Hook]`, calls `hp.append(hp, headers)` before the upgrade request [NW-connect]. `frame.nim` L200 raises above `WSMaxMessageSize` (20 MB) [NW-frame]. `types.nim` L26-27 defines `WSDefaultFrameSize` (1 MB) and `WSMaxMessageSize` (20 MB) [NW-types].
- Nim version drift: devbox pins nim 2.2.12; system nim is 2.2.4. nim-websock would need >= 2.2.14. Upgrading the pin is a separate decision.
- Hand-rolled Nim client: handshake via stdlib `asyncnet`/`httpclient` (or raw `asyncnet` sending the upgrade request, which gives full header control) plus frame encode/decode. Size is an estimate: 150-250 lines.
## 5. Node raw sockets vs global WebSocket (what each can do)
| need | global WebSocket (Node 24 tested) | `node:net` + hand-rolled frames | npm `ws` |
|---|---|---|---|
| Cookie + Origin | yes (`{headers}`, observed) | yes (raw handshake) | not evaluated |
| floods | yes (send loop; existing concurrency.mjs does it) | yes | not evaluated |
| oversized frame (>64 KB) | sends big strings; fragments per undici | yes (set any length) | not evaluated |
| malformed frames (rsv, unmasked, bad len, bad opcode) | no | yes | not evaluated |
| extra deps | none | none | would need lock file |
## 6. Effort: port .mjs vs rewrite in Nim (d)
Rough estimates, not measured.
- Port as-is to main repo: add `nodejs` to root `devbox.json` (one package line, lock regen). Copy `tools/lib.mjs` + the 4 WS scripts (~300 lines). Still need the prototype's run layout (`run/invites.txt`, nginx conf via `withserver.sh`, ports 18088/19000). Low effort, but the harnesses are tied to the prototype's layout and env.
- Rewrite in Nim: needs a WS client. Options: (i) hand-rolled client on stdlib (~150-250 lines, full control, no dep); (ii) treeform/ws + patch for headers (header patch on a fork, plus raw frames via `tcpSocket`); (iii) nim-websock (hooks and cap ok, but requires Nim upgrade). Floods and async fine in all Nim options.
- Hybrid: keep Node for the scenario drivers, Nim for the checker (leakcheck.nim already Nim, reads transcript, no WS). Adds no WS dep to Nim.
## 7. Open facts not yet verified
- Node 26 (devbox pin) header behavior: not tested.
- Node `WebSocket` headers option in official docs: not checked.
- treeform/ws and whisky runtime behavior: only `nim check`, not run against a server.
- niv/websocket.nim: not evaluated.
- npm `ws` package: not evaluated.
## Sources
Pinned upstream (commit links):
- [T-ws-readme] https://github.com/treeform/ws/blob/c9c0c7b51e2e83cc9305a895a0b144a297b4bea8/README.md
- [T-ws-connect] https://github.com/treeform/ws/blob/c9c0c7b51e2e83cc9305a895a0b144a297b4bea8/src/ws.nim (L72-185 handshake and newWebSocket)
- [T-ws-type] same file, `WebSocket* = ref object` (tcpSocket*, masked*) in the type block near the top
- [T-ws-recv] same file, recvFrame L321-400; receivePacket L402-413
- [T-ws-send] same file, send L291-320 (1 MB chunked writes)
- [NW-connect] https://github.com/status-im/nim-websock/blob/703f8f40c129699b703d7ef72f6897c05ab50f44/websock/websock.nim (connect L90-160, hooks L140-150)
- [NW-frame] https://github.com/status-im/nim-websock/blob/703f8f40c129699b703d7ef72f6897c05ab50f44/websock/frame.nim (L190-215)
- [NW-types] https://github.com/status-im/nim-websock/blob/703f8f40c129699b703d7ef72f6897c05ab50f44/websock/types.nim (L24-27, L95, hook type ~L147)
- [NW-session] https://github.com/status-im/nim-websock/blob/703f8f40c129699b703d7ef72f6897c05ab50f44/websock/session.nim (writeMessage L32, writeControl L64 not exported)
- [NW-nimble] https://github.com/status-im/nim-websock/blob/703f8f40c129699b703d7ef72f6897c05ab50f44/websock.nimble (`nim >= 2.2.14`)
- [WH-src] https://github.com/guzba/whisky/blob/494fb1046f5a563defc87ac36b8c62846ee7ea2c/src/whisky.nim (newWebSocket L156, receiveFrame L28)
- [WH-readme] https://github.com/guzba/whisky/blob/494fb1046f5a563defc87ac36b8c62846ee7ea2c/README.md
- [MUM-default] https://github.com/guzba/mummy/blob/01deb1ff694988635c7bf0f24f6088a0bdf6db5d/mummy.nim (newServer defaults L1476-1481: maxMessageLen = 64 KB; enforced L658)
- [SRV-newserver] local `/home/davide/Projects/GDR/FFF_vtt/prototype-server-vision/src/server.nim` L363 (`newServer(handler, wsHandler, workerThreads = 8)`, no maxMessageLen)
- [S-httpclient] local `/nix/store/3y9syv0p41rdaqzddashmdl475l0rbv0-nim-unwrapped-2.2.4/nim/lib/pure/httpclient.nim` L711-724 (getSocket)
- [P1..P6] local files cited in sections 2-3.
- [M1] `/home/davide/Projects/GDR/FFF_vtt/devbox.json`
- Node header test: `<scratchpad>/nodetest/hdr.mjs` (scratch, not committed).
- nimble index (names/URLs only): `/home/davide/Projects/GDR/FFF_vtt/prototype-server-vision/.devbox/nimble/packages_official.json`
- GitHub API metadata: `gh api repos/treeform/ws`, `repos/guzba/whisky`, `repos/status-im/nim-websock`, `repos/niv/websocket.nim` (pushed dates, archived flag).