Files
reverse-engineering-anything/tests/acceptance/stableArtifactParserFailureCli.test.ts
morluto 61b4ada453 fix(errors)!: split reader path refusals from malformed target bytes
ArtifactReaderFailure("format") covered two different facts: target
bytes that violate their format, and a selected path that is not a
regular file. The shared selected-file projections
(projectOwnedArtifactInputFailure and classifyProviderFailure's file
step) mapped both to invalid_request with an invalid_format path
issue, so corrupt BEAM files and PE certificate table bodies were
reported as caller argument errors.

Split the reason at its producers:

- Path-kind refusals now use the existing "path" selection reason:
  regular-file admission, the stable root, directory, ZIP, ASAR and
  PyInstaller entries, dyld inputs, the UEFI flash source and extract
  occurrence selection. They stay invalid_request.
- The BEAM parser and the PE certificate table parser raise the format
  owner's ParseFailureError (malformed or truncated, format "beam" or
  "pe-certificate-table"). A short file that does not start with FOR1
  is malformed, not truncated. A PKCS#7 name encoding the decoder does
  not implement is unsupported, not malformed.
- Short reads inside an admitted range (kernel module, kernel build
  config, ELF unwind ranges) are integrity read-coverage failures,
  because the file changed during the read.
- Internal contract breaks (stream chunk size, a non-Buffer keyed
  archive chunk) are invariant violations; an impossible descriptor
  byte count is an I/O failure.

The shared projections now treat only "path" as a selection error.
They take the malformed-target facts from the reader failure's own
parseFailure, which artifactParseFailure sets once from the owner's
ParseFailureError and rebuilt failures keep through primaryFacts.
ArtifactOperationError carries the same facts, so inspect_artifact
refusals (for example Android boot images) now include
details.format_failure, format, offset and field. Android boot entry
planning raises the format owner's malformed failure, and the
redundant shortcut and API-contract format branches are removed.

BREAKING CHANGE: callers that parse `code` see unsupported_target
instead of invalid_request for malformed or truncated bytes from
inspect_erlang_beam and from the inspect_pe_certificates certificate
table body. Truncated reads of a file that changed during
inspect_kernel_module, inspect_kernel_build_config and
inspect_elf_unwind_ranges now report artifact_changed. inspect_artifact
malformed and truncated refusals gain details.format_failure and
details.format. Existing fields are unchanged.
2026-11-23 20:11:49 -05:00

129 lines
4.4 KiB
TypeScript

import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
import { peCertificateFixture } from "../../src/native/pe/PeCertificates.fixture.js";
const fileIdentity = (path: string, bytes: Buffer) => ({
kind: "file-snapshot-hash",
path,
bytes: bytes.length,
sha256: createHash("sha256").update(bytes).digest("hex"),
});
for (const command of [
"inspect-python-pyc",
"inspect-erlang-beam",
"inspect-rust-binary",
"inspect-go-binary",
"inspect-elf-notes",
"inspect-elf-dependencies",
"inspect-elf-debug-link",
"inspect-elf-unwind-ranges",
"inspect-devicetree",
"inspect-pe-imports",
"inspect-kernel-module",
"inspect-apple-ips",
"inspect-pe-load-config",
"inspect-pe-certificates",
"inspect-pe-resources",
"inspect-native-exports",
"inspect-native-initializers",
"inspect-binary-hardening",
]) {
const operation = command.replaceAll("-", "_");
const isBeam = command === "inspect-erlang-beam";
cliTest(
`${command} retains the completed file identity after format refusal`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-format-refusal-");
const path = join(root, "foreign.bin");
const bytes = isBeam ? Buffer.from("FOR1") : Buffer.alloc(64, 0x7f);
await writeFile(path, bytes);
const result = await cli.run({ arguments: [command, path, "--json"] });
expect(result.exitCode, result.stderr).toBe(1);
expect(result.json).toMatchObject({
details: { partial_observation: fileIdentity(path, bytes) },
});
},
);
// One public owner for the byte readers' malformed-target projection: the
// bytes, not the request, are at fault, so no argument recovery is offered.
// Format-specific structure (an IPS body, a shortcut CLSID) keeps its own
// command test. A BEAM reader sees the ELF magic as a foreign container.
cliTest(
`${command} refuses a truncated ELF header as a malformed target`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-malformed-target-");
const path = join(root, "truncated.bin");
const bytes = Buffer.from("\x7fELF");
await writeFile(path, bytes);
const result = await cli.run({ arguments: [command, path, "--json"] });
expect(result.exitCode, result.stderr).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
category: "unsupported_target",
details: {
operation,
path,
format_failure: "malformed",
partial_observation: fileIdentity(path, bytes),
},
});
expect(result.json).not.toHaveProperty("recovery");
},
);
}
/** A PE image whose first WIN_CERTIFICATE declares less than its own header. */
const shortCertificateHeader = (): Buffer => {
const bytes = peCertificateFixture();
bytes.writeUInt32LE(4, bytes.readUInt32LE(0x98 + 96 + 4 * 8));
return bytes;
};
// Structure past the shared headers is refused by each format's own parser,
// which names its format and failure kind once for every adapter.
for (const { command, bytes, formatFailure, format } of [
{
command: "inspect-erlang-beam",
bytes: () => Buffer.from("FOR1"),
formatFailure: "truncated",
format: "beam",
},
{
command: "inspect-pe-certificates",
bytes: shortCertificateHeader,
formatFailure: "malformed",
format: "pe-certificate-table",
},
] as const)
cliTest(
`${command} refuses ${formatFailure} ${format} structure as the target's`,
async ({ cli }) => {
const root = await createTestTempDirectory("rea-format-body-");
const path = join(root, "target.bin");
const selected = bytes();
await writeFile(path, selected);
const result = await cli.run({ arguments: [command, path, "--json"] });
expect(result.exitCode, result.stderr).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
category: "unsupported_target",
retryable: false,
details: {
operation: command.replaceAll("-", "_"),
path,
format_failure: formatFailure,
format,
partial_observation: fileIdentity(path, selected),
},
});
expect(result.json).not.toHaveProperty("recovery");
},
);