Files
rea/src/native/pe/PeResourcesProvider.ts
T
Kaoru0822andClaude Opus 5.5 e1def2bf1a fix(native): report missing and non-file PE resource targets as input errors
`inspect_pe_resources` turned every acquisition failure other than
limits and access denial into `artifact_operation_failed` with "Artifact
could not be read or written. Check file access and try again." A
missing path, a directory, a symlink or a path under a regular file got
that advice, although the caller selected a target that cannot be a PE
image.

Map the stable reader's non-regular-file refusal and ENOENT/ENOTDIR to
`AnalysisInputError` issues on `path`, and a file that changed during the
read to `AnalysisArtifactChangedError`, as the sibling EVM carrier read
does. Other I/O failures stay artifact faults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 02:30:25 +00:00

101 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 errorCode = (cause: unknown): unknown =>
cause instanceof Error && "code" in cause ? cause.code : undefined;
// A missing or non-regular caller selection is an input error, as for the
// EVM bytecode carrier read; only other I/O remains an artifact fault.
const peResourcesFailure = (
cause: unknown,
input: InspectPeResourcesInput,
signal: AbortSignal | undefined,
) => {
const operation = "inspect_pe_resources";
if (signal?.aborted) return new AnalysisCancelledError(operation, { cause });
const reason = cause instanceof ArtifactReaderFailure ? cause.reason : "io";
const message = cause instanceof Error ? cause.message : String(cause);
if (reason === "limit")
return new AnalysisResourceConstraintError(
operation,
"memory",
message,
{
max_file_bytes: input.max_file_bytes,
max_entries: input.max_entries,
},
{ cause },
);
if (reason === "path")
return new AnalysisInputError(operation, { cause }, [
{ path: ["path"], reason: "invalid_format", message },
]);
if (reason === "integrity")
return new AnalysisArtifactChangedError(operation, input.path, message, {
cause,
});
const code = errorCode(cause);
if (code === "ENOENT" || code === "ENOTDIR")
return new AnalysisInputError(operation, { cause }, [
{
path: ["path"],
reason: "invalid_value",
message: `Selected PE image could not be read (${code}): ${input.path}.`,
},
]);
if (code === "EACCES" || code === "EPERM")
return new AnalysisAccessDeniedError(operation, input.path, code, {
cause,
});
return new ArtifactOperationError(
operation,
reason,
undefined,
cause instanceof Error ? cause.message : "PE artifact inspection failed.",
{ cause },
);
};