From 70f7df7dd55566a7b892ae576c8f9b809c256e04 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Sat, 14 Nov 2026 17:45:51 -0600 Subject: [PATCH] fix(id/web): reject non-u64 world snapshot/event sequences and participant ids (#10401) --- pkgs/id/web/src/world-sequence.test.ts | 186 +++++++++++++++++++++++++ pkgs/id/web/src/world.ts | 16 ++- 2 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 pkgs/id/web/src/world-sequence.test.ts diff --git a/pkgs/id/web/src/world-sequence.test.ts b/pkgs/id/web/src/world-sequence.test.ts new file mode 100644 index 0000000000..9c7fa8d58f --- /dev/null +++ b/pkgs/id/web/src/world-sequence.test.ts @@ -0,0 +1,186 @@ +import { readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; + +/** + * #10401 regression tests: the world page's snapshot/event validators + * checked sequence and participant-id fields only for finiteness, so + * -1, 1.5 and 1e100 passed although they are protocol u64s. An accepted + * snapshot assigns current_sequence straight to the reconnect cursor + * and an accepted event raises it via Math.max, poisoning the resume + * position sent as the join frame's after on reconnect. Oracle: a + * frame is accepted iff current_sequence / sequence / participant_id / + * participant id are safe non-negative integers (Number.isSafeInteger + * && >= 0, mirroring PR #4250's collab INIT version rule); a rejected + * frame changes no cursor, participant, or log state and surfaces only + * the invalid-frame status. + */ + +const here = dirname(fileURLToPath(import.meta.url)); + +function worldPageMarkup(): string { + const source = readFileSync(resolve(here, "../../crates/id-web/src/templates.rs"), "utf8"); + const start = source.indexOf("pub fn render_world_page"); + const open = source.indexOf('r#"', start); + const close = source.indexOf('"#;', open); + if (start < 0 || open < 0 || close < 0) throw new Error("world page template not found in templates.rs"); + return source.slice(open + 3, close); +} + +class FakeSocket extends EventTarget { + static readonly CONNECTING = 0; + static readonly OPEN = 1; + static readonly CLOSING = 2; + static readonly CLOSED = 3; + static readonly all: FakeSocket[] = []; + readyState = FakeSocket.CONNECTING; + bufferedAmount = 0; + onclose: ((event: Event) => void) | null = null; + readonly sentFrames: Array> = []; + constructor(readonly url: string) { + super(); + FakeSocket.all.push(this); + } + send(data: string): void { + if (this.readyState !== FakeSocket.OPEN) throw new Error(`send on a socket in state ${this.readyState}`); + this.sentFrames.push(JSON.parse(data) as Record); + } + close(): void { + if (this.readyState === FakeSocket.CLOSED) return; + this.readyState = FakeSocket.CLOSED; + setTimeout(() => this.dispatchEvent(new Event("close")), 0); + } + serverDrop(): void { + this.close(); + } + open(): void { + this.readyState = FakeSocket.OPEN; + this.dispatchEvent(new Event("open")); + } + deliver(frame: Record): void { + this.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(frame) })); + } + sent(type: string): Array> { + return this.sentFrames.filter((frame) => frame.type === type); + } +} + +class FakeResizeObserver { + constructor(readonly callback: ResizeObserverCallback) {} + observe(): void {} + unobserve(): void {} + disconnect(): void {} +} + +const flushAsync = () => new Promise((resolveFlush) => setTimeout(resolveFlush, 10)); + +describe("world page snapshot/event sequence shapes (#10401)", () => { + const realWebSocket = globalThis.WebSocket; + const realResizeObserver = globalThis.ResizeObserver; + let root: HTMLElement; + let socket: FakeSocket; + const q = (selector: string): T => { + const found = root.querySelector(selector); + if (!found) throw new Error(`missing ${selector}`); + return found; + }; + const status = () => q("[data-world-status]"); + const participants = () => q("[data-world-participants]"); + const log = () => q("[data-world-log]"); + + beforeAll(async () => { + vi.stubGlobal("WebSocket", FakeSocket); + vi.stubGlobal("ResizeObserver", FakeResizeObserver); + sessionStorage.clear(); + document.body.innerHTML = worldPageMarkup(); + await import("./world"); + root = document.querySelector("[data-world-app]") as HTMLElement; + expect(root.dataset.initialized).toBe("true"); + q("[data-world-capability]").value = "guest-capability"; + q("[data-world-join]").click(); + expect(FakeSocket.all).toHaveLength(1); + socket = FakeSocket.all[0]; + socket.open(); + socket.deliver({ + type: "snapshot", + snapshot: { world_id: "lobby", current_sequence: 4, participants: [{ id: 1, display_name: "ada" }] }, + }); + expect(status().textContent).toBe("Connected"); + expect(participants().textContent).toContain("ada"); + }); + + afterAll(() => { + vi.stubGlobal("WebSocket", realWebSocket); + vi.stubGlobal("ResizeObserver", realResizeObserver); + document.body.innerHTML = ""; + }); + + it("rejects snapshot current_sequence values -1, 1.5 and 1e100 without touching participants", () => { + for (const bad of [-1, 1.5, 1e100]) { + socket.deliver({ + type: "snapshot", + snapshot: { world_id: "lobby", current_sequence: bad, participants: [{ id: 2, display_name: "mallory" }] }, + }); + expect(status().textContent).toBe("Received an invalid world frame"); + expect(status().dataset.state).toBe("error"); + expect(participants().textContent).toContain("ada"); + expect(participants().textContent).not.toContain("mallory"); + } + }); + + it("rejects a snapshot whose participant id is fractional", () => { + socket.deliver({ + type: "snapshot", + snapshot: { world_id: "lobby", current_sequence: 5, participants: [{ id: 1.5, display_name: "mallory" }] }, + }); + expect(status().textContent).toBe("Received an invalid world frame"); + expect(participants().textContent).toContain("ada"); + expect(participants().textContent).not.toContain("mallory"); + }); + + it("rejects event sequence values -1, 1.5 and 1e100 and a fractional participant_id without logging", () => { + const linesBefore = log().childElementCount; + const malformed: Array> = [ + { sequence: -1, participant_id: 1, kind: { kind: "chat", data: "neg" } }, + { sequence: 1.5, participant_id: 1, kind: { kind: "chat", data: "frac" } }, + { sequence: 1e100, participant_id: 1, kind: { kind: "chat", data: "huge" } }, + { sequence: 5, participant_id: 1.5, kind: { kind: "chat", data: "who" } }, + ]; + for (const event of malformed) { + socket.deliver({ type: "event", event }); + expect(status().textContent).toBe("Received an invalid world frame"); + } + expect(log().childElementCount).toBe(linesBefore + malformed.length); + expect(log().textContent).not.toContain("neg"); + expect(log().textContent).not.toContain("frac"); + expect(log().textContent).not.toContain("huge"); + expect(log().textContent).not.toContain("who"); + expect(log().textContent).not.toContain("player 1.5"); + }); + + it("control: valid frames still apply, including participant id 0 and a safe-integer sequence", () => { + socket.deliver({ + type: "event", + event: { sequence: 5, participant_id: 1, kind: { kind: "chat", data: "hello" } }, + }); + expect(log().textContent).toContain("ada: hello"); + socket.deliver({ + type: "snapshot", + snapshot: { world_id: "lobby", current_sequence: 6, participants: [{ id: 0, display_name: "zero" }] }, + }); + expect(status().textContent).toBe("Connected"); + expect(participants().textContent).toContain("zero"); + }); + + it("reconnect join still sends the pre-malformed-frame cursor as after", async () => { + const countBefore = FakeSocket.all.length; + socket.serverDrop(); + await flushAsync(); + await vi.waitFor(() => expect(FakeSocket.all).toHaveLength(countBefore + 1), { timeout: 3000, interval: 50 }); + const reconnected = FakeSocket.all.at(-1) as FakeSocket; + reconnected.open(); + expect(reconnected.sent("join")).toHaveLength(1); + expect(reconnected.sent("join")[0].after).toBe(6); + }); +}); diff --git a/pkgs/id/web/src/world.ts b/pkgs/id/web/src/world.ts index 91c6342f25..06a56e536b 100644 --- a/pkgs/id/web/src/world.ts +++ b/pkgs/id/web/src/world.ts @@ -157,20 +157,28 @@ function initWorld(): void { const hasNonStringField = (frame: Record, fields: string[]): boolean => fields.some((field) => frame[field] !== undefined && typeof frame[field] !== "string"); const isFiniteNumber = (value: unknown): value is number => typeof value === "number" && Number.isFinite(value); + // #10401: snapshot/event sequence and participant-id fields are + // protocol u64s. Finiteness alone accepts -1, 1.5 and 1e100, and an + // accepted value is assigned to (snapshot) or raised into (event) + // the reconnect cursor, poisoning resume. Require the same + // safe-non-negative-integer shape PR #4250's collab INIT version + // check uses, so such frames fall into the rejectFrame() path. + const isSafeNonNegativeInteger = (value: unknown): value is number => + typeof value === "number" && Number.isSafeInteger(value) && value >= 0; const isWorldSnapshot = (value: unknown): value is NonNullable => isRecord(value) && typeof value.world_id === "string" && - isFiniteNumber(value.current_sequence) && + isSafeNonNegativeInteger(value.current_sequence) && Array.isArray(value.participants) && value.participants.every( (participant) => - isRecord(participant) && isFiniteNumber(participant.id) && typeof participant.display_name === "string", + isRecord(participant) && isSafeNonNegativeInteger(participant.id) && typeof participant.display_name === "string", ); const isWorldEvent = (value: unknown): value is NonNullable => { if ( !isRecord(value) || - !isFiniteNumber(value.sequence) || - !isFiniteNumber(value.participant_id) || + !isSafeNonNegativeInteger(value.sequence) || + !isSafeNonNegativeInteger(value.participant_id) || !isRecord(value.kind) ) { return false;