Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(native): keep the resolved bundle plist and permission failures
Kaoru0822418a5562f6f6
diff --git a/src/application/AppBundleExecutable.ts b/src/application/AppBundleExecutable.tsindex 36e4452f6..f3e8b30c3 100644--- a/src/application/AppBundleExecutable.ts+++ b/src/application/AppBundleExecutable.ts@@ -1,5 +1,5 @@import { execFile } from "node:child_process";-import { lstat, readdir, readFile, realpath } from "node:fs/promises";+import { lstat, readdir, readFile, realpath, stat } from "node:fs/promises";import { join } from "node:path";import { promisify } from "node:util";@@ -30,6 +30,12 @@ const BUNDLE_LAYOUTS: readonly BundleLayout[] = [{ plist: ["Info.plist"], programs: [], programsLabel: "the bundle root" },];+/** A resolved bundle program file and the Info.plist that declared it. */+export interface ResolvedAppBundle {+ readonly executable: string;+ readonly infoPlist?: string;+}+/*** Resolve an app bundle directory to its declared program file. macOS and* flat iOS-style layouts are read directly. An iOS app installed on a Mac is@@ -38,7 +44,7 @@ const BUNDLE_LAYOUTS: readonly BundleLayout[] = [*/export const resolveAppBundleExecutable = async (path: string,-): Promise<Result<string, BinaryTargetError>> => {+): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {const layout = await presentLayout(path);if (layout !== undefined) return resolveLayoutExecutable(path, layout);const wrapped = await wrappedBundle(path);@@ -60,40 +66,52 @@ const presentLayout = async (bundle: string,): Promise<BundleLayout | undefined> => {for (const layout of BUNDLE_LAYOUTS)- if (await isRegularFile(join(bundle, ...layout.plist))) return layout;+ if (await layoutPlistPresent(join(bundle, ...layout.plist))) return layout;return undefined;};-const isRegularFile = async (path: string): Promise<boolean> => {+/**+ * Whether a layout's Info.plist exists. Readable symlinks count, as they did+ * when the plist was read directly; any failure other than absence, such as+ * a permission denial, is left for the caller to report.+ */+const layoutPlistPresent = async (path: string): Promise<boolean> => {try {- return (await lstat(path)).isFile();- } catch {- return false;+ return (await stat(path)).isFile();+ } catch (cause: unknown) {+ if (isAbsence(cause)) return false;+ throw cause;}};+const isAbsence = (cause: unknown): boolean => {+ const code: unknown =+ cause instanceof Error ? Reflect.get(cause, "code") : undefined;+ return code === "ENOENT" || code === "ENOTDIR";+};+/** The single real `.app` directory inside an iOS-on-Mac `Wrapper`. */const wrappedBundle = async (bundle: string): Promise<string | undefined> => {const wrapper = join(bundle, "Wrapper");try {if (!(await lstat(wrapper)).isDirectory()) return undefined;- const apps = (await readdir(wrapper, { withFileTypes: true })).filter(- (entry) =>- entry.isDirectory() && entry.name.toLowerCase().endsWith(".app"),- );- const [app] = apps;- return apps.length === 1 && app !== undefined- ? join(wrapper, app.name)- : undefined;- } catch {- return undefined;+ } catch (cause: unknown) {+ if (isAbsence(cause)) return undefined;+ throw cause;}+ const apps = (await readdir(wrapper, { withFileTypes: true })).filter(+ (entry) => entry.isDirectory() && entry.name.toLowerCase().endsWith(".app"),+ );+ const [app] = apps;+ return apps.length === 1 && app !== undefined+ ? join(wrapper, app.name)+ : undefined;};const resolveLayoutExecutable = async (bundle: string,layout: BundleLayout,-): Promise<Result<string, BinaryTargetError>> => {+): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {const plistPath = join(bundle, ...layout.plist);let name: string;try {@@ -127,7 +145,7 @@ const resolveLayoutExecutable = async (`app program file leaves ${layout.programsLabel}`,),);- return ok(canonicalExecutable);+ return ok({ executable: canonicalExecutable, infoPlist: plistPath });} catch (cause: unknown) {return err(new BinaryTargetError(bundle, "app program file is missing", { cause }),diff --git a/src/application/BinaryTargetResolver.ts b/src/application/BinaryTargetResolver.tsindex 564a867af..938633e9b 100644--- a/src/application/BinaryTargetResolver.ts+++ b/src/application/BinaryTargetResolver.ts@@ -12,7 +12,10 @@ import { extname, isAbsolute, resolve } from "node:path";import { BinaryTargetError } from "../domain/configurationErrors.js";import type { BinaryTarget } from "../domain/binaryTargetTypes.js";import { err, ok, type Result } from "../domain/result.js";-import { resolveAppBundleExecutable } from "./AppBundleExecutable.js";+import {+ resolveAppBundleExecutable,+ type ResolvedAppBundle,+} from "./AppBundleExecutable.js";import {validateDosComLength,type ExecutableFormatHint,@@ -47,7 +50,9 @@ export const parseBinaryTarget = async (const canonical = await realpath(candidate);const resolved = await resolveAppBundle(canonical);if (!resolved.ok) return err(resolved.error);- const path = resolved.value;+ const { executable: path, infoPlist } = resolved.value;+ const bundle =+ infoPlist === undefined ? {} : { bundleInfoPlist: infoPlist };const handle = await open(path, "r");try {if (!(await handle.stat()).isFile())@@ -99,6 +104,7 @@ export const parseBinaryTarget = async (return ok({path,sourcePath: process.platform === "win32" ? candidate : canonical,+ ...bundle,sha256: await sha256Handle(handle),kind: "executable",...detected.value,@@ -208,9 +214,9 @@ const sha256Handle = async (handle: FileHandle): Promise<string> => {const resolveAppBundle = async (path: string,-): Promise<Result<string, BinaryTargetError>> => {+): Promise<Result<ResolvedAppBundle, BinaryTargetError>> => {const metadata = await stat(path);- if (!metadata.isDirectory()) return ok(path);+ if (!metadata.isDirectory()) return ok({ executable: path });if (extname(path).toLowerCase() !== ".app")return err(new BinaryTargetError(diff --git a/src/domain/binaryTargetTypes.ts b/src/domain/binaryTargetTypes.tsindex 9083b13c1..edb914706 100644--- a/src/domain/binaryTargetTypes.ts+++ b/src/domain/binaryTargetTypes.ts@@ -4,6 +4,8 @@ export type BinaryArchitecture = "x86" | "x86_64" | "arm" | "arm64";interface BinaryTargetIdentity {readonly path: string;readonly sourcePath?: string;+ /** Info.plist of the app bundle the target was resolved from. */+ readonly bundleInfoPlist?: string;readonly sha256: string;}diff --git a/src/native/NativeMacOSProvider.ts b/src/native/NativeMacOSProvider.tsindex 584b511f7..0d832d5e7 100644--- a/src/native/NativeMacOSProvider.ts+++ b/src/native/NativeMacOSProvider.ts@@ -713,14 +713,6 @@ const requireRegularPlist = async (}};-const isFile = async (path: string): Promise<boolean> => {- try {- return (await stat(path)).isFile();- } catch {- return false;- }-};-const resolvePlistPath = async (target: BinaryTarget,requested: string | undefined,@@ -732,17 +724,9 @@ const resolvePlistPath = async (const sourceMetadata = await stat(source);const root = sourceMetadata.isDirectory() ? source : dirname(target.path);if (requested !== undefined) return ok(resolve(root, requested));- const macOS = resolve(root, "Contents/Info.plist");- // iOS-style bundles, including the one inside an iOS-on-Mac wrapper, keep- // Info.plist beside the program file the target resolved to.- const beside = resolve(dirname(target.path), "Info.plist");- return ok(- sourceMetadata.isDirectory() &&- !(await isFile(macOS)) &&- (await isFile(beside))- ? beside- : macOS,- );+ // A bundle target records the Info.plist its program file was declared+ // in: Contents/Info.plist, or the root plist of an iOS-style bundle.+ return ok(target.bundleInfoPlist ?? resolve(root, "Contents/Info.plist"));} catch (cause: unknown) {return err(new ProviderAdapterError(IDENTITY.id, "inspect_plist", { cause }),diff --git a/tests/boundary/filesystem/binaryTarget.test.ts b/tests/boundary/filesystem/binaryTarget.test.tsindex 7096fcdce..979904547 100644--- a/tests/boundary/filesystem/binaryTarget.test.ts+++ b/tests/boundary/filesystem/binaryTarget.test.ts@@ -1,5 +1,5 @@import { execFile } from "node:child_process";-import { mkdir, realpath, symlink, writeFile } from "node:fs/promises";+import { chmod, mkdir, realpath, symlink, writeFile } from "node:fs/promises";import { join } from "node:path";import { promisify } from "node:util";@@ -306,10 +306,70 @@ describe("iOS-style app bundle targets", () => {const result = await parseBinaryTarget(app, directory, "arm64");expect(result.ok && result.value).toMatchObject({path: await realpath(join(app, "Flat")),+ bundleInfoPlist: join(await realpath(app), "Info.plist"),format: "mach-o",});});+ it.skipIf(process.platform === "win32")(+ "keeps the declaring Info.plist for a symlinked program file",+ async () => {+ const directory = await createTestTempDirectory("rea-target-");+ const app = join(directory, "Linked.app");+ await mkdir(join(app, "bin"), { recursive: true });+ await writeFile(join(app, "Info.plist"), plist("Linked"));+ await writeFile(+ join(app, "bin", "real"),+ thinMach(0xfeedfacf, 0x0100000c),+ );+ await symlink("bin/real", join(app, "Linked"));+ const result = await parseBinaryTarget(app, directory, "arm64");+ expect(result.ok && result.value).toMatchObject({+ path: await realpath(join(app, "bin", "real")),+ bundleInfoPlist: join(await realpath(app), "Info.plist"),+ });+ },+ );++ it.skipIf(process.platform === "win32")(+ "follows a readable Contents/Info.plist symlink",+ async () => {+ const directory = await createTestTempDirectory("rea-target-");+ const app = join(directory, "Linked.app");+ const contents = join(app, "Contents");+ await mkdir(join(contents, "MacOS"), { recursive: true });+ await writeFile(join(contents, "Real.plist"), plist("Linked"));+ await symlink("Real.plist", join(contents, "Info.plist"));+ await writeFile(+ join(contents, "MacOS", "Linked"),+ thinMach(0xfeedfacf, 0x0100000c),+ );+ const result = await parseBinaryTarget(app, directory, "arm64");+ expect(result.ok && result.value).toMatchObject({+ path: await realpath(join(contents, "MacOS", "Linked")),+ });+ },+ );++ it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(+ "reports a wrapper it cannot read as a permission denial",+ async () => {+ const directory = await createTestTempDirectory("rea-target-");+ const app = join(directory, "Locked.app");+ const wrapper = join(app, "Wrapper");+ await mkdir(join(wrapper, "Locked.app"), { recursive: true });+ await chmod(wrapper, 0o000);+ try {+ const result = await parseBinaryTarget(app, directory, "arm64");+ expect(result.ok).toBe(false);+ if (result.ok) throw new Error("Expected a permission denial");+ expect(result.error.message).toContain("permission denied");+ } finally {+ await chmod(wrapper, 0o755);+ }+ },+ );+it.skipIf(process.platform === "win32")("resolves an iOS-on-Mac wrapper through its single Wrapper bundle",async () => {diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.tsindex ed86df0e0..0fb8276cc 100644--- a/tests/boundary/providers/native/nativeProvider.test.ts+++ b/tests/boundary/providers/native/nativeProvider.test.ts@@ -279,7 +279,7 @@ describe("native macOS provider inspection", () => {});describe("native plist defaults for iOS-style bundles", () => {- it("defaults to the Info.plist beside a flat bundle's program file", async () => {+ it("defaults to the Info.plist the bundle program was resolved from", async () => {directory = await createTestTempDirectory("rea-native-flat-");const app = join(directory, "Flat.app");const executable = join(app, "Flat");@@ -289,7 +289,10 @@ describe("native plist defaults for iOS-style bundles", () => {const client = new NativeMacOSProvider(new FixtureRunner(),"darwin",- ).createClient(machoTarget(executable, app));+ ).createClient({+ ...machoTarget(executable, app),+ bundleInfoPlist: join(app, "Info.plist"),+ });const plist = await client.execute("inspect_plist", {});expect(plist.ok && plist.value.result).toMatchObject({