From d02d9a1694796abbc5eda433f29af36b1ffe7c3c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 18:48:32 -0500 Subject: [PATCH 1/2] id(web): accept u64 values above 2^53 in the TS shell-contract checker (#1441) The TS port capped every u64 at Number.MAX_SAFE_INTEGER while the Rust checker caps at u64::MAX and the contract leaves most u64 fields open, so contract-valid frames (install_begin seeds, view/records sequences) were falsely rejected with a bound that appears nowhere in the contract. u64 is now checked on its own: non-negative integers are accepted at any JS-representable magnitude (a wire value above 2^53 arrives rounded and cannot be proven out of range), while the bounds JS can check exactly still are -- an explicit min, and an explicit max within the safe range (total_bytes <= 16777216 still rejects). Regression tests pin the parity cases from #1441; the one failing web test (search-panel accessibility) fails identically on clean main. --- pkgs/id/web/src/shell-contract.ts | 20 +++++++++++++++++- pkgs/id/web/src/world-contract.test.ts | 28 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkgs/id/web/src/shell-contract.ts b/pkgs/id/web/src/shell-contract.ts index 0433634d94..52c4543818 100644 --- a/pkgs/id/web/src/shell-contract.ts +++ b/pkgs/id/web/src/shell-contract.ts @@ -61,7 +61,8 @@ const FIELD_TYPES = new Set(["string", "hex", "bool", "u16", "u32", "u64 const INTEGER_MAX: Partial> = { u16: 0xffff, u32: 0xffff_ffff, - u64: Number.MAX_SAFE_INTEGER, + // No u64 entry: its ceiling is u64::MAX, which no JS number can + // represent, so checkValue handles u64 on its own below. }; const encoder = new TextEncoder(); const byteLength = (text: string): number => encoder.encode(text).length; @@ -110,6 +111,23 @@ function lookup( } function checkValue(field: Field, value: unknown): string | null { + if (field.kind === "u64") { + if (!isInteger(value) || value < 0) return "must be a non-negative integer"; + // The Rust checker caps u64 at u64::MAX. A JS number cannot hold that + // ceiling, and a wire value above 2^53 arrives already rounded, so a + // non-negative integer up there cannot be proven out of range and is + // accepted -- rejecting it would fail frames the host accepts, on a + // bound that appears nowhere in the contract. The bounds JS can still + // check exactly are checked: an explicit min, and an explicit max + // within the safe range (a rounded value past such a max is past it + // truly, because the rounding of a huge value stays huge). + const min = field.min ?? 0; + if (value < min) return `must be at least ${min}, got ${value}`; + if (field.max !== undefined && field.max <= Number.MAX_SAFE_INTEGER && value > field.max) { + return `must be in ${min}..=${field.max}, got ${value}`; + } + return null; + } const typeMax = INTEGER_MAX[field.kind]; if (typeMax !== undefined) { if (!isInteger(value) || value < 0) return "must be a non-negative integer"; diff --git a/pkgs/id/web/src/world-contract.test.ts b/pkgs/id/web/src/world-contract.test.ts index 200e1ddb28..4a5a6d0e84 100644 --- a/pkgs/id/web/src/world-contract.test.ts +++ b/pkgs/id/web/src/world-contract.test.ts @@ -61,6 +61,34 @@ describe("shell contract validator", () => { if (!check.ok) expect(check.error).toContain(error); }); + it("accepts u64 values at and above 2^53, like the Rust checker (#1441)", () => { + const seed = (value: number) => + checkClientFrame(contract, { type: "install_begin", admin_token: "a", total_bytes: 1, seed: value }, "opening"); + expect(seed(2 ** 53).ok).toBe(true); + // u64::MAX as it arrives from wire JSON: already rounded by the parser. + expect(seed(JSON.parse("18446744073709551615") as number).ok).toBe(true); + expect(checkServerFrame(contract, { type: "view", sequence: 2 ** 53, data: "x", surface: null }).ok).toBe(true); + expect(checkServerFrame(contract, { type: "records", sequence: 2 ** 53, records: {} }).ok).toBe(true); + }); + + it("still enforces the u64 bounds JS can check exactly (#1441)", () => { + const begin = (patch: Record) => + checkClientFrame( + contract, + { type: "install_begin", admin_token: "a", total_bytes: 1, seed: 1, ...patch }, + "opening", + ); + expect(begin({ seed: -1 }).ok).toBe(false); + expect(begin({ seed: 1.5 }).ok).toBe(false); + expect(begin({ total_bytes: 0 }).ok).toBe(false); + const over = begin({ total_bytes: 16777217 }); + expect(over.ok).toBe(false); + if (!over.ok) { + expect(over.error).toContain("16777216"); + expect(over.error).not.toContain("9007199254740991"); + } + }); + it("rejects a seed leaked at any depth of a server frame", () => { const leak = checkServerFrame(contract, { type: "event", event: { kind: { program: { seed: 7 } } } }); expect(leak).toEqual({ ok: false, error: expect.stringContaining("leaks `seed` (7)") }); From 8748a88e93d7551a7ca2eb26285c70424932ff68 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Fri, 30 Oct 2026 21:22:52 -0500 Subject: [PATCH 2/2] id(web): reject u64 values provably above u64::MAX in the TS shell-contract checker (#1441) 2^64 is the rounded wire form of u64::MAX itself and stays accepted, but anything above it (2**65, 1e100) is unambiguously out of range and the Rust checker rejects it via value.as_u64() -- the TS port must too, or conformance tests would approve frames the host rejects. Pins the Codex P2 boundary cases in world-contract.test.ts. --- pkgs/id/web/src/shell-contract.ts | 7 ++++++- pkgs/id/web/src/world-contract.test.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkgs/id/web/src/shell-contract.ts b/pkgs/id/web/src/shell-contract.ts index 52c4543818..97497e884d 100644 --- a/pkgs/id/web/src/shell-contract.ts +++ b/pkgs/id/web/src/shell-contract.ts @@ -113,6 +113,12 @@ function lookup( function checkValue(field: Field, value: unknown): string | null { if (field.kind === "u64") { if (!isInteger(value) || value < 0) return "must be a non-negative integer"; + // u64::MAX itself (2^64 - 1) rounds to 2^64 in a JS number, so 2^64 is + // the rounded wire form of an in-range value and is accepted -- but + // anything above it is provably out of range (the next double up is + // 2^64 + 2048) and the Rust checker rejects it via value.as_u64(). + const min = field.min ?? 0; + if (value > 2 ** 64) return `must be in ${min}..=18446744073709551615, got ${value}`; // The Rust checker caps u64 at u64::MAX. A JS number cannot hold that // ceiling, and a wire value above 2^53 arrives already rounded, so a // non-negative integer up there cannot be proven out of range and is @@ -121,7 +127,6 @@ function checkValue(field: Field, value: unknown): string | null { // check exactly are checked: an explicit min, and an explicit max // within the safe range (a rounded value past such a max is past it // truly, because the rounding of a huge value stays huge). - const min = field.min ?? 0; if (value < min) return `must be at least ${min}, got ${value}`; if (field.max !== undefined && field.max <= Number.MAX_SAFE_INTEGER && value > field.max) { return `must be in ${min}..=${field.max}, got ${value}`; diff --git a/pkgs/id/web/src/world-contract.test.ts b/pkgs/id/web/src/world-contract.test.ts index 4a5a6d0e84..cfe5edd5ae 100644 --- a/pkgs/id/web/src/world-contract.test.ts +++ b/pkgs/id/web/src/world-contract.test.ts @@ -71,6 +71,18 @@ describe("shell contract validator", () => { expect(checkServerFrame(contract, { type: "records", sequence: 2 ** 53, records: {} }).ok).toBe(true); }); + it("rejects u64 values provably above u64::MAX, like the Rust checker (#1441)", () => { + const seed = (value: number) => + checkClientFrame(contract, { type: "install_begin", admin_token: "a", total_bytes: 1, seed: value }, "opening"); + // 2^64 is the rounded wire form of u64::MAX itself: ambiguous, accepted. + expect(seed(2 ** 64).ok).toBe(true); + const over64 = seed(2 ** 65); + expect(over64.ok).toBe(false); + if (!over64.ok) expect(over64.error).toContain("18446744073709551615"); + expect(seed(1e100).ok).toBe(false); + expect(checkServerFrame(contract, { type: "view", sequence: 2 ** 65, data: "x", surface: null }).ok).toBe(false); + }); + it("still enforces the u64 bounds JS can check exactly (#1441)", () => { const begin = (patch: Record) => checkClientFrame(