Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkgs/id/web/src/world-records.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLOutputElement>("[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<HTMLButtonElement>("[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<HTMLButtonElement>("[data-world-records-refresh]").click();
const recordsRequests = socket.sent("records").length;

const malformedRecords: Array<Record<string, unknown>> = [
{ 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);
});
});
14 changes: 14 additions & 0 deletions pkgs/id/web/src/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.
Expand Down
Loading