mirror of
https://github.com/morluto/rea.git
synced 2026-11-27 14:49:15 +00:00
* 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.
782 lines
27 KiB
TypeScript
782 lines
27 KiB
TypeScript
import { fileIdentity } from "../fixtures/evidenceSubject.js";
|
|
import { goBinarySchema } from "../../src/domain/go/goBinary.js";
|
|
import { evidenceResultOf } from "../../src/contracts/toolOutputSchemaPrimitives.js";
|
|
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { createHash } from "node:crypto";
|
|
import { expect } from "vitest";
|
|
import { machoImageWithUnknownCommand } from "../../src/artifacts/apple/MachoImage.fixture.js";
|
|
import {
|
|
createGoBinaryByteStringFixture,
|
|
createGoBinaryDiagnosticFixtures,
|
|
createGoBinaryFixture,
|
|
createGoSplitElfFixture,
|
|
createGoOverlappingElfFixture,
|
|
createGoBinaryPathFailureFixture,
|
|
createGoInlineAggregateFixture,
|
|
setGoPeVirtualSize,
|
|
setGoElfLoadFileSize,
|
|
setGoMachOSectionSize,
|
|
setGoMachOSegmentFileSize,
|
|
GO_MODULE_TEXT,
|
|
} from "../fixtures/go/image.js";
|
|
import { createGoRodataStringFixture } from "../fixtures/go/rodataStringImage.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
import { analysisViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
|
|
import { parseEvidence } from "../../src/domain/evidence.js";
|
|
import { lazyLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
|
import { expectCliMcpEvidenceParity } from "../fixtures/cliMcpEvidenceParity.js";
|
|
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
|
|
|
const connectLocalToolsMcp = lazyLocalToolsMcp();
|
|
|
|
cliTest(
|
|
"reads Mach-O inline Go strings beyond a header-only discovery section through CLI and MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-macho-inline-range-");
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const complete = createGoBinaryFixture({ format: "macho" });
|
|
setGoMachOSectionSize(complete.bytes, 32);
|
|
const truncated = createGoBinaryFixture({ format: "macho" });
|
|
setGoMachOSectionSize(truncated.bytes, 32);
|
|
setGoMachOSegmentFileSize(
|
|
truncated.bytes,
|
|
truncated.moduleOffset + truncated.moduleBytes.length - 1,
|
|
);
|
|
|
|
for (const item of [
|
|
{ name: "header-only", bytes: complete.bytes, expected: "success" },
|
|
{ name: "truncated", bytes: truncated.bytes, expected: "malformed" },
|
|
] as const) {
|
|
const path = join(root, `${item.name}.macho`);
|
|
await writeFile(path, item.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
const response = await call("inspect_go_binary", { path });
|
|
if (item.expected === "success") {
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.normalized_result).toMatchObject({
|
|
format: "macho",
|
|
build_info: {
|
|
header_offset: complete.headerOffset,
|
|
go_version: "go1.26.0",
|
|
module: { path: "example.com/tool/cmd/tool" },
|
|
},
|
|
});
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, response.structuredContent, path);
|
|
} else {
|
|
const expected = {
|
|
code: "unsupported_target",
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
format_failure: expect.stringMatching(/^(?:malformed|truncated)$/u),
|
|
},
|
|
};
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject(expected);
|
|
expect(parseMcpToolError(response).error).toMatchObject(expected);
|
|
}
|
|
}
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"counts inline Go string contents separately from varint framing through CLI and MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-inline-budget-");
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const maximum = 1024 * 1024;
|
|
const impossibleInline = createGoBinaryFixture();
|
|
impossibleInline.bytes.set(
|
|
[0x81, 0x80, 0x40],
|
|
impossibleInline.headerOffset + 32,
|
|
);
|
|
const impossiblePointer = createGoBinaryFixture({ encoding: "pointer" });
|
|
impossiblePointer.bytes.writeBigUInt64LE(BigInt(maximum + 1), 1544);
|
|
const cases = [
|
|
{
|
|
name: "exact",
|
|
fixture: createGoInlineAggregateFixture(maximum),
|
|
outcome: "success",
|
|
},
|
|
{
|
|
name: "over",
|
|
fixture: createGoInlineAggregateFixture(maximum + 1),
|
|
outcome: "resource",
|
|
},
|
|
{
|
|
name: "truncated",
|
|
fixture: createGoInlineAggregateFixture(512),
|
|
outcome: "malformed",
|
|
},
|
|
{
|
|
name: "impossible-inline",
|
|
fixture: impossibleInline,
|
|
outcome: "malformed",
|
|
},
|
|
{
|
|
name: "impossible-pointer",
|
|
fixture: impossiblePointer,
|
|
outcome: "malformed",
|
|
},
|
|
] as const;
|
|
|
|
for (const item of cases) {
|
|
if (item.name === "truncated")
|
|
setGoElfLoadFileSize(
|
|
item.fixture.bytes,
|
|
item.fixture.moduleOffset + item.fixture.moduleBytes.length - 1,
|
|
);
|
|
const path = join(root, `${item.name}.elf`);
|
|
await writeFile(path, item.fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
const response = await call("inspect_go_binary", { path });
|
|
|
|
if (item.outcome === "success") {
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
const buildInfo = evidence.normalized_result.build_info;
|
|
expect(buildInfo).not.toBeNull();
|
|
expect(
|
|
buildInfo!.version_location.bytes + buildInfo!.module_location.bytes,
|
|
).toBe(maximum);
|
|
expect(
|
|
item.fixture.moduleOffset +
|
|
item.fixture.moduleBytes.length -
|
|
item.fixture.headerOffset,
|
|
).toBeGreaterThan(maximum + 32);
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, response.structuredContent, path);
|
|
} else {
|
|
const code =
|
|
item.outcome === "resource"
|
|
? "resource_constraint"
|
|
: "unsupported_target";
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code,
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
...(item.outcome === "resource"
|
|
? {
|
|
resource: "memory",
|
|
reported_limits: {
|
|
boundary: "build-info",
|
|
maximum,
|
|
},
|
|
}
|
|
: {
|
|
format_failure: expect.stringMatching(
|
|
/^(?:malformed|truncated)$/u,
|
|
),
|
|
}),
|
|
},
|
|
});
|
|
expect(parseMcpToolError(response).error).toMatchObject({
|
|
code,
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
...(item.outcome === "resource"
|
|
? {
|
|
resource: "memory",
|
|
reported_limits: {
|
|
boundary: "build-info",
|
|
maximum,
|
|
},
|
|
}
|
|
: {
|
|
format_failure: expect.stringMatching(
|
|
/^(?:malformed|truncated)$/u,
|
|
),
|
|
}),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"delivers a Go CLI summary with its unchanged complete source bundle",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-summary-");
|
|
const path = join(root, "application.elf");
|
|
const output = join(root, "source.json");
|
|
await writeFile(path, createGoBinaryFixture().bytes);
|
|
const complete = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(complete.exitCode, complete.stderr).toBe(0);
|
|
const full = evidenceResultOf(goBinarySchema).parse(complete.json);
|
|
const summary = await cli.run({
|
|
arguments: [
|
|
"inspect-go-binary",
|
|
path,
|
|
"--detail",
|
|
"summary",
|
|
"--evidence_output",
|
|
output,
|
|
"--json",
|
|
],
|
|
});
|
|
expect(summary.exitCode, summary.stderr).toBe(0);
|
|
expect(summary.json).toMatchObject({
|
|
summary: {
|
|
normalized_result: {
|
|
kind: "summary",
|
|
parent_evidence_id: full.evidence_id,
|
|
source_coverage: { runtime_dependencies: "unknown" },
|
|
},
|
|
},
|
|
});
|
|
const bundle = JSON.parse(await readFile(output, "utf8")) as {
|
|
records: unknown[];
|
|
};
|
|
expect(
|
|
bundle.records
|
|
.map((record) => parseEvidence(record))
|
|
.find((record) => record.evidence_id === full.evidence_id),
|
|
).toEqual(full);
|
|
const refused = await cli.run({
|
|
arguments: [
|
|
"inspect-go-binary",
|
|
join(root, "missing"),
|
|
"--detail",
|
|
"summary",
|
|
"--json",
|
|
],
|
|
});
|
|
expect(refused.exitCode).toBe(1);
|
|
expect(refused.json).toMatchObject({
|
|
code: "invalid_request",
|
|
details: { issues: [{ path: ["evidence_output"] }] },
|
|
});
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"bounds overlapping ELF scans through CLI and keeps MCP responsive",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-overlap-");
|
|
const path = join(root, "overlapping.elf");
|
|
await writeFile(path, createGoOverlappingElfFixture());
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
const expected = {
|
|
code: "resource_constraint",
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
resource: "cpu",
|
|
reported_limits: {
|
|
boundary: "search",
|
|
maximum: 256 * 1024 * 1024,
|
|
},
|
|
},
|
|
};
|
|
expect(result.json).toMatchObject(expected);
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const response = await call("inspect_go_binary", { path });
|
|
expect(parseMcpToolError(response).error).toMatchObject(expected);
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"retains later-load sectionless ELF evidence through public CLI and stdio MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-split-elf-");
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const infos = [];
|
|
for (const sectionless of [false, true]) {
|
|
const fixture = createGoSplitElfFixture({ sectionless });
|
|
const path = join(root, sectionless ? "sectionless.elf" : "original.elf");
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.normalized_result.build_info).not.toBeNull();
|
|
infos.push(evidence.normalized_result.build_info);
|
|
const response = await call("inspect_go_binary", { path });
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, response.structuredContent, path);
|
|
}
|
|
expect(infos[1]).toEqual(infos[0]);
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"preserves PE pointer build info at zero VirtualSize through CLI and stdio MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-pe-zero-vsize-");
|
|
const fixture = createGoBinaryFixture({
|
|
format: "pe",
|
|
bits: 32,
|
|
encoding: "pointer",
|
|
});
|
|
const path = join(root, "zero-vsize.exe");
|
|
setGoPeVirtualSize(fixture.bytes, 0);
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(fileIdentity(evidence.subject).sha256).toBe(
|
|
createHash("sha256").update(fixture.bytes).digest("hex"),
|
|
);
|
|
expect(evidence.normalized_result.build_info).toMatchObject({
|
|
go_version: "go1.26.0",
|
|
encoding: "pointer",
|
|
header_offset: fixture.headerOffset,
|
|
module_text: GO_MODULE_TEXT,
|
|
version_location: { offset: fixture.versionOffset, bytes: 8 },
|
|
module_location: {
|
|
offset: fixture.moduleOffset,
|
|
bytes: fixture.moduleBytes.length,
|
|
},
|
|
});
|
|
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const response = await call("inspect_go_binary", { path });
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, response.structuredContent, path);
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"rejects misaligned 64-bit Mach-O through Go CLI and stdio MCP",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-macho-alignment-");
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const expectedFailure = {
|
|
code: "unsupported_target",
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
format_failure: expect.stringMatching(/^(?:malformed|truncated)$/u),
|
|
},
|
|
};
|
|
|
|
for (const fixture of [
|
|
{ wide: true, size: 12, bits: 64, malformed: true },
|
|
{ wide: true, size: 16, bits: 64, malformed: false },
|
|
{ wide: false, size: 12, bits: 32, malformed: false },
|
|
]) {
|
|
const path = join(root, `image-${fixture.bits}-${fixture.size}.macho`);
|
|
await writeFile(
|
|
path,
|
|
machoImageWithUnknownCommand(fixture.wide, fixture.size),
|
|
);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
const response = await call("inspect_go_binary", { path });
|
|
|
|
if (fixture.malformed) {
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject(expectedFailure);
|
|
expect(result.json).not.toHaveProperty("normalized_result");
|
|
expect(parseMcpToolError(response).error).toMatchObject(
|
|
expectedFailure,
|
|
);
|
|
expect(response.structuredContent).toBeUndefined();
|
|
continue;
|
|
}
|
|
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.normalized_result).toMatchObject({
|
|
format: "macho",
|
|
bits: fixture.bits,
|
|
build_info: null,
|
|
});
|
|
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, response.structuredContent, path);
|
|
}
|
|
await client.ping();
|
|
},
|
|
);
|
|
|
|
for (const expectedErrno of ["ELOOP", "ENAMETOOLONG"] as const) {
|
|
cliTest.skipIf(expectedErrno === "ELOOP" && process.platform === "win32")(
|
|
`reports native ${expectedErrno} path failure as invalid selected-path input through the public Go CLI`,
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-path-failure-");
|
|
const { path, errno } = await createGoBinaryPathFailureFixture(
|
|
root,
|
|
expectedErrno,
|
|
);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
issues: [
|
|
{
|
|
path: ["path"],
|
|
reason: "invalid_value",
|
|
message: expect.stringContaining(errno),
|
|
},
|
|
],
|
|
},
|
|
});
|
|
expect(result.json).toMatchObject({
|
|
details: { issues: [{ message: expect.stringContaining(path) }] },
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
cliTest(
|
|
"preserves invalid embedded Go text bytes through the public CLI",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-byte-strings-");
|
|
const path = join(root, "byte-strings.elf");
|
|
const fixture = createGoBinaryByteStringFixture();
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.normalized_result.build_info).toEqual(
|
|
fixture.expectedBuildInfo,
|
|
);
|
|
},
|
|
);
|
|
|
|
for (const fixture of createGoBinaryDiagnosticFixtures()) {
|
|
cliTest(
|
|
`reports ${fixture.code} for ${fixture.name} through the public Go CLI`,
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-mz-");
|
|
const path = join(root, `${fixture.name}.exe`);
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: fixture.code,
|
|
details: {
|
|
...fixture.details,
|
|
...(fixture.code === "unsupported_target" ? { path } : {}),
|
|
},
|
|
});
|
|
},
|
|
);
|
|
}
|
|
|
|
cliTest(
|
|
"reports a readable unsupported carrier through the public Go CLI",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-");
|
|
const path = join(root, "text.bin");
|
|
await writeFile(path, "This is not an executable container.");
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.json).toMatchObject({
|
|
code: "unsupported_target",
|
|
details: { operation: "inspect_go_binary", path },
|
|
});
|
|
},
|
|
);
|
|
|
|
for (const format of ["elf", "pe", "macho"] as const) {
|
|
for (const encoding of ["inline", "pointer"] as const) {
|
|
cliTest(
|
|
`inspects authored ${format} ${encoding} build metadata through the compiled CLI`,
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-");
|
|
const fixture = createGoBinaryFixture({ format, encoding });
|
|
const path = join(root, "application.bin");
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", "application.bin", "--json"],
|
|
cwd: root,
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.raw_result).toBeNull();
|
|
expect(evidence.subject?.local_path).toBe(path);
|
|
expect(fileIdentity(evidence.subject).sha256).toBe(
|
|
createHash("sha256").update(fixture.bytes).digest("hex"),
|
|
);
|
|
const report = evidence.normalized_result;
|
|
expect(report).toMatchObject({
|
|
format,
|
|
bits: 64,
|
|
byte_order: "little",
|
|
artifact: { path, bytes: fixture.bytes.length },
|
|
});
|
|
expect(report.build_info).toMatchObject({
|
|
header_offset: fixture.headerOffset,
|
|
encoding,
|
|
go_version: "go1.26.0",
|
|
module_text: GO_MODULE_TEXT,
|
|
module_bytes_base64: fixture.moduleBytes.toString("base64"),
|
|
module: {
|
|
path: "example.com/tool/cmd/tool",
|
|
main: {
|
|
path: "example.com/tool",
|
|
version: "(devel)",
|
|
sum: "",
|
|
replacement: null,
|
|
},
|
|
dependencies: [
|
|
{
|
|
path: "example.com/dependency",
|
|
version: "v1.2.3",
|
|
sum: "h1:sample",
|
|
replacement: { path: "../dependency", version: "", sum: "" },
|
|
},
|
|
],
|
|
complete: true,
|
|
unparsed_lines: [],
|
|
},
|
|
});
|
|
expect(report.build_info?.module.settings).toContainEqual({
|
|
key: "-ldflags",
|
|
value: "-s -w",
|
|
});
|
|
if (report.build_info === null)
|
|
throw new Error("Fixture build metadata must be present");
|
|
const version = report.build_info.version_location;
|
|
const module = report.build_info.module_location;
|
|
expect(
|
|
fixture.bytes
|
|
.subarray(version.offset, version.offset + version.bytes)
|
|
.toString("utf8"),
|
|
).toBe(report.build_info.go_version);
|
|
expect(
|
|
Buffer.compare(
|
|
fixture.bytes.subarray(module.offset, module.offset + module.bytes),
|
|
Buffer.from(report.build_info.module_bytes_base64, "base64"),
|
|
),
|
|
).toBe(0);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
cliTest("preserves MIPS identity in Go CLI Evidence", async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-mips-");
|
|
const fixture = createGoBinaryFixture();
|
|
fixture.bytes.writeUInt16LE(8, 18); // ELF EM_MIPS.
|
|
const path = join(root, "application.elf");
|
|
await writeFile(path, fixture.bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(fileIdentity(evidence.subject).architecture).toBe("mips");
|
|
expect(evidence.normalized_result).toMatchObject({
|
|
architecture: "mips",
|
|
bits: 64,
|
|
byte_order: "little",
|
|
});
|
|
});
|
|
|
|
cliTest(
|
|
"reports missing build information as unknown and preserves unsupported carriers distinctly",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-");
|
|
const path = join(root, "metadata-free.elf");
|
|
await writeFile(path, createGoBinaryFixture({ omitBuildInfo: true }).bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode).toBe(0);
|
|
const report = evidenceResultOf(goBinarySchema).parse(
|
|
result.json,
|
|
).normalized_result;
|
|
expect(report.build_info).toBeNull();
|
|
expect(report.limitations.join(" ")).toContain(
|
|
"does not establish that the binary is not Go",
|
|
);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"returns unknown dependency coverage for parse-complete GOPATH metadata in CLI Evidence",
|
|
async ({ cli }) => {
|
|
const moduleText =
|
|
"path\texample.com/tool/cmd/tool\n" +
|
|
"build\t-compiler=gc\n" +
|
|
"build\tGOOS=linux\n";
|
|
const root = await createTestTempDirectory("rea-go-gopath-cli-");
|
|
const path = join(root, "application.elf");
|
|
await writeFile(path, createGoBinaryFixture({ moduleText }).bytes);
|
|
const result = await cli.run({
|
|
arguments: ["inspect-go-binary", path, "--json"],
|
|
});
|
|
expect(result.exitCode, result.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(result.json);
|
|
expect(evidence.normalized_result.build_info?.module_text).toBe(moduleText);
|
|
expect(evidence.normalized_result.build_info?.module).toMatchObject({
|
|
path: "example.com/tool/cmd/tool",
|
|
main: null,
|
|
dependencies: [],
|
|
complete: true,
|
|
unparsed_lines: [],
|
|
});
|
|
expect(evidence.limitations.join(" ")).toContain(
|
|
"Dependency coverage is unknown",
|
|
);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"projects the same Go BOM from retained MCP and detached CLI Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cyclonedx-");
|
|
const path = join(root, "application.elf");
|
|
const evidencePath = join(root, "go-evidence.json");
|
|
const requestPath = join(root, "cyclonedx-view.json");
|
|
await writeFile(path, createGoBinaryFixture().bytes);
|
|
const { call } = await connectLocalToolsMcp();
|
|
const inspected = await call("inspect_go_binary", { path });
|
|
expect(inspected.isError, JSON.stringify(inspected)).not.toBe(true);
|
|
const parent = parseEvidence(inspected.structuredContent);
|
|
await writeFile(evidencePath, JSON.stringify(parent));
|
|
await unlink(path);
|
|
const view = { kind: "cyclonedx" } as const;
|
|
const retained = await call("inspect_analysis_view", {
|
|
source: { kind: "retained-evidence", evidence_id: parent.evidence_id },
|
|
view,
|
|
});
|
|
expect(retained.isError, JSON.stringify(retained)).not.toBe(true);
|
|
const mcpEvidence = parseEvidence(retained.structuredContent);
|
|
|
|
await writeFile(
|
|
requestPath,
|
|
JSON.stringify({
|
|
source: { kind: "evidence-file", path: evidencePath },
|
|
view,
|
|
}),
|
|
);
|
|
const viewed = await cli.run({
|
|
arguments: ["inspect-analysis-view", requestPath, "--json"],
|
|
});
|
|
expect(viewed.exitCode, viewed.stderr).toBe(0);
|
|
const cliEvidence = parseEvidence(viewed.json);
|
|
expect(cliEvidence.confidence).toBe("derived");
|
|
expect(mcpEvidence.confidence).toBe("derived");
|
|
expect(cliEvidence.normalized_result).toEqual(
|
|
mcpEvidence.normalized_result,
|
|
);
|
|
const result = analysisViewResultSchema.parse(
|
|
cliEvidence.normalized_result,
|
|
);
|
|
if (result.kind !== "cyclonedx")
|
|
throw new Error("Expected a CycloneDX view");
|
|
const mainRef = "pkg:golang/example.com/tool@%28devel%29";
|
|
expect(result).toMatchObject({
|
|
parent_evidence_id: parent.evidence_id,
|
|
parent_operation: "inspect_go_binary",
|
|
source_coverage: { aggregate: "unknown", records: "present" },
|
|
document: {
|
|
bomFormat: "CycloneDX",
|
|
specVersion: "1.6",
|
|
compositions: [{ aggregate: "unknown" }],
|
|
components: [
|
|
{ name: "example.com/tool", "bom-ref": mainRef, purl: mainRef },
|
|
{ name: "../dependency", "bom-ref": "go-module:1" },
|
|
],
|
|
dependencies: expect.arrayContaining([
|
|
{ ref: mainRef, dependsOn: ["go-module:1"] },
|
|
]),
|
|
},
|
|
});
|
|
expect(result.document.components[1]).not.toHaveProperty("purl");
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"recovers Go (ptr, len) string literals with CLI and MCP parity and rejects standalone --max-string-literals",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-go-cli-strings-");
|
|
const path = join(root, "strings.pe");
|
|
const fixture = createGoRodataStringFixture({
|
|
format: "pe",
|
|
rodata: Buffer.from(
|
|
"ListResourceProvidersgenaihttps://example.invalid/v1",
|
|
"utf8",
|
|
),
|
|
headers: [
|
|
{ section: "data", offsetInSection: 0, ptr: 0x102000n, len: 21n },
|
|
{ section: "data", offsetInSection: 16, ptr: 0x102015n, len: 5n },
|
|
{ section: "data", offsetInSection: 32, ptr: 0x10201an, len: 26n },
|
|
],
|
|
});
|
|
await writeFile(path, fixture.bytes);
|
|
|
|
const standaloneCap = await cli.run({
|
|
arguments: [
|
|
"inspect-go-binary",
|
|
path,
|
|
"--max-string-literals",
|
|
"2",
|
|
"--json",
|
|
],
|
|
});
|
|
expect(standaloneCap.exitCode).toBe(1);
|
|
expect(standaloneCap.json).toMatchObject({
|
|
code: "invalid_request",
|
|
details: {
|
|
operation: "inspect_go_binary",
|
|
issues: [{ path: ["maxStringLiterals"] }],
|
|
},
|
|
});
|
|
|
|
const cliResult = await cli.run({
|
|
arguments: [
|
|
"inspect-go-binary",
|
|
path,
|
|
"--include-string-literals",
|
|
"--max-string-literals",
|
|
"2",
|
|
"--json",
|
|
],
|
|
});
|
|
expect(cliResult.exitCode, cliResult.stderr).toBe(0);
|
|
const evidence = evidenceResultOf(goBinarySchema).parse(cliResult.json);
|
|
expect(evidence.normalized_result.string_literals).toMatchObject({
|
|
evidence_kind: "inferred",
|
|
discovered_count: 3,
|
|
max_items: 2,
|
|
truncated: true,
|
|
items: [{ value: "ListResourceProviders" }, { value: "genai" }],
|
|
});
|
|
|
|
const { client, call } = await connectLocalToolsMcp();
|
|
const mcpReply = await call("inspect_go_binary", {
|
|
path,
|
|
include_string_literals: true,
|
|
max_string_literals: 2,
|
|
});
|
|
expect(mcpReply.isError, JSON.stringify(mcpReply)).not.toBe(true);
|
|
expectCliMcpEvidenceParity(evidence, mcpReply.structuredContent, path);
|
|
await client.ping();
|
|
},
|
|
);
|