diff --git a/pkgs/id/web/src/shell-contract.ts b/pkgs/id/web/src/shell-contract.ts index 0372fa911a..e21e56a759 100644 --- a/pkgs/id/web/src/shell-contract.ts +++ b/pkgs/id/web/src/shell-contract.ts @@ -115,6 +115,22 @@ const isInteger = (value: unknown): value is number => typeof value === "number" export function parseShellContract(value: unknown): ShellContract { if (!isObject(value)) throw new Error("shell contract must be an object"); + if (typeof value.max_frame_bytes !== "number" || !Number.isFinite(value.max_frame_bytes) || value.max_frame_bytes < 0) { + throw new Error("max_frame_bytes must be a non-negative finite number"); + } + + if (!Array.isArray(value.routing_fields) || value.routing_fields.some((item) => typeof item !== "string")) { + throw new Error("routing_fields must be an array of strings"); + } + + if (!isObject(value.projection)) { + throw new Error("projection must be an object"); + } + + if (!Array.isArray(value.projection.redacted_keys) || value.projection.redacted_keys.some((item) => typeof item !== "string")) { + throw new Error("projection.redacted_keys must be an array of strings"); + } + for (const side of ["client_frames", "server_frames"] as const) { const frames = value[side]; if (!isObject(frames)) throw new Error(`${side} must be an object`); diff --git a/pkgs/id/web/src/world-contract.test.ts b/pkgs/id/web/src/world-contract.test.ts index ac242c7d96..8ceb012e21 100644 --- a/pkgs/id/web/src/world-contract.test.ts +++ b/pkgs/id/web/src/world-contract.test.ts @@ -43,6 +43,36 @@ function worldPageMarkup(): string { describe("shell contract validator", () => { const cap = "cap"; + it("validates top-level fields when loading a contract", () => { + const missingMaxBytes = structuredClone(contract); + delete (missingMaxBytes as any).max_frame_bytes; + expect(() => parseShellContract(missingMaxBytes)).toThrow("max_frame_bytes must be a non-negative finite number"); + + const negativeMaxBytes = structuredClone(contract); + (negativeMaxBytes as any).max_frame_bytes = -1; + expect(() => parseShellContract(negativeMaxBytes)).toThrow("max_frame_bytes must be a non-negative finite number"); + + const missingRoutingFields = structuredClone(contract); + delete (missingRoutingFields as any).routing_fields; + expect(() => parseShellContract(missingRoutingFields)).toThrow("routing_fields must be an array of strings"); + + const stringRoutingFields = structuredClone(contract); + (stringRoutingFields as any).routing_fields = "world"; + expect(() => parseShellContract(stringRoutingFields)).toThrow("routing_fields must be an array of strings"); + + const missingProjection = structuredClone(contract); + delete (missingProjection as any).projection; + expect(() => parseShellContract(missingProjection)).toThrow("projection must be an object"); + + const missingRedactedKeys = structuredClone(contract); + delete (missingRedactedKeys as any).projection.redacted_keys; + expect(() => parseShellContract(missingRedactedKeys)).toThrow("projection.redacted_keys must be an array of strings"); + + const stringRedactedKeys = structuredClone(contract); + (stringRedactedKeys as any).projection.redacted_keys = "seed"; + expect(() => parseShellContract(stringRedactedKeys)).toThrow("projection.redacted_keys must be an array of strings"); + }); + it("validates every client and server field spec when loading a contract", () => { expect(parseShellContract(contractDocument)).toEqual(contract);