fix(native): keep echoed codesign paths out of signature fields (#865)

codesign echoes the executable path and signing identifier verbatim,
and an ad hoc identifier derives from the file name. A file name with
line breaks added forged Authority lines, a signed file named after
codesign's unsigned message was reported unsigned, and path text could
supply entitlements. Parse display fields after the exact echoed path
and identifier, classify unsigned output by exit status, and read
requirements and entitlements from stdout.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 13:20:12 +08:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 4e17be5705
commit 4aa2b60c1d
6 changed files with 226 additions and 39 deletions
+30 -25
View File
@@ -6,7 +6,7 @@ export { NATIVE_MACOS_PROVIDER_IDENTITY } from "./NativeMacOSProviderMetadata.js
import { 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 = universal
+46 -5
View File
@@ -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;