* fix(apple): preflight XML plist nesting before keyed archive decoding inspect_keyed_archive documented a 128-level nesting limit, but the XML plist path reached recursive decoding before any depth check: plist and the prototype-key traversal in parseXmlPropertyList recurse once per nesting level, so a ~75 KiB, 3000-level XML plist exhausted the call stack and reported Maximum call stack size exceeded instead of the bounded-resource limit that shallower over-limit documents (129, 200) already produced. The CLI classified both as truncated, so no crash or classification bypass was involved; the diagnostic was the defect. Reject excessive nesting in parseXmlPropertyList before any recursive pass, using the projection's own limit (MAX_PLIST_DEPTH) so the error is identical to the one projectPlistValue raises: callers keep the limit/truncated classification with no error-handler translation. Depth counts values the way the projection does - container members nest one level deeper, dictionary keys name members rather than nesting, and a self-closing element is the value at its own level - so the guard rejects exactly the documents the projection rejected, at the same boundary, and every in-limit document decodes unchanged. The guard sits at the shared decoder boundary, so Interface Builder, app Info.plist resolution and native inspect_plist gain the same deterministic limit; the binary plist path keeps its existing expansion preflight. Binary plist behavior, ordinary XML decode, __proto__ omission counting, and archive digest/Evidence provenance are unchanged. OpenStep autodetection is untouched by this guard, as the existing Interface Builder preflight also scopes its depth check to XML. Regression coverage: decoder unit tests at and just past the limit for dictionary and array nesting with self-closing and opaque markup, module tests through decodeKeyedArchiveBytes including a valid NSKeyedArchiver XML envelope at the limit, a production-boundary test through inspectBundleKeyedArchive, and the compiled public CLI reporting code truncated / reason limit for a 3000-level plist. * fix(apple): check XML nesting against parsed element structure Reuse the iterative XML decoder document for depth preflight and prototype-key lookup. Bound recursive plist wrappers alongside dictionaries and arrays without interpreting markup inside attributes or DOCTYPE declarations as values. Validated with 104 focused tests including compiled CLI, MCP prototype-key, and Interface Builder coverage; npm run check and verify:test-discovery passed. No real-provider verification was run. --------- Co-authored-by: Zburgers <Zburgers@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import type { JsonValue } from "../jsonValue.js";
|
|
|
|
/**
|
|
* Maximum plist value nesting the JSON projection supports. The XML decode
|
|
* preflight shares this limit so oversized documents are rejected before any
|
|
* recursive pass rather than during one.
|
|
*/
|
|
export const MAX_PLIST_DEPTH = 128;
|
|
|
|
/** A decoded plist projected into JSON, with values JSON cannot express. */
|
|
export interface ProjectedPlistValue {
|
|
readonly value: JsonValue;
|
|
/** Reals decoded as NaN, which the decoder also uses for ±infinity. */
|
|
readonly unknownRealCount: number;
|
|
}
|
|
|
|
/**
|
|
* Project a decoded plist into JSON. Data and dates become explicit
|
|
* `$plist_type` objects; a non-finite real becomes an explicit unknown
|
|
* because the XML decoder reports NaN and ±infinity alike.
|
|
*/
|
|
export const projectPlistValue = (value: unknown): ProjectedPlistValue => {
|
|
let unknownRealCount = 0;
|
|
const project = (item: unknown, depth: number): JsonValue => {
|
|
if (depth > MAX_PLIST_DEPTH)
|
|
throw new RangeError(
|
|
`Plist exceeds ${String(MAX_PLIST_DEPTH)} nesting levels`,
|
|
);
|
|
if (item instanceof Uint8Array)
|
|
return {
|
|
$plist_type: "data",
|
|
base64: Buffer.from(item).toString("base64"),
|
|
};
|
|
if (item instanceof Date)
|
|
return { $plist_type: "date", iso8601: item.toISOString() };
|
|
if (typeof item === "number" && !Number.isFinite(item)) {
|
|
unknownRealCount += 1;
|
|
return { $plist_type: "real", value: null };
|
|
}
|
|
if (Array.isArray(item))
|
|
return item.map((entry: unknown) => project(entry, depth + 1));
|
|
if (item !== null && typeof item === "object")
|
|
return Object.fromEntries(
|
|
Object.entries(item).map(([key, entry]) => [
|
|
key,
|
|
project(entry, depth + 1),
|
|
]),
|
|
);
|
|
return z.union([z.string(), z.number(), z.boolean(), z.null()]).parse(item);
|
|
};
|
|
const projected = project(value, 0);
|
|
return { value: projected, unknownRealCount };
|
|
};
|