From 2753db0a1fd2918f351c68cb0a3aa5533587990e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 00:50:35 -0600 Subject: [PATCH] fix(id-web): validate world snapshot and event frames --- pkgs/id/web/src/world-records.test.ts | 83 +++++++++++++++++++++++++++ pkgs/id/web/src/world.ts | 61 +++++++++++++++++--- 2 files changed, 136 insertions(+), 8 deletions(-) diff --git a/pkgs/id/web/src/world-records.test.ts b/pkgs/id/web/src/world-records.test.ts index 784c00cd60..6a9b0f6bf6 100644 --- a/pkgs/id/web/src/world-records.test.ts +++ b/pkgs/id/web/src/world-records.test.ts @@ -174,4 +174,87 @@ describe("world page records fetch (#1458)", () => { expect(renderedRecords()).toEqual({ only: 1 }); expect(socket.sent("records")).toHaveLength(7); }); + + it("rejects malformed snapshots without partial state changes and accepts the next valid snapshot", () => { + const status = q("[data-world-status]"); + const participantList = q("[data-world-participants]"); + const log = q("[data-world-log]"); + const recordsRequests = socket.sent("records").length; + const malformedSnapshots: Array> = [ + { type: "snapshot" }, + { type: "snapshot", snapshot: null }, + { type: "snapshot", snapshot: { world_id: "bad", participants: [] } }, + { type: "snapshot", snapshot: { world_id: "bad", current_sequence: 90, participants: "bad" } }, + { type: "snapshot", snapshot: { world_id: "bad", current_sequence: Number.NaN, participants: [] } }, + { type: "snapshot", snapshot: { world_id: 3, current_sequence: 90, participants: [] } }, + { + type: "snapshot", + snapshot: { world_id: "bad", current_sequence: 90, participants: [{ id: 1, display_name: 2 }] }, + }, + ]; + + for (const frame of malformedSnapshots) { + socket.deliver(frame); + expect(status.textContent).toBe("Received an invalid world frame"); + expect(status.dataset.state).toBe("error"); + expect(participantList.textContent).toBe("ada"); + expect(socket.sent("records")).toHaveLength(recordsRequests); + } + expect(log.textContent).toContain("error: invalid world frame"); + + socket.deliver({ + type: "snapshot", + snapshot: { world_id: "recovered", current_sequence: 8, participants: [{ id: 2, display_name: "grace" }] }, + }); + expect(status.textContent).toBe("Connected"); + expect(status.dataset.state).toBe("good"); + expect(participantList.textContent).toBe("grace"); + expect(log.textContent).toContain("Connected to recovered at event 8"); + expect(socket.sent("records")).toHaveLength(recordsRequests + 1); + }); + + it("rejects malformed events before cursor or log changes and accepts the next valid event", async () => { + const status = q("[data-world-status]"); + const log = q("[data-world-log]"); + const recordsRequests = socket.sent("records").length; + + socket.deliver({ + type: "event", + event: { sequence: 9, participant_id: 2, kind: { kind: "chat", data: "recovered" } }, + }); + expect(log.textContent).toContain("grace: recovered"); + + const malformedEvents: Array> = [ + { type: "event", event: { sequence: 100, participant_id: 2 } }, + { type: "event", event: null }, + { type: "event", event: { sequence: 101, participant_id: 2, kind: null } }, + { type: "event", event: { sequence: 102, participant_id: 2, kind: { kind: "other", data: "bad" } } }, + { type: "event", event: { sequence: 103, participant_id: 2, kind: { kind: "chat", data: 1 } } }, + { type: "event", event: { sequence: 104, participant_id: 2, kind: { kind: "input", data: "bad" } } }, + { + type: "event", + event: { sequence: 105, participant_id: 2, kind: { kind: "input", data: [Number.NaN] } }, + }, + { type: "event", event: { sequence: 106, participant_id: "2", kind: { kind: "input", data: [27] } } }, + { type: "event", event: { participant_id: 2, kind: { kind: "chat", data: "bad sequence" } } }, + { type: "event", event: { sequence: Number.NaN, participant_id: 2, kind: { kind: "chat", data: "bad sequence" } } }, + ]; + + for (const frame of malformedEvents) { + socket.deliver(frame); + expect(status.textContent).toBe("Received an invalid world frame"); + expect(status.dataset.state).toBe("error"); + } + expect(socket.sent("records")).toHaveLength(recordsRequests); + expect(log.textContent).not.toContain("input #100"); + expect(log.textContent).not.toContain("bad sequence"); + + const closedSocket = socket; + closedSocket.close(); + await new Promise((resolve) => setTimeout(resolve, 600)); + const reconnectedSocket = FakeSocket.all.at(-1); + expect(reconnectedSocket).toBeDefined(); + reconnectedSocket!.open(); + expect(reconnectedSocket!.sent("join")[0]).toHaveProperty("after", 9); + }); }); diff --git a/pkgs/id/web/src/world.ts b/pkgs/id/web/src/world.ts index 965961996c..c140492c20 100644 --- a/pkgs/id/web/src/world.ts +++ b/pkgs/id/web/src/world.ts @@ -133,6 +133,39 @@ function initWorld(): void { status.dataset.state = state; }; + const isRecord = (value: unknown): value is Record => + typeof value === "object" && value !== null && !Array.isArray(value); + const isFiniteNumber = (value: unknown): value is number => + typeof value === "number" && Number.isFinite(value); + const isWorldSnapshot = (value: unknown): value is NonNullable => + isRecord(value) && + typeof value.world_id === "string" && + isFiniteNumber(value.current_sequence) && + Array.isArray(value.participants) && + value.participants.every( + (participant) => + isRecord(participant) && isFiniteNumber(participant.id) && typeof participant.display_name === "string", + ); + const isWorldEvent = (value: unknown): value is NonNullable => { + if ( + !isRecord(value) || + !isFiniteNumber(value.sequence) || + !isFiniteNumber(value.participant_id) || + !isRecord(value.kind) + ) { + return false; + } + if (value.kind.kind === "chat") return typeof value.kind.data === "string"; + if (value.kind.kind === "input") { + return Array.isArray(value.kind.data) && value.kind.data.every(isFiniteNumber); + } + return false; + }; + const rejectFrame = () => { + setStatus("Received an invalid world frame", "error"); + appendLine(log, "error: invalid world frame"); + }; + const showCapability = (value: string) => { capability.value = value; sessionStorage.setItem(storageKey, value); @@ -140,33 +173,45 @@ function initWorld(): void { }; const receive = (raw: string) => { - let frame: WorldFrame; + let parsed: unknown; try { - frame = JSON.parse(raw) as WorldFrame; + parsed = JSON.parse(raw) as unknown; } catch { - setStatus("Received an invalid world frame", "error"); + rejectFrame(); return; } + if (!isRecord(parsed) || typeof parsed.type !== "string") { + rejectFrame(); + return; + } + const frame = parsed as WorldFrame; switch (frame.type) { case "snapshot": { - if (!frame.snapshot) return; - cursor = frame.snapshot.current_sequence; + const snapshot = frame.snapshot; + if (!isWorldSnapshot(snapshot)) { + rejectFrame(); + return; + } + cursor = snapshot.current_sequence; participants.replaceChildren(); - for (const participant of frame.snapshot.participants) { + for (const participant of snapshot.participants) { const tag = document.createElement("span"); tag.className = "badge badge-outline mr-2"; tag.dataset.participant = String(participant.id); tag.textContent = participant.display_name; participants.append(tag); } - appendLine(log, `Connected to ${frame.snapshot.world_id} at event ${cursor}`); + appendLine(log, `Connected to ${snapshot.world_id} at event ${cursor}`); setStatus("Connected", "good"); requestRecords(); break; } case "event": { const event = frame.event; - if (!event) return; + if (!isWorldEvent(event)) { + rejectFrame(); + return; + } cursor = Math.max(cursor, event.sequence); if (event.kind.kind === "chat") { const who = participants.querySelector(`[data-participant="${event.participant_id}"]`);