* fix(domain): preserve prototype-named JSON members through Evidence boundaries Zod's z.json() record projection drops own __proto__ members as a prototype-pollution guard, so provider replies carrying them lost evidence before hashing/serialization and CLI/MCP output. The shared schema now validates with z.json() as before, then rebuilds the value keeping prototype-named own members as ordinary data properties (__proto__ via Object.defineProperty, so the prototype is never mutated). parseHarJson no longer rejects __proto__ members; the explicit unsupported case from #992 is replaced with a preservation regression, and the inspect_web_network_capture contract text is updated to match. Fixes #1036 * fix(domain): bound JSON depth and oversized payloads at parsing boundaries jsonValueSchema re-walked hostile input through z.json()'s recursive core, so a deeply nested payload from an MCP tool argument, inline evidence, provider result, or keyed archive threw RangeError: Maximum call stack size exceeded instead of a validation issue. The schema now runs an iterative depth guard first and keeps z.json() for full JSON semantics behind it, which also turns cyclic values into validation errors. The 1000-edge limit stays far below the range where the recursive core overflows its stack (measured between 2000 and 3000 on Node 24) while leaving room for legitimately deep target-derived trees such as compiled NIB view hierarchies, and the guard narrows with a typed record check instead of any or unchecked casts. The non-recursive JSON Schema advertisement stays intact on both the public and standard projection paths. Two payload sizes get explicit budgets tied to their producer constraints: web text artifacts reject beyond the 16 MiB source budget the web tooling already advertises, and native UI screenshot base64 beyond the 64 MiB helper output budget. Regenerated product catalog, skill catalog digest, and completion-ledger manifest follow the schema-boundary change. * fix(validation): preserve depth diagnostics across process inputs --------- Co-authored-by: DeryFerd <deryferdikao125@gmail.com> Co-authored-by: N0zoM1z0 <1705004398@qq.com>
88 lines
2.9 KiB
TypeScript
88 lines
2.9 KiB
TypeScript
import { z } from "zod";
|
|
|
|
/** Maximum JSON nesting accepted before recursive result cloning. */
|
|
export const MAX_JSON_DEPTH = 1000;
|
|
|
|
/** JSON-safe value shared by domain, application, and adapter boundaries. */
|
|
export type JsonValue =
|
|
| string
|
|
| number
|
|
| boolean
|
|
| null
|
|
| JsonValue[]
|
|
| { [key: string]: JsonValue };
|
|
|
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
const recordShape = z.record(z.string(), z.unknown());
|
|
|
|
const jsonValidationIssue = (root: unknown): string | undefined => {
|
|
const pending: { value: unknown; depth: number }[] = [
|
|
{ value: root, depth: 0 },
|
|
];
|
|
while (pending.length > 0) {
|
|
const item = pending.pop();
|
|
if (item === undefined) break;
|
|
if (item.depth > MAX_JSON_DEPTH)
|
|
return `JSON value exceeds maximum nesting depth of ${MAX_JSON_DEPTH}`;
|
|
const value = item.value;
|
|
if (
|
|
value === null ||
|
|
typeof value === "string" ||
|
|
typeof value === "boolean"
|
|
)
|
|
continue;
|
|
if (typeof value === "number") {
|
|
if (!Number.isFinite(value)) return "JSON numbers must be finite";
|
|
} else if (Array.isArray(value)) {
|
|
for (const child of value)
|
|
pending.push({ value: child, depth: item.depth + 1 });
|
|
} else if (isRecord(value) && recordShape.safeParse(value).success) {
|
|
// Inspect every own member, including keys Zod's record parser skips.
|
|
for (const child of Object.values(value))
|
|
pending.push({ value: child, depth: item.depth + 1 });
|
|
} else return "Invalid JSON value";
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
const preserveJsonValue = (value: JsonValue): JsonValue => {
|
|
if (Array.isArray(value)) return value.map(preserveJsonValue);
|
|
if (value !== null && typeof value === "object")
|
|
return Object.fromEntries<JsonValue>(
|
|
Object.entries(value).map(([key, child]) => [
|
|
key,
|
|
preserveJsonValue(child),
|
|
]),
|
|
);
|
|
return value;
|
|
};
|
|
|
|
const validatedJsonValue = z.custom<JsonValue>(
|
|
(value) => jsonValidationIssue(value) === undefined,
|
|
{
|
|
error: (issue) => jsonValidationIssue(issue.input) ?? "Invalid JSON value",
|
|
},
|
|
);
|
|
// Keep both stages nonrecursive in public and Standard Schema projections.
|
|
validatedJsonValue._zod.toJSONSchema = () => ({});
|
|
|
|
/** Parse bounded JSON and retain all own data members without prototype setters. */
|
|
export const jsonValueSchema = z
|
|
.unknown()
|
|
.nonoptional()
|
|
.pipe(validatedJsonValue)
|
|
.transform(preserveJsonValue);
|
|
jsonValueSchema._zod.toJSONSchema = () => ({});
|
|
|
|
const objectValidation = z.custom<Record<string, JsonValue>>(
|
|
isRecord,
|
|
"Expected JSON object",
|
|
);
|
|
const objectProjection = () => ({ type: "object", additionalProperties: {} });
|
|
objectValidation._zod.toJSONSchema = objectProjection;
|
|
|
|
/** JSON object boundary for caller-visible parameter maps. */
|
|
export const jsonObjectSchema = jsonValueSchema.pipe(objectValidation);
|
|
jsonObjectSchema._zod.toJSONSchema = objectProjection;
|