diff --git a/pkgs/id/web/src/shell-contract.ts b/pkgs/id/web/src/shell-contract.ts index 2ce02cd65c..1876e0fb03 100644 --- a/pkgs/id/web/src/shell-contract.ts +++ b/pkgs/id/web/src/shell-contract.ts @@ -112,6 +112,27 @@ 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"); + // Top-level fields the checks read, mirroring the Rust RawContract: each + // is required there, so a document missing one -- or carrying one of the + // wrong shape -- fails to load there. Validate them here too, so a + // malformed document is rejected at load with a named error instead of + // throwing a TypeError at check time (missing routing_fields/projection) + // or silently disabling a check (missing max_frame_bytes turns off the + // frame cap; a string routing_fields substring-matches field names). + if (!isInteger(value.max_frame_bytes) || value.max_frame_bytes < 0) { + throw new Error("max_frame_bytes must be a non-negative integer"); + } + if (!Array.isArray(value.routing_fields) || value.routing_fields.some((field) => typeof field !== "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((key) => typeof key !== "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 9f38fc27c6..f80cf604d4 100644 --- a/pkgs/id/web/src/world-contract.test.ts +++ b/pkgs/id/web/src/world-contract.test.ts @@ -58,6 +58,48 @@ describe("shell contract validator", () => { expect(() => parseShellContract(badBound)).toThrow("chat.text: max_bytes must be a finite number"); }); + it("rejects malformed top-level fields when loading a contract (#4323)", () => { + const without = (key: string): Record => { + const malformed = structuredClone(contract) as unknown as Record; + delete malformed[key]; + return malformed; + }; + + expect(() => parseShellContract(without("routing_fields"))).toThrow("routing_fields must be an array of strings"); + expect(() => parseShellContract(without("projection"))).toThrow("projection must be an object"); + expect(() => parseShellContract(without("max_frame_bytes"))).toThrow( + "max_frame_bytes must be a non-negative integer", + ); + + const stringRouting = structuredClone(contract) as unknown as Record; + stringRouting.routing_fields = "world"; + expect(() => parseShellContract(stringRouting)).toThrow("routing_fields must be an array of strings"); + + const mixedRouting = structuredClone(contract) as unknown as Record; + mixedRouting.routing_fields = ["world", 7]; + expect(() => parseShellContract(mixedRouting)).toThrow("routing_fields must be an array of strings"); + + const stringProjection = structuredClone(contract) as unknown as Record; + stringProjection.projection = "world_state"; + expect(() => parseShellContract(stringProjection)).toThrow("projection must be an object"); + + const withoutRedacted = structuredClone(contract) as unknown as Record; + withoutRedacted.projection = {}; + expect(() => parseShellContract(withoutRedacted)).toThrow("projection.redacted_keys must be an array of strings"); + + const mixedRedacted = structuredClone(contract) as unknown as Record; + mixedRedacted.projection = { redacted_keys: ["seed", 7] }; + expect(() => parseShellContract(mixedRedacted)).toThrow("projection.redacted_keys must be an array of strings"); + + const negativeCap = structuredClone(contract) as unknown as Record; + negativeCap.max_frame_bytes = -1; + expect(() => parseShellContract(negativeCap)).toThrow("max_frame_bytes must be a non-negative integer"); + + const fractionalCap = structuredClone(contract) as unknown as Record; + fractionalCap.max_frame_bytes = 1.5; + expect(() => parseShellContract(fractionalCap)).toThrow("max_frame_bytes must be a non-negative integer"); + }); + it("returns violations instead of throwing for malformed field specs", () => { const malformedClient = withFieldType("client", "chat", "text", "u65"); const clientCheck = checkClientFrame(malformedClient, { type: "chat", text: "hi" }, "joined");