diff --git a/pkgs/id/web/src/shell-contract.ts b/pkgs/id/web/src/shell-contract.ts index 0433634d94..97497e884d 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,28 @@ 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 + // 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). + 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..cfe5edd5ae 100644 --- a/pkgs/id/web/src/world-contract.test.ts +++ b/pkgs/id/web/src/world-contract.test.ts @@ -61,6 +61,46 @@ 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("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( + 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)") });