Report missing and non-file selections as input issues, malformed PE images as format errors, and unstable reads as artifact changes. Keep existing limit, permission, cancellation, and other I/O classifications. Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
|
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
|
import type { PeResourcesPort } from "../../application/binaryDiagnostics/PeResourcesPort.js";
|
|
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
|
|
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
|
|
import {
|
|
AnalysisAccessDeniedError,
|
|
AnalysisArtifactChangedError,
|
|
AnalysisCancelledError,
|
|
AnalysisInputError,
|
|
AnalysisResourceConstraintError,
|
|
} from "../../domain/analysisErrorCore.js";
|
|
import type { InspectPeResourcesInput } from "../../domain/native/peResources.js";
|
|
import { err, ok } from "../../domain/result.js";
|
|
import { PE_RESOURCES_PROVIDER } from "../../application/InvestigationProviders.js";
|
|
import { parsePeResources } from "./PeResourceParser.js";
|
|
|
|
/** Portable parser over one bounded, identity-checked regular file. */
|
|
export class PeResourcesProvider implements PeResourcesPort {
|
|
readonly identity = PE_RESOURCES_PROVIDER;
|
|
|
|
async inspect(input: InspectPeResourcesInput, options?: ExecutionOptions) {
|
|
try {
|
|
const snapshot = await readStableArtifact(
|
|
input.path,
|
|
input.max_file_bytes,
|
|
options?.signal,
|
|
);
|
|
return ok(
|
|
await parsePeResources(
|
|
snapshot.bytes,
|
|
{
|
|
path: input.path,
|
|
sha256: snapshot.sha256,
|
|
bytes: snapshot.bytes.length,
|
|
},
|
|
input,
|
|
options?.signal,
|
|
),
|
|
);
|
|
} catch (cause) {
|
|
return err(peResourcesFailure(cause, input, options?.signal));
|
|
}
|
|
}
|
|
}
|
|
|
|
const peResourcesFailure = (
|
|
cause: unknown,
|
|
input: InspectPeResourcesInput,
|
|
signal: AbortSignal | undefined,
|
|
) => {
|
|
const operation = "inspect_pe_resources";
|
|
if (signal?.aborted) return new AnalysisCancelledError(operation, { cause });
|
|
|
|
if (cause instanceof ArtifactReaderFailure) {
|
|
if (cause.reason === "limit")
|
|
return new AnalysisResourceConstraintError(
|
|
operation,
|
|
"memory",
|
|
cause.message,
|
|
{
|
|
max_file_bytes: input.max_file_bytes,
|
|
max_entries: input.max_entries,
|
|
},
|
|
{ cause },
|
|
);
|
|
if (cause.reason === "integrity")
|
|
return new AnalysisArtifactChangedError(
|
|
operation,
|
|
input.path,
|
|
cause.message,
|
|
{ cause },
|
|
);
|
|
if (cause.reason === "path" || cause.reason === "format")
|
|
return new AnalysisInputError(operation, { cause }, [
|
|
{
|
|
path: ["path"],
|
|
reason: "invalid_format",
|
|
message: cause.message,
|
|
},
|
|
]);
|
|
if (cause.reason === "cancelled")
|
|
return new AnalysisCancelledError(operation, { cause });
|
|
}
|
|
|
|
if (cause instanceof Error && "code" in cause) {
|
|
if (cause.code === "EACCES" || cause.code === "EPERM")
|
|
return new AnalysisAccessDeniedError(operation, input.path, cause.code, {
|
|
cause,
|
|
});
|
|
if (cause.code === "ENOENT" || cause.code === "ENOTDIR")
|
|
return new AnalysisInputError(operation, { cause }, [
|
|
{
|
|
path: ["path"],
|
|
reason: "invalid_value",
|
|
message: `Selected PE image could not be read (${cause.code}): ${input.path}.`,
|
|
},
|
|
]);
|
|
}
|
|
|
|
return new ArtifactOperationError(
|
|
operation,
|
|
cause instanceof ArtifactReaderFailure ? cause.reason : "io",
|
|
undefined,
|
|
cause instanceof Error ? cause.message : "PE artifact inspection failed.",
|
|
{ cause },
|
|
);
|
|
};
|