chore: merge main into feat/dyld-shared-cache

This commit is contained in:
morluto
2026-10-08 21:26:50 +08:00
10 changed files with 277 additions and 12 deletions
+7
View File
@@ -1,4 +1,5 @@
import { readFile } from "node:fs/promises";
import { z } from "zod";
import { AnalysisError } from "../../domain/analysisErrorBase.js";
import { AnalysisInputError } from "../../domain/analysisErrorCore.js";
@@ -113,6 +114,12 @@ const parseCaptureEvidence = (input: unknown) => {
try {
evidence = parseEvidence(input);
} catch (cause: unknown) {
if (cause instanceof z.ZodError)
throw new AnalysisInputError(
"compare_process_captures",
{ cause },
projectInputIssues(cause.issues, input),
);
throw invalidCaptureEvidence();
}
if (
+27
View File
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { MAX_JSON_DEPTH } from "../jsonValue.js";
import { projectKeyedArchive } from "./keyedArchive.js";
const deepValue = (depth: number): unknown => {
let value: unknown = 1;
for (let index = 0; index < depth; index += 1) value = { nested: value };
return value;
};
describe("projectKeyedArchive input bound", () => {
it("rejects an over-deep archive value with a validation error instead of crashing", () => {
let error: unknown;
try {
projectKeyedArchive(deepValue(MAX_JSON_DEPTH + 1), {
offset: 0,
limit: 1,
});
} catch (caught) {
error = caught;
}
expect(error).toBeInstanceOf(z.ZodError);
expect(error).not.toBeInstanceOf(RangeError);
});
});
+25
View File
@@ -4,6 +4,7 @@ import { describe, expect } from "vitest";
import { createAnalysisProfile } from "./analysisProfile.js";
import type { BinaryTarget } from "./binaryTarget.js";
import { createEvidence, evidenceSchema, parseEvidence } from "./evidence.js";
import { MAX_JSON_DEPTH } from "./jsonValue.js";
import { createEvidenceBundle } from "./evidenceBundle.js";
const TARGET: BinaryTarget = {
@@ -255,3 +256,27 @@ describe("DOS analysis evidence identity", () => {
);
});
});
describe("evidence parameter depth bound", () => {
it("rejects evidence whose parameters nest past the JSON depth limit", () => {
const evidence = createEvidence(TARGET, PROVIDER, {
operation: "health",
parameters: {},
result: true,
});
let value: unknown = 1;
for (let index = 0; index <= MAX_JSON_DEPTH; index += 1)
value = { nested: value };
const result = evidenceSchema.safeParse({
...evidence,
parameters: { attack: value },
});
expect(result.success).toBe(false);
if (result.success) return;
expect(
result.error.issues.some((issue) =>
issue.message.includes("maximum nesting depth"),
),
).toBe(true);
});
});
+66 -2
View File
@@ -1,8 +1,12 @@
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect, it } from "vitest";
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { jsonObjectSchema, jsonValueSchema } from "./jsonValue.js";
import {
jsonObjectSchema,
jsonValueSchema,
MAX_JSON_DEPTH,
} from "./jsonValue.js";
const values = [
"text",
@@ -35,3 +39,63 @@ it("still parses only JSON values at runtime", () => {
for (const value of [undefined, Number.NaN, Infinity, () => 0, [undefined]])
expect(jsonValueSchema.safeParse(value).success).toBe(false);
});
const deepObject = (depth: number): unknown => {
let value: unknown = 1;
for (let index = 0; index < depth; index += 1) value = { nested: value };
return value;
};
describe("jsonValueSchema depth bound", () => {
it("accepts values nested to the maximum depth", () => {
expect(jsonValueSchema.safeParse(deepObject(MAX_JSON_DEPTH)).success).toBe(
true,
);
});
it("rejects values nested past the maximum depth without throwing", () => {
const result = jsonValueSchema.safeParse(deepObject(MAX_JSON_DEPTH + 1));
expect(result.success).toBe(false);
if (result.success) return;
expect(result.error.issues[0]?.message).toContain("maximum nesting depth");
});
it.each(["object", "array"])(
"rejects the original 10000-level %s crash without throwing",
(kind) => {
let value: unknown = 1;
for (let index = 0; index < 10_000; index += 1)
value = kind === "object" ? { nested: value } : [value];
const result = jsonValueSchema.safeParse(value);
expect(result.success).toBe(false);
if (result.success) return;
expect(result.error.issues[0]?.message).toContain(
"maximum nesting depth",
);
},
);
it("rejects cyclic values instead of recursing forever", () => {
const cyclic: Record<string, unknown> = {};
cyclic.self = cyclic;
const result = jsonValueSchema.safeParse(cyclic);
expect(result.success).toBe(false);
});
it("keeps json semantics for non-JSON values", () => {
expect(jsonValueSchema.safeParse(undefined).success).toBe(false);
expect(jsonValueSchema.safeParse({ a: Number.NaN }).success).toBe(false);
expect(
jsonValueSchema.safeParse({ a: Number.POSITIVE_INFINITY }).success,
).toBe(false);
expect(jsonValueSchema.safeParse({ a: 1n }).success).toBe(false);
});
it("accepts ordinary nested JSON", () => {
expect(
jsonValueSchema.safeParse({
list: [1, "two", null, true, { deep: { further: [{}] } }],
}).success,
).toBe(true);
});
});
+56 -2
View File
@@ -1,10 +1,64 @@
import { z } from "zod";
/** JSON-safe value shared by domain, application, and adapter boundaries. */
export const jsonValueSchema = z.json();
/**
* Maximum nesting depth accepted for JSON values.
*
* Sits well below the point where the recursive `z.json()` 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.
*/
export const MAX_JSON_DEPTH = 1000;
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const exceedsMaxDepth = (root: unknown): boolean => {
// Depth counts edges from the root.
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 true;
const value = item.value;
if (Array.isArray(value)) {
for (const child of value)
pending.push({ value: child, depth: item.depth + 1 });
} else if (isRecord(value)) {
for (const key of Object.keys(value))
pending.push({ value: value[key], depth: item.depth + 1 });
}
}
return false;
};
/**
* JSON-safe value shared by domain, application, and adapter boundaries.
*
* An iterative depth guard runs before the recursive `z.json()` core so
* hostile nested payloads surface as validation issues instead of RangeErrors.
* The guard only refuses over-deep values; ordinary JSON parsing semantics
* (including which members `z.json()` keeps) are unchanged.
*/
const jsonCore = z.json();
// Every JSON value satisfies `{}`. Advertise that instead of Zod's
// self-referential `$defs` projection, which MCP clients and model APIs that
// reject recursive JSON Schemas refuse. Runtime parsing still admits only JSON.
// The override sits on the core as well as on the pipe because the standard
// JSON Schema projection walks into pipe stages without consulting the outer
// node, while the public toJSONSchema projection stops at the pipe.
jsonCore._zod.toJSONSchema = () => ({});
export const jsonValueSchema = z
.unknown()
.superRefine((value, context) => {
if (exceedsMaxDepth(value))
context.addIssue({
code: "custom",
message: `JSON value exceeds maximum nesting depth of ${MAX_JSON_DEPTH}`,
});
})
.pipe(jsonCore);
jsonValueSchema._zod.toJSONSchema = () => ({});
/** JSON object boundary for caller-visible parameter maps. */