From 4d77e7177e4917189b019b3d0fb14691512f7044 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 16:02:32 -0600 Subject: [PATCH] fix(id-web): bound host-view record paging with an advance check (#9043) --- pkgs/id/web/src/world-http.test.ts | 27 ++++++++++++++++++++++++- pkgs/id/web/src/world-http.ts | 32 ++++++++++++++++++++++++++++++ pkgs/id/web/src/world.ts | 6 ++++-- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/pkgs/id/web/src/world-http.test.ts b/pkgs/id/web/src/world-http.test.ts index a5267f6fb3..3609559b39 100644 --- a/pkgs/id/web/src/world-http.test.ts +++ b/pkgs/id/web/src/world-http.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { parseWorldHostViewPage, parseWorldInviteResponse } from "./world-http"; +import { advanceHostViewCursor, HOST_VIEW_MAX_PAGES, parseWorldHostViewPage, parseWorldInviteResponse } from "./world-http"; // Oracle: the server contracts in pkgs/id/src/web/world_ws.rs — // HostViewResponse (all fields always present; snapshot/view/surface/ @@ -94,3 +94,28 @@ describe("parseWorldInviteResponse", () => { }); }); }); + +describe("advanceHostViewCursor (#9043)", () => { + it("ends paging on a null cursor (control)", () => { + expect(advanceHostViewCursor({ previous: null, next: null, page: 1 })).toBeNull(); + expect(advanceHostViewCursor({ previous: "a", next: null, page: 2 })).toBeNull(); + }); + + it("passes an advancing cursor through (control)", () => { + expect(advanceHostViewCursor({ previous: null, next: "a", page: 1 })).toBe("a"); + expect(advanceHostViewCursor({ previous: "a", next: "b", page: 2 })).toBe("b"); + }); + + it("refuses a cursor that repeats the one just requested", () => { + expect(() => advanceHostViewCursor({ previous: "a", next: "a", page: 2 })).toThrow( + /^Host view unavailable: records cursor did not advance/, + ); + }); + + it("refuses to page past the named bound even with advancing cursors", () => { + expect(() => + advanceHostViewCursor({ previous: "a", next: "b", page: HOST_VIEW_MAX_PAGES }), + ).toThrow(/^Host view unavailable: records paging exceeded/); + expect(advanceHostViewCursor({ previous: "a", next: "b", page: HOST_VIEW_MAX_PAGES - 1 })).toBe("b"); + }); +}); diff --git a/pkgs/id/web/src/world-http.ts b/pkgs/id/web/src/world-http.ts index 0faecb63fc..c06bee57da 100644 --- a/pkgs/id/web/src/world-http.ts +++ b/pkgs/id/web/src/world-http.ts @@ -89,6 +89,38 @@ export function parseWorldHostViewPage(payload: unknown): WorldHostViewPage { }; } +/** + * The host-view page loop's pagination guard (#6622 contract, #9043). + * + * The loop follows `next_after` until null. The cursor's type is + * already validated by `parseWorldHostViewPage` and the server pins + * its emission to string-or-null, but the loop itself must still + * terminate against a buggy or compromised host: a cursor that does + * not advance past the one just requested would re-request the same + * page forever, and a cursor stream that never ends would fetch + * forever. Both end here with the page's designed error vocabulary. + * Each server page is a full budget-bounded records page, so + * HOST_VIEW_MAX_PAGES is far beyond any view the page can render. + */ +export const HOST_VIEW_MAX_PAGES = 256; + +export function advanceHostViewCursor(input: { + readonly previous: string | null; + readonly next: string | null; + readonly page: number; +}): string | null { + if (input.next === null) return null; + if (input.next === input.previous) { + throw new Error("Host view unavailable: records cursor did not advance; refresh the host view"); + } + if (input.page >= HOST_VIEW_MAX_PAGES) { + throw new Error( + `Host view unavailable: records paging exceeded ${HOST_VIEW_MAX_PAGES} pages; refresh the host view`, + ); + } + return input.next; +} + /** * Validate an `/api/world/invite` response at the fetch boundary. The * capability is a credential: a missing, non-string, or empty one must diff --git a/pkgs/id/web/src/world.ts b/pkgs/id/web/src/world.ts index 3d26518c91..91c6342f25 100644 --- a/pkgs/id/web/src/world.ts +++ b/pkgs/id/web/src/world.ts @@ -1,7 +1,7 @@ /** Browser presentation for the server-authoritative world session. */ import { terminalBytesForKey } from "./world-keyboard"; -import { parseWorldHostViewPage, parseWorldInviteResponse } from "./world-http"; +import { advanceHostViewCursor, parseWorldHostViewPage, parseWorldInviteResponse } from "./world-http"; import { parseWorldSeed } from "./world-seed"; import { applyScreenPatch, renderWorldSurface, type ScreenPatch, type ScreenSurface } from "./world-surface"; @@ -467,11 +467,13 @@ function initWorld(): void { renderedViewRevision = null; const world = selectedWorld(); let after: string | null = null; + let pages = 0; let recordsSequence: number | null = null; const allRecords: Record = {}; let firstPage = true; try { do { + pages += 1; const params = new URLSearchParams(); if (world) params.set("world", world); if (after) params.set("after", after); @@ -518,7 +520,7 @@ function initWorld(): void { } appendLine(log, `Viewing ${result.snapshot?.world_id ?? world ?? "default world"} as host (read-only)`); } - after = result.next_after; + after = advanceHostViewCursor({ previous: after, next: result.next_after, page: pages }); } while (after !== null); recordsView.textContent = JSON.stringify(allRecords, null, 2); setStatus("Read-only host view", "good");