Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

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

morlutodc038d16bba9
diff --git a/src/application/AppBundleExecutable.ts b/src/application/AppBundleExecutable.ts
index 12624ca02..7ab79a3cf 100644
--- a/src/application/AppBundleExecutable.ts
+++ b/src/application/AppBundleExecutable.ts
@@ -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(
diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.ts
index 5f45cf9d6..ac691b55e 100644
--- a/src/application/BinaryTargetResolver.ts
+++ b/src/application/BinaryTargetResolver.ts
@@ -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;
diff --git a/src/artifacts/apple/InterfaceBuilderAnalysis.ts b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
index 20af7a3cb..fb90594b3 100644
--- a/src/artifacts/apple/InterfaceBuilderAnalysis.ts
+++ b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
@@ -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),
diff --git a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
index 4dc3d6c64..8469d93f0 100644
--- a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
+++ b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
@@ -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",
diff --git a/src/artifacts/apple/KeyedArchiveReader.test.ts b/src/artifacts/apple/KeyedArchiveReader.test.ts
index 027bbcd3f..b619788a1 100644
--- a/src/artifacts/apple/KeyedArchiveReader.test.ts
+++ b/src/artifacts/apple/KeyedArchiveReader.test.ts
@@ -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
diff --git a/src/artifacts/apple/KeyedArchiveReader.ts b/src/artifacts/apple/KeyedArchiveReader.ts
index 5d2f8fab5..7bd64511e 100644
--- a/src/artifacts/apple/KeyedArchiveReader.ts
+++ b/src/artifacts/apple/KeyedArchiveReader.ts
@@ -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.
diff --git a/src/artifacts/apple/NibArchive.ts b/src/artifacts/apple/NibArchive.ts
index 79b8a0d05..bd8ef12ca 100644
--- a/src/artifacts/apple/NibArchive.ts
+++ b/src/artifacts/apple/NibArchive.ts
@@ -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);
diff --git a/src/artifacts/apple/PlistNumberLiterals.ts b/src/artifacts/apple/PlistNumberLiterals.ts
index b491a7274..6fec67c74 100644
--- a/src/artifacts/apple/PlistNumberLiterals.ts
+++ b/src/artifacts/apple/PlistNumberLiterals.ts
@@ -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;
diff --git a/src/artifacts/inventory/ArtifactGraphConstruction.ts b/src/artifacts/inventory/ArtifactGraphConstruction.ts
index 35bd2f22d..b7b194744 100644
--- a/src/artifacts/inventory/ArtifactGraphConstruction.ts
+++ b/src/artifacts/inventory/ArtifactGraphConstruction.ts
@@ -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) {
diff --git a/src/domain/apple/appleArchiveFormat.ts b/src/domain/apple/appleArchiveFormat.ts
new file mode 100644
index 000000000..e831266f1
--- /dev/null
+++ b/src/domain/apple/appleArchiveFormat.ts
@@ -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);
diff --git a/src/native/AppleMachoSelection.test.ts b/src/native/AppleMachoSelection.test.ts
index 7344b1b6a..5c19026e0 100644
--- a/src/native/AppleMachoSelection.test.ts
+++ b/src/native/AppleMachoSelection.test.ts
@@ -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([]);
diff --git a/src/native/AppleMachoSelection.ts b/src/native/AppleMachoSelection.ts
index bfc7a37d6..9ee93f995 100644
--- a/src/native/AppleMachoSelection.ts
+++ b/src/native/AppleMachoSelection.ts
@@ -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),