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
27 changes: 26 additions & 1 deletion pkgs/id/web/src/world-http.test.ts
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -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");
});
});
32 changes: 32 additions & 0 deletions pkgs/id/web/src/world-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pkgs/id/web/src/world.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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<string, unknown> = {};
let firstPage = true;
try {
do {
pages += 1;
const params = new URLSearchParams();
if (world) params.set("world", world);
if (after) params.set("after", after);
Expand Down Expand Up @@ -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");
Expand Down
Loading