mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35: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>
300 lines
8.6 KiB
TypeScript
300 lines
8.6 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import {
|
|
changedEvidenceSubject,
|
|
fileIdentity,
|
|
} from "../../../tests/fixtures/evidenceSubject.js";
|
|
import { metadataReportSubjectFormat } from "../apple/machoTargetSelection.js";
|
|
import { binaryMetadataArchitecture } from "../binaryMetadataArchitecture.js";
|
|
import { createEvidence } from "../evidence.js";
|
|
import { freezeJsonSnapshot } from "../immutableJson.js";
|
|
import type { RustBinary } from "../rust/rustBinary.js";
|
|
import { projectAnalysisView } from "./analysisView.js";
|
|
import type { AnalysisViewParent } from "./analysisViewParent.js";
|
|
|
|
const report: RustBinary = {
|
|
artifact: { path: "/artifacts/rust", sha256: "a".repeat(64), bytes: 512 },
|
|
format: "elf",
|
|
architecture: "amd64",
|
|
bits: 64,
|
|
byte_order: "little",
|
|
auditable_metadata: {
|
|
section_name: ".dep-v0",
|
|
file_offset: 32,
|
|
section_bytes: 64,
|
|
compressed_bytes: 64,
|
|
uncompressed_bytes: 128,
|
|
format_version: null,
|
|
packages: [
|
|
{
|
|
name: "root",
|
|
version: "1",
|
|
source: "local",
|
|
kind: null,
|
|
root: true,
|
|
dependencies: [0],
|
|
features: [],
|
|
},
|
|
],
|
|
root_package_index: 0,
|
|
dependency_edges: [{ from_index: 0, to_index: 0 }],
|
|
cyclic_dependency_indices: [0],
|
|
},
|
|
rustc_commit: null,
|
|
limitations: [],
|
|
};
|
|
const parent = (value = structuredClone(report)): AnalysisViewParent => {
|
|
const architecture = binaryMetadataArchitecture(value.architecture);
|
|
const evidence = createEvidence(
|
|
{
|
|
path: value.artifact.path,
|
|
sha256: value.artifact.sha256,
|
|
format: metadataReportSubjectFormat(value),
|
|
...(architecture === undefined ? {} : { architecture }),
|
|
},
|
|
{ id: "rust-auditable", name: "Rust reader", version: "1" },
|
|
{
|
|
operation: "inspect_rust_binary",
|
|
parameters: { path: value.artifact.path },
|
|
result: value,
|
|
},
|
|
);
|
|
return {
|
|
predicateType: evidence.predicate_type,
|
|
evidenceId: evidence.evidence_id,
|
|
operation: evidence.operation,
|
|
subject: evidence.subject,
|
|
normalizedResult: value,
|
|
limitations: [],
|
|
};
|
|
};
|
|
|
|
it("revalidates mutable and merely frozen Rust imports despite a claimed seal", () => {
|
|
const mutable = structuredClone(report);
|
|
const input = { ...parent(mutable), sealedResult: true };
|
|
expect(projectAnalysisView(input, { kind: "summary" }).ok).toBe(true);
|
|
Object.freeze(mutable);
|
|
mutable.auditable_metadata!.dependency_edges[0]!.to_index = 99;
|
|
expect(
|
|
projectAnalysisView(input, {
|
|
kind: "rust",
|
|
collection: "edges",
|
|
offset: 0,
|
|
limit: 1,
|
|
}).ok,
|
|
).toBe(false);
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"pages historical Rust feature omissions with a sealed source: %s",
|
|
(sealed) => {
|
|
const previous = structuredClone(report);
|
|
const metadata = previous.auditable_metadata;
|
|
if (metadata === null) throw new Error("Expected Rust metadata");
|
|
const legacy = {
|
|
...previous,
|
|
auditable_metadata: {
|
|
...metadata,
|
|
packages: metadata.packages.map(
|
|
({ features: _features, ...pkg }) => pkg,
|
|
),
|
|
},
|
|
} as unknown as RustBinary;
|
|
if (sealed) freezeJsonSnapshot(legacy);
|
|
const input = { ...parent(legacy), sealedResult: sealed };
|
|
const result = projectAnalysisView(input, {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
fields: ["features"],
|
|
offset: 0,
|
|
limit: 10,
|
|
});
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value).toMatchObject({
|
|
page: { rows: [{ index: 0, features: null }] },
|
|
});
|
|
},
|
|
);
|
|
|
|
it("checks exact artifact identity and subject architecture for every Rust view", () => {
|
|
for (const changed of [
|
|
{ format: "pe" as const },
|
|
{ architecture: "arm64" as const },
|
|
{ sha256: "b".repeat(64) },
|
|
]) {
|
|
const source = parent();
|
|
if (
|
|
source.subject === undefined ||
|
|
fileIdentity(source.subject).format !== "elf"
|
|
)
|
|
throw new Error("Missing ELF source");
|
|
const input = {
|
|
...source,
|
|
subject: changedEvidenceSubject(source.subject, changed),
|
|
};
|
|
expect(projectAnalysisView(input, { kind: "summary" }).ok).toBe(false);
|
|
expect(
|
|
projectAnalysisView(input, {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
offset: 0,
|
|
limit: 1,
|
|
}).ok,
|
|
).toBe(false);
|
|
}
|
|
});
|
|
|
|
const withSectionName = (
|
|
value: RustBinary,
|
|
section_name: NonNullable<RustBinary["auditable_metadata"]>["section_name"],
|
|
): RustBinary => ({
|
|
...value,
|
|
auditable_metadata:
|
|
value.auditable_metadata === null
|
|
? null
|
|
: { ...value.auditable_metadata, section_name },
|
|
});
|
|
|
|
it("refuses imported reports whose container facts contradict their format", () => {
|
|
const wasm: RustBinary = {
|
|
...structuredClone(report),
|
|
format: "wasm",
|
|
architecture: "wasm",
|
|
bits: null,
|
|
byte_order: "little",
|
|
};
|
|
const accepted = projectAnalysisView(parent(wasm), { kind: "summary" });
|
|
if (!accepted.ok || accepted.value.kind !== "summary")
|
|
throw new Error("WebAssembly summary failed");
|
|
expect(accepted.value.summary).toMatchObject({
|
|
format: "wasm",
|
|
architecture: "wasm",
|
|
bits: null,
|
|
byte_order: "little",
|
|
});
|
|
for (const value of [
|
|
{ ...wasm, architecture: "x86_64" },
|
|
{ ...wasm, byte_order: "big" as const },
|
|
{ ...wasm, bits: 32 as const },
|
|
{ ...structuredClone(report), architecture: "wasm" },
|
|
withSectionName(wasm, "__DATA,.dep-v0"),
|
|
withSectionName(structuredClone(report), "__DATA,.dep-v0"),
|
|
])
|
|
expect(projectAnalysisView(parent(value), { kind: "summary" }).ok).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("bounds inherited warnings and retains cycle counts without walking a cyclic graph", () => {
|
|
const input = { ...parent(), limitations: ["x".repeat(300_000)] };
|
|
const result = projectAnalysisView(input, { kind: "summary" });
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value).toMatchObject({
|
|
summary: {
|
|
format: "elf",
|
|
bits: 64,
|
|
auditable_metadata: {
|
|
format_version: null,
|
|
counts: { packages: 1, edges: 1, cyclic_packages: 1 },
|
|
},
|
|
},
|
|
source_coverage: {
|
|
embedded_records: "complete",
|
|
runtime_dependencies: "unknown",
|
|
},
|
|
});
|
|
expect(JSON.stringify(result.value).length).toBeLessThan(5000);
|
|
expect(result.value.limitations.join(" ")).toContain(input.evidenceId);
|
|
});
|
|
|
|
it("pages matching original indices and omits dependency vectors unless selected", () => {
|
|
const value = structuredClone(report);
|
|
expect(value.auditable_metadata?.packages[0]?.features).toEqual([]);
|
|
value.auditable_metadata!.packages.push({
|
|
name: "other",
|
|
version: "1",
|
|
source: "local",
|
|
kind: null,
|
|
root: false,
|
|
dependencies: [],
|
|
features: [],
|
|
});
|
|
const source = parent(value);
|
|
const first = projectAnalysisView(source, {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
offset: 0,
|
|
limit: 1,
|
|
});
|
|
if (!first.ok || first.value.kind !== "rust")
|
|
throw new Error("Rust page failed");
|
|
expect(first.value.page.rows).toEqual([
|
|
{
|
|
index: 0,
|
|
name: "root",
|
|
version: "1",
|
|
source: "local",
|
|
kind: null,
|
|
root: true,
|
|
},
|
|
]);
|
|
expect(first.value.coverage).toMatchObject({
|
|
total: 2,
|
|
examined: 1,
|
|
exhausted: false,
|
|
next_offset: 1,
|
|
});
|
|
const next = projectAnalysisView(source, {
|
|
kind: "rust",
|
|
collection: "packages",
|
|
offset: 1,
|
|
limit: 1,
|
|
fields: [],
|
|
});
|
|
if (!next.ok || next.value.kind !== "rust")
|
|
throw new Error("Rust continuation failed");
|
|
expect(next.value.page.rows).toEqual([{ index: 1 }]);
|
|
expect(next.value.coverage).toMatchObject({
|
|
total: 2,
|
|
examined: 1,
|
|
exhausted: true,
|
|
next_offset: null,
|
|
});
|
|
});
|
|
|
|
it.each(["packages", "edges"] as const)(
|
|
"distinguishes unavailable Rust %s from decoded empty records",
|
|
(collection) => {
|
|
for (const available of [false, true]) {
|
|
const metadata = report.auditable_metadata!;
|
|
const result = projectAnalysisView(
|
|
parent({
|
|
...report,
|
|
auditable_metadata: available
|
|
? {
|
|
...metadata,
|
|
packages: [],
|
|
root_package_index: null,
|
|
dependency_edges: [],
|
|
cyclic_dependency_indices: [],
|
|
}
|
|
: null,
|
|
}),
|
|
{ kind: "rust", collection, offset: 3, limit: 1 },
|
|
);
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.coverage).toEqual({
|
|
status: available ? "empty" : "unavailable",
|
|
examined: 0,
|
|
total: available ? 0 : null,
|
|
next_offset: null,
|
|
exhausted: available,
|
|
});
|
|
expect(result.value).toMatchObject({
|
|
source_coverage: {
|
|
embedded_records: available ? "complete" : "unknown",
|
|
},
|
|
});
|
|
}
|
|
},
|
|
);
|