fix(apple): preserve archive signatures and metadata byte identity

This commit is contained in:
morluto
2026-10-13 07:34:52 +08:00
parent 653067d799
commit dc038d16bb
12 changed files with 82 additions and 28 deletions
@@ -33,6 +33,10 @@ import { jsonValueSchema, type JsonValue } from "../../domain/jsonValue.js";
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
import {
hasBinaryPlistSignature,
hasNibArchiveSignature,
} from "../../domain/apple/appleArchiveFormat.js";
const MAX_AGGREGATE_INPUT_BYTES = 32 * 1024 * 1024;
const MAX_AGGREGATE_DECODE_BYTES = 256 * 1024 * 1024;
@@ -94,8 +98,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
remainingInputBytes,
);
aggregateInputBytes += bytes.length;
const isNibArchive =
bytes.subarray(0, 10).toString("ascii") === "NIBArchive";
const isNibArchive = hasNibArchiveSignature(bytes);
let nib: ReturnType<typeof projectNibArchive> | null = null;
let xmlText: string | undefined;
let binaryPrototypeKeyObserved = false;
@@ -113,7 +116,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
"NIBArchive representation exceeds the aggregate Interface Builder decode budget",
);
} else {
if (bytes.subarray(0, 8).toString("ascii") !== "bplist00")
if (!hasBinaryPlistSignature(bytes))
xmlText = decodeXmlPlistText(bytes);
const decodedReservation = preflightPropertyListDecode(
bytes,
@@ -665,7 +668,7 @@ const decodePlist = (
readonly value: JsonValue;
readonly omittedPrototypeKeys: number | null;
} => {
const binary = bytes.subarray(0, 8).toString("ascii") === "bplist00";
const binary = hasBinaryPlistSignature(bytes);
const { value, omittedPrototypeKeys } = binary
? {
value: parseBinary(bytes),
@@ -1,4 +1,5 @@
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
import { hasBinaryPlistSignature } from "../../domain/apple/appleArchiveFormat.js";
/** Reason reported when an archive would exceed the shared decoding budget. */
export type InterfaceBuilderBudgetReason = "aggregate_decode_budget_exhausted";
@@ -94,13 +95,12 @@ export const preflightPropertyListDecode = (
maxBytes: number,
xmlText?: string,
): PlistDecodePreflight => {
const expansion =
bytes.subarray(0, 8).toString("ascii") === "bplist00"
? estimateBinaryPlistExpansion(bytes, maxBytes)
: {
estimatedBytes: estimateXmlPlistExpansion(bytes, maxBytes, xmlText),
binaryPrototypeKeyObserved: false,
};
const expansion = hasBinaryPlistSignature(bytes)
? estimateBinaryPlistExpansion(bytes, maxBytes)
: {
estimatedBytes: estimateXmlPlistExpansion(bytes, maxBytes, xmlText),
binaryPrototypeKeyObserved: false,
};
if (expansion.estimatedBytes > maxBytes)
throw new InterfaceBuilderDecodeBudgetExceeded(
"aggregate_decode_budget_exhausted",
+18 -1
View File
@@ -23,6 +23,23 @@ const archive = {
$top: { root: { UID: 1 }, other: { UID: 2 } },
};
describe("inert keyed archive decoding", () => {
it.each(["bplist00", "NIBArchive"])(
"rejects high-bit mutations of %s as malformed bytes rather than another archive kind",
(signature) => {
for (let index = 0; index < signature.length; index += 1) {
const bytes = Buffer.from(signature);
bytes[index] = (bytes[index] ?? 0) | 0x80;
expect(() =>
decodeKeyedArchiveBytes(bytes, { offset: 0, limit: 1 }),
).toThrow(
expect.objectContaining({
code: "ERR_ENCODING_INVALID_ENCODED_DATA",
}),
);
}
},
);
it("preserves CF$UID, malformed references and original pagination identities", () => {
const bytes = Buffer.from(
build({
@@ -356,7 +373,7 @@ describe("XML plist nesting preflight", () => {
return cursor;
};
it.each([129, 200, 3000, 4000])(
it.each([129, 4000])(
"rejects %i-level XML nesting as the documented resource limit",
(depth) => {
// Recursive decoding exceeds the call stack before the projection limit
+6 -2
View File
@@ -27,6 +27,10 @@ import { ArtifactReaderFailure } from "../ArtifactReader.js";
import type { ArtifactEntry } from "../ArtifactReader.js";
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
import {
hasBinaryPlistSignature,
hasNibArchiveSignature,
} from "../../domain/apple/appleArchiveFormat.js";
import { estimateBinaryPlistExpansion } from "./InterfaceBuilderDecodeBudget.js";
const MAX_BYTES = 64 * 1024 * 1024;
@@ -40,12 +44,12 @@ export const decodeKeyedArchiveBytes = (
) => {
if (bytes.length > MAX_BYTES)
throw new RangeError("Keyed archive exceeds 64 MiB");
if (bytes.subarray(0, 10).toString("ascii") === "NIBArchive")
if (hasNibArchiveSignature(bytes))
throw new KeyedArchiveKindError(
"Selected file is a compiled NIBArchive, not a Foundation plist archive; decode it with decode_interface_builder",
"nib-archive",
);
const binary = bytes.subarray(0, 8).toString("ascii") === "bplist00";
const binary = hasBinaryPlistSignature(bytes);
const xmlText = binary ? undefined : decodeXmlPlistText(bytes);
// plist.parseBinary recurses on every object reference and copies shared
// containers, so a few hundred bytes can exhaust memory or the stack.
+2 -2
View File
@@ -1,11 +1,11 @@
import type { JsonValue } from "../../domain/jsonValue.js";
import { hasNibArchiveSignature } from "../../domain/apple/appleArchiveFormat.js";
import { TextDecoder } from "node:util";
import {
InterfaceBuilderDecodeBudget,
InterfaceBuilderDecodeBudgetExceeded,
} from "./InterfaceBuilderDecodeBudget.js";
const MAGIC = Buffer.from("NIBArchive", "ascii");
const DEFAULT_DECODE_BUDGET_BYTES = 128 * 1024 * 1024;
interface NibObjectRecord {
@@ -39,7 +39,7 @@ export const decodeNibArchive = (
bytes: Buffer,
budget = new InterfaceBuilderDecodeBudget(DEFAULT_DECODE_BUDGET_BYTES),
): NibArchiveDocument => {
if (bytes.length < 50 || !bytes.subarray(0, MAGIC.length).equals(MAGIC))
if (bytes.length < 50 || !hasNibArchiveSignature(bytes))
throw new TypeError("NIBArchive magic or header is invalid");
const formatVersion = readU32(bytes, 10);
+2 -4
View File
@@ -1,5 +1,6 @@
import type { PlistNumberLiterals } from "../../domain/apple/plistNumbers.js";
import { decodeXmlText } from "../../domain/propertyListKeys.js";
import { hasBinaryPlistSignature } from "../../domain/apple/appleArchiveFormat.js";
const collector = () => {
const integers = new Map<number, Set<string>>();
@@ -124,10 +125,7 @@ export const binaryPlistNumberLiterals = (
bytes: Buffer,
): PlistNumberLiterals => {
const output = collector();
if (
bytes.length < 40 ||
bytes.subarray(0, 8).toString("ascii") !== "bplist00"
)
if (bytes.length < 40 || !hasBinaryPlistSignature(bytes))
return output.result(true);
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const trailer = bytes.length - 32;