fix(native): verify nested code and keep ticket read failures local

Address review findings in inspect_signature:
- Verify with `codesign --verify --deep --strict`. Shallow verification
  misses a file added inside a nested XPC service, or a modified dylib
  inside a framework. verify:code-signature now tampers inside the
  nested XPC service and requires `invalid`.
- Classify verification as `unsigned` only when the top-level target is
  unsigned. Unsigned nested code inside a signed bundle is `invalid`.
- Report a stapled ticket that exists but cannot be read as
  `unreadable`, with the error code in a new `reason` field, instead of
  failing the whole signature inspection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 11:54:32 +00:00
co-authored by Claude Opus 5.5
parent 25ca63f231
commit 37824e8c46
12 changed files with 155 additions and 50 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ explicit limitation; architecture inventory locations remain available.
`linker-signed`, …), slot counts, platform identifier and runtime
version;
- sealed resources;
- a local `codesign --verify --strict` result, with diagnostics and the
- a local `codesign --verify --deep --strict` result, with diagnostics and the
nested code it validated;
- whether a notarization ticket is stapled at `Contents/CodeResources`.
The ticket itself and Apple's notarization record are not checked,
+2 -2
View File
@@ -669,9 +669,9 @@
"mcp_prompts": 6
},
"digests": {
"tools_sha256": "1b01577d3ad5b8815e08a52a5681c2af738935c43695b4a21d36f1d26333e680",
"tools_sha256": "c7373daff6f47d20cd5a172d0ef8371e748ae19b181371bfdd7a0a63e0958ddd",
"prompts_sha256": "ef2de8814390b340623c32001610aa2709d90bed51537b6e562ab3abf144ab4d",
"combined_sha256": "07b4bcbc620ade41ffa4da3a5676d92fe3e7366fc60eebdb543bec7ba359d3f8",
"combined_sha256": "c78943b1c2690688aafee61608a87a0a1e7385a496533bc9f72cbdee72e09a37",
"providers_sha256": "a7d885531ee25d42c51385444faec544d34cd7eba6250a7dc75c4faedd4454a3"
}
}
+2 -2
View File
@@ -76,8 +76,8 @@ copies of a clang-built fixture with real `codesign`: ad hoc, hardened
runtime, hardened runtime with relaxing entitlements, and `library,restrict`
flags. It checks that `inspect-signature` decodes those flags and derives the
expected security facets. It also checks a platform binary, which must report a
platform identifier, and a bundle with an added unsealed file, which must fail
strict verification. CLI and stdio MCP results must match.
platform identifier, and bundles with an added unsealed file, at the top level
and inside a nested XPC service, which must fail deep strict verification. CLI and stdio MCP results must match.
`verify:macos-bundle` needs only macOS with Command Line Tools. It compiles a
source-owned app with `clang`: a versioned framework, XPC services, an app
extension, a login item, a privileged helper, launchd plists, and a helper tool,
@@ -1,5 +1,5 @@
{
"manifest_id": "ecm_7c023e043e069545c78f60e894c1f66a357a9b9435a1f1b3ade09eb7b1a821f1",
"manifest_id": "ecm_f6604e02c812e3c564e5dc64c531321fbe4d3f788e90830b3584e330b1f8fdd7",
"verifier": {
"id": "rea-managed-conformance",
"version": "1"
@@ -13,7 +13,7 @@
"skill_digests": [
{
"skill_id": "reverse-engineer-anything",
"sha256": "81e7b30e94cec01efa31f18553919665b3617282f5cc23ee01a3e158ef665468"
"sha256": "cb96a48bb7c9c0f06e03d9caacd129fd6995091d339b63e269de11d363a2a3a3"
}
],
"claims": [
+17
View File
@@ -132,6 +132,22 @@ try {
assert.equal(platform.verification.status, "valid");
assert.equal(facets(platform)["library-validation"], "enforced");
// Only deep verification sees a file added inside nested code.
const nested = join(root, "Nested.app");
await exec("/usr/bin/ditto", [app, nested]);
await writeFile(
join(nested, "Contents/XPCServices/Svc.xpc/Contents/added.txt"),
"unsealed",
);
const nestedTamper = await artifactCli("inspect-signature", nested);
assert.equal(nestedTamper.verification.status, "invalid");
assert.ok(
nestedTamper.verification.diagnostics.some((line) =>
line.includes("Svc.xpc"),
),
JSON.stringify(nestedTamper.verification),
);
await mkdir(join(app, "Contents/Resources"), { recursive: true });
await writeFile(join(app, "Contents/Resources/added.txt"), "unsealed");
const tampered = await artifactCli("inspect-signature", app);
@@ -155,6 +171,7 @@ try {
],
platform_identifier: platform.code_directory.platform_identifier,
tampered_verification: tampered.verification.status,
nested_tamper_verification: nestedTamper.verification.status,
})}\n`,
);
} finally {
+1 -1
View File
@@ -4,7 +4,7 @@ description: Reverse engineer native, managed, Electron/JavaScript, packaged, fi
metadata:
version: "28"
tool_count: 131
catalog_digest: "07b4bcbc620ade41ffa4da3a5676d92fe3e7366fc60eebdb543bec7ba359d3f8"
catalog_digest: "c78943b1c2690688aafee61608a87a0a1e7385a496533bc9f72cbdee72e09a37"
---
# REA
+4 -2
View File
@@ -42,7 +42,7 @@ export const sealedResourcesSchema = z.object({
files: z.number().int().nullable(),
});
/** Local `codesign --verify --strict` outcome for the signed code. */
/** Local `codesign --verify --deep --strict` outcome for the signed code. */
export const signatureVerificationSchema = z.object({
path: z.string(),
status: z.enum(["valid", "invalid", "unsigned"]),
@@ -53,13 +53,15 @@ export const signatureVerificationSchema = z.object({
/** Presence of a notarization ticket stapled to an app bundle. */
export const stapledTicketSchema = z.object({
status: z.enum(["present", "absent", "not-applicable"]),
status: z.enum(["present", "absent", "unreadable", "not-applicable"]),
path: z.string().nullable(),
sha256: z
.string()
.regex(/^[a-f0-9]{64}$/u)
.nullable(),
size: z.number().int().nonnegative().nullable(),
/** Why an existing ticket file could not be read, such as EACCES. */
reason: z.string().nullable(),
});
const facetSchema = z.discriminatedUnion("facet", [
File diff suppressed because one or more lines are too long
+5 -6
View File
@@ -346,7 +346,8 @@ class NativeMacOSClient implements AnalysisClient {
const verify = await this.#run(
"inspect_signature",
"codesign",
["--verify", "--strict", "--verbose=2", verifyPath],
// --deep verifies nested frameworks, helpers, extensions and XPC services.
["--verify", "--deep", "--strict", "--verbose=2", verifyPath],
{ signal, acceptNonZero: true },
);
if (!verify.ok) return verify;
@@ -387,11 +388,9 @@ class NativeMacOSClient implements AnalysisClient {
...parsed,
designated_requirement: requirementText,
entitlements: entitlementValue.value,
verification: signatureVerification(
verify.value,
verifyPath,
isNonzeroUnsignedObservation(verify.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),
stapled_ticket: await stapledTicket(this.target, signal),
security_facets: deriveSecurityFacets({
signed: !unsigned,
+57 -32
View File
@@ -64,47 +64,72 @@ export const stapledTicket = async (
): Promise<InspectSignature["stapled_ticket"]> => {
const bundle = target.sourcePath;
if (bundle?.toLowerCase().endsWith(".app") !== true)
return { status: "not-applicable", path: null, sha256: null, size: null };
return {
status: "not-applicable",
path: null,
sha256: null,
size: null,
reason: null,
};
const relative = "Contents/CodeResources";
const path = join(bundle, relative);
try {
const metadata = await lstat(path);
if (!metadata.isFile())
return { status: "absent", path: null, sha256: null, size: null };
} catch (cause: unknown) {
if (
cause instanceof Error &&
"code" in cause &&
(cause.code === "ENOENT" || cause.code === "ENOTDIR")
)
return { status: "absent", path: null, sha256: null, size: null };
throw cause;
}
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
try {
const hash = createHash("sha256");
const buffer = new Uint8Array(HASH_CHUNK_BYTES);
let size = 0;
for (;;) {
signal?.throwIfAborted();
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
if (bytesRead === 0) break;
size += bytesRead;
hash.update(buffer.subarray(0, bytesRead));
if (!metadata.isFile()) return absent;
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
try {
const hash = createHash("sha256");
const buffer = new Uint8Array(HASH_CHUNK_BYTES);
let size = 0;
for (;;) {
signal?.throwIfAborted();
const { bytesRead } = await handle.read(buffer, 0, buffer.length, null);
if (bytesRead === 0) break;
size += bytesRead;
hash.update(buffer.subarray(0, bytesRead));
}
return {
status: "present",
path: relative,
sha256: hash.digest("hex"),
size,
reason: null,
};
} finally {
await handle.close();
}
return {
status: "present",
path: relative,
sha256: hash.digest("hex"),
size,
};
} finally {
await handle.close();
} catch (cause: unknown) {
const code = errorCode(cause);
if (code === "ENOENT" || code === "ENOTDIR") return absent;
// A privacy or ACL denial leaves the ticket's presence unknown, not the
// whole signature inspection.
if (code === "EACCES" || code === "EPERM")
return {
status: "unreadable",
path: relative,
sha256: null,
size: null,
reason: code,
};
throw cause;
}
};
const absent = {
status: "absent",
path: null,
sha256: null,
size: null,
reason: null,
} as const;
const errorCode = (cause: unknown): string | undefined =>
cause instanceof Error && "code" in cause && typeof cause.code === "string"
? cause.code
: undefined;
/** Limitations that keep verification and notarization claims bounded. */
export const SIGNATURE_POSTURE_LIMITATIONS = [
"Signature verification is local `codesign --verify --strict`; certificate revocation, Gatekeeper policy, and Apple's notarization records are not checked.",
"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.",
];
+1
View File
@@ -72,6 +72,7 @@ export const parseCodeSignature = (
path: null,
sha256: null,
size: null,
reason: null,
},
security_facets: [],
limitations: unsigned
@@ -1,5 +1,5 @@
import { createHash } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
@@ -72,6 +72,7 @@ describe("native signature posture", () => {
path: "Contents/CodeResources",
sha256: createHash("sha256").update(ticket).digest("hex"),
size: ticket.length,
reason: null,
});
expect(
Object.fromEntries(
@@ -132,4 +133,64 @@ describe("native signature posture", () => {
stapled_ticket: { status: "not-applicable" },
});
});
it.skipIf(process.getuid?.() === 0)(
"keeps inspecting when the stapled ticket cannot be read",
async () => {
const { app, executable } = await fixtureApp("ticket");
const ticket = join(app, "Contents/CodeResources");
await chmod(ticket, 0o000);
try {
const signature = await new NativeMacOSProvider(
new FixtureRunner(),
"darwin",
)
.createClient(machoTarget(executable, app))
.execute("inspect_signature", {});
expect(signature.ok && signature.value.result).toMatchObject({
stapled_ticket: {
status: "unreadable",
path: "Contents/CodeResources",
sha256: null,
reason: "EACCES",
},
verification: { status: "valid" },
});
} finally {
await chmod(ticket, 0o644);
}
},
);
it("reports unsigned nested code inside a signed bundle as invalid", async () => {
const { app, executable } = await fixtureApp(undefined);
const signature = await new NativeMacOSProvider(
new UnsignedNestedRunner(),
"darwin",
)
.createClient(machoTarget(executable, app))
.execute("inspect_signature", {});
expect(signature.ok && signature.value.result).toMatchObject({
signed: true,
verification: { status: "invalid", exit_code: 1 },
});
});
});
/** A signed bundle whose nested helper is unsigned. */
class UnsignedNestedRunner 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 =
"/Applications/Fixture.app/Contents/Helpers/tool: code object is not signed at all\nIn subcomponent: /Applications/Fixture.app/Contents/Helpers/tool\n";
return ok({
...result.value,
stdout: "",
stderr,
stdoutBytes: 0,
stderrBytes: Buffer.byteLength(stderr),
exitCode: 1,
});
}
}