mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* feat(rust): read cargo-auditable dependencies from WebAssembly modules cargo-auditable embeds the same zlib-compressed `.dep-v0` JSON in a WebAssembly custom section for wasm32 targets, but inspect_rust_binary rejected every module as an unsupported target because only ELF, PE and thin Mach-O containers were recognized. Walk the section framing of version 1 core modules to locate the `.dep-v0` custom-section payload and the data section, where rustc keeps its constant /rustc/<hash>/ paths. Rust lookups now read one format-neutral layout, so native and WebAssembly images share a single dependency-section owner. WebAssembly declares no container word size, so `bits` is null for that format only; the Evidence subject format is `file`, as for inspect_wasm_artifact. Truncated sections, invalid LEB128 sizes, custom-section names that run past their section or are not UTF-8, duplicate `.dep-v0` sections and a second data section are malformed. Components, such as wasm32-wasip2 output, embed core modules and are refused as unsupported, like universal Mach-O containers, rather than inspected through one selected module. * fix(rust): bound the WebAssembly section walk The WebAssembly reader visited every section synchronously, and the format sets no section count. A 256 MiB input of three-byte custom sections with empty names walked about 89 million sections and held the event loop for about 7.4 seconds, delaying cancellation and other MCP requests; empty type sections took about 1.9 seconds. Refuse a module with more than 65,536 sections with a typed cpu resource constraint that reports maximum_wasm_sections, as the Go header search reports its work budget. Producer output holds a few dozen sections, and the same inputs are now refused within tens of milliseconds. * fix(rust): accept WebAssembly Evidence in retained dependency views Retained Rust views compared the parent subject with a Mach-O-only format mapping, so WebAssembly Evidence, whose subject format is file, was refused as a different artifact. The report domain now owns the subject format next to the architecture mapping, and the service and the view both use it. The real CLI/MCP view workflow now also covers a WebAssembly module. * fix(rust): name the WebAssembly section-walk allowance in recovery Fixed-allowance recovery mapped every Rust resource other than capacity and transport to the parser allowance, so the section-walk refusal said host memory does not raise it. Each Rust resource now maps to its own allowance, and the cpu bound is a fixed work allowance. The MCP error contract test checks that the refusal is not retryable and names that allowance. * fix(rust): require WebAssembly container facts in reports The report schema tied a null word size to WebAssembly, but it accepted a wasm report that named a native architecture or big-endian byte order, and a native report that named the wasm architecture. Retained views parse imported Evidence with this schema, so such a contradictory report was accepted. The architecture is now wasm exactly when the container is WebAssembly, and a WebAssembly report must be little-endian. * docs(rust): record the WebAssembly dependency-metadata feature Feature work now carries a spec, plan and task list under docs/specs. This adds that record for reading cargo-auditable metadata from WebAssembly core modules and indexes it. The record states that it was written during review and lists the verification that remains open. * fix(rust): bind section names to containers and share the LEB128 reader The report schema accepted a dependency section name that does not belong to the report's container, such as a WebAssembly report naming the Mach-O section. rustDependencySectionName now owns that name for the reader and the schema, which refuses a mismatch for every container. The WebAssembly reader decoded section sizes with its own loop. It now uses the shared readLeb128, and an integer cut off by the end of the file is truncated rather than malformed. * fix(rust): classify a cut-off WebAssembly integer by its boundary The reader called an unfinished LEB128 integer truncated whenever its limit equalled the file length. A name length cut off inside the final section was therefore truncated, and the same bytes followed by another section were malformed. Each caller now states whether the file or the enclosing section bounds the integer. * fix(rust): report a duplicate WebAssembly dependency section first The section walk kept two .dep-v0 matches but walked on, so a module with two dependency sections and more than 65,536 sections after them was refused for the section bound instead of the duplicate it had already established. The walk now ends at the second match. * test(rust): assert WebAssembly container facts instead of limitation prose The CLI acceptance test matched an English limitation sentence. It now checks format, architecture, bits and byte order on the parsed report. * fix(rust): state container word size as 32 | 64 | null Output contract: inspect_rust_binary bits, including the summary view, is 32 | 64 | null. Null is valid only for WebAssembly and is not a number. ELF, PE and Mach-O reports stay 32 or 64. The schema description, guide, feature record and skill state that, and CLI renderers keep null instead of substituting a word size. --------- Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
519 lines
17 KiB
TypeScript
519 lines
17 KiB
TypeScript
import { fileSubjectArchitecture } from "../fixtures/evidenceSubject.js";
|
|
import { readFile, unlink, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect } from "vitest";
|
|
import { parseEvidence, createEvidence } from "../../src/domain/evidence.js";
|
|
import { metadataReportSubjectFormat } from "../../src/domain/apple/machoTargetSelection.js";
|
|
import { createEvidenceBundle } from "../../src/domain/evidenceBundle.js";
|
|
import { rustBinarySchema } from "../../src/domain/rust/rustBinary.js";
|
|
import { analysisViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
|
|
import { readEvidenceBundle } from "../../src/application/EvidenceBundleFiles.js";
|
|
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
|
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
|
import {
|
|
createRustImage,
|
|
createRustHashOverflowImage,
|
|
createRustWasmModule,
|
|
rustDependencyPayload,
|
|
} from "../fixtures/rust/image.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
import { cliTest } from "../support/cli/cliFixture.js";
|
|
|
|
/** One synthetic cargo-auditable artifact for each supported container. */
|
|
const rustArtifact = (
|
|
format: "elf" | "pe" | "macho" | "wasm",
|
|
payload: Buffer,
|
|
): Buffer =>
|
|
format === "wasm"
|
|
? createRustWasmModule({ payload })
|
|
: createRustImage({ format, payload });
|
|
|
|
cliTest(
|
|
"keeps compiler-prefix coverage in CLI summaries until the selected budget admits every hash",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-compiler-summary-");
|
|
const path = join(root, "compiler-paths.elf");
|
|
await writeFile(path, createRustHashOverflowImage());
|
|
for (const maxHashes of [64, 65]) {
|
|
const output = join(root, `source-${String(maxHashes)}.json`);
|
|
const delivered = await cli.run({
|
|
arguments: [
|
|
"inspect-rust-binary",
|
|
path,
|
|
"--detail",
|
|
"summary",
|
|
"--evidence_output",
|
|
output,
|
|
"--max-rustc-hashes",
|
|
String(maxHashes),
|
|
"--full-output",
|
|
"--json",
|
|
],
|
|
});
|
|
expect(delivered.exitCode, delivered.stderr).toBe(0);
|
|
const envelope = delivered.json as {
|
|
data: { summary: { normalized_result: unknown } };
|
|
meta: { cli_controls?: unknown };
|
|
};
|
|
// The summary wraps the view; the CLI still names the option for the
|
|
// control the complete Evidence records.
|
|
expect(envelope.meta.cli_controls).toEqual(
|
|
maxHashes === 64
|
|
? { max_rustc_hashes: "--max-rustc-hashes" }
|
|
: undefined,
|
|
);
|
|
const summary = analysisViewResultSchema.parse(
|
|
envelope.data.summary.normalized_result,
|
|
);
|
|
if (
|
|
summary.kind !== "summary" ||
|
|
summary.parent_operation !== "inspect_rust_binary"
|
|
)
|
|
throw new Error("Expected a Rust summary");
|
|
expect(summary.summary.rustc_commit).toEqual({
|
|
evidence_kind: "inferred",
|
|
hashes_count: maxHashes,
|
|
...(maxHashes === 64
|
|
? {
|
|
coverage: {
|
|
status: "partial",
|
|
reason: "max_rustc_hashes",
|
|
effective_limit: 64,
|
|
limit: expect.objectContaining({
|
|
parameter: "max_rustc_hashes",
|
|
effective_value: 64,
|
|
}),
|
|
},
|
|
}
|
|
: {}),
|
|
});
|
|
expect(summary.summary.auditable_metadata?.counts.packages).toBe(2);
|
|
}
|
|
},
|
|
);
|
|
|
|
/** Both authenticated retention and portable imports query the same captured facts. */
|
|
const assertRetainedPages = async (
|
|
call: Awaited<ReturnType<typeof connectLocalToolsMcp>>["call"],
|
|
evidenceId: string,
|
|
exported: string,
|
|
) => {
|
|
for (const [filter, expected] of [
|
|
[
|
|
{ names: ["same"], versions: ["2"], fields: ["name", "version"] },
|
|
[{ index: 1, name: "same", version: "2" }],
|
|
],
|
|
[{ names: ["same"], indices: [0], fields: [] }, [{ index: 0 }]],
|
|
[{ names: ["same"], versions: [], fields: [] }, []],
|
|
] as const) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source: { kind: "retained-evidence", evidence_id: evidenceId },
|
|
view: { kind: "rust", collection: "packages", ...filter },
|
|
});
|
|
expect(reply.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({ page: { rows: expected } });
|
|
}
|
|
|
|
for (const source of [
|
|
{ kind: "retained-evidence", evidence_id: evidenceId },
|
|
{ kind: "evidence-file", path: exported },
|
|
]) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
names: ["same"],
|
|
indices: [0, 1, 99],
|
|
offset: 1,
|
|
limit: 1,
|
|
fields: [],
|
|
},
|
|
});
|
|
expect(reply.isError, JSON.stringify(reply)).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
parent_evidence_id: evidenceId,
|
|
page: { rows: [{ index: 1 }] },
|
|
coverage: {
|
|
total: 2,
|
|
examined: 1,
|
|
exhausted: true,
|
|
next_offset: null,
|
|
},
|
|
});
|
|
const edgeReply = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "edges",
|
|
from_indices: [1, 3],
|
|
to_indices: [0],
|
|
fields: ["from_index", "to_index"],
|
|
},
|
|
});
|
|
expect(edgeReply.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(edgeReply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
page: {
|
|
collection: "edges",
|
|
rows: [
|
|
{ index: 1, from_index: 1, to_index: 0 },
|
|
{ index: 3, from_index: 3, to_index: 0 },
|
|
],
|
|
},
|
|
});
|
|
const empty = await call("inspect_analysis_view", {
|
|
source,
|
|
view: {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
names: [],
|
|
versions: ["1"],
|
|
fields: ["name"],
|
|
},
|
|
});
|
|
expect(
|
|
parseEvidence(empty.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
page: { rows: [] },
|
|
coverage: { total: 0, exhausted: true },
|
|
});
|
|
}
|
|
};
|
|
|
|
type CycloneDxCliRun = {
|
|
exitCode: number | null;
|
|
stderr: string;
|
|
json: unknown;
|
|
};
|
|
|
|
const expectCycloneDxView = async (
|
|
call: Awaited<ReturnType<typeof connectLocalToolsMcp>>["call"],
|
|
cli: { run: (input: { arguments: string[] }) => Promise<CycloneDxCliRun> },
|
|
input: { exported: string; format: string; root: string },
|
|
): Promise<void> => {
|
|
const { exported, format, root } = input;
|
|
const request = {
|
|
source: { kind: "evidence-file", path: exported },
|
|
view: { kind: "cyclonedx" },
|
|
} as const;
|
|
const cyclonedx = await call("inspect_analysis_view", request);
|
|
expect(cyclonedx.isError, JSON.stringify(cyclonedx)).not.toBe(true);
|
|
const result = analysisViewResultSchema.parse(
|
|
parseEvidence(cyclonedx.structuredContent).normalized_result,
|
|
);
|
|
if (result.kind !== "cyclonedx") throw new Error("Expected a CycloneDX view");
|
|
expect(result.document).toMatchObject({
|
|
bomFormat: "CycloneDX",
|
|
specVersion: "1.6",
|
|
compositions: [{ aggregate: "unknown" }],
|
|
});
|
|
const documentText = JSON.stringify(result.document);
|
|
expect(documentText).toContain("pkg:cargo/same@1");
|
|
expect(documentText).not.toContain("pkg:cargo/app@1");
|
|
if (format !== "elf") return;
|
|
const requestPath = join(root, "cyclonedx-view.json");
|
|
await writeFile(requestPath, JSON.stringify(request));
|
|
const cliView = await cli.run({
|
|
arguments: ["inspect-analysis-view", requestPath, "--json"],
|
|
});
|
|
expect(cliView.exitCode, cliView.stderr).toBe(0);
|
|
const cliResult = analysisViewResultSchema.parse(
|
|
parseEvidence(cliView.json).normalized_result,
|
|
);
|
|
if (cliResult.kind !== "cyclonedx")
|
|
throw new Error("Expected a CycloneDX view");
|
|
expect(cliResult.document).toEqual(result.document);
|
|
};
|
|
|
|
cliTest(
|
|
"queries stable Rust packages and edges through real CLI/MCP and portable Evidence",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-views-");
|
|
const { call } = await connectLocalToolsMcp();
|
|
for (const format of ["elf", "pe", "macho", "wasm"] as const) {
|
|
const path = join(root, `source.${format}`);
|
|
const output = join(root, `${format}-source.json`);
|
|
const payload = rustDependencyPayload({
|
|
packages: [
|
|
{
|
|
name: "same",
|
|
version: "1",
|
|
source: "crates.io",
|
|
dependencies: [2],
|
|
features: ["default"],
|
|
},
|
|
{
|
|
name: "same",
|
|
version: "2",
|
|
source: "git+https://upstream.example/repo",
|
|
dependencies: [0, 2],
|
|
},
|
|
{ name: "leaf", version: "1", source: "local" },
|
|
{
|
|
name: "app",
|
|
version: "1",
|
|
source: "local",
|
|
dependencies: [0, 1],
|
|
root: true,
|
|
},
|
|
],
|
|
});
|
|
await writeFile(path, rustArtifact(format, payload));
|
|
const fullReply = await call("inspect_rust_binary", { path });
|
|
expect(fullReply.isError).not.toBe(true);
|
|
const full = parseEvidence(fullReply.structuredContent);
|
|
const report = rustBinarySchema.parse(full.normalized_result);
|
|
const explicit = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "complete",
|
|
});
|
|
expect(parseEvidence(explicit.structuredContent)).toEqual(full);
|
|
const selection = {
|
|
collection: "packages",
|
|
names: ["same"],
|
|
fields: ["name", "version", "dependencies", "features"],
|
|
};
|
|
const direct = await call("inspect_rust_binary", {
|
|
path,
|
|
view: selection,
|
|
});
|
|
expect(direct.isError, JSON.stringify(direct)).not.toBe(true);
|
|
const selected = parseEvidence(
|
|
direct.structuredContent,
|
|
).normalized_result;
|
|
expect(selected).toMatchObject({
|
|
kind: "rust",
|
|
parent_evidence_id: full.evidence_id,
|
|
page: {
|
|
collection: "packages",
|
|
rows: [
|
|
{
|
|
index: 0,
|
|
name: "same",
|
|
version: "1",
|
|
dependencies: [2],
|
|
features: ["default"],
|
|
},
|
|
{ index: 1, name: "same", version: "2", dependencies: [0, 2] },
|
|
],
|
|
},
|
|
});
|
|
const summary = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "summary",
|
|
});
|
|
expect(summary.isError).not.toBe(true);
|
|
expect(
|
|
parseEvidence(summary.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
kind: "summary",
|
|
summary: {
|
|
auditable_metadata: {
|
|
counts: { packages: 4, edges: 5 },
|
|
format_version: null,
|
|
},
|
|
},
|
|
source_coverage: {
|
|
embedded_records: "complete",
|
|
runtime_dependencies: "unknown",
|
|
},
|
|
});
|
|
const delivered = await cli.run({
|
|
arguments: [
|
|
"inspect-rust-binary",
|
|
path,
|
|
"--view",
|
|
JSON.stringify(selection),
|
|
"--evidence_output",
|
|
output,
|
|
"--json",
|
|
],
|
|
});
|
|
expect(delivered.exitCode, delivered.stderr).toBe(0);
|
|
expect(delivered.json).toMatchObject({
|
|
summary: { normalized_result: selected },
|
|
});
|
|
const bundle = await readEvidenceBundle(output);
|
|
if (!bundle.ok) throw bundle.error;
|
|
expect(
|
|
bundle.value.records.find(
|
|
(row) => row.evidence_id === full.evidence_id,
|
|
),
|
|
).toEqual({ ...full, caller_input_path: path });
|
|
const exported = join(root, `${format}-record.json`);
|
|
await writeFile(exported, JSON.stringify(full));
|
|
await unlink(path);
|
|
|
|
await assertRetainedPages(call, full.evidence_id, exported);
|
|
await expectCycloneDxView(call, cli, { exported, format, root });
|
|
const malformed = structuredClone(report);
|
|
malformed.auditable_metadata!.dependency_edges[0]!.to_index = 999;
|
|
const invalid = createEvidence(
|
|
{
|
|
path,
|
|
sha256: report.artifact.sha256,
|
|
format: metadataReportSubjectFormat({ format }),
|
|
...fileSubjectArchitecture(full.subject),
|
|
},
|
|
full.provider,
|
|
{
|
|
operation: full.operation,
|
|
parameters: full.parameters,
|
|
result: malformed,
|
|
},
|
|
);
|
|
expect(
|
|
parseMcpToolError(
|
|
await call("inspect_analysis_view", {
|
|
source: { kind: "inline", evidence: invalid },
|
|
view: { kind: "rust", collection: "edges" },
|
|
}),
|
|
).error,
|
|
).toMatchObject({ code: "invalid_request" });
|
|
}
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"queries historical feature omissions from authenticated bundles without changing the parent",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-historical-features-");
|
|
const path = join(root, "source.elf");
|
|
await writeFile(
|
|
path,
|
|
createRustImage({
|
|
format: "elf",
|
|
payload: rustDependencyPayload({
|
|
packages: [{ name: "old", version: "1", source: "local" }],
|
|
}),
|
|
}),
|
|
);
|
|
const { call } = await connectLocalToolsMcp();
|
|
const captured = await call("inspect_rust_binary", { path });
|
|
expect(captured.isError, JSON.stringify(captured.content)).not.toBe(true);
|
|
const full = parseEvidence(captured.structuredContent);
|
|
const report = rustBinarySchema.parse(full.normalized_result);
|
|
const metadata = report.auditable_metadata;
|
|
if (metadata === null) throw new Error("Expected Rust dependency metadata");
|
|
const historical = createEvidence(
|
|
{
|
|
path,
|
|
sha256: report.artifact.sha256,
|
|
format: "elf",
|
|
...fileSubjectArchitecture(full.subject),
|
|
},
|
|
full.provider,
|
|
{
|
|
operation: full.operation,
|
|
parameters: full.parameters,
|
|
result: {
|
|
...report,
|
|
auditable_metadata: {
|
|
...metadata,
|
|
packages: metadata.packages.map(
|
|
({ features: _features, ...row }) => row,
|
|
),
|
|
},
|
|
},
|
|
},
|
|
);
|
|
const bundle = join(root, "historical-bundle.json");
|
|
const record = join(root, "historical-record.json");
|
|
await writeFile(bundle, JSON.stringify(createEvidenceBundle([historical])));
|
|
await writeFile(record, JSON.stringify(historical));
|
|
await unlink(path);
|
|
const view = { kind: "rust", collection: "packages", fields: ["features"] };
|
|
for (const source of [
|
|
{ kind: "retained-evidence", evidence_id: historical.evidence_id },
|
|
{ kind: "evidence-file", path: record },
|
|
]) {
|
|
const reply = await cli.run({
|
|
arguments: [
|
|
"inspect-analysis-view",
|
|
JSON.stringify({ source, view }),
|
|
...(source.kind === "retained-evidence"
|
|
? ["--evidence-bundle", bundle]
|
|
: []),
|
|
"--json",
|
|
],
|
|
});
|
|
expect(reply.exitCode, reply.stderr + JSON.stringify(reply.json)).toBe(0);
|
|
expect(parseEvidence(reply.json).normalized_result).toMatchObject({
|
|
parent_evidence_id: historical.evidence_id,
|
|
page: { rows: [{ index: 0, features: null }] },
|
|
});
|
|
}
|
|
const imported = await call("import_evidence_bundle", { path: bundle });
|
|
expect(imported.isError, JSON.stringify(imported.content)).not.toBe(true);
|
|
for (const fields of [["features"], [], ["features"]]) {
|
|
const reply = await call("inspect_analysis_view", {
|
|
source: {
|
|
kind: "retained-evidence",
|
|
evidence_id: historical.evidence_id,
|
|
},
|
|
view: { ...view, fields },
|
|
});
|
|
expect(reply.isError, JSON.stringify(reply.content)).not.toBe(true);
|
|
expect(
|
|
parseEvidence(reply.structuredContent).normalized_result,
|
|
).toMatchObject({
|
|
parent_evidence_id: historical.evidence_id,
|
|
page: {
|
|
rows: [
|
|
fields.length === 0 ? { index: 0 } : { index: 0, features: null },
|
|
],
|
|
},
|
|
});
|
|
}
|
|
expect(JSON.parse(await readFile(record, "utf8"))).toEqual(historical);
|
|
},
|
|
);
|
|
|
|
cliTest(
|
|
"preserves unknown Rust metadata and refuses invalid compact CLI choices before reading",
|
|
async ({ cli }) => {
|
|
const root = await createTestTempDirectory("rea-rust-view-missing-");
|
|
const path = join(root, "plain.elf");
|
|
await writeFile(
|
|
path,
|
|
createRustImage({ format: "elf", omitSection: true }),
|
|
);
|
|
const { call } = await connectLocalToolsMcp();
|
|
const summary = await call("inspect_rust_binary", {
|
|
path,
|
|
detail: "summary",
|
|
});
|
|
expect(summary.isError).not.toBe(true);
|
|
const view = analysisViewResultSchema.parse(
|
|
parseEvidence(summary.structuredContent).normalized_result,
|
|
);
|
|
expect(view).toMatchObject({
|
|
summary: { auditable_metadata: null },
|
|
source_coverage: {
|
|
embedded_records: "unknown",
|
|
runtime_dependencies: "unknown",
|
|
},
|
|
});
|
|
const missing = join(root, "never-created");
|
|
for (const args of [
|
|
["--detail", "summary"],
|
|
["--view", "{"],
|
|
["--detail", "complete", "--view", '{"collection":"packages"}'],
|
|
]) {
|
|
const refused = await cli.run({
|
|
arguments: ["inspect-rust-binary", missing, ...args, "--json"],
|
|
});
|
|
expect(refused.exitCode).toBe(1);
|
|
expect(refused.json).toMatchObject({ code: "invalid_request" });
|
|
expect(JSON.stringify(refused.json)).not.toContain("ENOENT");
|
|
}
|
|
expect((await readFile(path)).length).toBeGreaterThan(0);
|
|
},
|
|
);
|