diff --git a/pkgs/id/web/src/world-records.test.ts b/pkgs/id/web/src/world-records.test.ts index b412d58845..e458797d02 100644 --- a/pkgs/id/web/src/world-records.test.ts +++ b/pkgs/id/web/src/world-records.test.ts @@ -294,4 +294,48 @@ describe("world page records fetch (#1458)", () => { reconnectedSocket!.open(); expect(reconnectedSocket!.sent("join")[0]).toHaveProperty("after", 9); }); + + it("rejects wrong-shape records frames without changing rendered records or fetch state (#7908)", () => { + const status = q("[data-world-status]"); + + // The events test above ends by reconnecting: the module's live + // socket is the newest one, and the describe-scope `socket` is stale. + socket = FakeSocket.all.at(-1)!; + + // Establish a known render baseline through one clean fetch. + q("[data-world-records-refresh]").click(); + socket.deliver({ type: "records", records: { seed: 1 }, next_after: null }); + expect(renderedRecords()).toEqual({ seed: 1 }); + + // Start a fresh fetch; every frame below arrives mid-fetch. + q("[data-world-records-refresh]").click(); + const recordsRequests = socket.sent("records").length; + + const malformedRecords: Array> = [ + { type: "records", records: "abc", next_after: null }, + { type: "records", records: [10, 20], next_after: null }, + { type: "records", records: 42, next_after: null }, + { type: "records", records: true, next_after: null }, + { type: "records", records: null, next_after: null }, + // A valid records object must not merge when the cursor is wrong: + // the frame is validated whole, before any state changes. + { type: "records", records: { ok: 1 }, next_after: 42 }, + { type: "records", records: { ok: 1 }, next_after: { cursor: "x" } }, + ]; + + for (const frame of malformedRecords) { + socket.deliver(frame); + expect(status.textContent).toBe("Received an invalid world frame"); + expect(status.dataset.state).toBe("error"); + // Rendered records untouched, and no bad cursor was echoed back: + // the fetch sent nothing new and is still pending. + expect(renderedRecords()).toEqual({ seed: 1 }); + expect(socket.sent("records")).toHaveLength(recordsRequests); + } + + // The pending fetch still completes normally on the next valid frame. + socket.deliver({ type: "records", records: { final: 2 }, next_after: null }); + expect(renderedRecords()).toEqual({ final: 2 }); + expect(socket.sent("records")).toHaveLength(recordsRequests); + }); }); diff --git a/pkgs/id/web/src/world.ts b/pkgs/id/web/src/world.ts index 9c01ea7c8d..c31d92e937 100644 --- a/pkgs/id/web/src/world.ts +++ b/pkgs/id/web/src/world.ts @@ -174,6 +174,16 @@ function initWorld(): void { } return false; }; + // #7908: the records frame is shape-checked like its snapshot/event + // siblings. `records` merges into the rendered set, so a non-object + // value (a string merges per-character, an array per-index, a number + // silently as an empty page) corrupts the render; `next_after` is + // echoed back verbatim as the `after` cursor, so a non-string cursor + // poisons the pagination chain. Both must match the declared + // WorldFrame contract before anything is merged or adopted. + const isWorldRecords = (frame: WorldFrame): boolean => + (frame.records === undefined || isRecord(frame.records)) && + (frame.next_after === undefined || frame.next_after === null || typeof frame.next_after === "string"); const rejectFrame = () => { setStatus("Received an invalid world frame", "error"); appendLine(log, "error: invalid world frame"); @@ -267,6 +277,10 @@ function initWorld(): void { break; case "records": { if (!recordsPending) return; + if (!isWorldRecords(frame)) { + rejectFrame(); + return; + } // #2953 (define, don't assign): frame.records is peer-supplied, so // its keys are stored with define semantics — Object.assign would // run a "__proto__" key through the setter instead of storing it.