Files
rea/tests/acceptance/elfNotesCli.test.ts
T
morluto 89c1dd3c32 fix(errors): refuse malformed target bytes as a target, not invalid input (#14195)
* fix(errors): refuse malformed target bytes as a target, not invalid input

parseFailureAnalysisError is the shared owner that turns a byte-format
parser's ParseFailure into a public refusal. It mapped malformed and
truncated bytes to an AnalysisInputError on the path field. The envelope
then reported invalid_request, with inspect_input_contract recovery and
"correct the listed fields". The arguments were valid; the target bytes
were the problem. Readers that wrap the ParseFailureError in an
ArtifactReaderFailure, and ArtifactOperationError reason=malformed, were
classified the same way.

Add AnalysisMalformedTargetError. It keeps the parser's kind, format,
detail, offset and field, and projects as unsupported_target with
details.format_failure. Its recovery is to choose another operation or a
well-formed copy of the target. Route both parser entry points through it
and reclassify ArtifactOperationError reason=malformed. Argument and
selection errors (missing files, directories, symlinks, JSON arguments)
stay invalid_request.

Fixes #14170

* docs(maven): describe duplicate variant names as malformed metadata

Duplicate Gradle variant names go through malformedInput, which now
reports unsupported_target with details.format_failure: malformed.
Point the earlier statement at that section instead of the old
invalid_format code.

* test(errors): expect malformed-target refusals from CLI byte-format readers

Truncated IPS, ELF, PE and shortcut bytes now project as unsupported_target with format_failure=malformed. Update the CLI acceptance cases that still expected invalid_request.

* fix(errors): keep a cancelled read a cancellation whatever its abort reason

projectOwnedArtifactInputFailure read the reader failure's cause without
checking the reader's own reason. A cancelled read carries the caller's
opaque abort reason as that cause, so aborting with a ParseFailureError
turned the cancellation into a non-retryable malformed-target refusal
before the shared cancellation projection ran. Only a reader whose own
reason is malformed or truncated now yields a malformed target.

The ELF dependency checkpoint cancellation test now also aborts with a
parser failure as the reason, through the public provider read.

* test(pe): expect malformed-target refusals from the PE filesystem boundaries

Short non-PE bytes now refuse the target rather than the request field.
The missing-file, directory, symlink and child-path controls keep their
caller-input expectations.

* fix(elf): refuse malformed ELF note bytes as a target, not invalid input

inspect_elf_notes shared this PR's root cause: its parser reported
structural violations as a reader "format" failure, which the shared
stable-artifact projection treats as a request-field error, so truncated
or non-ELF bytes still returned invalid_request. The parser now raises
malformed or truncated ParseFailureErrors, which the shared projection
reports as a malformed target with format_failure and format facts.

Consumers that interpreted the old reader reason now use the parser's
facts: debug-link build-id observation keys on ParseFailureError kinds,
and binary hardening drops its rewrap of note-parser format failures
into BinaryFormatFailure, which the parser's own failure supersedes.

* test(errors): own the byte readers' malformed-target projection in one CLI table

The PR repeated the same malformed -> unsupported_target mapping in a
dedicated CLI test per command and again in each provider's filesystem
boundary test. The stable-artifact parser-failure CLI table now owns it:
every byte reader it covers (now including ELF dependencies, debug link,
unwind ranges and native initializers) refuses a truncated ELF header as
a malformed target with operation, path, format_failure, retained file
identity and no argument recovery.

Removed as redundant with that table: the truncated or non-ELF/non-PE
CLI tests for ELF debug link, dependencies, unwind ranges and notes,
native initializers, and PE imports and certificates; and the
malformed-file blocks in thirteen filesystem boundary tests, which keep
their directory, symlink, missing-file and child-path controls. Format
specific refusals (a truncated IPS body, a wrong shortcut CLSID, DWARF
units) keep their own tests, as do the owner tests in
parseFailureAnalysisError and ArtifactOperationFailure.

* test(errors): expect malformed target bytes as unsupported_target in acceptance tests

The first full acceptance run of this PR found five expectations still
asserting invalid_request for target-byte failures: the timing test's
truncated ELF, a truncated DTB, duplicate Rust dependency JSON, and PE32
and PE64 load-config images without an MZ header. Assert the
unsupported_target code, with format_failure malformed where the
non-MZ PE path matches the already-updated PE imports test.
2026-11-24 00:44:40 +00:00

225 lines
7.8 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 {
compressedElfNoteFixture,
compressedElfNoteSectionFixture,
elfExtendedSectionCountFixture,
elfNote,
elfNoteFixture,
elfNoteWithUnreadableSectionNameFixture,
} from "../../src/native/elf/ElfNotes.fixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest("reports a GNU build-id through the public CLI", async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-cli-");
const path = join(root, "program");
await writeFile(path, elfNoteFixture({ ownership: "program" }));
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
result.json,
);
expect(
evidence.normalized_result.entries.find(
(entry) => entry.type_name === "gnu_build_id",
)?.gnu?.build_id_hex,
).toBe("0123456789abcdef0123456789abcdef01234567");
});
cliTest(
"reports FDO producer assertions in public ELF note Evidence",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-fdo-cli-");
const path = join(root, "program");
const metadata = [{ soname: ["libdemo.so.1"], feature: "examples" }];
const notes = elfNote(
"FDO",
0x407c0c0a,
Buffer.from(JSON.stringify(metadata) + "\0"),
);
await writeFile(
path,
elfNoteFixture({
ownership: "section",
notes,
sectionName: ".note.dlopen",
}),
);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.entries[0]).toMatchObject({
type_name: "fdo_dlopen_metadata",
fdo: { metadata, issue: null },
});
expect(evidence.limitations).toContain(
"FDO packaging and dlopen metadata are unverified producer declarations.",
);
},
);
cliTest(
"resolves section note names from a compressed string table through the CLI",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-compressed-cli-");
const path = join(root, "compressed-names.elf");
const bytes = compressedElfNoteFixture(64, true, 2);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.tables[0]?.name).toBe(
".note.gnu.build-id",
);
expect(evidence.normalized_result.entries[0]?.gnu?.build_id_hex).toBe(
"0123456789abcdef0123456789abcdef01234567",
);
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
},
);
cliTest(
"keeps GNU notes when a section-name offset is outside its string table",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-name-cli-");
const path = join(root, "program");
const bytes = elfNoteWithUnreadableSectionNameFixture("outside");
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.tables[0]?.name).toBeNull();
expect(evidence.normalized_result.limitations.join(" ")).toMatch(
/name offset.*string table/iu,
);
expect(evidence.normalized_result.entries[0]?.gnu?.build_id_hex).toBe(
"0123456789abcdef0123456789abcdef01234567",
);
expect(evidence.normalized_result.coverage).toMatchObject({
status: "complete",
examined_tables: 1,
examined_entries: 1,
});
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
},
);
cliTest(
"resolves a relative ELF path against the working directory",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-cli-rel-");
await writeFile(
join(root, "unsigned"),
elfNoteFixture({ ownership: "none" }),
);
const result = await cli.run({
arguments: ["inspect-elf-notes", "unsigned", "--json"],
cwd: root,
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
result.json,
);
expect(evidence.normalized_result.tables).toEqual([]);
expect(evidence.subject?.local_path).toBe(join(root, "unsigned"));
},
);
cliTest(
"reports compressed note storage as unsupported without mutation",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-storage-cli-");
const path = join(root, "aliased.o");
const bytes = compressedElfNoteSectionFixture(64, true, "both");
// The program starts inside the compressed section and ends beyond it.
bytes.writeBigUInt64LE(bytes.readBigUInt64LE(64 + 8) + 4n, 64 + 8);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
message: expect.stringMatching(/compressed.*note/iu),
});
expect(Buffer.compare(await readFile(path), bytes)).toBe(0);
},
);
cliTest(
"reports valid extended numbering as unsupported without mutation",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-extended-cli-");
const path = join(root, "extended-count.elf");
await writeFile(path, elfExtendedSectionCountFixture());
const before = await readFile(path);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
message: expect.stringMatching(/extended.*number/iu),
});
expect((await readFile(path)).equals(before)).toBe(true);
},
);
cliTest(
"rejects dangling section-name references and preserves sectionless notes",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-elf-notes-presence-cli-");
const path = join(root, "program");
for (const ownership of ["program", "none"] as const) {
const bytes = elfNoteFixture({ ownership });
bytes.writeUInt16LE(5, 62);
await writeFile(path, bytes);
const result = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "unsupported_target",
category: "unsupported_target",
details: {
operation: "inspect_elf_notes",
path,
format_failure: "malformed",
format: "elf",
},
});
expect((await readFile(path)).equals(bytes)).toBe(true);
bytes.writeUInt16LE(0, 62);
await writeFile(path, bytes);
const accepted = await cli.run({
arguments: ["inspect-elf-notes", path, "--json"],
});
expect(accepted.exitCode, accepted.stderr).toBe(0);
const evidence = toolContract("inspect_elf_notes").outputSchema.parse(
accepted.json,
);
expect(evidence.normalized_result.coverage.status).toBe("complete");
expect(evidence.normalized_result.tables).toHaveLength(
ownership === "none" ? 0 : 1,
);
}
},
);