From b8d3bb9ca602f1c6c65b2ccdfeb3d4931e4cddbb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 2 Nov 2026 13:23:26 +0000 Subject: [PATCH] Fixes #4323: Validate top-level fields in shell contract This adds validation for missing and malformed `max_frame_bytes`, `routing_fields`, and `projection` objects during `parseShellContract`. Missing or incorrectly typed top-level fields will now throw an explicit descriptive error at load time instead of crashing session checkers midway or silently disabling the frame size check. Regression tests covering these fields were also added. Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- pkgs/id/web/src/shell-contract.ts | 16 ++++++++++++++ pkgs/id/web/src/world-contract.test.ts | 30 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) 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);