Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
src/lobby.security.test.ts proves non-drivers are refused, chat HTML stays text, floods collapse, and a mute survives a reconnect #134
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/124-src-lobby-security-test-ts-proves-non-dr → main 4 commits fork 1 thread · 1 unresolved
1 file
changed +154
−0
Software/so101/src/lobby.security.test.ts added +154
| @@ -0,0 +1,154 @@ | ||
| 1 | import { afterAll, afterEach, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import WebSocket from "ws"; | |
| 6 | import { Admin } from "./admin.ts"; | |
| 7 | import { Accounts } from "./accounts.ts"; | |
| 8 | import { FeetechBus } from "./bus.ts"; | |
| 9 | import { Chat } from "./chat.ts"; | |
| 10 | import { FakeBus } from "./fake-bus.ts"; | |
| 11 | import { sign } from "./identity.ts"; | |
| 12 | import { Lobby } from "./lobby.ts"; | |
| 13 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 14 | import { startServer, type RunningServer } from "./server.ts"; | |
| 15 | import { Turns } from "./turns.ts"; | |
| 16 | ||
| 17 | const KEY = "test-identity-key"; | |
| 18 | const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); | |
| 19 | const shut = (s: { close(): Promise<void> } | undefined) => (s ? Promise.race([s.close(), sleep(500)]) : undefined); | |
| 20 | const as = (id: string, name: string, role = "user") => { | |
| 21 | const identity = `${id}|${name}|${role}|${Date.now() + 3_600_000}`; | |
| 22 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 23 | }; | |
| 24 | const ALICE = as("u-alice", "alice"); | |
| 25 | const BOB = as("u-bob", "bob"); | |
| 26 | const OWNER = as("u-owner", "trevato", "admin"); | |
| 27 | ||
| 28 | type Msg = Record<string, any>; | |
| 29 | async function client(port: number, headers: Record<string, string>) { | |
| 30 | const ws = new WebSocket(`ws://127.0.0.1:${port}/live`, { headers }); | |
| 31 | const heard: Msg[] = []; | |
| 32 | const waiters: Array<{ match: (m: Msg) => boolean; resolve: (m: Msg) => void }> = []; | |
| 33 | ws.on("message", (data) => { | |
| 34 | const m = JSON.parse(String(data)) as Msg; | |
| 35 | heard.push(m); | |
| 36 | for (const w of [...waiters]) if (w.match(m)) waiters.splice(waiters.indexOf(w), 1), w.resolve(m); | |
| 37 | }); | |
| 38 | await new Promise((r, j) => (ws.once("open", r), ws.once("error", j))); | |
| 39 | const next = (match: (m: Msg) => boolean, ms = 2000) => | |
| 40 | new Promise<Msg>((resolve, reject) => { | |
| 41 | waiters.push({ match, resolve }); | |
| 42 | setTimeout(() => reject(new Error(`nothing matched in ${ms} ms; heard ${JSON.stringify(heard.slice(-5))}`)), ms); | |
| 43 | }); | |
| 44 | const first = (match: (m: Msg) => boolean) => Promise.resolve(heard.find(match) ?? next(match)); | |
| 45 | return { ws, heard, next, first, send: (m: Msg) => ws.send(JSON.stringify(m)) }; | |
| 46 | } | |
| 47 | ||
| 48 | let simulation: RunningServer; | |
| 49 | beforeAll(async () => { | |
| 50 | simulation = await startServer(new FeetechBus(new FakeBus([1, 2, 6])), { host: "127.0.0.1", port: 0, ids: [1, 2, 6], posesFile: join(mkdtempSync(join(tmpdir(), "so101-security-")), "poses.json"), watchdogMs: 60 }); | |
| 51 | }); | |
| 52 | afterAll(() => shut(simulation)); | |
| 53 | ||
| 54 | describe("lobby security", () => { | |
| 55 | let relay: RunningRelay | undefined; | |
| 56 | let lobby: Lobby | undefined; | |
| 57 | const sockets: WebSocket[] = []; | |
| 58 | afterEach(async () => { | |
| 59 | for (const s of sockets.splice(0)) s.terminate(); | |
| 60 | await shut(relay); | |
| 61 | lobby?.close(); | |
| 62 | relay = lobby = undefined; | |
| 63 | }); | |
| 64 | const start = async (chat = new Chat()) => { | |
| 65 | lobby = new Lobby({ identityKey: KEY, turns: new Turns(), chat, admin: new Admin(), accounts: new Accounts() }); | |
| 66 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby }); | |
| 67 | return relay.port; | |
| 68 | }; | |
| 69 | const join = async (port: number, headers: Record<string, string>) => { | |
| 70 | const c = await client(port, headers); | |
| 71 | sockets.push(c.ws); | |
| 72 | return c; | |
| 73 | }; | |
| 74 | const post = (port: number, path: string, headers: Record<string, string>) => | |
| 75 | fetch(`http://127.0.0.1:${port}${path}`, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: "{}" }); | |
| 76 | /** Everything sent before this has been handled once the answer to a history request is back. */ | |
| 77 | const settle = async (c: Awaited<ReturnType<typeof client>>) => { | |
| 78 | const answer = c.next((m) => m.type === "history"); | |
| 79 | c.send({ type: "history", before: 1 }); | |
| 80 | await answer; | |
| 81 | }; | |
| 82 | ||
| 83 | test("someone signed in who is not driving is refused every command; /stop is the owner's", async () => { | |
| 84 | const port = await start(); | |
| 85 | const bob = await join(port, BOB); | |
| 86 | bob.send({ type: "join" }); | |
| 87 | await bob.next((m) => m.type === "state" && m.driver?.name === "bob"); | |
| 88 | for (const path of ["/torque", "/move", "/pose/go", "/play", "/record/start", "/record/stop"]) { | |
| 89 | const refused = await post(port, path, ALICE); | |
| 90 | expect([path, refused.status]).toEqual([path, 403]); | |
| 91 | } | |
| 92 | expect((await post(port, "/stop", ALICE)).status).toBe(403); | |
| 93 | expect((await post(port, "/stop", OWNER)).status).toBe(200); | |
| 94 | }); | |
| 95 | ||
| 96 | test("chat HTML stays text, and frames that are not JSON objects get an error", async () => { | |
| 97 | const chat = new Chat({ rateMs: 0 }); | |
| 98 | const port = await start(chat); | |
| 99 | const alice = await join(port, ALICE); | |
| 100 | const evil = `<img src=x onerror=alert(1)><script>alert("hi")</script>`; | |
| 101 | const said = alice.next((m) => m.type === "said"); | |
| 102 | alice.send({ type: "post", text: evil }); | |
| 103 | expect((await said).message.text).toBe(evil); | |
| 104 | expect(chat.messages.at(-1)?.text).toBe(evil); | |
| 105 | for (const frame of ["not json {", "[1,2]", "42", "null", '"text"']) { | |
| 106 | const err = alice.next((m) => m.type === "error"); | |
| 107 | alice.ws.send(frame); | |
| 108 | expect((await err).error).toMatch(/JSON/); | |
| 109 | } | |
| 110 | }); | |
| 111 | ||
| 112 | test("300 chat frames and 300 join frames from one person give one line and one place", async () => { | |
| 113 | const chat = new Chat(); | |
| 114 | const port = await start(chat); | |
| 115 | const bob = await join(port, BOB); | |
| 116 | bob.send({ type: "join" }); | |
| 117 | await bob.next((m) => m.type === "state" && m.driver?.name === "bob"); | |
| 118 | const alice = await join(port, ALICE); | |
| 119 | for (let i = 0; i < 300; i++) alice.send({ type: "post", text: `spam ${i}` }); | |
| 120 | for (let i = 0; i < 300; i++) alice.send({ type: "join" }); | |
| 121 | await settle(alice); | |
| 122 | expect(chat.messages.filter((m) => m.from?.id === "u-alice")).toHaveLength(1); | |
| 123 | expect(lobby!.turns.queue.map((p) => p.id)).toEqual(["u-alice"]); | |
| 124 | }); | |
| 125 | ||
| 126 | test("only the owner mutes or ends a turn, and a mute survives a reconnect", async () => { | |
| 127 | const chat = new Chat({ rateMs: 0 }); | |
| 128 | const port = await start(chat); | |
| 129 | const alice = await join(port, ALICE); | |
| 130 | const bob = await join(port, BOB); | |
| 131 | alice.send({ type: "join" }); | |
| 132 | await bob.next((m) => m.type === "state" && m.driver?.name === "alice"); | |
| 133 | ||
| 134 | bob.send({ type: "mute", id: "u-alice", on: true }); | |
| 135 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner"); | |
| 136 | bob.send({ type: "end", id: "u-alice" }); | |
| 137 | expect((await bob.next((m) => m.type === "error")).error).toContain("only the owner"); | |
| 138 | expect(chat.muted.size).toBe(0); | |
| 139 | expect(lobby!.turns.driver?.id).toBe("u-alice"); | |
| 140 | ||
| 141 | const owner = await join(port, OWNER); | |
| 142 | owner.send({ type: "mute", id: "u-alice", on: true }); | |
| 143 | await alice.next((m) => m.type === "hello" && m.muted === true); | |
| 144 | alice.ws.close(); | |
| 145 | const again = await join(port, ALICE); | |
| 146 | expect((await again.first((m) => m.type === "hello")).muted).toBe(true); | |
| 147 | again.send({ type: "post", text: "still here" }); | |
| 148 | expect((await again.next((m) => m.type === "error")).error).toContain("muted"); | |
| 149 | expect(chat.messages.some((m) => m.from?.id === "u-alice")).toBe(false); | |
| 150 | ||
| 151 | owner.send({ type: "end", id: "u-alice" }); | |
| 152 | await bob.next((m) => m.type === "state" && m.driver === null); | |
| 153 | }); | |
| 154 | }); |
new above
offline
This
joinshadows thejoinimported from node:path at the top of the file. It works, but a reader will trip on it. Rename it, for example toconnect.