Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(apple): bound Interface Builder XML plists by shared value depth (#4114)

Kaoru08223776f218e5e5
diff --git a/src/artifacts/apple/InterfaceBuilderAnalysis.ts b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
index 32760e082..c8837f969 100644
--- a/src/artifacts/apple/InterfaceBuilderAnalysis.ts
+++ b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
@@ -84,6 +84,8 @@ export const analyzeInterfaceBuilderBundle = async (input: {
recordOmittedArchive(entry.path, "max_documents");
unreportedOmittedArchives += selection.unreportedOmitted;
for (const entry of selection.selected) {
+ // Plist representation reserved for this document before decoding.
+ let plistReservation = 0;
try {
const remainingInputBytes =
MAX_AGGREGATE_INPUT_BYTES - aggregateInputBytes;
@@ -132,6 +134,7 @@ export const analyzeInterfaceBuilderBundle = async (input: {
decodedReservation.estimatedBytes,
"plist representation exceeds the aggregate Interface Builder decode budget",
);
+ plistReservation = decodedReservation.estimatedBytes;
binaryPrototypeKeyObserved =
decodedReservation.binaryPrototypeKeyObserved;
}
@@ -165,6 +168,9 @@ export const analyzeInterfaceBuilderBundle = async (input: {
} catch (cause: unknown) {
if (input.signal?.aborted === true) throw cause;
if (cause instanceof InterfaceBuilderDecodeBudgetExceeded) {
+ // A document rejected while decoding, e.g. beyond the shared depth
+ // limit, retains no representation; later documents keep its share.
+ decodeBudget.release(plistReservation);
omitted += 1;
recordOmittedArchive(entry.path, cause.reason);
continue;
@@ -700,6 +706,24 @@ const readEntry = async (
return Buffer.concat(chunks, length);
};
+/**
+ * The shared parser bounds plist value nesting before its recursive decode and
+ * reports the limit as a RangeError; that is an omitted archive, not a
+ * malformed one.
+ */
+const parseXmlWithinDecodeBudget = (text: string) => {
+ try {
+ return parseXmlPropertyList(text);
+ } catch (cause: unknown) {
+ if (cause instanceof RangeError)
+ throw new InterfaceBuilderDecodeBudgetExceeded(
+ "aggregate_decode_budget_exhausted",
+ cause.message,
+ );
+ throw cause;
+ }
+};
+
/** Project decoded plist data and dates while retaining omitted-key coverage. */
const decodePlist = (
bytes: Buffer,
@@ -716,7 +740,7 @@ const decodePlist = (
value: parseBinary(bytes),
omittedPrototypeKeys: binaryPrototypeKeyObserved ? null : 0,
}
- : parseXmlPropertyList(xmlText ?? decodeXmlPlistText(bytes));
+ : parseXmlWithinDecodeBudget(xmlText ?? decodeXmlPlistText(bytes));
const projected = projectPlistValue(value);
return {
value: projected.value,
diff --git a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
index 5d143f52a..2eb11bf11 100644
--- a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
+++ b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
@@ -49,6 +49,14 @@ export class InterfaceBuilderDecodeBudget {
this.#remainingBytes -= bytes;
}
+ /** Return a reservation whose representation was rejected and not retained. */
+ release(bytes: number): void {
+ this.#remainingBytes = Math.min(
+ this.#initialBytes,
+ this.#remainingBytes + bytes,
+ );
+ }
+
/** Reserve an upper bound for one JSON-serialized string occurrence. */
reserveJsonStringOccurrence(value: string): void {
// JSON escaping uses at most six ASCII bytes per UTF-16 code unit, plus
@@ -117,7 +125,8 @@ const estimateXmlPlistExpansion = (
const xml = xmlText ?? decodeXmlPlistText(bytes);
const decodedSourceBytes = xml.length * 2;
let elementCount = 0;
- let xmlDepth = 0;
+ // Value nesting is bounded by the shared plist parser, which counts plist
+ // value depth; raw element depth would also count the envelope and keys.
for (const token of xmlPreflightTokens(xml)) {
if (token.doctype) {
// Only a declaration outside opaque text can introduce an internal
@@ -130,15 +139,6 @@ const estimateXmlPlistExpansion = (
continue;
}
elementCount += 1;
- if (token.closing) xmlDepth -= 1;
- else if (!token.selfClosing) {
- xmlDepth += 1;
- if (xmlDepth > 128)
- throw new InterfaceBuilderDecodeBudgetExceeded(
- "aggregate_decode_budget_exhausted",
- "XML plist nesting exceeds the aggregate Interface Builder decode budget",
- );
- }
if (decodedSourceBytes * 2 + elementCount * 128 > maxBytes)
return maxBytes + 1;
}
@@ -179,13 +179,7 @@ function* xmlPreflightTokens(xml: string) {
}
if (end === xml.length) return;
cursor = end + 1;
- if (doctype || element)
- yield {
- doctype,
- internalSubset,
- closing: xml[start + 1] === "/",
- selfClosing: xml[end - 1] === "/",
- };
+ if (doctype || element) yield { doctype, internalSubset };
}
}
diff --git a/tests/boundary/filesystem/interfaceBuilderBundle.test.ts b/tests/boundary/filesystem/interfaceBuilderBundle.test.ts
index 1b2954b7b..a418c8f0b 100644
--- a/tests/boundary/filesystem/interfaceBuilderBundle.test.ts
+++ b/tests/boundary/filesystem/interfaceBuilderBundle.test.ts
@@ -4,9 +4,10 @@ import { join } from "node:path";
import { promisify } from "node:util";
import { buildBinary } from "plist";
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { analyzeInterfaceBuilderBundle } from "../../../src/artifacts/apple/InterfaceBuilderAnalysis.js";
+import { InterfaceBuilderDecodeBudget } from "../../../src/artifacts/apple/InterfaceBuilderDecodeBudget.js";
import { encodeNibArchiveFixture } from "../../../src/artifacts/apple/NibArchive.fixture.js";
import { thinMach } from "../../../src/domain/binaryTarget.fixture.js";
import { xcodeIbtoolAvailable } from "../../fixtures/ibtool.js";
@@ -907,6 +908,57 @@ describe("aggregate Interface Builder archive retention budget", () => {
});
});
+describe("Interface Builder XML plist depth", () => {
+ it("bounds XML archives by plist value depth, not element depth", async () => {
+ const root = await createTestTempDirectory("rea-ib-depth-test-");
+ const bundle = join(root, "Example.app");
+ const resources = join(bundle, "Contents", "Resources");
+ await mkdir(resources, { recursive: true });
+ // The root dict is value depth 0, so the leaf sits at `nested + 1`. The
+ // plist envelope and keys add XML elements but not value depth.
+ const document = (nested: number) =>
+ `<?xml version="1.0"?><plist version="1.0"><dict>${"<key>child</key><dict>".repeat(nested)}<key>leaf</key><integer>1</integer>${"</dict>".repeat(nested)}</dict></plist>`;
+ await writeFile(join(resources, "AtLimit.nib"), document(127));
+ await writeFile(join(resources, "BeyondLimit.nib"), document(128));
+
+ const release = vi.spyOn(InterfaceBuilderDecodeBudget.prototype, "release");
+ const result = await analyzeInterfaceBuilderBundle({
+ bundlePath: bundle,
+ targetSha256: "e".repeat(64),
+ });
+ const released = release.mock.calls.map(([bytes]) => bytes);
+ release.mockRestore();
+
+ // The rejected archive returns its reservation to later documents.
+ expect(released).toHaveLength(1);
+ expect(released[0]).toBeGreaterThan(0);
+ expect(result.documents.map(({ relative_path }) => relative_path)).toEqual([
+ "Contents/Resources/AtLimit.nib",
+ ]);
+ expect(result.graph.coverage).toContainEqual(
+ expect.objectContaining({
+ facet: "archive_decode",
+ status: "partial",
+ reason: "aggregate_decode_budget_exhausted",
+ examined: 2,
+ omitted: 1,
+ }),
+ );
+ expect(result.limitations).toContain(
+ "Contents/Resources/BeyondLimit.nib: omitted because aggregate decode budget exhausted.",
+ );
+ });
+
+ it("restores released representation bytes up to the initial budget", () => {
+ const budget = new InterfaceBuilderDecodeBudget(100);
+ budget.reserve(60, "reserve");
+ budget.release(60);
+ expect(budget.remainingBytes).toBe(100);
+ budget.release(10);
+ expect(budget.remainingBytes).toBe(100);
+ });
+});
+
describe("bounded Interface Builder archive decoding", () => {
it("counts malformed archives against the document limit", async () => {
const root = await createTestTempDirectory("rea-ib-test-");