Files
reverse-engineering-anything/tests/conformance/providers/managed/managedNativeVerification.test.ts
T
morluto b5e98915c6 fix(tools)!: simplify direct invocation and harden boundary contracts (#555)
Remove REA permission grants, scope ceilings, approval confirmations, and inert settings. Preserve selected local evidence, accept caller-chosen commands and paths, and report actual runtime failures.

Harden provider results, schema serialization, snapshot evidence binding, archive paths, source maps, native screenshots, managed EH parsing, and cancellation cleanup. Regenerate tool metadata and document direct invocation.

BREAKING CHANGE: Removed permission configuration and policy commands, approval fields, filesystem scope inputs, and fixed confirmation options. Process capture inherits the host environment and accepts command names; callers must refresh tool schemas and migrate removed fields.
2026-10-05 20:16:44 +08:00

266 lines
7.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { verifyManagedNativeBoundariesEvidence } from "../../../../src/application/ManagedNativeVerificationService.js";
import { MANAGED_NATIVE_VERIFICATION_EXAMPLE } from "../../../../src/contracts/managedWorkflowExamples.js";
import { createEvidence } from "../../../../src/domain/evidence.js";
import {
managedNativeVerificationInputSchema,
managedNativeVerificationResultSchema,
verifyManagedNativeBoundaries,
} from "../../../../src/domain/managedNativeVerification.js";
import { inspectMachoSchema } from "../../../../src/domain/nativeInspection.js";
const exampleInput = () =>
managedNativeVerificationInputSchema.parse(
MANAGED_NATIVE_VERIFICATION_EXAMPLE,
);
const nativeEvidenceWithExports = (names: readonly string[]) => {
const input = exampleInput();
const native = input.native_observations[0];
if (native === undefined || native.subject === null)
throw new Error("missing native example");
const macho = inspectMachoSchema.parse(native.normalized_result);
return createEvidence(
{
path: native.subject.local_path,
sha256: native.subject.digest.sha256,
format: native.subject.format,
...(native.subject.architecture === null
? {}
: { architecture: native.subject.architecture }),
},
native.provider,
{
operation: native.operation,
parameters: native.parameters,
result: {
...macho,
exports: {
...macho.exports,
items: names.map((name, index) => ({
name,
address: `0x${(0x1000 + index).toString(16)}`,
weak: false,
reexport: false,
source: "nm",
})),
total: names.length,
},
},
rawResult: native.raw_result,
confidence: native.confidence,
authority: native.authority,
environment: native.environment,
limitations: native.limitations,
locations: native.locations,
evidenceLinks: native.evidence_links,
},
);
};
describe("managed/native boundary verification", () => {
it("verifies a P/Invoke declaration against native export Evidence", () => {
const result = verifyManagedNativeBoundaries(exampleInput());
expect(result).toMatchObject({
algorithm: {
name: "rea-managed-native-verification",
token_to_address_mapping: "not-inferred",
},
managed_boundary: {
artifact_sha256: "6".repeat(64),
mvid: "00112233-4455-4677-8899-aabbccddeeff",
pinvoke_imports_total: 1,
},
native_observations: {
total: 1,
accepted: 1,
unsupported: 0,
symbols: 1,
},
summary: {
verified: 1,
inferred: 0,
unresolved: 0,
contradicted: 0,
},
pinvoke_imports: [
{
managed: {
import_name: "open_native",
import_scope_name: "nativehelper.dll",
declaration_verification: "managed-declaration-only",
},
status: "verified",
basis: "exact-export-name",
confidence: "observed",
matched_native: {
name: "open_native",
source: "macho-export",
},
},
],
});
expect(result.verification_id).toMatch(/^mnv_[a-f0-9]{64}$/u);
expect(result.limitations.join(" ")).toContain(
"does not prove CLR binding",
);
});
it("reports module mismatch as a contradiction within supplied Evidence", () => {
const input = exampleInput();
const native = input.native_observations[0];
if (native === undefined || native.subject === null)
throw new Error("missing native example");
const mismatched = createEvidence(
{
path: "/examples/other.dll",
sha256: native.subject.digest.sha256,
format: "pe",
...(native.subject.architecture === null
? {}
: { architecture: native.subject.architecture }),
},
native.provider,
{
operation: native.operation,
parameters: native.parameters,
result: native.normalized_result,
rawResult: native.raw_result,
confidence: native.confidence,
authority: native.authority,
environment: native.environment,
limitations: native.limitations,
locations: native.locations,
evidenceLinks: native.evidence_links,
},
);
const result = verifyManagedNativeBoundaries({
...input,
native_observations: [mismatched],
});
expect(result.summary).toMatchObject({
verified: 0,
contradicted: 1,
});
expect(result.pinvoke_imports[0]).toMatchObject({
status: "contradicted",
basis: "module-mismatch",
});
});
it("treats supported native Evidence with no symbols as unresolved", () => {
const result = verifyManagedNativeBoundaries({
...exampleInput(),
native_observations: [nativeEvidenceWithExports([])],
});
expect(result.native_observations).toMatchObject({
accepted: 1,
unsupported: 0,
symbols: 0,
});
expect(result.summary).toMatchObject({
verified: 0,
unresolved: 1,
});
expect(result.pinvoke_imports[0]).toMatchObject({
status: "unresolved",
basis: "no-native-candidate",
});
});
it("does not verify a symbol whose spelling differs only by case", () => {
const result = verifyManagedNativeBoundaries({
...exampleInput(),
native_observations: [nativeEvidenceWithExports(["OPEN_NATIVE"])],
});
expect(result.summary).toMatchObject({ verified: 0, unresolved: 1 });
expect(result.pinvoke_imports[0]).toMatchObject({
status: "unresolved",
matched_native: null,
candidates: [],
});
});
it("retains every observed native candidate", () => {
const result = verifyManagedNativeBoundaries({
...exampleInput(),
native_observations: [
nativeEvidenceWithExports(["open_native", "_open_native"]),
],
});
expect(result.coverage).toMatchObject({
status: "complete-within-inputs",
});
expect(result.pinvoke_imports[0]?.candidates).toHaveLength(2);
});
it("wraps verification in derived workflow Evidence", () => {
const evidence = verifyManagedNativeBoundariesEvidence(exampleInput());
expect(evidence.ok).toBe(true);
if (!evidence.ok) return;
expect(evidence.value).toMatchObject({
operation: "verify_managed_native_boundaries",
provider: { id: "rea-dotnet-workflows" },
confidence: "inferred",
authority: "analyst-inference",
normalized_result: {
summary: { verified: 1 },
},
});
expect(evidence.value.evidence_links).toHaveLength(2);
});
});
describe("managed/native verification result algebra", () => {
it("rejects states without their required native match", () => {
const result = verifyManagedNativeBoundaries(exampleInput());
const verification = result.pinvoke_imports[0];
expect(verification).toBeDefined();
if (verification === undefined) return;
expect(
managedNativeVerificationResultSchema.safeParse({
...result,
pinvoke_imports: [
{
...verification,
matched_native: null,
candidates: [],
},
],
}).success,
).toBe(false);
expect(
managedNativeVerificationResultSchema.safeParse({
...result,
pinvoke_imports: [
{
...verification,
status: "unresolved",
basis: "no-native-candidate",
confidence: "unknown",
},
],
}).success,
).toBe(false);
expect(
managedNativeVerificationResultSchema.safeParse({
...result,
coverage: {
status: "complete-within-inputs",
omitted_native_observations: 0,
omitted_candidates: 1,
},
}).success,
).toBe(false);
});
});