fix(apple): decode bundle plist bytes before executable lookup

This commit is contained in:
morluto
2026-10-09 06:32:09 +08:00
parent 6495171812
commit d575a7340b
7 changed files with 200 additions and 10 deletions
+5 -2
View File
@@ -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
+3 -2
View File
@@ -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 },
),
);
@@ -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;
@@ -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";
+1 -1
View File
@@ -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;
@@ -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"
@@ -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 =
'<?xml version="1.0" encoding="UTF-16"?><plist version="1.0"><dict><key>CFBundleExecutable</key><string>App</string><key>CFBundleIdentifier</key><string>dev.rea.encoding</string></dict></plist>';
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("<string>App</string>")[0] + "<string>"),
Buffer.from([0xff]),
Buffer.from("</string>" + utf8.split("<string>App</string>")[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,
);
},
);