fix(native): require Apple origin for platform facets and keep cancellation

- Treat a CodeDirectory platform identifier as a platform binary only when
  the signature satisfies `anchor apple`, checked with
  `codesign --verify -R="anchor apple"`; otherwise the dependent facets are
  unknown. Developer ID code chains to Apple Root CA but fails this check.
- Report every security facet as unknown when architecture slices differ in
  signing state.
- Map an aborted signal that escapes a native helper, such as ticket
  hashing, to AnalysisCancelledError instead of an output error.
- Treat a target as an app bundle only when it was opened from a bundle
  directory, so a regular file named X.app is verified as a bare Mach-O.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 12:43:47 +00:00
co-authored by Claude Opus 5.5
parent 73eb20a4b9
commit ce8e969112
9 changed files with 295 additions and 46 deletions
+52 -7
View File
@@ -24,6 +24,13 @@ const directory = (
executable_segment_flags: 1,
});
type PostureInput = Parameters<typeof deriveSecurityFacets>[0];
const derive = (
input: Omit<PostureInput, "mixedSlices" | "appleOrigin"> &
Partial<PostureInput>,
) => deriveSecurityFacets({ mixedSlices: false, appleOrigin: false, ...input });
const states = (facets: ReturnType<typeof deriveSecurityFacets>) =>
Object.fromEntries(facets.map(({ facet, state }) => [facet, state]));
@@ -47,7 +54,7 @@ describe("derived security facets", () => {
for (const signed of [false, true])
expect(
states(
deriveSecurityFacets({
derive({
signed,
codeDirectory: signed ? directory(0x2) : null,
entitlements: null,
@@ -66,7 +73,7 @@ describe("derived security facets", () => {
it("restricts hardened-runtime code unless entitlements relax it", () => {
expect(
states(
deriveSecurityFacets({
derive({
signed: true,
codeDirectory: directory(RUNTIME),
entitlements: { "com.apple.security.app-sandbox": true },
@@ -80,7 +87,7 @@ describe("derived security facets", () => {
"executable-memory": "restricted",
"app-sandbox": "sandboxed",
});
const relaxed = deriveSecurityFacets({
const relaxed = derive({
signed: true,
codeDirectory: directory(RUNTIME),
entitlements: {
@@ -104,7 +111,7 @@ describe("derived security facets", () => {
});
it("lets explicit flags and platform identity override entitlements", () => {
const flagged = deriveSecurityFacets({
const flagged = derive({
signed: true,
codeDirectory: directory(0x2000 | 0x800 | RUNTIME),
entitlements: {
@@ -118,11 +125,12 @@ describe("derived security facets", () => {
});
expect(
states(
deriveSecurityFacets({
derive({
signed: true,
codeDirectory: directory(RUNTIME, 26),
entitlements: null,
entitlementsKnown: true,
appleOrigin: true,
}),
),
).toMatchObject({
@@ -131,11 +139,48 @@ describe("derived security facets", () => {
"debugger-attach": "blocked",
});
});
});
describe("security facets that need more than one CodeDirectory", () => {
it("applies platform policy only to Apple-signed platform identifiers", () => {
const crafted = derive({
signed: true,
codeDirectory: directory(0, 26),
entitlements: null,
entitlementsKnown: true,
});
expect(states(crafted)).toMatchObject({
"library-validation": "unknown",
"dyld-environment-variables": "unknown",
"debugger-attach": "unknown",
});
expect(crafted[0]?.evidence).toEqual([
"platform identifier",
"Apple origin not verified",
]);
});
it("leaves every facet unknown when slices differ in signing state", () => {
const mixed = derive({
signed: true,
codeDirectory: directory(0x2000 | RUNTIME, 26),
entitlements: null,
entitlementsKnown: false,
appleOrigin: true,
mixedSlices: true,
});
expect(new Set(mixed.map(({ state }) => state))).toEqual(
new Set(["unknown"]),
);
expect(mixed[0]?.evidence).toEqual([
"architecture slices differ in signing state",
]);
});
it("keeps facets unknown when flags or entitlements are unavailable", () => {
expect(
states(
deriveSecurityFacets({
derive({
signed: true,
codeDirectory: directory(RUNTIME),
entitlements: null,
@@ -151,7 +196,7 @@ describe("derived security facets", () => {
});
expect(
states(
deriveSecurityFacets({
derive({
signed: true,
codeDirectory: directory(null),
entitlements: null,
+85 -28
View File
@@ -146,8 +146,30 @@ interface PostureInput {
readonly entitlements: JsonValue | null;
/** False when mixed or unreadable signing states hide the aggregate entitlements. */
readonly entitlementsKnown: boolean;
/** Architecture slices differ in signing state, so no one CodeDirectory describes the process. */
readonly mixedSlices: boolean;
/**
* The signature satisfies `anchor apple`. Only then does a CodeDirectory
* platform identifier mean an Apple platform binary.
*/
readonly appleOrigin: boolean;
}
/** Whether the CodeDirectory claims a platform binary, and whether Apple signed it. */
const platformBinary = (
input: PostureInput,
): "apple" | "unverified" | "none" =>
(input.codeDirectory?.platform_identifier ?? null) === null
? "none"
: input.appleOrigin
? "apple"
: "unverified";
const UNVERIFIED_PLATFORM = [
"platform identifier",
"Apple origin not verified",
] as const;
const has = (input: PostureInput, flag: string): boolean =>
input.codeDirectory?.flags?.names.includes(flag) === true;
@@ -169,13 +191,21 @@ const libraryValidation = (input: PostureInput): SecurityFacet => {
evidence: ["CodeDirectory flags unavailable"],
explanation,
};
if (input.codeDirectory.platform_identifier !== null)
return {
...base,
state: "enforced",
evidence: ["platform binary"],
explanation,
};
const platform = platformBinary(input);
if (platform !== "none")
return platform === "apple"
? {
...base,
state: "enforced",
evidence: ["platform binary"],
explanation,
}
: {
...base,
state: "unknown",
evidence: [...UNVERIFIED_PLATFORM],
explanation,
};
if (has(input, "library-validation"))
return {
...base,
@@ -232,13 +262,21 @@ const dyldEnvironment = (input: PostureInput): SecurityFacet => {
evidence: ["CodeDirectory flag restrict"],
explanation,
};
if ((input.codeDirectory?.platform_identifier ?? null) !== null)
return {
...base,
state: "ignored",
evidence: ["platform binary"],
explanation,
};
const platform = platformBinary(input);
if (platform !== "none")
return platform === "apple"
? {
...base,
state: "ignored",
evidence: ["platform binary"],
explanation,
}
: {
...base,
state: "unknown",
evidence: [...UNVERIFIED_PLATFORM],
explanation,
};
if (!has(input, "runtime"))
return input.signed && (input.codeDirectory?.flags ?? null) === null
? {
@@ -296,13 +334,21 @@ const debuggerAttach = (input: PostureInput): SecurityFacet => {
evidence: ["com.apple.security.get-task-allow"],
explanation,
};
if ((input.codeDirectory?.platform_identifier ?? null) !== null)
return {
...base,
state: "blocked",
evidence: ["platform binary"],
explanation,
};
const platform = platformBinary(input);
if (platform !== "none")
return platform === "apple"
? {
...base,
state: "blocked",
evidence: ["platform binary"],
explanation,
}
: {
...base,
state: "unknown",
evidence: [...UNVERIFIED_PLATFORM],
explanation,
};
if (has(input, "runtime"))
return input.entitlementsKnown
? {
@@ -411,10 +457,21 @@ const appSandbox = (input: PostureInput): SecurityFacet => {
};
/** Derive launch-time security facets from observed flags and entitlements. */
export const deriveSecurityFacets = (input: PostureInput): SecurityFacet[] => [
libraryValidation(input),
dyldEnvironment(input),
debuggerAttach(input),
executableMemory(input),
appSandbox(input),
];
export const deriveSecurityFacets = (input: PostureInput): SecurityFacet[] => {
const facets = [
libraryValidation(input),
dyldEnvironment(input),
debuggerAttach(input),
executableMemory(input),
appSandbox(input),
];
// Each slice runs under its own signature; the inspected one does not
// describe an unsigned slice of the same file.
return input.mixedSlices
? facets.map((facet) => ({
...facet,
state: "unknown" as const,
evidence: ["architecture slices differ in signing state"],
}))
: facets;
};
+23
View File
@@ -195,6 +195,10 @@ class NativeMacOSClient implements AnalysisClient {
)
: observation;
} catch (cause: unknown) {
if (cause instanceof AnalysisError) return err(cause);
// A helper that observed the abort escapes as a DOMException.
if (options?.signal?.aborted)
return err(new AnalysisCancelledError(operation));
return err(
new AnalysisOutputError(operation, "Native output parsing failed", {
cause,
@@ -351,6 +355,23 @@ class NativeMacOSClient implements AnalysisClient {
entitlements.value,
verify.value,
];
// A platform identifier means a platform binary only in Apple's own
// signatures, which satisfy `anchor apple`; Developer ID code does not.
let appleOrigin = false;
if (
!unsigned &&
(parsed.code_directory?.platform_identifier ?? null) !== null
) {
const anchor = await this.#run(
"inspect_signature",
"codesign",
["--verify", "-R=anchor apple", verifyPath],
{ signal, acceptNonZero: true },
);
if (!anchor.ok) return anchor;
captures.push(anchor.value);
appleOrigin = anchor.value.exitCode === 0;
}
const limitations = [
...parsed.limitations,
...SIGNATURE_POSTURE_LIMITATIONS,
@@ -391,6 +412,8 @@ class NativeMacOSClient implements AnalysisClient {
codeDirectory: parsed.code_directory,
entitlements: entitlementValue.value,
entitlementsKnown: unsigned || entitlements.value.exitCode === 0,
mixedSlices: mixedSigning,
appleOrigin,
}),
provenance,
limitations,
+15 -7
View File
@@ -13,10 +13,18 @@ const HASH_CHUNK_BYTES = 64 * 1024;
export const signedCodePath = (target: {
readonly path: string;
readonly sourcePath?: string;
}): string =>
target.sourcePath?.toLowerCase().endsWith(".app") === true
? target.sourcePath
: target.path;
readonly bundleInfoPlist?: string;
}): string => appBundle(target) ?? target.path;
/**
* The app bundle directory a target was opened from. Only a bundle opened as a
* directory carries its Info.plist; a regular file named `X.app` is not one.
*/
const appBundle = (target: {
readonly sourcePath?: string;
readonly bundleInfoPlist?: string;
}): string | undefined =>
target.bundleInfoPlist === undefined ? undefined : target.sourcePath;
/**
* Project a local `codesign --verify --strict` capture without reinterpreting
@@ -59,11 +67,11 @@ export const signatureVerification = (
* Apple's notarization record are not checked, because that needs the network.
*/
export const stapledTicket = async (
target: { readonly sourcePath?: string },
target: { readonly sourcePath?: string; readonly bundleInfoPlist?: string },
signal?: AbortSignal,
): Promise<InspectSignature["stapled_ticket"]> => {
const bundle = target.sourcePath;
if (bundle?.toLowerCase().endsWith(".app") !== true)
const bundle = appBundle(target);
if (bundle === undefined)
return {
status: "not-applicable",
path: null,