Files
reverse-engineering-anything/src/resources/ArtifactOperationFailure.ts
T
a5aee4f9e9 feat(artifacts): inventory Android super image metadata (#14928)
* feat(artifacts): inventory Android super image metadata

Refs #14391

* fix(artifacts): preserve partial Android super metadata evidence

* fix(artifacts): recognize Android super PE overlays

Refs #14928

* fix(artifacts): reject an Android super device with no usable sector

A first usable sector at the device size was accepted, so extent checks
reported a different field. Keep stored copy failures to malformed,
truncated, and unsupported now that format failures include unrecognized.

* fix(artifacts): preserve Android super partial evidence on budget exhaustion

* fix(mcp): preserve existing provider partial observation contracts

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-30 06:15:43 -05:00

145 lines
5.3 KiB
TypeScript

import { analysisSettingLimit } from "../domain/analysisSettingDefinitions.js";
import { isFormatTargetFailureKind } from "../domain/targetFailure.js";
import {
projectZstandardFrameAllowance,
ZstandardFrameAllowanceError,
} from "../domain/compression/zstandardFrameAllowance.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { AnalysisTargetFailureError } from "../domain/analysisErrorCore.js";
import { ArtifactOperationError } from "../domain/artifactOperationError.js";
import { InternalInvariantError } from "../domain/internalInvariant.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import {
artifactFileCancellationCause,
bindArtifactFileCleanup,
} from "./ArtifactFileOperation.js";
/**
* Translate one artifact boundary failure while retaining cleanup evidence. A
* broken internal assumption is a REA defect, not an artifact read failure, so
* it is rethrown for the provider adapter to report.
*
* A format owner's refusal of the bytes becomes the target failure of `path`,
* the target the caller selected, whichever member the reader was reading.
*/
export const translateArtifactFailure = (
operation: string,
path: string,
cause: unknown,
): AnalysisError => {
if (cause instanceof InternalInvariantError) throw cause;
if (cause instanceof ZstandardFrameAllowanceError)
return projectZstandardFrameAllowance(operation, cause);
if (cause instanceof ArtifactReaderFailure) {
const target = artifactReaderTargetFailure(operation, path, cause);
const error = target ?? translateArtifactOperationFailure(operation, cause);
if (target !== undefined) bindArtifactFileCleanup(cause, target);
for (const secondary of cause.secondaryFailures ?? [])
error.retainSecondaryFailure(
translateArtifactFailure(operation, path, secondary),
);
return error;
}
// Keep typed Evidence and other analysis failures instead of reducing them to I/O.
if (cause instanceof AnalysisError) return cause;
return new ArtifactOperationError(operation, "io", undefined, undefined, {
cause,
});
};
/**
* Translate a reader failure of the operation rather than of its target, such
* as a cancellation or a cleanup failure. A format owner's refusal is a
* target failure and needs {@link translateArtifactFailure} with its path.
*/
export const translateArtifactOperationFailure = (
operation: string,
cause: ArtifactReaderFailure,
): ArtifactOperationError => {
const reason = artifactOperationReason(cause);
const error = new ArtifactOperationError(
operation,
reason,
cause.details,
cause.message,
{
...(cause.userMessage === undefined
? {}
: { userMessage: cause.userMessage }),
cause:
cause.reason === "cancelled"
? artifactFileCancellationCause(cause)
: cause,
...(cause.cleanup === undefined ? {} : { cleanup: cause.cleanup }),
...(cause.partialObservation === undefined
? {}
: { partialObservation: cause.partialObservation }),
...(cause.admission === undefined ? {} : { admission: cause.admission }),
...(cause.boundary === undefined ? {} : { boundary: cause.boundary }),
...(cause.readerLimit === undefined
? {}
: { readerLimit: cause.readerLimit }),
...(cause.parserLimit === undefined
? {}
: { parserLimit: cause.parserLimit }),
...(cause.fixedAllowance === undefined
? {}
: { fixedAllowance: cause.fixedAllowance }),
...(cause.integrityCheck === undefined
? {}
: { integrityCheck: cause.integrityCheck }),
},
);
if (cause.admission !== undefined)
error.bindLimit({
...analysisSettingLimit(
cause.admission.kind === "asar-header"
? "asar.max_header_bytes"
: "asar.max_listing_bytes",
cause.admission.maximumBytes,
cause.admission.source,
),
...(cause.admission.requiredBytes === undefined
? {}
: { required_value: cause.admission.requiredBytes }),
});
bindArtifactFileCleanup(cause, error);
return error;
};
/**
* The target failure a reader's format owner raised, as the refusal of
* `path`, with the reader's cleanup and completed observation. Undefined when
* `cause` is not a format owner's refusal.
*/
export const artifactReaderTargetFailure = (
operation: string,
path: string,
cause: unknown,
): AnalysisTargetFailureError | undefined =>
cause instanceof ArtifactReaderFailure && cause.parseFailure !== undefined
? new AnalysisTargetFailureError(operation, path, cause.parseFailure, {
cause,
...(cause.cleanup === undefined ? {} : { cleanup: cause.cleanup }),
...(cause.partialObservation === undefined
? {}
: { partialObservation: cause.partialObservation }),
})
: undefined;
/**
* The operation reason of a reader failure that carries no target failure.
* A target reason without the owner's facts is a reader defect.
*/
export const artifactOperationReason = (
cause: ArtifactReaderFailure,
): ArtifactOperationError["reason"] => {
const { reason } = cause;
if (isFormatTargetFailureKind(reason))
throw new InternalInvariantError(
`Artifact reader raised ${reason} without its format facts`,
{ cause },
);
return reason;
};