fix(apple): preserve archive signatures and metadata byte identity
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
parseXmlPropertyList,
|
||||
} from "../domain/propertyListKeys.js";
|
||||
import { decodeXmlPlistText } from "../domain/propertyListXmlText.js";
|
||||
import { hasBinaryPlistSignature } from "../domain/apple/appleArchiveFormat.js";
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
|
||||
/** Decoded representation bound for one binary Info.plist. */
|
||||
@@ -195,11 +196,10 @@ const resolveLayoutExecutable = async (
|
||||
|
||||
let name: string;
|
||||
try {
|
||||
name =
|
||||
plist.subarray(0, 6).toString("ascii") === "bplist"
|
||||
? await (fileSystem.decodeBinaryPlist?.(plistPath) ??
|
||||
parseBinaryPlistExecutable(plist))
|
||||
: parseXmlPlistExecutable(decodeXmlPlistText(plist));
|
||||
name = hasBinaryPlistSignature(plist)
|
||||
? await (fileSystem.decodeBinaryPlist?.(plistPath) ??
|
||||
parseBinaryPlistExecutable(plist))
|
||||
: parseXmlPlistExecutable(decodeXmlPlistText(plist));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new BinaryTargetError(
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
} from "../domain/rawBinary.js";
|
||||
import type { BinaryArchitecture } from "../domain/binaryTargetTypes.js";
|
||||
import { observeMzWindowsHeader } from "../domain/dosMz.js";
|
||||
import { hasBinaryPlistSignature } from "../domain/apple/appleArchiveFormat.js";
|
||||
import {
|
||||
hasZipSignature,
|
||||
isZipPackageFormat,
|
||||
@@ -295,7 +296,7 @@ const detectArtifactFormat = async (
|
||||
return zipPackageFormatForPath(lower) ?? "zip";
|
||||
}
|
||||
// Binary plists, including keyed archives, often use other extensions.
|
||||
if (observed.bytesRead === 8 && magic.equals(Buffer.from("bplist00")))
|
||||
if (hasBinaryPlistSignature(magic.subarray(0, observed.bytesRead)))
|
||||
return "plist";
|
||||
const named = namedArtifactFormat(lower);
|
||||
if (named !== undefined) return named;
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
|
||||
import { digestCanonicalValue } from "../../domain/canonicalDigest.js";
|
||||
import { hasBinaryPlistSignature } from "../../domain/apple/appleArchiveFormat.js";
|
||||
import {
|
||||
artifactEdgeId,
|
||||
artifactIdForContent,
|
||||
@@ -440,7 +441,7 @@ export const classifyArtifactBytes = (
|
||||
fileSize = prefix.length,
|
||||
): ArtifactNode["format"] => {
|
||||
if (hasZipSignature(prefix)) return "zip";
|
||||
if (prefix.subarray(0, 8).equals(Buffer.from("bplist00"))) return "plist";
|
||||
if (hasBinaryPlistSignature(prefix)) return "plist";
|
||||
const fatFormat = classifyFatArtifact(prefix, fileSize);
|
||||
if (fatFormat !== null) return fatFormat;
|
||||
if (prefix.length >= 4) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
const BINARY_PLIST_MAGIC = [0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30];
|
||||
const NIB_ARCHIVE_MAGIC = [
|
||||
0x4e, 0x49, 0x42, 0x41, 0x72, 0x63, 0x68, 0x69, 0x76, 0x65,
|
||||
];
|
||||
|
||||
/** Recognize the exact binary plist version supported by the decoder, without validating its body. */
|
||||
export const hasBinaryPlistSignature = (bytes: Uint8Array): boolean =>
|
||||
BINARY_PLIST_MAGIC.every((byte, index) => bytes[index] === byte);
|
||||
|
||||
/** Recognize the serialized signature without claiming a valid NIB object table. */
|
||||
export const hasNibArchiveSignature = (bytes: Uint8Array): boolean =>
|
||||
NIB_ARCHIVE_MAGIC.every((byte, index) => bytes[index] === byte);
|
||||
@@ -11,6 +11,23 @@ import { parseMachoLayout } from "./AppleMachoSelection.js";
|
||||
import { decodeAppleDispatchMetadata } from "./AppleDispatchMetadata.js";
|
||||
|
||||
describe("native Mach-O slice selection", () => {
|
||||
it("preserves high-bit section-name bytes instead of ASCII-folding metadata names", () => {
|
||||
const segment = Buffer.alloc(152);
|
||||
segment.writeUInt32LE(0x19, 0);
|
||||
segment.writeUInt32LE(segment.length, 4);
|
||||
segment.writeUInt32LE(1, 64);
|
||||
// `0xdf` becomes `_` under Buffer's ASCII decoder, falsely producing
|
||||
// the known `__objc_classlist` name from a malformed high-bit byte.
|
||||
Buffer.from("__objc_classlist", "ascii").copy(segment, 72);
|
||||
segment[72] = 0xdf;
|
||||
const layout = parseMachoLayout(
|
||||
Buffer.from(machoImage({ commands: [segment] })),
|
||||
"arm64",
|
||||
);
|
||||
|
||||
expect(layout.sections[0]?.name).toBe("ß_objc_classlist");
|
||||
});
|
||||
|
||||
it("accepts a FAT slice whose table identity and embedded header agree", () => {
|
||||
const fat = fatImage([{ cpu: CPU.arm64, bytes: machoImage({}) }]);
|
||||
expect(parseMachoLayout(Buffer.from(fat), "arm64").segments).toEqual([]);
|
||||
|
||||
@@ -260,7 +260,8 @@ const collectSegment = (
|
||||
state.sections.push({
|
||||
name: bytes
|
||||
.subarray(position, position + 16)
|
||||
.toString("ascii")
|
||||
// ASCII decoding masks high bits and can fabricate known metadata names.
|
||||
.toString("latin1")
|
||||
.replace(/\0.*$/u, ""),
|
||||
address: bytes.readBigUInt64LE(position + 32),
|
||||
size: Number(sectionSize),
|
||||
|
||||
Reference in New Issue
Block a user