From d575a7340bb19b84733c79b14de9f4df7181c6b3 Mon Sep 17 00:00:00 2001
From: morluto <76467478+morluto@users.noreply.github.com>
Date: Fri, 9 Oct 2026 05:50:49 +0800
Subject: [PATCH] fix(apple): decode bundle plist bytes before executable
lookup
---
docs/native-investigation.md | 7 +-
src/application/AppBundleExecutable.ts | 5 +-
.../apple/InterfaceBuilderAnalysis.ts | 2 +-
.../apple/InterfaceBuilderDecodeBudget.ts | 2 +-
src/artifacts/apple/KeyedArchiveReader.ts | 2 +-
.../propertyListXmlText.ts} | 4 +-
.../applications/appBundleEncodingCli.test.ts | 188 ++++++++++++++++++
7 files changed, 200 insertions(+), 10 deletions(-)
rename src/{artifacts/apple/XmlPropertyListText.ts => domain/propertyListXmlText.ts} (90%)
create mode 100644 tests/acceptance/applications/appBundleEncodingCli.test.ts
diff --git a/docs/native-investigation.md b/docs/native-investigation.md
index 4a98b90d8..753ebdb3e 100644
--- a/docs/native-investigation.md
+++ b/docs/native-investigation.md
@@ -59,11 +59,14 @@ complete observation.
a source-owned AppKit XIB with `ibtool`. Storyboards require an installed
iOS platform; unsupported archive forms remain explicit.
XML plist archives accept UTF-8 and BOM-marked UTF-16 in either byte order.
- The shared keyed-archive and Interface Builder decoder consumes an initial
+ App bundle `Info.plist` executable resolution uses the same decoder as keyed
+ archives and Interface Builder. The decoder consumes an initial
BOM and rejects malformed byte sequences, unsupported encoding declarations,
and declarations that disagree with the detected encoding. It never substitutes
UTF-8 for a declared encoding it cannot process. Archive and evidence digests
- still identify the original serialized bytes. Interface Builder decoding
+ still identify the original serialized bytes. Bundle resolution reports
+ malformed bytes and unsupported encodings against the original plist path
+ before looking up the executable. Interface Builder decoding
limits aggregate input archives to 32 MiB.
Before decoding, it also bounds the serialized structure: expanded binary
plist references, NIB table records, XML elements and text, plus their
diff --git a/src/application/AppBundleExecutable.ts b/src/application/AppBundleExecutable.ts
index 06d2251a6..7df3ca03a 100644
--- a/src/application/AppBundleExecutable.ts
+++ b/src/application/AppBundleExecutable.ts
@@ -8,6 +8,7 @@ import { z } from "zod";
import { BinaryTargetError } from "../domain/configurationErrors.js";
import { isPathWithinRoot } from "../domain/localPath.js";
import { parseXmlPropertyList } from "../domain/propertyListKeys.js";
+import { decodeXmlPlistText } from "../domain/propertyListXmlText.js";
import { err, ok, type Result } from "../domain/result.js";
const execFileAsync = promisify(execFile);
@@ -194,7 +195,7 @@ const resolveLayoutExecutable = async (
? await (fileSystem.decodeBinaryPlist ?? readBinaryPlistExecutable)(
plistPath,
)
- : parseXmlPlistExecutable(plist.toString("utf8"));
+ : parseXmlPlistExecutable(decodeXmlPlistText(plist));
} catch (cause: unknown) {
return err(
new BinaryTargetError(
@@ -203,7 +204,7 @@ const resolveLayoutExecutable = async (
? `permission denied decoding app Info.plist ${plistPath}${codeDescription(cause)}`
: isSystemErrorCode(cause)
? `could not decode app Info.plist ${plistPath}${codeDescription(cause)}`
- : `app Info.plist is malformed or lacks CFBundleExecutable: ${plistPath}`,
+ : `app Info.plist is malformed, unsupported, or lacks CFBundleExecutable: ${plistPath}${cause instanceof Error ? ` (${cause.message})` : ""}`,
{ cause },
),
);
diff --git a/src/artifacts/apple/InterfaceBuilderAnalysis.ts b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
index 00b19fe15..f8358d3a8 100644
--- a/src/artifacts/apple/InterfaceBuilderAnalysis.ts
+++ b/src/artifacts/apple/InterfaceBuilderAnalysis.ts
@@ -26,7 +26,7 @@ import {
} from "./NibViewHierarchy.js";
import { jsonValueSchema, type JsonValue } from "../../domain/jsonValue.js";
-import { decodeXmlPlistText } from "./XmlPropertyListText.js";
+import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
const MAX_AGGREGATE_INPUT_BYTES = 32 * 1024 * 1024;
const MAX_AGGREGATE_DECODE_BYTES = 256 * 1024 * 1024;
diff --git a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
index 6ec1c82f1..58a9b1ce3 100644
--- a/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
+++ b/src/artifacts/apple/InterfaceBuilderDecodeBudget.ts
@@ -1,4 +1,4 @@
-import { decodeXmlPlistText } from "./XmlPropertyListText.js";
+import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
/** Reason reported when an archive would exceed the shared decoding budget. */
export type InterfaceBuilderBudgetReason = "aggregate_decode_budget_exhausted";
diff --git a/src/artifacts/apple/KeyedArchiveReader.ts b/src/artifacts/apple/KeyedArchiveReader.ts
index 795f5b126..54401c4a1 100644
--- a/src/artifacts/apple/KeyedArchiveReader.ts
+++ b/src/artifacts/apple/KeyedArchiveReader.ts
@@ -16,7 +16,7 @@ import {
import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js";
import { ArtifactReaderFailure } from "../ArtifactReader.js";
-import { decodeXmlPlistText } from "./XmlPropertyListText.js";
+import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
const MAX_BYTES = 64 * 1024 * 1024;
diff --git a/src/artifacts/apple/XmlPropertyListText.ts b/src/domain/propertyListXmlText.ts
similarity index 90%
rename from src/artifacts/apple/XmlPropertyListText.ts
rename to src/domain/propertyListXmlText.ts
index 8fb472d04..2d6820120 100644
--- a/src/artifacts/apple/XmlPropertyListText.ts
+++ b/src/domain/propertyListXmlText.ts
@@ -1,7 +1,5 @@
-import { TextDecoder } from "node:util";
-
/** Decode BOM-aware XML plist text with fatal byte validation. */
-export const decodeXmlPlistText = (bytes: Buffer): string => {
+export const decodeXmlPlistText = (bytes: Uint8Array): string => {
const encoding =
bytes[0] === 0xff && bytes[1] === 0xfe
? "utf-16le"
diff --git a/tests/acceptance/applications/appBundleEncodingCli.test.ts b/tests/acceptance/applications/appBundleEncodingCli.test.ts
new file mode 100644
index 000000000..e19ac7333
--- /dev/null
+++ b/tests/acceptance/applications/appBundleEncodingCli.test.ts
@@ -0,0 +1,188 @@
+import { mkdir, writeFile } from "node:fs/promises";
+import { join, resolve } from "node:path";
+
+import { Client } from "@modelcontextprotocol/client";
+import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
+import { describe, expect, onTestFinished } from "vitest";
+
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+const encodingFixture = () => {
+ const xml =
+ 'CFBundleExecutableAppCFBundleIdentifierdev.rea.encoding';
+ const utf16 = Buffer.from(xml, "utf16le");
+ const utf8 = xml.replace("UTF-16", "UTF-8");
+ const cases = [
+ {
+ name: "UTF-8 BOM",
+ bytes: Buffer.concat([
+ Buffer.from([0xef, 0xbb, 0xbf]),
+ Buffer.from(utf8),
+ ]),
+ valid: true,
+ },
+ {
+ name: "UTF-16 LE",
+ bytes: Buffer.concat([Buffer.from([0xff, 0xfe]), utf16]),
+ valid: true,
+ },
+ {
+ name: "UTF-16 BE",
+ bytes: Buffer.concat([
+ Buffer.from([0xfe, 0xff]),
+ Buffer.from(utf16).swap16(),
+ ]),
+ valid: true,
+ },
+ {
+ name: "unsupported declaration",
+ bytes: Buffer.from(utf8.replace("UTF-8", "Shift_JIS")),
+ valid: false,
+ },
+ {
+ name: "declaration mismatch",
+ bytes: Buffer.from(xml),
+ valid: false,
+ },
+ {
+ name: "truncated UTF-16",
+ bytes: Buffer.concat([Buffer.from([0xff, 0xfe]), utf16.subarray(0, -1)]),
+ valid: false,
+ },
+ {
+ name: "malformed UTF-8 executable",
+ bytes: Buffer.concat([
+ Buffer.from(utf8.split("App")[0] + ""),
+ Buffer.from([0xff]),
+ Buffer.from("" + utf8.split("App")[1]),
+ ]),
+ valid: false,
+ },
+ ];
+ return { utf16, cases };
+};
+
+describe.skipIf(process.platform !== "darwin")(
+ "app bundle XML byte boundaries",
+ () => {
+ cliTest(
+ "resolves real compiled bundles without changing executable names during decoding",
+ async ({ cli, processes }) => {
+ const directory = await createTestTempDirectory("rea-bundle-encoding-");
+ const app = join(directory, "Example.app");
+ const contents = join(app, "Contents");
+ await mkdir(join(contents, "MacOS"), { recursive: true });
+ const compiled = await processes.run("/usr/bin/xcrun", [
+ "clang",
+ resolve("tests/conformance/interface-builder/fixture.c"),
+ "-o",
+ join(contents, "MacOS", "App"),
+ ]);
+ expect(compiled.exitCode, compiled.stderr).toBe(0);
+ const { utf16, cases } = encodingFixture();
+ for (const { name, bytes, valid } of cases) {
+ await writeFile(join(contents, "Info.plist"), bytes);
+ const result = await cli.run({
+ arguments: ["inspect-plist", app, "--json"],
+ environment: {
+ REA_LOG_LEVEL: "silent",
+ REA_ANALYSIS_PROVIDER: "auto",
+ },
+ });
+ expect(
+ result.exitCode,
+ `${name}: ${result.stdout}\n${result.stderr}`,
+ ).toBe(valid ? 0 : 1);
+ if (valid)
+ expect(result.json).toMatchObject({
+ normalized_result: {
+ value: {
+ CFBundleExecutable: "App",
+ CFBundleIdentifier: "dev.rea.encoding",
+ },
+ bundle: { executable: "App" },
+ },
+ });
+ else
+ expect(result.json).toMatchObject({
+ error: "Analysis failed",
+ details: {
+ path: join(contents, "Info.plist"),
+ reason: expect.stringContaining(
+ "app Info.plist is malformed, unsupported, or lacks CFBundleExecutable",
+ ),
+ },
+ });
+ }
+ const transport = new StdioClientTransport({
+ command: process.execPath,
+ args: [resolve("scripts/rea.mjs"), "mcp"],
+ env: {
+ PATH: process.env.PATH ?? "",
+ REA_LOG_LEVEL: "silent",
+ REA_ANALYSIS_PROVIDER: "auto",
+ },
+ stderr: "pipe",
+ });
+ const client = new Client({
+ name: "bundle-encoding-parity",
+ version: "1",
+ });
+ onTestFinished(async () => {
+ try {
+ await client.close();
+ } finally {
+ await transport.close();
+ }
+ });
+ await client.connect(transport);
+ await writeFile(
+ join(contents, "Info.plist"),
+ Buffer.concat([
+ Buffer.from([0xfe, 0xff]),
+ Buffer.from(utf16).swap16(),
+ ]),
+ );
+ const opened = await client.callTool({
+ name: "open_binary",
+ arguments: { path: app },
+ });
+ expect(opened.isError, JSON.stringify(opened)).not.toBe(true);
+ const inspected = await client.callTool({
+ name: "inspect_plist",
+ arguments: {},
+ });
+ expect(inspected.isError, JSON.stringify(inspected)).not.toBe(true);
+ expect(inspected.structuredContent).toMatchObject({
+ result: {
+ value: {
+ CFBundleExecutable: "App",
+ CFBundleIdentifier: "dev.rea.encoding",
+ },
+ },
+ });
+ await writeFile(
+ join(contents, "Info.plist"),
+ Buffer.concat([Buffer.from([0xff, 0xfe]), utf16.subarray(0, -1)]),
+ );
+ const invalid = await client.callTool({
+ name: "open_binary",
+ arguments: { path: app },
+ });
+ expect(invalid.isError).toBe(true);
+ expect(invalid.structuredContent).toMatchObject({
+ error: {
+ details: {
+ path: join(contents, "Info.plist"),
+ reason: expect.stringContaining(
+ "app Info.plist is malformed, unsupported, or lacks CFBundleExecutable",
+ ),
+ },
+ },
+ });
+ },
+ 45_000,
+ );
+ },
+);