diff --git a/pkgs/id/web/src/world-records.test.ts b/pkgs/id/web/src/world-records.test.ts index b412d58845..27da8d4635 100644 --- a/pkgs/id/web/src/world-records.test.ts +++ b/pkgs/id/web/src/world-records.test.ts @@ -247,6 +247,50 @@ describe("world page records fetch (#1458)", () => { expect(socket.sent("records")).toHaveLength(recordsRequests + 1); }); + it("rejects wrong-shape records frames (#7908) without rendering, echoing a cursor, or closing the fetch", () => { + const status = q("[data-world-status]"); + const recordsView = q("[data-world-records]"); + const renderedBefore = recordsView.textContent; + + q("[data-world-records-refresh]").click(); + const requestsBefore = socket.sent("records").length; + + // records that is not a plain object: string renders per-character + // entries pre-fix, an array per-index entries, a scalar an empty + // world. A non-string next_after was echoed back as `after`. + const malformedFrames: 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: { x: 1 }, next_after: 123 }, + { type: "records", records: { x: 1 }, next_after: { k: 1 } }, + ]; + for (const frame of malformedFrames) { + socket.deliver(frame); + expect(status.textContent).toBe("Received an invalid world frame"); + expect(status.dataset.state).toBe("error"); + // No follow-up page request (a non-string cursor is never + // echoed) and the rendered records are untouched. + expect(socket.sent("records")).toHaveLength(requestsBefore); + expect(recordsView.textContent).toBe(renderedBefore); + } + + // The fetch is still in flight: the next well-formed frame is + // accepted and completes it exactly as before. The refresh click + // above landed while the snapshot test's fetch was in flight, so + // it was coalesced (#1458): completing this fetch runs that one + // refetch, as a fresh first-page request with no `after` - never + // an echo of a rejected cursor. + socket.deliver({ type: "records", records: { z9: 9 }, next_after: null }); + expect(renderedRecords()).toEqual({ z9: 9 }); + expect(socket.sent("records")).toHaveLength(requestsBefore + 1); + expect(socket.sent("records").at(-1)).not.toHaveProperty("after"); + socket.deliver({ type: "records", records: { z9: 9 }, next_after: null }); + expect(renderedRecords()).toEqual({ z9: 9 }); + expect(socket.sent("records")).toHaveLength(requestsBefore + 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]"); diff --git a/pkgs/id/web/src/world.ts b/pkgs/id/web/src/world.ts index 175a41ca41..89e397274d 100644 --- a/pkgs/id/web/src/world.ts +++ b/pkgs/id/web/src/world.ts @@ -173,6 +173,16 @@ function initWorld(): void { } return false; }; + // #7908: the records frame is validated like its snapshot/event + // siblings. `records` must be a plain object (absent is an empty + // page, as before) and `next_after` a string cursor or null/absent. + // Anything else is rejected whole: a string records would render + // per-character entries, a scalar would silently render an empty + // world, and a non-string cursor would be echoed back to the server + // verbatim as the next page's `after`. + const isWorldRecordsFrame = (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"); @@ -266,6 +276,10 @@ function initWorld(): void { break; case "records": { if (!recordsPending) return; + if (!isWorldRecordsFrame(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.