fix(native): flag nested-code paths that codesign split on newlines

codesign prints one nested path per line, so a nested component whose name
contains a newline is split into fragments. inspect_signature now checks
that every --validated: path it reports exists, and names any that does not
in a limitation instead of trusting it as a complete path. The result stays
deterministic: codesign's progress lines arrive in no fixed order, so the raw
output is not echoed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 13:05:23 +00:00
co-authored by Claude Opus 5.5
parent 4f46836178
commit 949ab427fb
4 changed files with 84 additions and 5 deletions
+7
View File
@@ -73,6 +73,13 @@ try {
"executable-memory": "unrestricted",
"app-sandbox": "not-sandboxed",
});
// Deep verification reports the fixture's real nested code, all on disk.
assert.ok(plain.verification.validated_nested_code.length > 0);
assert.ok(
!plain.limitations.some((limitation) =>
limitation.includes("which does not exist"),
),
);
await withArtifactMcp(app, async (client) => {
const viaMcp = await artifactMcpResult(client, "inspect_signature");
assert.deepEqual(
+10 -3
View File
@@ -58,6 +58,7 @@ import {
signatureVerification,
signedCodePath,
stapledTicket,
unconfirmedNestedCode,
} from "./NativeSignaturePosture.js";
import { deriveSecurityFacets } from "../domain/codeSigningPosture.js";
import { parseDemangledSymbols } from "./parsers/demangle.js";
@@ -396,6 +397,14 @@ class NativeMacOSClient implements AnalysisClient {
captures.push(...slices.value.captures);
limitations.push(...slices.value.limitations);
}
// Only the top-level signature decides "unsigned"; unsigned nested
// code inside a signed bundle makes the bundle invalid.
const verification = signatureVerification(
verify.value,
verifyPath,
unsigned,
);
limitations.push(...(await unconfirmedNestedCode(verification)));
const provenance = captures.map((capture) =>
invocation(capture, this.target.path),
);
@@ -403,9 +412,7 @@ class NativeMacOSClient implements AnalysisClient {
...parsed,
designated_requirement: requirementText,
entitlements: entitlementValue.value,
// Only the top-level signature decides "unsigned"; unsigned nested
// code inside a signed bundle makes the bundle invalid.
verification: signatureVerification(verify.value, verifyPath, unsigned),
verification,
stapled_ticket: await stapledTicket(this.target, signal),
security_facets: deriveSecurityFacets({
signed: !unsigned,
+25
View File
@@ -141,3 +141,28 @@ export const SIGNATURE_POSTURE_LIMITATIONS = [
"Signature verification is local `codesign --verify --deep --strict`; certificate revocation, Gatekeeper policy, and Apple's notarization records are not checked.",
"Security facets are derived from CodeDirectory flags and entitlements; runtime policy such as System Integrity Protection, AMFI, and setuid bits can further restrict a process.",
];
/**
* codesign prints one nested path per line, so a path that contains a newline
* is split into fragments. A reported path that does not exist on disk is
* named in a limitation instead of being trusted as complete.
*/
export const unconfirmedNestedCode = async (
verification: NonNullable<InspectSignature["verification"]>,
): Promise<string[]> => {
const missing: string[] = [];
for (const path of verification.validated_nested_code)
try {
await lstat(path);
} catch (cause: unknown) {
const code = errorCode(cause);
// A denied lookup cannot confirm the path, but is no sign of a split one.
if (code === "EACCES" || code === "EPERM") continue;
if (code !== "ENOENT" && code !== "ENOTDIR") throw cause;
missing.push(path);
}
return missing.map(
(path) =>
`codesign reported validated nested code at ${JSON.stringify(path)}, which does not exist; a nested path probably contains a newline, so validated_nested_code and diagnostics hold fragments of it.`,
);
};
@@ -14,13 +14,15 @@ import {
nativeMachoTarget as machoTarget,
} from "../../../fixtures/nativeCommands.js";
const TAMPERED_STDERR =
"--prepared:/Applications/Fixture.app/Contents/XPCServices/B.xpc\n--validated:/Applications/Fixture.app/Contents/XPCServices/B.xpc\n--validated:/Applications/Fixture.app/Contents/PlugIns/A.appex\n/Applications/Fixture.app: a sealed resource is missing or invalid\nfile modified: /Applications/Fixture.app/Contents/Resources/en.lproj/Main.nib\n";
/** Fail only strict verification, as a tampered bundle does. */
class TamperedRunner extends FixtureRunner {
override async run(tool: string, arguments_: readonly string[]) {
const result = await super.run(tool, arguments_);
if (!result.ok || arguments_[0] !== "--verify") return result;
const stderr =
"--prepared:/Applications/Fixture.app/Contents/XPCServices/B.xpc\n--validated:/Applications/Fixture.app/Contents/XPCServices/B.xpc\n--validated:/Applications/Fixture.app/Contents/PlugIns/A.appex\n/Applications/Fixture.app: a sealed resource is missing or invalid\nfile modified: /Applications/Fixture.app/Contents/Resources/en.lproj/Main.nib\n";
const stderr = TAMPERED_STDERR;
return ok({
...result.value,
stdout: "",
@@ -225,6 +227,27 @@ describe("native signature posture boundaries", () => {
});
});
it("flags reported nested code that does not exist as a split path", async () => {
const { app, executable } = await fixtureApp(undefined);
const signature = await new NativeMacOSProvider(
new NewlinePathRunner(),
"darwin",
)
.createClient(machoTarget(executable, app))
.execute("inspect_signature", {});
if (!signature.ok) throw signature.error;
const result = inspectSignatureSchema.parse(signature.value.result);
// The line projection splits the name; the fragment is flagged.
expect(result.verification?.validated_nested_code).toEqual([
`${app}/Contents/Helpers/odd`,
]);
expect(result.limitations).toContainEqual(
expect.stringContaining(
`validated nested code at ${JSON.stringify(`${app}/Contents/Helpers/odd`)}, which does not exist`,
),
);
});
it("reports cancellation while hashing the stapled ticket", async () => {
const { app, executable } = await fixtureApp("ticket");
const controller = new AbortController();
@@ -279,6 +302,23 @@ class PlatformRunner extends FixtureRunner {
}
}
/** A nested helper whose file name contains a newline. */
class NewlinePathRunner extends FixtureRunner {
override async run(tool: string, arguments_: readonly string[]) {
const result = await super.run(tool, arguments_);
if (!result.ok || arguments_[0] !== "--verify") return result;
const bundle = arguments_.at(-1) ?? "";
const stderr = `--validated:${bundle}/Contents/Helpers/odd\nname.app\n${bundle}: valid on disk\n`;
return ok({
...result.value,
stdout: "",
stderr,
stdoutBytes: 0,
stderrBytes: Buffer.byteLength(stderr),
});
}
}
/** Cancel the request once local verification has finished. */
class AbortAfterVerifyRunner extends FixtureRunner {
constructor(private readonly controller: AbortController) {