fix(managed): bind graph facts to their source Evidence

This commit is contained in:
morluto
2026-10-10 00:35:17 +08:00
parent b112d44a39
commit cc7b5af077
4 changed files with 309 additions and 124 deletions
@@ -16,43 +16,51 @@ import {
managedPeFixtureTarget,
} from "../../../src/dotnet/ManagedPe.fixture.js";
describe("managed application graph projection", () => {
it("projects managed metadata and native declarations into authenticated graph Evidence", () => {
const bytes = buildManagedPeFixture({
pinvoke: {
moduleName: "user32.dll",
importName: "MessageBoxW",
mappingFlags: 0x0345,
},
});
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
const managedArtifact = inspectManagedArtifactBytes(bytes, binary);
const members = inspectManagedMembersBytes(bytes, binary);
const boundaries = inspectManagedNativeBoundariesBytes(bytes, binary);
const artifactEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
const createManagedInteropEvidence = () => {
const bytes = buildManagedPeFixture({
pinvoke: {
moduleName: "user32.dll",
importName: "MessageBoxW",
mappingFlags: 0x0345,
},
});
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
const managedArtifact = inspectManagedArtifactBytes(bytes, binary);
const members = inspectManagedMembersBytes(bytes, binary);
const boundaries = inspectManagedNativeBoundariesBytes(bytes, binary);
return {
artifactEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_artifact",
parameters: {},
result: managedArtifact,
rawResult: null,
limitations: managedArtifact.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
});
const memberEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
}),
memberEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: members,
rawResult: null,
limitations: members.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
});
const boundaryEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
}),
boundaryEvidence: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_native_boundaries",
parameters: {},
result: boundaries,
rawResult: null,
limitations: boundaries.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
});
}),
};
};
describe("managed application graph projection", () => {
it("projects managed metadata and native declarations into authenticated graph Evidence", () => {
const { artifactEvidence, memberEvidence, boundaryEvidence } =
createManagedInteropEvidence();
const evidence = projectManagedApplicationGraphEvidence({
managed_artifact: artifactEvidence,
@@ -103,21 +111,51 @@ describe("managed application graph projection", () => {
"managed-pinvoke-import",
]),
);
const allEvidenceIds = [
artifactEvidence.evidence_id,
memberEvidence.evidence_id,
boundaryEvidence.evidence_id,
].sort();
const observedEvidenceIds = (kind: string): readonly string[] => {
const observation = graph.nodes.find((node) => node.kind === kind)
?.observations[0];
if (observation === undefined)
throw new Error(`Expected ${kind} observation`);
return observation.evidence.evidence_ids;
};
expect(observedEvidenceIds("artifact")).toEqual(allEvidenceIds);
expect(observedEvidenceIds("managed-assembly")).toEqual([
artifactEvidence.evidence_id,
]);
expect(observedEvidenceIds("managed-module")).toEqual([
artifactEvidence.evidence_id,
]);
for (const kind of ["managed-type", "managed-method", "managed-field"])
expect(observedEvidenceIds(kind)).toEqual([memberEvidence.evidence_id]);
expect(observedEvidenceIds("managed-pinvoke-import")).toEqual([
boundaryEvidence.evidence_id,
]);
for (const edge of graph.edges.filter(
({ relation }) => relation === "contains",
))
expect(edge.evidence.evidence_ids).toEqual(allEvidenceIds);
const method = graph.nodes.find(
({ kind, observations }) =>
kind === "managed-method" &&
observations[0]?.label === "Fixture.Program.Main",
);
expect(method).toBeDefined();
expect(
graph.edges.some(
({ source_node_id, relation, target_node_id }) =>
source_node_id === method?.node_id &&
relation === "imports" &&
graph.nodes.find(({ node_id: id }) => id === target_node_id)?.kind ===
"managed-pinvoke-import",
),
).toBe(true);
const importEdge = graph.edges.find(
({ source_node_id, relation, target_node_id }) =>
source_node_id === method?.node_id &&
relation === "imports" &&
graph.nodes.find(({ node_id: id }) => id === target_node_id)?.kind ===
"managed-pinvoke-import",
);
expect(importEdge).toBeDefined();
expect(importEdge?.evidence.evidence_ids).toEqual(
[memberEvidence.evidence_id, boundaryEvidence.evidence_id].sort(),
);
expect(
graph.nodes.flatMap(({ observations }) =>
observations.map(({ evidence }) => evidence.authority),
@@ -167,7 +205,7 @@ describe("managed application graph request", () => {
});
describe("managed application graph coverage", () => {
it("rejects managed Evidence digest mismatch and discloses missing artifact observations", () => {
it("rejects managed Evidence whose artifact digest differs from its subject", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/Mismatch.dll");
const members = inspectManagedMembersBytes(bytes, binary);
@@ -191,7 +229,14 @@ describe("managed application graph coverage", () => {
expect(JSON.stringify(rejected.error)).toContain(
`Managed Evidence inspect_managed_members (${mismatched.evidence_id}) subject SHA-256 ${binary.sha256} does not match normalized artifact SHA-256 ${"b".repeat(64)}`,
);
});
});
describe("managed application graph identity facts", () => {
it("uses member Evidence for its module and reports only emitted identity nodes", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/MembersOnly.dll");
const members = inspectManagedMembersBytes(bytes, binary);
const aligned = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
@@ -204,11 +249,92 @@ describe("managed application graph coverage", () => {
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projected.value).normalized_result,
);
const graph = parseJavaScriptApplicationGraph(result.graph);
expect(result.summary).toMatchObject({ assemblies: 0, modules: 1 });
expect(
graph.nodes.find(({ kind }) => kind === "managed-module")
?.observations[0],
).toMatchObject({
evidence: {
extractor: { operation: "inspect_managed_members" },
evidence_ids: [aligned.evidence_id],
},
});
expect(result.evidence_links).toEqual([aligned.evidence_id]);
expect(result.limitations).toContain(
"Managed artifact Evidence was not supplied; assembly identity observations are absent.",
);
});
it("binds a boundaries-only module node to the boundary Evidence that supplied it", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/BoundariesOnly.dll");
const boundaries = inspectManagedNativeBoundariesBytes(bytes, binary);
const boundaryEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_native_boundaries",
parameters: {},
result: boundaries,
rawResult: null,
limitations: boundaries.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
});
const projected = projectManagedApplicationGraphEvidence({
managed_native_boundaries: boundaryEvidence,
});
if (!projected.ok) throw projected.error;
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projected.value).normalized_result,
);
const graph = parseJavaScriptApplicationGraph(result.graph);
expect(result.summary).toMatchObject({ assemblies: 0, modules: 1 });
expect(
graph.nodes.find(({ kind }) => kind === "managed-module")
?.observations[0],
).toMatchObject({
evidence: {
extractor: { operation: "inspect_managed_native_boundaries" },
evidence_ids: [boundaryEvidence.evidence_id],
},
});
expect(result.evidence_links).toEqual([boundaryEvidence.evidence_id]);
});
it("reports no assembly or module when the inspector observes no identity rows", () => {
const bytes = buildManagedPeFixture({
moduleRowCount: 0,
assemblyRowCount: 0,
});
const binary = managedPeFixtureTarget(bytes, "/fixture/NoIdentityRows.exe");
const inspection = inspectManagedArtifactBytes(bytes, binary);
expect(inspection).toMatchObject({ module: null, assembly: null });
const artifactEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_artifact",
parameters: {},
result: inspection,
rawResult: null,
limitations: inspection.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
});
const projected = projectManagedApplicationGraphEvidence({
managed_artifact: artifactEvidence,
});
if (!projected.ok) throw projected.error;
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projected.value).normalized_result,
);
const graph = parseJavaScriptApplicationGraph(result.graph);
expect(result.summary).toMatchObject({ assemblies: 0, modules: 0 });
expect(
graph.nodes.filter(
({ kind }) => kind === "managed-assembly" || kind === "managed-module",
),
).toEqual([]);
});
});
describe("managed application graph coverage", () => {
it("preserves partial parser coverage in graph and per-fact coverage", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");