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
+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;