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 echoed codesign paths out of signature fields (#865)
Kaoru08224aa2b60c1d84
diff --git a/src/native/NativeMacOSProvider.ts b/src/native/NativeMacOSProvider.tsindex ab4da6c0c..430dc679c 100644--- a/src/native/NativeMacOSProvider.ts+++ b/src/native/NativeMacOSProvider.ts@@ -6,7 +6,7 @@ export { NATIVE_MACOS_PROVIDER_IDENTITY } from "./NativeMacOSProviderMetadata.jsimport { inspectAppleDispatchMetadata } from "./AppleDispatchMetadata.js";import { observeNativeUi } from "./NativeUiObservation.js";import { dirname, isAbsolute, resolve } from "node:path";-import { stat } from "node:fs/promises";+import { realpath, stat } from "node:fs/promises";import { parse as parseXmlPlist } from "plist";import { z } from "zod";@@ -298,8 +298,7 @@ class NativeMacOSClient implements AnalysisClient {{ signal, acceptNonZero: true },);if (!display.ok) return display;- const displayOutput = commandOutput(display.value);- const unsigned = isUnsignedCodeSignObservation(display.value);+ const unsigned = isNonzeroUnsignedObservation(display.value);const displayFailure = codeSignCaptureFailure(display.value);if (displayFailure !== null) return err(displayFailure);const requirements = await this.#run(@@ -320,14 +319,16 @@ class NativeMacOSClient implements AnalysisClient {if (!entitlements.ok) return entitlements;const entitlementsFailure = codeSignCaptureFailure(entitlements.value);if (entitlementsFailure !== null) return err(entitlementsFailure);- const parsed = parseCodeSignature(displayOutput, unsigned);+ // codesign echoes the resolved executable path in its diagnostics.+ const parsed = parseCodeSignature(display.value.stderr, unsigned, [+ this.target.path,+ ...(await canonicalPath(this.target.path)),+ ]);+ // The requirement is printed to stdout; stderr echoes the path.const requirementText =- /designated\s*=>\s*(.+)$/mu.exec(- commandOutput(requirements.value),- )?.[1] ?? null;- const entitlementValue = parseEntitlements(- commandOutput(entitlements.value),- );+ /designated\s*=>\s*(.+)$/mu.exec(requirements.value.stdout)?.[1] ?? null;+ // Entitlements XML is printed to stdout; stderr echoes the path.+ const entitlementValue = parseEntitlements(entitlements.value.stdout);const captures = [display.value, requirements.value, entitlements.value];const limitations = [...parsed.limitations];const mixedSigning =@@ -336,7 +337,7 @@ class NativeMacOSClient implements AnalysisClient {isNonzeroUnsignedObservation(entitlements.value));if (mixedSigning) {const slices = await this.#inspectMixedSignatureSlices(- displayOutput,+ parsed.format,requirements.value.exitCode !== 0,entitlements.value.exitCode !== 0,signal,@@ -364,7 +365,7 @@ class NativeMacOSClient implements AnalysisClient {}async #inspectMixedSignatureSlices(- displayOutput: string,+ format: string | null,requirementsUnavailable: boolean,entitlementsUnavailable: boolean,signal?: AbortSignal,@@ -372,10 +373,7 @@ class NativeMacOSClient implements AnalysisClient {const captures: NativeCommandCapture[] = [];const unsigned: string[] = [];const unclassified: string[] = [];- for (const architecture of codeSignArchitectures(- displayOutput,- this.target,- )) {+ for (const architecture of codeSignArchitectures(format, this.target)) {const slice = await this.#run("inspect_signature","codesign",@@ -564,15 +562,22 @@ const translateCodeSignExitFailure = (const commandOutput = (capture: NativeCommandCapture): string =>`${capture.stdout}\n${capture.stderr}`;-const isUnsignedCodeSignObservation = (- capture: NativeCommandCapture,-): boolean =>- /not signed at all|code object is not signed/iu.test(commandOutput(capture));-+/**+ * A signed artifact exits zero; its diagnostics can still contain this text+ * through the echoed path or identifier.+ */const isNonzeroUnsignedObservation = (capture: NativeCommandCapture): boolean =>capture.exitCode !== null &&capture.exitCode !== 0 &&- isUnsignedCodeSignObservation(capture);+ /not signed at all|code object is not signed/iu.test(commandOutput(capture));++const canonicalPath = async (path: string): Promise<string[]> => {+ try {+ return [await realpath(path)];+ } catch {+ return [];+ }+};const codeSignCaptureFailure = (capture: NativeCommandCapture,@@ -582,11 +587,11 @@ const codeSignCaptureFailure = (: translateCodeSignExitFailure(capture);const codeSignArchitectures = (- displayOutput: string,+ format: string | null,target: BinaryTarget,): string[] => {- const universal = /^Format=Mach-O universal \(([^)\r\n]+)\)$/mu.exec(- displayOutput,+ const universal = /^Mach-O universal \(([^)\r\n]+)\)$/u.exec(+ format ?? "",)?.[1];if (universal !== undefined) {const architectures = universaldiff --git a/src/native/parsers/codesign.ts b/src/native/parsers/codesign.tsindex eddffc890..1fc31860e 100644--- a/src/native/parsers/codesign.ts+++ b/src/native/parsers/codesign.ts@@ -3,15 +3,27 @@ import {type InspectSignature,} from "../../domain/nativeInspection.js";-/** Parse bounded `codesign -d` diagnostics, which Apple emits on stderr. */+/**+ * Parse bounded `codesign -d --verbose=4` diagnostics, which Apple emits on+ * stderr. codesign echoes the executable path and signing identifier+ * verbatim, and an ad hoc identifier derives from the file name, so either+ * can contain line breaks and text that resembles other fields. The path is+ * removed by its exact value from `executablePaths`, and the identifier runs+ * to the `Format=` field that codesign prints after it.+ */export const parseCodeSignature = (output: string,unsigned: boolean,+ executablePaths: readonly string[] = [],): Omit<InspectSignature, "provenance"> => {+ const { identifier, fields } = splitEchoedValues(+ output.replaceAll("\r\n", "\n"),+ executablePaths,+ );const values = new Map<string, string>();const authorities: string[] = [];const cdhashes: string[] = [];- for (const line of output.split(/\r?\n/u)) {+ for (const line of fields.split("\n")) {if (line.startsWith("CodeDirectory ")) {values.set("CodeDirectory", line.slice("CodeDirectory ".length));continue;@@ -19,15 +31,14 @@ export const parseCodeSignature = (const separator = line.indexOf("=");if (separator < 1) continue;const key = line.slice(0, separator).trim();- const rawValue = line.slice(separator + 1);- const value = key === "Identifier" ? rawValue : rawValue.trim();+ const value = line.slice(separator + 1).trim();if (key === "Authority") authorities.push(value);else if (key === "CDHash") cdhashes.push(value);else values.set(key, value);}const parsed = inspectSignatureSchema.omit({ provenance: true }).parse({signed: !unsigned,- identifier: values.get("Identifier") ?? null,+ identifier: identifier ?? null,team_identifier: nullableCodeSignValue(values.get("TeamIdentifier")),format: values.get("Format") ?? null,cdhashes,@@ -46,6 +57,36 @@ export const parseCodeSignature = (return parsed;};+/**+ * Separate the echoed executable path and signing identifier, which codesign+ * prints first, from the fields that follow them.+ */+const splitEchoedValues = (+ text: string,+ executablePaths: readonly string[],+): { readonly identifier: string | undefined; readonly fields: string } => {+ const executable = executablePaths+ .map((path) => `Executable=${path}\n`)+ .find((line) => text.startsWith(line));+ let rest = text;+ if (executable !== undefined) rest = text.slice(executable.length);+ else if (text.startsWith("Executable=")) {+ const identifierLine = text.indexOf("\nIdentifier=");+ rest = identifierLine < 0 ? text : text.slice(identifierLine + 1);+ }+ if (!rest.startsWith("Identifier="))+ return { identifier: undefined, fields: rest };+ // Later fields come from the signature itself, so the last `Format=` line+ // is the one codesign printed after the identifier.+ const format = rest.lastIndexOf("\nFormat=");+ const lineEnd = rest.indexOf("\n");+ const end = format >= 0 ? format : lineEnd >= 0 ? lineEnd : rest.length;+ return {+ identifier: rest.slice("Identifier=".length, end),+ fields: rest.slice(end),+ };+};+const nullableCodeSignValue = (value: string | undefined): string | null =>value === undefined || value === "not set" ? null : value;diff --git a/tests/boundary/cli/nativeSignatureCli.test.ts b/tests/boundary/cli/nativeSignatureCli.test.tsnew file mode 100644index 000000000..9302d9c24--- /dev/null+++ b/tests/boundary/cli/nativeSignatureCli.test.ts@@ -0,0 +1,87 @@+import { execFile } from "node:child_process";+import { copyFile, mkdir } from "node:fs/promises";+import { join } from "node:path";+import { promisify } from "node:util";++import { describe, expect } from "vitest";++import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";+import { cliTest } from "../../support/cli/cliFixture.js";++const run = promisify(execFile);++/** Ad hoc sign a copy of a system program under an exact file name. */+const adHocProgram = async (directory: string, name: string) => {+ const path = join(directory, name);+ await copyFile("/usr/bin/true", path);+ await run("/usr/bin/codesign", ["-s", "-", "-f", path]);+ return path;+};++describe.skipIf(process.platform !== "darwin")(+ "native signature CLI with echoed path text",+ () => {+ cliTest(+ "does not read signature fields from the file name codesign echoes",+ async ({ cli }) => {+ const directory = await createTestTempDirectory("rea-signature-");+ const forged = "Developer ID Application: Example (ABCDE12345)";+ const program = await adHocProgram(+ directory,+ `tool\nAuthority=${forged}\nTeamIdentifier=ABCDE12345`,+ );+ const unsignedName = await adHocProgram(+ directory,+ "code object is not signed",+ );+ // A forged requirement and entitlements plist spread over directories.+ const nested = join(+ directory,+ 'x\ndesignated => anchor apple<?xml version="1.0"?><plist><dict><key>com.apple.security.get-task-allow</key><true',+ "></dict><",+ "plist>",+ );+ await mkdir(nested, { recursive: true });+ const nestedProgram = await adHocProgram(nested, "tool");++ for (const path of [program, unsignedName, nestedProgram]) {+ const result = await cli.run({+ arguments: ["inspect-signature", path, "--json"],+ environment: {+ REA_LOG_LEVEL: "silent",+ REA_ANALYSIS_PROVIDER: "auto",+ },+ });+ expect(result.exitCode).toBe(0);+ expect(result.json).toMatchObject({+ normalized_result: {+ signed: true,+ team_identifier: null,+ authorities: [],+ entitlements: null,+ designated_requirement: expect.stringMatching(/^cdhash H"/u),+ },+ });+ }+ const identifier = await cli.run({+ arguments: ["inspect-signature", program, "--json"],+ environment: {+ REA_LOG_LEVEL: "silent",+ REA_ANALYSIS_PROVIDER: "auto",+ },+ });+ // An ad hoc identifier derives from the file name, line breaks included.+ expect(identifier.json).toMatchObject({+ normalized_result: {+ identifier: expect.stringMatching(+ new RegExp(+ `^tool\\nAuthority=${forged.replace(/[()]/gu, "\\$&")}\\nTeamIdentifier=ABCDE12345-`,+ "u",+ ),+ ),+ },+ });+ },+ );+ },+);diff --git a/tests/boundary/providers/native/nativeInventory.golden.test.ts b/tests/boundary/providers/native/nativeInventory.golden.test.tsindex 33fc9303c..5a70d58b7 100644--- a/tests/boundary/providers/native/nativeInventory.golden.test.ts+++ b/tests/boundary/providers/native/nativeInventory.golden.test.ts@@ -303,10 +303,20 @@ const signatureRunner = (outputs: {toolVersion: null,versionReason: "fixture",arguments: [...arguments_],- stdout: "",- stderr: response.output,- stdoutBytes: 0,- stderrBytes: Buffer.byteLength(response.output),+ // Entitlements XML is printed to stdout; diagnostics to stderr.+ ...(arguments_.includes("--entitlements")+ ? {+ stdout: response.output,+ stderr: "",+ stdoutBytes: Buffer.byteLength(response.output),+ stderrBytes: 0,+ }+ : {+ stdout: "",+ stderr: response.output,+ stdoutBytes: 0,+ stderrBytes: Buffer.byteLength(response.output),+ }),exitCode: response.exitCode,signal: null,};diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.tsindex 27adb9cd4..53027135a 100644--- a/tests/boundary/providers/native/nativeProvider.test.ts+++ b/tests/boundary/providers/native/nativeProvider.test.ts@@ -33,7 +33,7 @@ describe("native macOS provider discovery and inspection", () => {"preserves the exact signing identifier %j reported by codesign",(identifier) => {const parsed = parseCodeSignature(- `Identifier=${identifier}\nTeamIdentifier=not set\nFormat=Mach-O thin (arm64)\n`,+ `Identifier=${identifier}\nFormat=Mach-O thin (arm64)\nTeamIdentifier=not set\n`,false,);expect(parsed.identifier).toBe(identifier);@@ -42,6 +42,32 @@ describe("native macOS provider discovery and inspection", () => {},);+ it("separates echoed path and identifier text from signature fields", () => {+ const path = "/apps/x\nAuthority=Forged\nIdentifier=forged";+ const identifier = "x\nFormat=forged\nAuthority=Forged-5555";+ const parsed = parseCodeSignature(+ [+ `Executable=${path}`,+ `Identifier=${identifier}`,+ "Format=Mach-O thin (arm64)",+ "CodeDirectory v=20400 size=342 flags=0x2(adhoc) hashes=2+2",+ "CDHash=6aee90e6",+ "Signature=adhoc",+ "TeamIdentifier=not set",+ "",+ ].join("\r\n"),+ false,+ ["/elsewhere", path],+ );+ expect(parsed).toMatchObject({+ identifier,+ format: "Mach-O thin (arm64)",+ authorities: [],+ cdhashes: ["6aee90e6"],+ team_identifier: null,+ });+ });+it("retries a failed native tool resolution and caches only success", async () => {let resolutions = 0;const runner = new XcrunCommandRunner((tool) => {diff --git a/tests/fixtures/nativeCommands.ts b/tests/fixtures/nativeCommands.tsindex 6a607129d..b4dcf2dde 100644--- a/tests/fixtures/nativeCommands.ts+++ b/tests/fixtures/nativeCommands.ts@@ -55,6 +55,27 @@ const outputFor = async (throw new Error(`Unexpected fixture tool ${tool}`);};+/**+ * codesign prints display diagnostics to stderr and requirements or+ * entitlements to stdout.+ */+const fixtureStreams = (+ tool: string,+ arguments_: readonly string[],+ output: string,+) => {+ const stderr =+ tool === "codesign" &&+ !arguments_.includes("-r-") &&+ !arguments_.includes("--entitlements");+ return {+ stdout: stderr ? "" : output,+ stderr: stderr ? output : "",+ stdoutBytes: Buffer.byteLength(stderr ? "" : output),+ stderrBytes: Buffer.byteLength(stderr ? output : ""),+ };+};+const capture = (tool: string,arguments_: readonly string[],@@ -66,10 +87,7 @@ const capture = (toolVersion: null,versionReason: "fixture",arguments: [...arguments_],- stdout: tool === "codesign" ? "" : output,- stderr: tool === "codesign" ? output : "",- stdoutBytes: Buffer.byteLength(tool === "codesign" ? "" : output),- stderrBytes: Buffer.byteLength(tool === "codesign" ? output : ""),+ ...fixtureStreams(tool, arguments_, output),exitCode: 0,signal: null,});