Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkgs/id/web/src/shell-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down
42 changes: 42 additions & 0 deletions pkgs/id/web/src/world-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> => {
const malformed = structuredClone(contract) as unknown as Record<string, unknown>;
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<string, unknown>;
stringRouting.routing_fields = "world";
expect(() => parseShellContract(stringRouting)).toThrow("routing_fields must be an array of strings");

const mixedRouting = structuredClone(contract) as unknown as Record<string, unknown>;
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<string, unknown>;
stringProjection.projection = "world_state";
expect(() => parseShellContract(stringProjection)).toThrow("projection must be an object");

const withoutRedacted = structuredClone(contract) as unknown as Record<string, unknown>;
withoutRedacted.projection = {};
expect(() => parseShellContract(withoutRedacted)).toThrow("projection.redacted_keys must be an array of strings");

const mixedRedacted = structuredClone(contract) as unknown as Record<string, unknown>;
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<string, unknown>;
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<string, unknown>;
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");
Expand Down
Loading