Files
rea/tests/acceptance/peLoadConfigCli.test.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

147 lines
5.4 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import {
PE_LOAD_CONFIG_FIXTURE,
peLoadConfigImage,
} from "../../src/native/pe/PeLoadConfig.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
for (const bits of [32, 64] as const) {
cliTest(
`reads a valid PE${String(bits)} load config across CLI and MCP`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pe-config-valid-");
const call = await cli.sharedMcpCall!();
const path = join(root, "valid.exe");
const bytes = peLoadConfigImage({ bits });
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-pe-load-config", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const response = await call("inspect_pe_load_config", { path });
expect(response.isError).not.toBe(true);
for (const value of [result.json, response.structuredContent]) {
const report = toolContract(
"inspect_pe_load_config",
).outputSchema.parse(value).normalized_result;
expect(report.bits).toBe(bits);
expect(report.tls?.callbacks).toHaveLength(3);
expect(report.load_config?.guard_flags?.raw).toBe(0x500);
}
expect((await readFile(path)).equals(bytes)).toBe(true);
},
);
cliTest(
`reports PE${bits} TLS template metadata across section extent boundaries`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-pe-config-padding-cli-");
for (const [range, virtualSize, fileOffset, section] of [
[[0x2010, 0x2020], 0x20, 0x1610, ".text"],
[[0x2010, 0x2020], 0, 0x1610, ".text"],
[[0x2180, 0x2280], undefined, null, ".text"],
[[0x2200, 0x2300], undefined, null, ".text"],
[[0x2010, 0x2020], 0x10, null, null],
] as const) {
const zeroFillCallback = virtualSize === undefined;
await writeFile(
join(root, "app.exe"),
peLoadConfigImage({
bits,
zeroFillCallback,
...(virtualSize === undefined
? {}
: { textVirtualSize: virtualSize }),
rawDataRange: range,
}),
);
const result = await cli.run({
arguments: ["inspect-pe-load-config", "app.exe", "--json"],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract(
"inspect_pe_load_config",
).outputSchema.parse(result.json);
const base = bits === 32 ? 0x10000000n : 0x180000000n;
expect(evidence.normalized_result.tls).toMatchObject({
start_address_of_raw_data: `0x${(base + BigInt(range[0])).toString(16)}`,
end_address_of_raw_data: `0x${(base + BigInt(range[1])).toString(16)}`,
raw_data_size: range[1] - range[0],
raw_data_file_offset: fileOffset,
raw_data_section: section,
});
expect(evidence.normalized_result.tls?.callbacks).toHaveLength(
zeroFillCallback ? 1 : 3,
);
expect(evidence.normalized_result.load_config?.guard_flags?.raw).toBe(
0x500,
);
expect(evidence.subject?.local_path).toBe(join(root, "app.exe"));
}
},
);
cliTest(`reads PE${bits} TLS callbacks through the CLI`, async ({ cli }) => {
const root = await createTestTempDirectory("rea-pe-config-cli-");
await writeFile(join(root, "app.exe"), peLoadConfigImage({ bits }));
const result = await cli.run({
arguments: ["inspect-pe-load-config", "app.exe", "--json"],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_pe_load_config").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.bits).toBe(bits);
expect(evidence.normalized_result.tls?.callbacks).toHaveLength(3);
expect(evidence.normalized_result.load_config).toMatchObject({
dynamic_value_reloc_table_offset:
PE_LOAD_CONFIG_FIXTURE.dynamicRelocOffset,
dynamic_value_reloc_table_section:
PE_LOAD_CONFIG_FIXTURE.dynamicRelocSection,
});
expect(evidence.subject?.local_path).toBe(join(root, "app.exe"));
});
}
cliTest("fails closed at the callback limit", async ({ cli }) => {
const root = await createTestTempDirectory("rea-pe-config-budget-");
const path = join(root, "app.exe");
await writeFile(path, peLoadConfigImage());
const result = await cli.run({
arguments: [
"inspect-pe-load-config",
path,
"--max-callbacks",
"1",
"--json",
],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "resource_constraint",
retryable: false,
details: { reported_limits: { boundary: "callbacks", maximum: 1 } },
remediation: {
action: expect.stringContaining("Pass a larger --max-callbacks"),
},
});
const retried = await cli.run({
arguments: [
"inspect-pe-load-config",
path,
"--max-callbacks",
"3",
"--json",
],
});
expect(retried.exitCode, retried.stderr).toBe(0);
const report = toolContract("inspect_pe_load_config").outputSchema.parse(
retried.json,
).normalized_result;
expect(report.tls?.callbacks).toHaveLength(3);
});