mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +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>
307 lines
9.7 KiB
TypeScript
307 lines
9.7 KiB
TypeScript
import {
|
|
BinaryResourceFailure,
|
|
type ImageSection,
|
|
} from "../domain/binaryContainer.js";
|
|
import { malformedInput, unsupportedInput } from "../domain/parseFailure.js";
|
|
import { keyedBy, uniqueKeyed } from "../domain/keyedCollection.js";
|
|
import { readBinaryContainer } from "../domain/native/readBinaryContainer.js";
|
|
import type {
|
|
RustAuditableMetadata,
|
|
RustBinary,
|
|
RustContainerBits,
|
|
} from "../domain/rust/rustBinary.js";
|
|
import {
|
|
DEFAULT_RUSTC_HASH_LIMIT,
|
|
rustcPartialCoverage,
|
|
rustDependencySectionName,
|
|
} from "../domain/rust/rustBinary.js";
|
|
import { RUST_PRODUCER_DEFAULTS } from "../domain/rust/rustResourceLimits.js";
|
|
import {
|
|
readAuditableSection,
|
|
RUST_AUDITABLE_FORMAT,
|
|
RustAuditableResourceFailure,
|
|
} from "./RustAuditableReader.js";
|
|
import { hasWasmPreamble, readRustWasmSections } from "./RustWasmImage.js";
|
|
|
|
const RUSTC_PREFIX = Buffer.from("/rustc/");
|
|
const HASH_BYTES = 40;
|
|
|
|
/** Container identity plus optional dependency metadata and inferred rustc commits. */
|
|
export interface RustBinaryImage {
|
|
readonly format: RustBinary["format"];
|
|
readonly architecture: string;
|
|
readonly bits: RustContainerBits;
|
|
readonly byte_order: RustBinary["byte_order"];
|
|
readonly auditable_metadata: RustAuditableMetadata | null;
|
|
readonly rustc_commit: RustBinary["rustc_commit"];
|
|
readonly section_names_limitation?: string;
|
|
}
|
|
|
|
interface FileSpan {
|
|
readonly offset: number;
|
|
readonly size: number;
|
|
}
|
|
|
|
/** The stored dependency stream and the PE padding allowance that may follow it. */
|
|
interface DependencySpan extends FileSpan {
|
|
readonly peFileAlignment?: number | undefined;
|
|
}
|
|
|
|
/** Container identity and the exact file spans that Rust metadata lookups read. */
|
|
interface RustImageLayout extends Pick<
|
|
RustBinaryImage,
|
|
"format" | "architecture" | "bits" | "byte_order" | "section_names_limitation"
|
|
> {
|
|
readonly section_name: RustAuditableMetadata["section_name"];
|
|
readonly dependency: DependencySpan | null;
|
|
/** Spans where rustc stores constant `/rustc/<hash>/` path strings. */
|
|
readonly compiler_strings: readonly FileSpan[];
|
|
}
|
|
|
|
/** Read dependency metadata from container sections without executing the image. */
|
|
export const readRustBinaryImage = (
|
|
bytes: Buffer,
|
|
maxRustcHashes = DEFAULT_RUSTC_HASH_LIMIT,
|
|
maxOutputBytes: number = RUST_PRODUCER_DEFAULTS.output_bytes,
|
|
): RustBinaryImage => {
|
|
const { section_name, dependency, compiler_strings, ...identity } =
|
|
hasWasmPreamble(bytes) ? wasmLayout(bytes) : nativeLayout(bytes);
|
|
return {
|
|
...identity,
|
|
auditable_metadata:
|
|
dependency === null
|
|
? null
|
|
: readAuditableSection(
|
|
section_name,
|
|
dependency.offset,
|
|
bytes.subarray(
|
|
dependency.offset,
|
|
dependency.offset + dependency.size,
|
|
),
|
|
{ maxOutputBytes, peFileAlignment: dependency.peFileAlignment },
|
|
),
|
|
rustc_commit: rustcCommit(
|
|
bytes,
|
|
compiler_strings,
|
|
maxRustcHashes,
|
|
maxOutputBytes,
|
|
),
|
|
};
|
|
};
|
|
|
|
const nativeLayout = (bytes: Buffer): RustImageLayout => {
|
|
const container = readContainer(bytes);
|
|
const section_name = rustDependencySectionName(container.format);
|
|
const dependency = dependencySection(section_name, container.sections);
|
|
return {
|
|
format: container.format,
|
|
architecture: container.architecture,
|
|
bits: container.bits,
|
|
byte_order: container.byte_order,
|
|
...(container.section_names_limitation === undefined
|
|
? {}
|
|
: { section_names_limitation: container.section_names_limitation }),
|
|
section_name,
|
|
dependency:
|
|
dependency === null
|
|
? null
|
|
: {
|
|
offset: dependency.offset,
|
|
size: dependency.size,
|
|
peFileAlignment:
|
|
dependency.format === "pe" &&
|
|
dependency.virtual_size === 0 &&
|
|
dependency.file_alignment > 0 &&
|
|
dependency.raw_size % dependency.file_alignment === 0
|
|
? dependency.file_alignment
|
|
: undefined,
|
|
},
|
|
compiler_strings: [
|
|
...container.sections.filter((section) => section.read_only),
|
|
...(container.format === "elf"
|
|
? (container.load_regions ?? [])
|
|
.filter((region) => region.read && !region.write && !region.execute)
|
|
.map(({ mapping }) => mapping)
|
|
: []),
|
|
],
|
|
};
|
|
};
|
|
|
|
/** WebAssembly fixes little-endian encoding but declares no container word size. */
|
|
const wasmLayout = (bytes: Buffer): RustImageLayout => {
|
|
const { dependency_sections, data_section } = readRustWasmSections(bytes);
|
|
if (dependency_sections.length > 1) throw duplicateDependencySection();
|
|
return {
|
|
format: "wasm",
|
|
architecture: "wasm",
|
|
bits: null,
|
|
byte_order: "little",
|
|
section_name: rustDependencySectionName("wasm"),
|
|
dependency: dependency_sections[0] ?? null,
|
|
compiler_strings: data_section === null ? [] : [data_section],
|
|
};
|
|
};
|
|
|
|
/** cargo-auditable writes one dependency section; a second leaves the list ambiguous. */
|
|
const duplicateDependencySection = () =>
|
|
malformedInput(
|
|
RUST_AUDITABLE_FORMAT,
|
|
"Image contains more than one dependency section",
|
|
);
|
|
|
|
const readContainer = (bytes: Buffer) => {
|
|
try {
|
|
return readBinaryContainer(bytes);
|
|
} catch (cause) {
|
|
// A container format failure is already a ParseFailureError; it keeps
|
|
// its own kind and facts instead of being restated as a Rust failure.
|
|
if (cause instanceof BinaryResourceFailure)
|
|
throw new RustAuditableResourceFailure(
|
|
{
|
|
resource: "memory",
|
|
reported_limits: { maximum_bytes: cause.maximum_bytes },
|
|
},
|
|
cause.message,
|
|
{
|
|
cause,
|
|
},
|
|
);
|
|
throw cause;
|
|
}
|
|
};
|
|
|
|
const dependencySection = (
|
|
expected: RustAuditableMetadata["section_name"],
|
|
sections: readonly ImageSection[],
|
|
): ImageSection | null => {
|
|
const selected = uniqueKeyed(
|
|
keyedBy(sections, ({ name }) => name),
|
|
expected,
|
|
);
|
|
if (!selected.ok) throw duplicateDependencySection();
|
|
const section = selected.value;
|
|
if (section === undefined) return null;
|
|
if (section.compressed)
|
|
throw unsupportedInput(
|
|
RUST_AUDITABLE_FORMAT,
|
|
"Compressed dependency sections are unsupported",
|
|
{ undecodedContent: true },
|
|
);
|
|
if (!section.file_backed)
|
|
throw malformedInput(
|
|
RUST_AUDITABLE_FORMAT,
|
|
"Dependency section is not file-backed",
|
|
);
|
|
return section;
|
|
};
|
|
|
|
const rustcCommit = (
|
|
bytes: Buffer,
|
|
spans: readonly FileSpan[],
|
|
maxHashes: number,
|
|
maxOutputBytes: number,
|
|
): RustBinaryImage["rustc_commit"] => {
|
|
const retention: RustcPathRetention = {
|
|
hashes: [],
|
|
seen: new Set(),
|
|
maxHashes,
|
|
maxBytes: maxOutputBytes,
|
|
bytes: 2,
|
|
reason: undefined,
|
|
};
|
|
// Overlapping section and ELF load ranges are scanned once, so total work
|
|
// stays bounded by the file size however many mappings reference a range.
|
|
for (const range of mergedFileRanges(spans)) {
|
|
collectHashes(bytes, range, retention);
|
|
if (retention.reason !== undefined) break;
|
|
}
|
|
return retention.hashes.length === 0
|
|
? null
|
|
: {
|
|
evidence_kind: "inferred",
|
|
hashes: retention.hashes,
|
|
...(retention.reason !== undefined
|
|
? {
|
|
coverage: rustcPartialCoverage(
|
|
retention.reason,
|
|
retention.reason === "report_bytes"
|
|
? maxOutputBytes
|
|
: maxHashes,
|
|
),
|
|
}
|
|
: {}),
|
|
};
|
|
};
|
|
|
|
/** One owner for compiler-path uniqueness, retained output and exhaustion. */
|
|
interface RustcPathRetention {
|
|
readonly hashes: { hash: string; file_offset: number }[];
|
|
readonly seen: Set<string>;
|
|
readonly maxHashes: number;
|
|
readonly maxBytes: number;
|
|
bytes: number;
|
|
reason: "max_rustc_hashes" | "report_bytes" | undefined;
|
|
}
|
|
|
|
const mergedFileRanges = (
|
|
ranges: readonly FileSpan[],
|
|
): { readonly offset: number; readonly end: number }[] => {
|
|
const sorted = ranges
|
|
.filter(({ size }) => size > 0)
|
|
.map(({ offset, size }) => ({ offset, end: offset + size }))
|
|
.sort((left, right) => left.offset - right.offset);
|
|
const merged: { offset: number; end: number }[] = [];
|
|
for (const range of sorted) {
|
|
const last = merged.at(-1);
|
|
if (last !== undefined && range.offset <= last.end)
|
|
last.end = Math.max(last.end, range.end);
|
|
else merged.push({ ...range });
|
|
}
|
|
return merged;
|
|
};
|
|
|
|
const collectHashes = (
|
|
bytes: Buffer,
|
|
range: { readonly offset: number; readonly end: number },
|
|
retention: RustcPathRetention,
|
|
): void => {
|
|
const region = bytes.subarray(range.offset, range.end);
|
|
let cursor = 0;
|
|
while (cursor < region.length) {
|
|
const at = region.indexOf(RUSTC_PREFIX, cursor);
|
|
if (at < 0) return;
|
|
const start = at + RUSTC_PREFIX.length;
|
|
const hash = hashAt(region, start);
|
|
if (hash !== null && !retention.seen.has(hash)) {
|
|
if (retention.seen.size >= retention.maxHashes) {
|
|
retention.reason = "max_rustc_hashes";
|
|
return;
|
|
}
|
|
const entry = { hash, file_offset: range.offset + at };
|
|
const entryBytes = Buffer.byteLength(JSON.stringify(entry)) + 1;
|
|
if (entryBytes > retention.maxBytes - retention.bytes) {
|
|
retention.reason = "report_bytes";
|
|
return;
|
|
}
|
|
retention.bytes += entryBytes;
|
|
retention.seen.add(hash);
|
|
retention.hashes.push(entry);
|
|
}
|
|
cursor = at + 1;
|
|
}
|
|
};
|
|
|
|
const hashAt = (region: Buffer, start: number): string | null => {
|
|
const end = start + HASH_BYTES;
|
|
// Windows builds join the remapped `/rustc/<hash>` prefix with `\`.
|
|
if (end >= region.length || (region[end] !== 0x2f && region[end] !== 0x5c))
|
|
return null;
|
|
for (let index = start; index < end; index += 1) {
|
|
const byte = region[index] ?? 0;
|
|
const hex =
|
|
(byte >= 0x30 && byte <= 0x39) || (byte >= 0x61 && byte <= 0x66);
|
|
if (!hex) return null;
|
|
}
|
|
return region.toString("latin1", start, end);
|
|
};
|