Files
rea/src/native/pe/PeLoadConfigProvider.ts
T
morluto 0aca049128 feat(native)!: give the PE readers per-call text, entry and metadata budgets (#14659)
* feat(native)!: give the PE readers per-call text, entry and metadata budgets

inspect_pe_imports refused at a fixed 16 MiB of retained names,
inspect_pe_unwind_ranges at a fixed 2,097,152 exception-directory entries
and inspect_pe_resources at a fixed 8 MiB result. They now accept
max_text_bytes (default 16 MiB, at most 256 MiB), max_entries (default
2,097,152, at most 16,777,216) and max_metadata_bytes (default 8 MiB, at
most 256 MiB). The DLL, function and TLS callback refusals report
details.limit for max_dlls, max_functions and max_callbacks instead of
authored recovery, and an unwind refusal reports the declared entry count
as required_value.

The shared PE failure projection no longer maps an unnamed
BinaryResourceFailure to "The PE structural decoding allowance": no PE
reader produces one, since PE header and section tables are bounded by the
format.

Part of #14234 and #14235.

BREAKING CHANGE: the PE retained-text, exception-directory entry and
resource metadata refusals report details.limit instead of a fixed_ceiling
refusal.

* fix(native): update PE advertised schema summaries, test types, and assert retry success

* test: compare PE resource refusals across adapters with each adapter's control spelling

* test: avoid shadowing the CLI fixture in the PE resources parity check
2026-11-26 05:39:56 +00:00

112 lines
3.5 KiB
TypeScript

import {
maxFileBytesReadBudget,
PE_FILE_CEILING_BYTES,
} from "../../domain/artifactReadBudget.js";
import { projectPeBinaryFailure } from "./PeBinaryFailure.js";
import {
closeArtifactResources,
withStableArtifactHandle,
assertArtifactReadActive,
ArtifactResourceScope,
} from "../../resources/index.js";
import type { PeLoadConfigPort } from "../../ports/binaryDiagnostics/PeLoadConfigPort.js";
import type { ExecutionOptions } from "../../ports/AnalysisProvider.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import {
MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES,
peLoadConfigCallbackLimit,
type InspectPeLoadConfigInput,
} from "../../domain/native/peLoadConfig.js";
import { err, ok } from "../../domain/result.js";
import { PE_LOAD_CONFIG_PROVIDER } from "../../domain/investigationProviders.js";
import {
parsePeLoadConfigFromHandle,
PE_LOAD_CONFIG_LIMITS,
type PeLoadConfigLimit,
} from "./PeLoadConfigParser.js";
const OPERATION = "inspect_pe_load_config";
const PE_HEADER_PREFIX_BYTES = 4096;
/** Portable TLS/load-config reader over one bounded, identity-checked regular file. */
export class PeLoadConfigProvider implements PeLoadConfigPort {
readonly identity = PE_LOAD_CONFIG_PROVIDER;
readonly #resources = new ArtifactResourceScope();
close(): Promise<void> {
return closeArtifactResources(
() => this.#resources.close(),
PE_LOAD_CONFIG_PROVIDER,
OPERATION,
);
}
async inspect(input: InspectPeLoadConfigInput, options?: ExecutionOptions) {
let observed:
| { readonly sha256: string; readonly bytes: number }
| undefined;
try {
return await this.#resources.run(async () =>
withStableArtifactHandle(
input.path,
PE_FILE_CEILING_BYTES,
{
readBudget: maxFileBytesReadBudget(
input.max_file_bytes,
MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES,
),
retainBytes: (size) => Math.min(size, PE_HEADER_PREFIX_BYTES),
signal: options?.signal,
resourceScope: this.#resources,
onIdentity: (identity) => {
observed = identity;
},
},
async (handle) => {
assertArtifactReadActive(options?.signal);
const report = await parsePeLoadConfigFromHandle(
handle,
{
path: input.path,
sha256: handle.sha256,
bytes: handle.size,
},
input,
options?.signal,
);
assertArtifactReadActive(options?.signal);
return ok(report);
},
),
);
} catch (cause) {
return err(peLoadConfigFailure(cause, input, observed));
}
}
}
const peLoadConfigFailure = (
cause: unknown,
input: InspectPeLoadConfigInput,
observed?: { readonly sha256: string; readonly bytes: number },
): AnalysisError =>
projectPeBinaryFailure<PeLoadConfigLimit>(
cause,
input,
{
operation: OPERATION,
providerId: PE_LOAD_CONFIG_PROVIDER.id,
fileCeiling: {
name: "The PE whole-file ceiling (32-bit file offsets)",
bytes: PE_FILE_CEILING_BYTES,
},
limits: PE_LOAD_CONFIG_LIMITS,
// A running callback count knows only the budget it passed.
limitRecovery: (_limit, failure) => ({
retryable: false,
limit: peLoadConfigCallbackLimit(failure.maximum_bytes),
}),
},
observed,
);