mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
6f267022ed
* feat(errors)!: add the TargetFailure model, its projection table and conformance registry Introduce one domain-owned TargetFailure value whose kind alone derives the public code, category, retryability, recovery and details, for CLI and MCP alike. Selected-file malformed and truncated refusals are the first area on it: AnalysisMalformedTargetError is replaced by AnalysisTargetFailureError, and ParseFailure's byte-structure variants are TargetFailure variants rather than a parallel type. The shared stableArtifactParserFailureCli loop is replaced by a declarative registry (tests/support/targetFailure/readers/*.ts) and one generated suite that runs every registered case through the CLI and MCP. BREAKING CHANGE: selected-file malformed and truncated refusals add details.failure_kind, and a truncated target's remediation now says to re-acquire the complete file instead of choosing another target. Codes are unchanged. Affected operations: every operation that refuses a selected file's malformed or truncated bytes, including the ELF, PE, Mach-O, DWARF, Go, Rust, BEAM, pyc, devicetree, Apple IPS, kernel module, binary hardening, managed PE/CLI and IL2CPP readers. * test(managed): name the target failure error in the merged status test * docs(reference): name the error CSV instead of linking a non-page file * fix(tests): expect the path reason for non-regular dyld inputs #14225 refuses a non-regular dyld input as ArtifactReaderFailure("path"); dylibFileLifetime still expected "format" and fails on main's shard 9. * fix(tests): expect io, invariant and path where #14225 reclassified stableArtifact expected "format" for an impossible descriptor byte count (now io) and an invalid chunk size (now InternalInvariantError); directoryArtifactReaderOpen expected "format" for a non-file entry (now path, the same change as #14238). All three fail on main. * fix(errors)!: raise container reader byte faults as malformed or truncated targets Delete the ambiguous `format` reason from ArtifactReaderFailure and ArtifactOperationError. Each container reader now raises its owner's fact once, through malformedArtifact / truncatedArtifact, with format, offset, field and measured extents. Short reads inside a measured extent are read-coverage integrity failures, path-kind refusals are `path`, foreign or recovery-requiring inputs are `unsupported`, host tool output is `io`, and REA defects are InternalInvariantError. The trace, NTFS journal, release exposure, web capture, native macOS and web artifact adapters project the reader's parsed facts as AnalysisTargetFailureError instead of invalid_request. Inventory malformed, truncated, unsupported and cancelled reasons take their code from the TargetFailure projection table, so a truncated container is unsupported_target with failure_kind "truncated" on both paths. BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact, analyze_javascript_application, inspect_desktop_application, inspect_keyed_archive, inspect_asset_catalog, inspect_nuget_nupkg, trace_dylib_resolution, trace_web_module_imports and trace_web_source_location report damaged container bytes as unsupported_target (details.reason "malformed" or "truncated", with failure_kind and format) instead of artifact_operation_failed with reason "format", and a truncated container as unsupported_target instead of truncated. inspect_playwright_trace, inspect_ntfs_journal, audit_release_exposure, the web capture inspection operations and the caller-selected import and source maps report damaged selected bytes as unsupported_target instead of invalid_request. The `format` reason no longer appears in details.reason. * test(artifacts): expect target failure codes in compiled CLI and MCP suites * fix(errors): give the integrity kind a schema-valid code and list the reference page artifact_integrity_mismatch's projection schema requires declared and calculated digests, which a TargetFailure of kind integrity does not carry, so the kind now projects artifact_operation_failed with category integrity_mismatch. The model test validates every kind's projection against analysisErrorProjectionSchema. The reference page joins the documentation sidebar. * fix(artifacts): report a short carrier read as a changed file and keep unsupported desktop probes local A Bun or Node SEA short read inside a range already checked against the measured size meant the file changed, not that it was truncated. The carrier range reader and Bun's readExact now raise read-coverage integrity for it, and keep 'outside the file' as the truncated case. DesktopFileInspection records an unsupported managed-metadata probe as a row issue again, so later file checks still run. * test(errors): register PE DOS-signature and truncated IPS refusals and drop their per-command copies The non-MZ PE imports and load-config cases (PE32/PE32+, ZZ and high-bit signatures) and the truncated Apple IPS body move into the conformance registry, which runs them through the CLI and MCP and now also checks that a refusal leaves the selected file unchanged. The per-command tests keep only their valid-image controls. * feat(errors)!: make failure_kind the identifier and derive code and retry from it Apply the owner review on #14239 to the TargetFailure model: - remove details.format_failure; failure_kind is the only identifier; - give integrity, conflict and io their own codes (target_integrity_mismatch, input_conflict, host_io_failed) and derive inventory malformed, truncated and unsupported codes from the same table; - add a retry hint (none, backoff, after_change) derived from the kind, with io refined by its transient errno fact; retryable stays the unchanged-retry flag and is true exactly for backoff; - rename the limit-cut result code and category truncated to partial; - drop cancelled and resource from the target kinds and spell identifiers in American English (unrecognized); - return a target failure a tool handler throws as a projected isError result instead of letting the SDK flatten it to text. BREAKING CHANGE: details.format_failure is removed (read details.failure_kind). The truncated result code and category are now partial. Target integrity failures report target_integrity_mismatch, host read failures host_io_failed and disagreeing inputs input_conflict. Inventory operations (inventory_artifact, inspect_artifact, extract_artifact and the other inventory callers) report truncated container bytes as unsupported_target. Every target failure carries a top-level retry hint. * fix(artifacts): keep changed-file short reads, invalid offsets and EOF declarations apart PyInstaller reads inside the measured size, and .NET bundle manifest refills, now raise read-coverage integrity on a zero-byte read instead of truncated. A .NET bundle field and a Godot PCK directory entry that extend past the file are truncated (with offset and extents) instead of malformed. An absent or invalid ASAR packed offset is malformed; only a valid range past the archive is truncated. The .NET manifest string offset is reported in file coordinates. * fix(artifacts): verify the carrier before blaming a short read on the bytes The NTFS $MFT decoder, positional members (FIT, uImage, Bun, Deno) and .NET bundle members now re-verify the source before reporting a short stream as malformed, so a file that changed during the read reports the change. The truncation site builds the model's measured extent. * docs(android): name unsupported_target for truncated boot image structures * fix(errors)!: report a target that changed during the read as failure_kind changed Inventory integrity failures whose check means the target moved under the read (container, member and unpacked-entry identity, read coverage, traversal coverage) now take the model's changed projection: code artifact_changed, retry backoff and details.failure_kind changed. A selected target that no longer matches the one the session admitted gets its own admitted-identity check and keeps its reopen recovery. BREAKING CHANGE: inventory_artifact, inspect_artifact, extract_artifact and the other inventory operations report a target that changed during the read as artifact_changed (retryable, failure_kind changed) instead of artifact_operation_failed. Admission-identity refusals report integrity_check admitted-identity instead of container-identity. * test(artifacts): expect admitted-identity for selections replaced before admission A selected source replaced before admission fails the admitted-identity check rather than container-identity since the changed/admitted split. * test(go): name the fixture and result when an MZ carrier is not refused through MCP * test(web): expect the partial code for a refused source-map admission The limit-cut code is partial; this Windows-lane test arrived from main with the old truncated spelling. * test(errors): check the MCP target failure as the text clients receive The stdio transport removes the private structured carrier from every isError result, so the conformance suite asserted a field that never reaches clients and failed every case. The projected diagnostic is checked from the JSON text instead. * test(artifacts): expect artifact_changed when selected bytes change after inventory Bundle resources replaced after open and unselected JavaScript sources rewritten after inventory now fail a change check, so they report artifact_changed with retry backoff rather than artifact_operation_failed. * fix(errors): report a session digest mismatch as admitted-identity The digest the session admitted does not change when the same request is repeated, so targetDigestChangedFailure was inviting a backoff retry that fails the same way. It now names admitted-identity, which stays artifact_operation_failed and not retryable, and tells the caller to reopen. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(artifacts): classify unused PyInstaller compressed input as layout zlib stops at the first complete stream, so a declared span that arrives intact but is not fully consumed is trailing junk or a second stream, not a file that changed. A source that ends before that span stays read-coverage. The cached-carrier test replaces the file with a different size so a shared timestamp cannot hide the rewrite. Co-authored-by: morluto <morluto@users.noreply.github.com> * test(applications): expect unsupported_target for a damaged trace stream A trace member whose CRC fails is a malformed zip target. The partial Evidence assertion now checks that code, failure_kind and format. Co-authored-by: morluto <morluto@users.noreply.github.com> * test(dylib): expect admitted-identity when the opened target is replaced The session keeps the digest it admitted, so replacing the executable and repeating trace_dylib_resolution is not a retryable container change. Co-authored-by: morluto <morluto@users.noreply.github.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <morluto@users.noreply.github.com>
690 lines
22 KiB
TypeScript
690 lines
22 KiB
TypeScript
import {
|
|
readerIntegrityFailure,
|
|
bindArtifactFileCleanup,
|
|
artifactFileCompletion,
|
|
artifactFilePrimaryFacts,
|
|
artifactFileCancellationCause,
|
|
ArtifactFileCleanupFailure,
|
|
type ArtifactFilePrimaryFacts,
|
|
ArtifactResourceScope,
|
|
ArtifactReaderFailure,
|
|
} from "../resources/index.js";
|
|
import { InternalInvariantError } from "../domain/internalInvariant.js";
|
|
import { translateNativeCommandFailure } from "./NativeCommandAnalysisError.js";
|
|
import {
|
|
unreadablePlist,
|
|
type NativePlistSelection,
|
|
} from "./NativePlistSource.js";
|
|
import { NATIVE_MACOS_PROVIDER_IDENTITY as IDENTITY } from "./NativeMacosProviderIdentity.js";
|
|
import { realpath } from "node:fs/promises";
|
|
import { createAnalysisExecutionEvidence } from "../domain/analysisObservation.js";
|
|
|
|
import { z } from "zod";
|
|
|
|
import {
|
|
createAnalysisExecution,
|
|
type AnalysisOperation,
|
|
} from "../ports/AnalysisProvider.js";
|
|
import {
|
|
NATIVE_TOOL_CONTRACTS,
|
|
type NativeToolName,
|
|
} from "../contracts/native/nativeToolContracts.js";
|
|
import {
|
|
isMachOFormat,
|
|
type BinaryTarget,
|
|
} from "../domain/binaryTargetTypes.js";
|
|
import type { EvidenceLocation } from "../domain/evidence.js";
|
|
import {
|
|
AnalysisOutputError,
|
|
AnalysisTargetFailureError,
|
|
cancelledBy,
|
|
carriedReason,
|
|
} from "../domain/analysisErrorCore.js";
|
|
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import {
|
|
jsonValueSchema,
|
|
ownedJsonValueSchema,
|
|
type JsonValue,
|
|
} from "../domain/jsonValue.js";
|
|
import {
|
|
projectPlistValue,
|
|
nonFiniteRealLimitation,
|
|
negativeZeroRealLimitation,
|
|
unknownDateLimitation,
|
|
unknownScalarLimitation,
|
|
} from "../domain/apple/plistValue.js";
|
|
import { createPlistNumberProjection } from "../domain/apple/plistNumbers.js";
|
|
import { parseXmlPropertyList } from "../domain/propertyListKeys.js";
|
|
import { plistDuplicateKeyLimitations } from "../domain/apple/plistDuplicateKeys.js";
|
|
import { machoKnownCodeSignSelector } from "../domain/apple/machoArchitectureSelection.js";
|
|
import {
|
|
inspectPlistSchema,
|
|
type NativeCommandInvocation,
|
|
} from "../domain/native/nativeInspection.js";
|
|
import type { Result } from "../domain/result.js";
|
|
import {
|
|
NativeCommandFailure,
|
|
failureCapture,
|
|
type NativeCommandCleanupFailure,
|
|
type NativeCommandCapture,
|
|
} from "../providers/NativeCommandRunner.js";
|
|
import type { ParsedPlist } from "./parsers/plist.js";
|
|
import { ParseFailureError } from "../domain/parseFailure.js";
|
|
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
|
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { NativeMachoSourceVerificationFailure } from "./NativeMachoSource.js";
|
|
import {
|
|
isParseLimit,
|
|
parseFailureAnalysisError,
|
|
parseLimitConstraint,
|
|
} from "../domain/parseFailureAnalysisError.js";
|
|
import type { ResolvedAppBundleResourceRoot } from "../domain/apple/bundleResourceRoot.js";
|
|
|
|
export const nativeCommandCleanupFailure = (
|
|
failures: readonly NativeCommandCleanupFailure[],
|
|
): ProviderCleanupError => {
|
|
const failure = new ProviderCleanupError(
|
|
IDENTITY.id,
|
|
[...new Set(failures.flatMap(({ resources }) => resources))],
|
|
{
|
|
reason: "Native command process cleanup remains incomplete",
|
|
failures: failures.map(({ reason, resources }) => ({
|
|
reason,
|
|
resources: [...resources],
|
|
})),
|
|
},
|
|
);
|
|
if (
|
|
failures.length === 0 ||
|
|
failures.some(({ marker }) => marker === undefined)
|
|
)
|
|
return failure;
|
|
const markers = failures.flatMap(({ marker }) =>
|
|
marker === undefined ? [] : [marker],
|
|
);
|
|
return markers.length === 0
|
|
? failure
|
|
: failure.bindCleanupDependencies(markers);
|
|
};
|
|
|
|
/** Each file workflow retains its own exact resource set through client close. */
|
|
export const closeNativeFileScope = async (
|
|
scope: ArtifactResourceScope,
|
|
resource: string,
|
|
): Promise<AnalysisError | undefined> => {
|
|
try {
|
|
await scope.close();
|
|
return undefined;
|
|
} catch (cause: unknown) {
|
|
const observation = ArtifactReaderFailure.cleanupObservation(
|
|
cause,
|
|
resource,
|
|
);
|
|
const failure = new ProviderCleanupError(
|
|
IDENTITY.id,
|
|
observation.resources,
|
|
{ reason: observation.reason },
|
|
{ operation: "close_binary", cause },
|
|
);
|
|
bindArtifactFileCleanup(cause, failure);
|
|
return failure;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Project a producer failure. A broken internal assumption is a REA defect, not
|
|
* native output, so it is rethrown rather than reported as output parsing.
|
|
*/
|
|
export const nativeOutputFailure = (
|
|
operation: AnalysisOperation,
|
|
cause: unknown,
|
|
): AnalysisError => {
|
|
if (cause instanceof InternalInvariantError) throw cause;
|
|
return new AnalysisOutputError(operation, "Native output parsing failed", {
|
|
cause,
|
|
});
|
|
};
|
|
|
|
const nativeMachoCompletedFailure = (
|
|
value: unknown,
|
|
): AnalysisError | undefined =>
|
|
value !== null &&
|
|
typeof value === "object" &&
|
|
"ok" in value &&
|
|
value.ok === false &&
|
|
"error" in value &&
|
|
value.error instanceof AnalysisError
|
|
? value.error
|
|
: undefined;
|
|
|
|
const nativeMachoSourcePrimary = (
|
|
{ cause }: ArtifactFilePrimaryFacts,
|
|
path: string,
|
|
operation: "inspect_macho" | "list_architectures",
|
|
): AnalysisError => {
|
|
if (cause instanceof AnalysisError) return cause;
|
|
if (isParseLimit(cause, "macho"))
|
|
return parseLimitConstraint(operation, "memory", cause, {
|
|
retryable: false,
|
|
});
|
|
const formatFailure = parseFailureAnalysisError(operation, path, cause);
|
|
if (formatFailure !== undefined) return formatFailure;
|
|
const completed =
|
|
cause instanceof NativeMachoSourceVerificationFailure
|
|
? cause.completed
|
|
: undefined;
|
|
const failed =
|
|
cause instanceof NativeMachoSourceVerificationFailure &&
|
|
cause.outcome.kind === "failed"
|
|
? nativeMachoSourcePrimary(
|
|
artifactFilePrimaryFacts(cause.outcome.cause),
|
|
path,
|
|
operation,
|
|
)
|
|
: undefined;
|
|
const completedFailure = nativeMachoCompletedFailure(completed);
|
|
const previous = failed ?? completedFailure;
|
|
const optionsWithoutCause =
|
|
previous === undefined
|
|
? {}
|
|
: {
|
|
diagnostics: {
|
|
previous_error: jsonValueSchema.parse(
|
|
projectAnalysisError(previous),
|
|
),
|
|
},
|
|
};
|
|
const options = { cause, ...optionsWithoutCause };
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "cancelled") {
|
|
// A returned failure is already the producer's physical outcome. A later
|
|
// cancelled identity check must not replace its output or cleanup owner.
|
|
if (completedFailure !== undefined)
|
|
return analysisErrorWithCancellation(
|
|
completedFailure,
|
|
cancelledBy(
|
|
operation,
|
|
carriedReason(artifactFileCancellationCause(cause)),
|
|
),
|
|
);
|
|
return cancelledBy(
|
|
operation,
|
|
carriedReason(artifactFileCancellationCause(cause)),
|
|
optionsWithoutCause,
|
|
);
|
|
}
|
|
if (
|
|
cause instanceof ArtifactReaderFailure &&
|
|
cause.parseFailure !== undefined
|
|
)
|
|
return new AnalysisTargetFailureError(
|
|
operation,
|
|
path,
|
|
cause.parseFailure,
|
|
options,
|
|
);
|
|
if (cause instanceof ArtifactReaderFailure && cause.reason === "integrity")
|
|
return readerIntegrityFailure(operation, path, cause, options);
|
|
return new ProviderAdapterError(IDENTITY.id, operation, {
|
|
...options,
|
|
userMessage: "The selected Mach-O source could not be read safely.",
|
|
remediationAction:
|
|
"Inspect the selected path and its read access, then retry.",
|
|
});
|
|
};
|
|
|
|
const nativeMachoCompletion = (
|
|
cause: unknown,
|
|
target: BinaryTarget,
|
|
parameters: Readonly<Record<string, JsonValue>>,
|
|
operation: "inspect_macho" | "list_architectures",
|
|
) => {
|
|
const completion = artifactFileCompletion(cause);
|
|
// The file operation only completes after the producer result has passed
|
|
// the final identity check. Both Mach-O command workflows own their callbacks.
|
|
const outcome = completion?.value as
|
|
| Result<NativeObservation, AnalysisError>
|
|
| undefined;
|
|
const observation =
|
|
outcome?.ok === true
|
|
? createAnalysisExecutionEvidence(
|
|
operation,
|
|
parameters,
|
|
{
|
|
...createAnalysisExecution(outcome.value.result, IDENTITY, {
|
|
limitations: outcome.value.limitations,
|
|
locations: outcome.value.locations,
|
|
}),
|
|
subject: target,
|
|
},
|
|
"observed",
|
|
{ immutable: true },
|
|
)
|
|
: undefined;
|
|
const completedFailure = nativeMachoCompletedFailure(completion?.value);
|
|
const completionCancellation =
|
|
completion?.cancellation === undefined
|
|
? undefined
|
|
: cancelledBy(
|
|
operation,
|
|
carriedReason(completion.cancellation.reason),
|
|
completedFailure === undefined ||
|
|
completedFailure.cleanupIncomplete ||
|
|
(cause instanceof ArtifactReaderFailure &&
|
|
cause.cleanup !== undefined)
|
|
? {}
|
|
: {
|
|
diagnostics: {
|
|
previous_error: jsonValueSchema.parse(
|
|
projectAnalysisError(completedFailure),
|
|
),
|
|
},
|
|
},
|
|
);
|
|
return { observation, completedFailure, completionCancellation };
|
|
};
|
|
|
|
/** Every native reclassification retains the command and independent file owners. */
|
|
export const nativeFailureWithCommandCleanup = (
|
|
primary: AnalysisError,
|
|
producer: AnalysisError,
|
|
operation: string,
|
|
descriptorCleanup?: AnalysisError,
|
|
): AnalysisError => {
|
|
if (!producer.cleanupIncomplete) return primary;
|
|
const cleanup = descriptorCleanup ?? producer;
|
|
return cleanup === primary
|
|
? primary
|
|
: analysisErrorWithCleanupFailure(
|
|
primary,
|
|
cleanup,
|
|
operation,
|
|
).bindCleanupDependencies([
|
|
primary,
|
|
producer,
|
|
...(descriptorCleanup === undefined ? [] : [descriptorCleanup]),
|
|
]);
|
|
};
|
|
|
|
/** The exact callback error remains an owner even if verification reclassifies it. */
|
|
const nativeMachoProducerFailure = (
|
|
cause: unknown,
|
|
): AnalysisError | undefined => {
|
|
if (cause instanceof AnalysisError) return cause;
|
|
if (!(cause instanceof NativeMachoSourceVerificationFailure))
|
|
return undefined;
|
|
return cause.outcome.kind === "failed"
|
|
? nativeMachoProducerFailure(cause.outcome.cause)
|
|
: nativeMachoCompletedFailure(cause.outcome.value);
|
|
};
|
|
|
|
export const nativeMachoSourceFailure = (
|
|
cause: unknown,
|
|
target: BinaryTarget,
|
|
parameters: Readonly<Record<string, JsonValue>>,
|
|
operation: "inspect_macho" | "list_architectures",
|
|
): AnalysisError => {
|
|
const { observation, completedFailure, completionCancellation } =
|
|
nativeMachoCompletion(cause, target, parameters, operation);
|
|
const source = artifactFilePrimaryFacts(cause);
|
|
const producerFailure =
|
|
completedFailure ?? nativeMachoProducerFailure(source.cause);
|
|
// Classification and ownership are independent: neither a changed source nor
|
|
// a released descriptor settles the command's retained cleanup capability.
|
|
const retainOwnedCleanup = (
|
|
selected: AnalysisError,
|
|
descriptorCleanup?: AnalysisError,
|
|
): AnalysisError => {
|
|
if (!producerFailure?.cleanupIncomplete) return selected;
|
|
const primary =
|
|
selected === descriptorCleanup
|
|
? (descriptorCleanup.cleanupPrimary ?? producerFailure)
|
|
: selected === completionCancellation
|
|
? producerFailure
|
|
: selected;
|
|
if (completionCancellation !== undefined)
|
|
analysisErrorWithCancellation(primary, completionCancellation);
|
|
return nativeFailureWithCommandCleanup(
|
|
primary,
|
|
producerFailure,
|
|
operation,
|
|
descriptorCleanup,
|
|
);
|
|
};
|
|
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) {
|
|
const physical =
|
|
cause instanceof ArtifactFileCleanupFailure
|
|
? cause.fileCleanupFailure
|
|
: undefined;
|
|
const previous =
|
|
physical?.outcome.kind === "failed"
|
|
? nativeMachoSourcePrimary(
|
|
artifactFilePrimaryFacts(cause),
|
|
target.path,
|
|
operation,
|
|
)
|
|
: physical?.outcome.kind === "completed"
|
|
? (completedFailure ?? completionCancellation)
|
|
: completionCancellation;
|
|
const failure = new ProviderCleanupError(
|
|
IDENTITY.id,
|
|
cause.cleanup.resources,
|
|
{
|
|
reason: cause.cleanup.reason,
|
|
previous_error:
|
|
previous === undefined
|
|
? null
|
|
: jsonValueSchema.parse(projectAnalysisError(previous)),
|
|
...(completionCancellation !== undefined &&
|
|
previous !== completionCancellation
|
|
? {
|
|
completion_cancellation: jsonValueSchema.parse(
|
|
projectAnalysisError(completionCancellation),
|
|
),
|
|
}
|
|
: {}),
|
|
},
|
|
{
|
|
operation,
|
|
cause,
|
|
cleanupPrimary: previous,
|
|
partialObservation: observation,
|
|
...(completionCancellation === undefined && observation !== undefined
|
|
? { completedObservation: observation }
|
|
: {}),
|
|
},
|
|
);
|
|
// Do not embed the potentially large observation in the projected
|
|
// cancellation diagnostics as well as the cleanup error's observation.
|
|
if (observation !== undefined)
|
|
completionCancellation?.retainPartialObservation(observation);
|
|
bindArtifactFileCleanup(cause, failure);
|
|
return retainOwnedCleanup(failure, failure);
|
|
}
|
|
if (completionCancellation !== undefined) {
|
|
if (observation !== undefined)
|
|
completionCancellation.retainPartialObservation(observation);
|
|
return retainOwnedCleanup(completionCancellation);
|
|
}
|
|
return retainOwnedCleanup(
|
|
nativeMachoSourcePrimary(source, target.path, operation),
|
|
);
|
|
};
|
|
|
|
export interface NativeObservation {
|
|
readonly result: JsonValue;
|
|
readonly limitations: readonly string[];
|
|
readonly locations: readonly EvidenceLocation[];
|
|
}
|
|
|
|
export const isNativeOperation = (
|
|
operation: AnalysisOperation,
|
|
): operation is NativeToolName =>
|
|
NATIVE_TOOL_CONTRACTS.some(({ name }) => name === operation);
|
|
|
|
const translateCodeSignExitFailure = (
|
|
capture: NativeCommandCapture,
|
|
resources: ArtifactResourceScope,
|
|
): AnalysisError =>
|
|
translateNativeCommandFailure(
|
|
"inspect_signature",
|
|
new NativeCommandFailure(
|
|
"codesign",
|
|
"nonzero-exit",
|
|
capture.exitCode,
|
|
undefined,
|
|
// Accepted output stays complete until the provider decides it failed.
|
|
failureCapture(capture, false),
|
|
),
|
|
resources,
|
|
);
|
|
|
|
/**
|
|
* Accept only codesign's exact unsigned diagnostic for the bound snapshot,
|
|
* optionally preceded by its verified Executable path echo.
|
|
* The operand is framed byte-for-byte so caller-controlled path text cannot
|
|
* supply the phrase that determines signature state.
|
|
*/
|
|
export const isNonzeroUnsignedObservation = (
|
|
capture: NativeCommandCapture,
|
|
snapshotPath: string,
|
|
executablePaths: readonly string[],
|
|
): boolean => {
|
|
if (
|
|
capture.exitCode === null ||
|
|
capture.exitCode === 0 ||
|
|
capture.signal !== null ||
|
|
capture.arguments.at(-1) !== snapshotPath ||
|
|
capture.stdout !== ""
|
|
)
|
|
return false;
|
|
const diagnostic = `${snapshotPath}: code object is not signed at all`;
|
|
return [
|
|
"",
|
|
...executablePaths.flatMap((path) => [
|
|
`Executable=${path}\n`,
|
|
`Executable=${path}\r\n`,
|
|
]),
|
|
].some((prefix) =>
|
|
[diagnostic, `${diagnostic}\n`, `${diagnostic}\r\n`].some(
|
|
(body) => capture.stderr === `${prefix}${body}`,
|
|
),
|
|
);
|
|
};
|
|
|
|
export const canonicalPath = async (path: string): Promise<string[]> => {
|
|
try {
|
|
return [await realpath(path)];
|
|
} catch {
|
|
return [];
|
|
}
|
|
};
|
|
|
|
export const codeSignCaptureFailure = (
|
|
resources: ArtifactResourceScope,
|
|
capture: NativeCommandCapture,
|
|
snapshotPath: string,
|
|
executablePaths: readonly string[],
|
|
): AnalysisError | null =>
|
|
capture.exitCode === 0 ||
|
|
isNonzeroUnsignedObservation(capture, snapshotPath, executablePaths)
|
|
? null
|
|
: translateCodeSignExitFailure(capture, resources);
|
|
|
|
export const codeSignArchitectures = (
|
|
format: string | null,
|
|
target: BinaryTarget,
|
|
): { architectures: string[]; unknownSlices: boolean } => {
|
|
// An architecture-selected codesign display describes the selected slice,
|
|
// not the admitted container. The target owns the container's slice list.
|
|
if (target.kind === "executable" && target.format === "mach-o-universal") {
|
|
if (target.universalSlices === undefined)
|
|
return { architectures: [], unknownSlices: true };
|
|
const slices = target.universalSlices;
|
|
// Each known selector names exactly one slice, so known names are unique.
|
|
const names = slices.map((_, index) =>
|
|
machoKnownCodeSignSelector(slices, index),
|
|
);
|
|
const known = names.filter((name): name is string => name !== undefined);
|
|
return {
|
|
architectures: known,
|
|
unknownSlices: known.length !== names.length,
|
|
};
|
|
}
|
|
const universal = /^Mach-O universal \(([^)\r\n]+)\)$/u.exec(
|
|
format ?? "",
|
|
)?.[1];
|
|
if (universal !== undefined) {
|
|
const architectures = universal
|
|
.split(/\s+/u)
|
|
.filter((architecture) => /^[A-Za-z][A-Za-z0-9_.]*$/u.test(architecture));
|
|
if (architectures.length > 0)
|
|
return {
|
|
architectures: [...new Set(architectures)],
|
|
unknownSlices: false,
|
|
};
|
|
}
|
|
if (format?.startsWith("Mach-O thin ") === true)
|
|
return { architectures: [], unknownSlices: false };
|
|
if (target.kind !== "executable" || !isMachOFormat(target.format))
|
|
return { architectures: [], unknownSlices: false };
|
|
return {
|
|
architectures: target.availableArchitectures.map((architecture) =>
|
|
architecture === "x86" ? "i386" : architecture,
|
|
),
|
|
unknownSlices: false,
|
|
};
|
|
};
|
|
|
|
export const invocation = (
|
|
capture: NativeCommandCapture,
|
|
artifactPath: string,
|
|
alias = "$ARTIFACT",
|
|
): NativeCommandInvocation => ({
|
|
tool: capture.tool,
|
|
command: [
|
|
capture.executable,
|
|
...capture.arguments.map((argument) =>
|
|
argument === artifactPath ? alias : argument,
|
|
),
|
|
],
|
|
tool_version: capture.toolVersion,
|
|
version_reason: capture.versionReason,
|
|
executable_sha256: capture.executableSha256,
|
|
exit: { code: capture.exitCode, signal: capture.signal },
|
|
stdout_bytes: capture.stdoutBytes,
|
|
stderr_bytes: capture.stderrBytes,
|
|
});
|
|
|
|
export const nativePlistObservation = (input: {
|
|
readonly parsed: ParsedPlist;
|
|
readonly format: z.infer<typeof inspectPlistSchema>["format"];
|
|
readonly path: string;
|
|
readonly sourceSha256: string;
|
|
readonly resourceRoot: ResolvedAppBundleResourceRoot | undefined;
|
|
readonly provenance: readonly NativeCommandInvocation[];
|
|
}): NativeObservation => {
|
|
const { parsed, format, path, sourceSha256, resourceRoot, provenance } =
|
|
input;
|
|
const result = inspectPlistSchema.parse({
|
|
format,
|
|
value: parsed.value,
|
|
bundle: parsed.bundle,
|
|
source_path: path,
|
|
source_sha256: sourceSha256,
|
|
...(resourceRoot === undefined
|
|
? {}
|
|
: {
|
|
resource_root: {
|
|
caller_path: resourceRoot.callerPath,
|
|
canonical_path: resourceRoot.canonicalPath,
|
|
info_plist_path: resourceRoot.infoPlistPath,
|
|
layout: resourceRoot.layout,
|
|
executable_sha256: resourceRoot.executableSha256,
|
|
},
|
|
}),
|
|
provenance,
|
|
limitations: parsed.limitations,
|
|
});
|
|
return {
|
|
result: ownedJsonValueSchema.parse(result),
|
|
limitations: parsed.limitations,
|
|
locations: [{ kind: "artifact-path", path }],
|
|
};
|
|
};
|
|
|
|
export const parseEntitlements = (
|
|
output: string,
|
|
): {
|
|
readonly value: JsonValue;
|
|
readonly limitations: readonly string[];
|
|
} => {
|
|
const start = output.indexOf("<?xml");
|
|
const end = output.lastIndexOf("</plist>");
|
|
if (start < 0 || end < start) return { value: null, limitations: [] };
|
|
const decoded = parseXmlPropertyList(
|
|
output.slice(start, end + "</plist>".length),
|
|
);
|
|
const projected = projectPlistValue(decoded.value);
|
|
const numbers = createPlistNumberProjection(decoded.numberLiterals);
|
|
const value = numbers.project(projected.value);
|
|
return {
|
|
value,
|
|
limitations: [
|
|
...numbers.limitations(),
|
|
...plistDuplicateKeyLimitations(decoded.duplicateKeys),
|
|
...(projected.nonFiniteRealCount === 0
|
|
? []
|
|
: [nonFiniteRealLimitation(projected.nonFiniteRealCount)]),
|
|
...(projected.negativeZeroRealCount === 0
|
|
? []
|
|
: [negativeZeroRealLimitation(projected.negativeZeroRealCount)]),
|
|
...(projected.unknownDateCount === 0
|
|
? []
|
|
: [unknownDateLimitation(projected.unknownDateCount)]),
|
|
...(projected.unknownScalarCount === 0
|
|
? []
|
|
: [unknownScalarLimitation(projected.unknownScalarCount)]),
|
|
],
|
|
};
|
|
};
|
|
|
|
/** Native admission failures preserve the selected/default source and physical command output. */
|
|
export const nativePlistDecodeFailure = (
|
|
selection: NativePlistSelection,
|
|
command: NativeCommandCapture,
|
|
operand: string,
|
|
): AnalysisError => {
|
|
const capture = failureCapture(command, false);
|
|
return unreadablePlist(
|
|
selection,
|
|
`plutil could not decode it as a property list (${plutilDiagnostic(capture, operand, selection.path)})`,
|
|
{
|
|
capturedOutput: {
|
|
stdout: capture.stdout,
|
|
stderr: capture.stderr,
|
|
truncated: capture.truncated,
|
|
stdout_bytes: capture.stdoutBytes,
|
|
stderr_bytes: capture.stderrBytes,
|
|
exit_code: capture.exitCode,
|
|
signal: capture.signal,
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
/**
|
|
* The reason REA's XML reader refused a plist's syntax. CoreFoundation accepts
|
|
* some documents that strict XML rejects (#12234), so plutil decides those.
|
|
* Plist structure errors and refused document types are not syntax refusals.
|
|
*/
|
|
export const xmlSyntaxRefusal = (error: unknown): string | undefined => {
|
|
const cause = error instanceof AnalysisOutputError ? error.cause : undefined;
|
|
return cause instanceof ParseFailureError &&
|
|
cause.failure.format === "xml" &&
|
|
cause.kind === "malformed"
|
|
? cause.message
|
|
: undefined;
|
|
};
|
|
|
|
const plutilDiagnostic = (
|
|
capture: Pick<NativeCommandCapture, "stdout" | "stderr">,
|
|
operand: string,
|
|
sourcePath: string,
|
|
): string => {
|
|
const diagnostics = [capture.stderr, capture.stdout]
|
|
.map((stream) => {
|
|
const text = stream.trim();
|
|
const path = [operand, sourcePath].find((path) =>
|
|
text.startsWith(`${path}: `),
|
|
);
|
|
return path === undefined ? text : text.slice(path.length + 2);
|
|
})
|
|
.filter((text) => text.length > 0);
|
|
return diagnostics.length === 0
|
|
? "plutil printed no diagnostic"
|
|
: diagnostics.join("\n");
|
|
};
|