mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 10:04:50 +00:00
* fix(macho)!: report a FAT header or table cut off by the file's end as truncated The universal Mach-O container reader measured the file's size but reported a FAT header shorter than 8 bytes as malformed, and a FAT table that runs past the file's end as not FAT at all. Callers could not tell a cut-off download from corrupt bytes. readFatContainerHeader now returns a typed truncated state carrying the measured file size and the length the header or table needs. A CAFEBABE count that reads as a JVM class version stays not FAT, because a short class file is not evidence of a universal file. Universal image selection, the native container reader and the lipo-selected slice resolver raise that fact as a truncated ParseFailure with an extent, and the malformed-target projection reports it as details.available_bytes and details.required_bytes. Complete but corrupt FAT data (zero count, overlapping slices, slices inside the table) stays malformed. BREAKING CHANGE: inspect_go_binary, inspect_rust_binary, inspect_dwarf_units, inspect_native_initializers, inspect_native_dispatch_metadata, the IL2CPP binary readers and the macOS provider's lipo-selected slice reads now report a FAT header or table cut off by the file's end with details.format_failure "truncated" (previously "malformed", or an unsupported or not-universal result for a table past the end), plus details.available_bytes and details.required_bytes. * test(macho): type every format-owner row with its asserted format failure
250 lines
8.4 KiB
TypeScript
250 lines
8.4 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import {
|
|
fatImage,
|
|
machoImage,
|
|
} from "../../src/artifacts/apple/MachoImage.fixture.js";
|
|
import { buildManagedPeFixture } from "../../src/dotnet/ManagedPe.fixture.js";
|
|
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 managed PE whose first section's raw data starts past the end of the file. */
|
|
const managedSectionPastEof = () => {
|
|
const bytes = buildManagedPeFixture();
|
|
const pe = bytes.readUInt32LE(0x3c);
|
|
const sections = pe + 24 + bytes.readUInt16LE(pe + 20);
|
|
bytes.writeUInt32LE(bytes.length + 0x1000, sections + 20);
|
|
return bytes;
|
|
};
|
|
|
|
/** A universal Mach-O whose first declared slice runs into the second. */
|
|
const overlappingUniversalMacho = () => {
|
|
const slice = (type: number, subtype: number) => ({
|
|
cpu: { type, subtype },
|
|
bytes: machoImage({ cpu: { type, subtype }, wide: true }),
|
|
});
|
|
const bytes = Buffer.from(
|
|
fatImage([slice(0x01000007, 3), slice(0x0100000c, 0)]),
|
|
);
|
|
const first = bytes.readUInt32BE(8 + 8);
|
|
const second = bytes.readUInt32BE(8 + 20 + 8);
|
|
bytes.writeUInt32BE(second - first + 16, 8 + 12);
|
|
return bytes;
|
|
};
|
|
|
|
/** A universal Mach-O cut off inside its FAT table: 20 of its 48 table bytes. */
|
|
const truncatedUniversalMacho = () => {
|
|
const slice = (type: number, subtype: number) => ({
|
|
cpu: { type, subtype },
|
|
bytes: machoImage({ cpu: { type, subtype }, wide: true }),
|
|
});
|
|
return Buffer.from(
|
|
fatImage([slice(0x01000007, 3), slice(0x0100000c, 0)]),
|
|
).subarray(0, 20);
|
|
};
|
|
|
|
const nativeUniversalCommands = [
|
|
"inspect-go-binary",
|
|
"inspect-rust-binary",
|
|
"inspect-dwarf-units",
|
|
"inspect-native-initializers",
|
|
];
|
|
|
|
// Format owners with their own mappers: a managed PE/CLI reader and the
|
|
// universal Mach-O container read before slice selection. The selected file's
|
|
// bytes are at fault, so each is a malformed or truncated target at that
|
|
// file's path; a truncated one carries the measured and needed lengths.
|
|
interface FormatOwnerRow {
|
|
readonly command: string;
|
|
readonly bytes: () => Buffer;
|
|
readonly failure: "malformed" | "truncated";
|
|
readonly format: { readonly format: string } & Record<string, unknown>;
|
|
readonly extra: readonly string[];
|
|
readonly identity: boolean;
|
|
}
|
|
|
|
for (const { command, bytes, failure, format, extra, identity } of [
|
|
...[
|
|
"inspect-managed-artifact",
|
|
"inspect-managed-members",
|
|
"inspect-managed-native-boundaries",
|
|
].map((command): FormatOwnerRow => ({
|
|
command,
|
|
bytes: managedSectionPastEof,
|
|
failure: "malformed",
|
|
format: { format: "pe-cli", field: "pe.section-data" },
|
|
extra: [],
|
|
identity: false,
|
|
})),
|
|
...nativeUniversalCommands.map((command): FormatOwnerRow => ({
|
|
command,
|
|
bytes: overlappingUniversalMacho,
|
|
failure: "malformed",
|
|
format: { format: "macho", reason: "Mach-O slices 0 and 1 overlap." },
|
|
extra: ["--architecture", "arm64"],
|
|
identity: true,
|
|
})),
|
|
...nativeUniversalCommands.map((command): FormatOwnerRow => ({
|
|
command,
|
|
bytes: truncatedUniversalMacho,
|
|
failure: "truncated",
|
|
format: { format: "macho", available_bytes: 20, required_bytes: 48 },
|
|
extra: ["--architecture", "arm64"],
|
|
identity: true,
|
|
})),
|
|
]) {
|
|
cliTest(
|
|
`${command} refuses ${failure} ${format.format} bytes as a ${failure} target`,
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-format-owner-");
|
|
const path = join(root, "malformed.bin");
|
|
const written = bytes();
|
|
await writeFile(path, written);
|
|
const result = await cli.run({
|
|
arguments: [command, path, ...extra, "--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: failure,
|
|
...format,
|
|
...(identity
|
|
? { partial_observation: fileIdentity(path, written) }
|
|
: {}),
|
|
},
|
|
});
|
|
expect(result.json).not.toHaveProperty("details.issues");
|
|
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");
|
|
},
|
|
);
|