fix(javascript): honor cancellation before evidence publication (#1098)

This commit is contained in:
Ysopwnable
2026-10-09 01:15:50 +08:00
committed by GitHub
parent f8e143a3bc
commit cc0ee2edc6
18 changed files with 1080 additions and 74 deletions
+35 -5
View File
@@ -1,4 +1,5 @@
import { z } from "zod";
import { isImmutableJsonSnapshot } from "./immutableJson.js";
/** Maximum JSON nesting accepted before recursive result cloning. */
export const MAX_JSON_DEPTH = 1000;
@@ -15,17 +16,46 @@ export type 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 immutableValidation = new WeakMap<object, string | undefined>();
/** Validate JSON meaning and nesting without cloning a trusted immutable value. */
export const jsonValueValidationIssue = (root: unknown): string | undefined => {
const steps = jsonValueValidationSteps(root);
for (;;) {
const step = steps.next();
if (step.done) return step.value;
}
};
/** Validate JSON in bounded steps; cache only authenticated immutable roots. */
export function* jsonValueValidationSteps(
root: unknown,
): Generator<void, string | undefined> {
const immutable =
typeof root === "object" && root !== null && isImmutableJsonSnapshot(root)
? root
: undefined;
if (immutable !== undefined && immutableValidation.has(immutable))
return immutableValidation.get(immutable);
const finish = (issue: string | undefined): string | undefined => {
if (immutable !== undefined) immutableValidation.set(immutable, issue);
return issue;
};
const pending: { value: unknown; depth: number }[] = [
{ value: root, depth: 0 },
];
let examined = 0;
while (pending.length > 0) {
if (examined++ === 4096) {
examined = 0;
yield;
}
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}`;
return finish(
`JSON value exceeds maximum nesting depth of ${MAX_JSON_DEPTH}`,
);
const value = item.value;
if (
value === null ||
@@ -34,7 +64,7 @@ export const jsonValueValidationIssue = (root: unknown): string | undefined => {
)
continue;
if (typeof value === "number") {
if (!Number.isFinite(value)) return "JSON numbers must be finite";
if (!Number.isFinite(value)) return finish("JSON numbers must be finite");
} else if (Array.isArray(value)) {
for (const child of value)
pending.push({ value: child, depth: item.depth + 1 });
@@ -42,10 +72,10 @@ export const jsonValueValidationIssue = (root: unknown): string | undefined => {
// 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";
} else return finish("Invalid JSON value");
}
return undefined;
};
return finish(undefined);
}
const preserveJsonValue = (value: JsonValue): JsonValue => {
if (Array.isArray(value)) return value.map(preserveJsonValue);