Files
reverse-engineering-anything/tests/composition/analysis-sessions/managedApplicationGraph.test.ts
T
morluto 1f24b3fa57 feat(evidence)!: name the Evidence subject by its target identity (format revision 2) (#11416)
* feat(evidence)!: name the Evidence subject by its target identity (format revision 2)

The Evidence subject carries subject.identity (kind, sha256, format,
architecture, selection) and the Evidence ID commits to it, so raw/DOL
entry points and ESP flash offsets of the same bytes are different
subjects. Every record declares evidence_format: 2; revision 1 bundles,
standalone records and snapshots are refused with the recreate guidance.
binaryTargetIdentity is the one projection of an open target, used by
Evidence and snapshot comparison.

BREAKING CHANGE: Evidence format revision 2. Evidence IDs commit to the
subject's target identity (subject.identity, including the raw/DOL entry
point or ESP flash offsets), and every record declares evidence_format: 2.
Evidence bundles, records and analysis snapshots from earlier releases are
refused; recreate the observations with this release.

* refactor(analysis-view): read parent subjects through the Evidence identity accessors (#11423)

Analysis views compare their parent's file identity (evidenceFileTarget)
and take path and digest from evidenceSubjectTarget instead of reading
the subject's digest, format and architecture members, which duplicate
subject.identity until they are removed. A counted guard
(evidence-subject-declaration-read) keeps new direct reads out and
records the 20 remaining files.

* fix(evidence): carry format refusals through Evidence readers and bind target selections

- safeParseEvidence/safeParseImmutableEvidence return the format refusal as
  an expected failure; every consumer maps it to the one integrity refusal
  with the recreate guidance. An unmarked object is an earlier record only
  when it has every member records have always carried.
- ArtifactProvider inventory roots and provider subjects that name the
  target's own bytes carry the target's selection (ESP flash offsets).
- Snapshot rows bind only Evidence whose identity, selection included,
  matches the snapshot target.
- Entry-point selections use the raw-binary entry point form.

* fix(evidence): refuse unauthenticated or earlier-format records instead of reading them as absent

- recognizeEvidence (evidenceValidation) replaces tryParseEvidence: input
  without an evidence_id is not Evidence (ok(undefined)); a record that names
  one but fails authentication or is from another format revision is an
  EvidenceIntegrityError carrying the recreate guidance, never undefined.
- Direct analysis retains that refusal as a secondary failure instead of
  silently skipping source capture; cleanup projection, source-closure
  annotation and the flutter, debugger and release-exposure recoveries use
  the same owner (the last three previously read the unauthenticated schema).
- A desktop capture.json from an earlier format is refused with the recreate
  guidance instead of "not desktop application capture Evidence".

* fix(evidence): advertise revision 2 ledger examples and rank delivery omissions by serialized size

- The obligation ledger and reconstruction coverage examples named
  evidence_format 1, which revision 2 refuses; they now use
  EVIDENCE_FORMAT_REVISION.
- MCP delivery chose which unretained recovery to omit by a parsed-storage
  lower bound clipped at the response budget. Once revision 2 subjects made
  the source record's estimate cross the budget, both candidates tied at the
  clip and the smaller source was omitted ahead of the larger completed
  observation. Candidates are now ranked by serialized size, which is what
  the budget counts.

* refactor(native): compare function snapshots by their Evidence target identity (#11450)

Function snapshots carry the subject's file target identity instead of the
duplicated subject declaration. Call paths and function comparisons compare
targets through sameTargetIdentity, so the same bytes opened at a different
raw entry point or ESP flash offset no longer count as one coordinate space:
build_call_path refuses the mix, and compare_functions no longer matches
entries by address across selections. Structural candidates and native
function matching read the architecture and digest through
evidenceFileTarget.

Refs #11417

* refactor(artifacts): compare artifact and WASM summaries by their Evidence target identity (#11461)

Artifact comparison authenticates a summary against its retained inventory
through sameTargetIdentity and rebinds the alias with the whole target,
selection included, so a flash-selected inventory summary taken under an
alias path is no longer refused. WASM comparison, the analysis view
artifact projection and the inventory root check read the digest, format
and display path through the Evidence identity accessors.

Refs #11417

* refactor(evidence): read record subjects through the identity accessors in JS, managed, ledger and bundle readers (#11470)

JavaScript application graphs, runtime reconciliation, managed inspection
and native verification sources, the record summary filter, the ledger's
residual-unknown target and bundle scope selection read the subject digest,
format and display path through evidenceFileTarget and
evidenceSubjectDisplay. No comparison changes: each reader matches one
record's facts against its own payload or a caller filter.

Refs #11417

* refactor(evidence)!: name the Evidence subject only by its target identity (#11497)

* refactor(evidence)!: name the Evidence subject only by its target identity

The subject keeps `identity` and the display facts `name` and `local_path`.
The duplicated `digest`, `format` and `architecture` members are removed; the
format-owned declaration now validates the identity itself. Readers the type
checker found move to the owner accessors; untyped reads in verify scripts,
the efficiency harness and test matchers move to `subject.identity`. The
`evidence-subject-declaration-read` text guard and its baseline are deleted.

Fixes #11473
Refs #11417

BREAKING CHANGE: the Evidence subject no longer has `digest`, `format` or
`architecture`; read `subject.identity.sha256`, `subject.identity.format` and
`subject.identity.architecture`. A record that still carries them is refused.

* test(evidence): give the Flutter verifier fixture the identity subject and check the DOL declaration without the ID

* test(evidence): drop the removed subject members from the declaration matrix and tamper fixtures

* feat(evidence)!: give every Evidence record an explicit subject identity (#11536)

* feat(evidence)!: give every Evidence record an explicit subject identity

A producer that cannot name a file passes an `unknown` target identity
with a closed reason (derived-from-linked-evidence, not-observed,
unsupported-subject) instead of no subject. The subject is non-null;
unknown subjects have no display facts. The generic missing-subject
limitation and `subjectUnavailableReason` are removed; producer-specific
explanations are ordinary limitations. Comparison identity requires the
derived unknown, and null-subject consumers ask the owner accessors.

Refs #11417

BREAKING CHANGE: every Evidence record has a subject. A record without a
file target names `subject.identity` `{ "kind": "unknown", "reason": … }`
with `name` and `local_path` null, instead of `subject: null` and the
limitation "Artifact identity is unavailable for this observation.".
Their Evidence IDs change.

* fix(evidence): name explicit unknown subjects in JavaScript callers and keep a managed graph's file subject

The verification scripts are not type-checked, so five createEvidence
callers still passed undefined (or a missing first target) after the subject
became required. They now pass unknownTarget("not-observed").

A managed application graph chose the first present input subject. Every
subject is now present, so an unknown artifact subject hid a file subject on
the member or boundary Evidence. The graph now takes the first input subject
that names a file.

* test(evidence): assert the subject kind instead of a presence the type now guarantees

Every Evidence record has a subject after this change, so test guards and
assertions that checked it for null always passed. Each now checks that the
fixture subject names a file, or is removed where a later fileIdentity,
fileSubjectTarget or fileSubjectDisplay call already requires that. Non-null
assertions on Evidence subjects are removed; analysis-view inputs, whose
subject is optional, keep an undefined check.

* test(evidence): expect the Evidence subject format under its identity

Two boundary tests still matched subject.format, which slice 2e moved into
subject.identity. toMatchObject is not type-checked against the Evidence
shape, so they failed only when run (the signature test on macOS).
2026-11-16 06:28:01 +08:00

1216 lines
43 KiB
TypeScript

import { unknownTarget } from "../../../src/domain/targetIdentity.js";
import { describe, expect, it } from "vitest";
import { projectManagedApplicationGraphEvidence } from "../../../src/application/managed/ManagedApplicationGraphService.js";
import {
managedApplicationGraphReferenceInputSchema,
managedApplicationGraphResultSchema,
} from "../../../src/domain/managed/managedApplicationGraph.js";
import { MANAGED_APPLICATION_GRAPH_EXAMPLE } from "../../../src/contracts/managed/managedWorkflowExamples.js";
import { traceApplicationFeatureEvidence } from "../../../src/application/javascript/JavaScriptApplicationWorkflowService.js";
import { MANAGED_STATIC_PROVIDER } from "../../../src/domain/investigationProviders.js";
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
import { parseJavaScriptApplicationGraph } from "../../../src/domain/javascript/javascriptApplicationGraph.js";
import {
managedMemberInspectionSchema,
managedNativeBoundaryInspectionSchema,
} from "../../../src/domain/managed/managedArtifact.js";
import { inspectManagedArtifactBytes } from "../../../src/dotnet/ManagedArtifactInspector.js";
import { inspectManagedMembersBytes } from "../../../src/dotnet/ManagedMemberInspector.js";
import { inspectManagedNativeBoundariesBytes } from "../../../src/dotnet/ManagedNativeBoundaryInspector.js";
import {
buildManagedPeFixture,
managedPeFixtureTarget,
} from "../../../src/dotnet/ManagedPe.fixture.js";
/** Tiny-format CIL method body. */
const tinyIl = (il: readonly number[]): Buffer =>
Buffer.from([(il.length << 2) | 2, ...il]);
const memberGraph = (
bytes: Buffer,
options: Parameters<typeof inspectManagedMembersBytes>[2] = {},
) => {
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
const members = inspectManagedMembersBytes(bytes, binary, options);
const memberEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: members,
rawResult: null,
limitations: members.limitations,
});
const projection = projectManagedApplicationGraphEvidence({
managed_members: memberEvidence,
});
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
const graph = parseJavaScriptApplicationGraph(result.graph);
return { members, memberEvidence, result, graph };
};
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 }],
}),
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 }],
}),
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 MethodDef scope", () => {
it("preserves MethodDef page omissions from decoded member Evidence", () => {
const bytes = buildManagedPeFixture({
ilBody: tinyIl([0x2a]),
methodRowCount: 3,
});
const full = memberGraph(bytes);
expect(full.members.method_rows).toEqual({ offset: 0, count: 3, total: 3 });
expect(full.result.coverage.status).toBe("complete-within-inputs");
for (const options of [
{ methodLimit: 0 },
{ methodOffset: 1, methodLimit: 1 },
{ methodOffset: 3 },
]) {
const page = memberGraph(bytes, options);
expect(page.members.coverage.state).toBe("complete");
expect(page.result.summary.methods).toBe(page.members.method_rows!.count);
expect(page.result.coverage.status).toBe("partial");
expect(page.result.limitations).toContain(
`Managed member Evidence examined MethodDef rows at offset ${page.members.method_rows!.offset} with count ${page.members.method_rows!.count} of 3 total rows; methods outside that interval and their relations are omitted, not absent.`,
);
}
});
it("keeps an unexamined MethodDef scope unknown", () => {
const bytes = buildManagedPeFixture({ ilBody: tinyIl([0x2a]) });
const binary = managedPeFixtureTarget(bytes);
const members = inspectManagedMembersBytes(bytes, binary);
const evidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: { ...members, method_rows: null },
rawResult: null,
limitations: members.limitations,
});
const projection = projectManagedApplicationGraphEvidence({
managed_members: evidence,
});
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
expect(result.coverage.status).toBe("partial");
expect(result.summary.methods).toBe(1);
expect(result.limitations).toContainEqual(
expect.stringMatching(
/MethodDef row scope.*methods.*relations.*unknown/iu,
),
);
});
});
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,
managed_members: memberEvidence,
managed_native_boundaries: boundaryEvidence,
});
expect(
evidence.ok,
evidence.ok
? undefined
: `${JSON.stringify(evidence.error)} cause=${String(evidence.error.cause)}`,
).toBe(true);
if (!evidence.ok) throw new Error("projection failed");
const parsed = parseEvidence(evidence.value);
expect(parsed).toMatchObject({
operation: "project_managed_application_graph",
predicate_type: "rea.managed-application-graph",
provider: { id: "rea-dotnet-workflows" },
confidence: "inferred",
authority: "analyst-inference",
evidence_links: [
artifactEvidence.evidence_id,
memberEvidence.evidence_id,
boundaryEvidence.evidence_id,
],
});
const result = managedApplicationGraphResultSchema.parse(
parsed.normalized_result,
);
const graph = parseJavaScriptApplicationGraph(result.graph);
expect(result.summary).toMatchObject({
assemblies: 1,
modules: 1,
types: 1,
methods: 1,
fields: 1,
pinvoke_imports: 1,
});
expect(graph.nodes.map(({ kind }) => kind)).toEqual(
expect.arrayContaining([
"artifact",
"managed-assembly",
"managed-module",
"managed-type",
"managed-method",
"managed-field",
"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,
]);
const containmentEdges = graph.edges.filter(
({ relation }) => relation === "contains",
);
expect(containmentEdges.length).toBeGreaterThan(0);
for (const edge of containmentEdges)
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();
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),
),
).toContain("managed-static-analysis");
const trace = traceApplicationFeatureEvidence({
application: parsed,
native_observations: [],
seed: {
kind: "string",
value: "MessageBoxW",
match: "exact",
case_sensitive: true,
},
direction: "incoming",
});
expect(trace.ok, trace.ok ? undefined : JSON.stringify(trace.error)).toBe(
true,
);
if (!trace.ok) throw new Error("trace failed");
expect(trace.value.result).toMatchObject({
source_evidence_id: parsed.evidence_id,
summary: { matched_seeds: 1 },
});
});
});
it("preserves only a managed graph's matching subject in feature traces", () => {
const { artifactEvidence } = createManagedInteropEvidence();
const selectedArtifact = parseEvidence({
...artifactEvidence,
subject: { ...artifactEvidence.subject, name: "selected managed image" },
});
const projected = projectManagedApplicationGraphEvidence({
managed_artifact: selectedArtifact,
});
if (!projected.ok) throw projected.error;
const parsed = parseEvidence(projected.value);
expect(parsed.subject).toEqual(selectedArtifact.subject);
const query = {
seed: {
kind: "string" as const,
value: "MessageBoxW",
match: "exact" as const,
case_sensitive: true,
},
direction: "incoming" as const,
};
const trace = traceApplicationFeatureEvidence({
application: parsed,
...query,
});
if (!trace.ok) throw trace.error;
expect(trace.value.evidence.subject).toEqual(selectedArtifact.subject);
const foreign = createEvidence(
{ path: "/other.exe", sha256: "f".repeat(64), format: "pe" },
parsed.provider,
{
operation: parsed.operation,
predicateType: parsed.predicate_type,
parameters: parsed.parameters,
result: parsed.normalized_result,
confidence: "inferred",
authority: "analyst-inference",
evidenceLinks: parsed.evidence_links,
},
);
const foreignTrace = traceApplicationFeatureEvidence({
application: foreign,
...query,
});
if (!foreignTrace.ok) throw foreignTrace.error;
expect(foreignTrace.value.evidence.subject.identity.kind).toBe("unknown");
const contradictory = createEvidence(
{ path: "/other.exe", sha256: "f".repeat(64), format: "pe" },
parsed.provider,
{
operation: parsed.operation,
predicateType: parsed.predicate_type,
parameters: parsed.parameters,
result: {
...(parsed.normalized_result as Record<string, unknown>),
root_artifact_sha256: "f".repeat(64),
},
confidence: "inferred",
authority: "analyst-inference",
evidenceLinks: parsed.evidence_links,
},
);
const contradictoryTrace = traceApplicationFeatureEvidence({
application: contradictory,
...query,
});
expect(contradictoryTrace).toMatchObject({
ok: false,
error: {
_tag: "AnalysisInputError",
issues: expect.arrayContaining([
expect.objectContaining({
path: ["application", "normalized_result", "root_artifact_sha256"],
}),
]),
},
});
});
it("names the graph after a later file subject when the artifact subject is unknown", () => {
const { artifactEvidence, memberEvidence } = createManagedInteropEvidence();
const unboundArtifact = createEvidence(
unknownTarget("not-observed"),
artifactEvidence.provider,
{
operation: artifactEvidence.operation,
parameters: artifactEvidence.parameters,
result: artifactEvidence.normalized_result,
rawResult: null,
limitations: artifactEvidence.limitations,
},
);
const projected = projectManagedApplicationGraphEvidence({
managed_artifact: unboundArtifact,
managed_members: memberEvidence,
});
if (!projected.ok) throw projected.error;
expect(parseEvidence(projected.value).subject).toEqual(
memberEvidence.subject,
);
});
describe("managed application graph request", () => {
it("accepts any nonempty set of inline sources and rejects duplicates", () => {
const evidence = MANAGED_APPLICATION_GRAPH_EXAMPLE.managed_members;
expect(
managedApplicationGraphReferenceInputSchema.safeParse({
managed_members: evidence,
}).success,
).toBe(true);
expect(
managedApplicationGraphReferenceInputSchema.safeParse({}).success,
).toBe(false);
expect(
managedApplicationGraphReferenceInputSchema.safeParse({
managed_members: evidence,
managed_native_boundaries: evidence,
}).success,
).toBe(false);
});
});
describe("managed application graph coverage", () => {
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);
const mismatched = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
artifact: { ...members.artifact, sha256: "b".repeat(64) },
identity_scope: {
...members.identity_scope,
requires_artifact_sha256: "b".repeat(64),
},
},
});
const rejected = projectManagedApplicationGraphEvidence({
managed_members: mismatched,
});
if (rejected.ok) throw new Error("mismatched managed Evidence accepted");
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.each([
["managed_members", "memberEvidence"],
["managed_native_boundaries", "boundaryEvidence"],
] as const)(
"binds a module from %s to its supplying Evidence",
(inputKey, evidenceKey) => {
const source = createManagedInteropEvidence()[evidenceKey];
const projected = projectManagedApplicationGraphEvidence({
[inputKey]: source,
});
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: source.operation },
evidence_ids: [source.evidence_id],
},
});
expect(result.evidence_links).toEqual([source.evidence_id]);
expect(result.limitations).toContainEqual(
expect.stringMatching(
/artifact Evidence.*not supplied.*assembly identity.*absent/iu,
),
);
},
);
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([]);
});
it("projects a module whose metadata name is empty without a label", () => {
const bytes = buildManagedPeFixture({ ilBody: Buffer.from([0x06, 0x2a]) });
const control = inspectManagedMembersBytes(
bytes,
managedPeFixtureTarget(bytes, "/fixture/EmptyModuleName.dll"),
);
if (control.module === null) throw new Error("Expected a Module row");
// Point the Module row's Name at the string heap's empty entry.
bytes.writeUInt16LE(0, control.module.row_offset + 2);
const binary = managedPeFixtureTarget(
bytes,
"/fixture/EmptyModuleName.dll",
);
const members = inspectManagedMembersBytes(bytes, binary);
expect(members.module?.name).toBe("");
const projected = projectManagedApplicationGraphEvidence({
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: members,
rawResult: null,
limitations: members.limitations,
locations: [{ kind: "artifact-path", path: binary.path }],
}),
});
if (!projected.ok) throw projected.error;
const graph = parseJavaScriptApplicationGraph(
managedApplicationGraphResultSchema.parse(
parseEvidence(projected.value).normalized_result,
).graph,
);
expect(
graph.nodes.find(({ kind }) => kind === "managed-module")
?.observations[0],
).toMatchObject({ label: null, properties: { name: "" } });
expect(graph.nodes.map(({ kind }) => kind)).toEqual(
expect.arrayContaining([
"managed-module",
"managed-type",
"managed-method",
"managed-field",
]),
);
});
});
it("normalizes MVID casing for node identity while preserving observed values", () => {
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
const members = managedMemberInspectionSchema.parse(
parseEvidence(memberEvidence).normalized_result,
);
if (members.module === null)
throw new Error("Expected fixture module identity");
const uppercaseMvid = members.module.mvid?.toUpperCase();
if (uppercaseMvid === undefined || uppercaseMvid === null)
throw new Error("Expected fixture module MVID");
const uppercaseMembers = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
module: {
...members.module,
mvid: uppercaseMvid,
enc_id: null,
enc_base_id: null,
},
identity_scope: {
...members.identity_scope,
requires_mvid: uppercaseMvid,
},
},
rawResult: null,
limitations: members.limitations,
},
);
const baseline = projectManagedApplicationGraphEvidence({
managed_members: memberEvidence,
managed_native_boundaries: boundaryEvidence,
});
const uppercase = projectManagedApplicationGraphEvidence({
managed_members: uppercaseMembers,
managed_native_boundaries: boundaryEvidence,
});
if (!baseline.ok) throw baseline.error;
if (!uppercase.ok) throw uppercase.error;
const baselineResult = managedApplicationGraphResultSchema.parse(
parseEvidence(baseline.value).normalized_result,
);
const uppercaseResult = managedApplicationGraphResultSchema.parse(
parseEvidence(uppercase.value).normalized_result,
);
const baselineModule = parseJavaScriptApplicationGraph(
baselineResult.graph,
).nodes.find(({ kind }) => kind === "managed-module");
const uppercaseGraph = parseJavaScriptApplicationGraph(uppercaseResult.graph);
const uppercaseModule = uppercaseGraph.nodes.find(
({ kind }) => kind === "managed-module",
);
expect(uppercaseGraph.edges).toContainEqual(
expect.objectContaining({
relation: "imports",
source_node_id: uppercaseGraph.nodes.find(
({ kind }) => kind === "managed-method",
)?.node_id,
}),
);
expect(uppercaseModule?.node_id).toBe(baselineModule?.node_id);
expect(uppercaseModule?.observations[0]?.properties.mvid).toBe(uppercaseMvid);
});
describe("managed application graph module identity consistency", () => {
it("rejects contradictory known module MVIDs across same-artifact inputs", () => {
const { artifactEvidence, memberEvidence, boundaryEvidence } =
createManagedInteropEvidence();
const boundaries = managedNativeBoundaryInspectionSchema.parse(
parseEvidence(boundaryEvidence).normalized_result,
);
const conflictingMvid = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const conflictingBoundaries = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_native_boundaries",
parameters: {},
result: {
...boundaries,
module:
boundaries.module === null
? null
: { ...boundaries.module, mvid: conflictingMvid },
identity_scope: {
...boundaries.identity_scope,
requires_mvid: conflictingMvid,
},
},
rawResult: null,
limitations: boundaries.limitations,
},
);
const projected = projectManagedApplicationGraphEvidence({
managed_artifact: artifactEvidence,
managed_members: memberEvidence,
managed_native_boundaries: conflictingBoundaries,
});
if (projected.ok) throw new Error("conflicting MVIDs were accepted");
expect(JSON.stringify(projected.error)).toContain(
"conflicting known module MVIDs or token scopes",
);
});
it("rejects contradictory known module declaration fields", () => {
const { artifactEvidence, memberEvidence } = createManagedInteropEvidence();
const members = managedMemberInspectionSchema.parse(
parseEvidence(memberEvidence).normalized_result,
);
if (members.module === null)
throw new Error("Expected fixture module identity");
const conflictingMembers = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
module: {
...members.module,
generation: members.module.generation + 1,
},
},
rawResult: null,
limitations: members.limitations,
},
);
const projected = projectManagedApplicationGraphEvidence({
managed_artifact: artifactEvidence,
managed_members: conflictingMembers,
});
expect(projected.ok).toBe(false);
if (projected.ok)
throw new Error("conflicting module declaration was accepted");
expect(JSON.stringify(projected.error)).toContain(
"conflicting module generation values",
);
});
});
describe("managed application graph token scope", () => {
it("checks declared token artifact and MVID scopes without a subject digest", () => {
const { memberEvidence } = createManagedInteropEvidence();
const members = managedMemberInspectionSchema.parse(
parseEvidence(memberEvidence).normalized_result,
);
const wrongMvidValue = "ffffffff-ffff-ffff-ffff-ffffffffffff";
const wrongMvidEvidence = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
identity_scope: {
...members.identity_scope,
requires_mvid: wrongMvidValue,
},
},
rawResult: null,
limitations: members.limitations,
},
);
const wrongArtifactEvidence = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
identity_scope: {
...members.identity_scope,
requires_artifact_sha256: "b".repeat(64),
},
},
rawResult: null,
limitations: members.limitations,
},
);
const wrongMvidResult = projectManagedApplicationGraphEvidence({
managed_members: wrongMvidEvidence,
});
expect(wrongMvidResult.ok).toBe(false);
if (wrongMvidResult.ok)
throw new Error("mismatched token MVID was accepted");
expect(JSON.stringify(wrongMvidResult.error)).toContain("token scope MVID");
const wrongArtifact = projectManagedApplicationGraphEvidence({
managed_members: wrongArtifactEvidence,
});
expect(wrongArtifact.ok).toBe(false);
if (wrongArtifact.ok)
throw new Error("mismatched token artifact digest was accepted");
expect(JSON.stringify(wrongArtifact.error)).toContain(
"token scope SHA-256",
);
});
it.each([
"module identity absent",
"module MVID unknown",
"token scope MVID unknown",
] as const)("rejects inconsistent identity when %s", (variant) => {
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
const members = managedMemberInspectionSchema.parse(
parseEvidence(memberEvidence).normalized_result,
);
if (members.module === null || members.module.mvid === null)
throw new Error("Expected fixture module identity and MVID");
const module =
variant === "module identity absent"
? null
: variant === "module MVID unknown"
? { ...members.module, mvid: null }
: members.module;
const identity_scope =
variant === "token scope MVID unknown"
? { ...members.identity_scope, requires_mvid: null }
: members.identity_scope;
const inconsistentMembers = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: { ...members, module, identity_scope },
rawResult: null,
limitations: members.limitations,
},
);
const projected = projectManagedApplicationGraphEvidence({
managed_members: inconsistentMembers,
managed_native_boundaries: boundaryEvidence,
});
expect(projected.ok).toBe(false);
if (projected.ok)
throw new Error("An inconsistent token scope was accepted");
expect(JSON.stringify(projected.error)).toContain("token scope MVID");
});
});
describe("unknown managed application graph token scope", () => {
it("keeps absent MVID scope unknown and marks cross-source token joins partial", () => {
const { memberEvidence, boundaryEvidence } = createManagedInteropEvidence();
const members = managedMemberInspectionSchema.parse(
parseEvidence(memberEvidence).normalized_result,
);
const boundaries = managedNativeBoundaryInspectionSchema.parse(
parseEvidence(boundaryEvidence).normalized_result,
);
const unknownMembers = createEvidence(
unknownTarget("not-observed"),
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
module:
members.module === null
? null
: {
...members.module,
mvid: null,
enc_id: null,
enc_base_id: null,
},
identity_scope: { ...members.identity_scope, requires_mvid: null },
},
rawResult: null,
limitations: members.limitations,
},
);
const projected = projectManagedApplicationGraphEvidence({
managed_members: unknownMembers,
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.modules).toBe(1);
expect(result.coverage.status).toBe("partial");
expect(result.limitations).toContainEqual(
expect.stringMatching(
/boundary tokens.*not linked.*member nodes.*MVID.*not established/iu,
),
);
const moduleObservation = graph.nodes.find(
({ kind }) => kind === "managed-module",
)?.observations[0];
expect(moduleObservation?.properties.mvid).toBe(boundaries.module?.mvid);
expect(moduleObservation?.evidence.extractor.operation).toBe(
"inspect_managed_native_boundaries",
);
const method = graph.nodes.find(
({ kind, observations }) =>
kind === "managed-method" &&
observations[0]?.label === "Fixture.Program.Main",
);
expect(method).toBeDefined();
const importEdge = graph.edges.find(
({ relation, target_node_id }) =>
relation === "imports" &&
graph.nodes.find(({ node_id }) => node_id === target_node_id)?.kind ===
"managed-pinvoke-import",
);
expect(importEdge).toBeDefined();
expect(
graph.nodes.find(({ node_id }) => node_id === importEdge?.source_node_id)
?.kind,
).toBe("artifact");
});
});
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");
const members = inspectManagedMembersBytes(bytes, binary);
const parserPartialMembers = {
...members,
coverage: {
state: "partial" as const,
issues: [
{
code: "invalid-blob" as const,
scope: "metadata.#Blob",
offset: 0x0a00,
detail: "Blob content leaves #Blob",
},
],
},
};
const parserPartialEvidence = createEvidence(
binary,
MANAGED_STATIC_PROVIDER,
{
operation: "inspect_managed_members",
parameters: {},
result: parserPartialMembers,
rawResult: null,
limitations: parserPartialMembers.limitations,
},
);
const parserPartialProjection = projectManagedApplicationGraphEvidence({
managed_members: parserPartialEvidence,
});
if (!parserPartialProjection.ok)
throw new Error("partial projection failed");
const parserPartialResult = managedApplicationGraphResultSchema.parse(
parseEvidence(parserPartialProjection.value).normalized_result,
);
expect(parserPartialResult.coverage).toMatchObject({
status: "partial",
});
expect(parserPartialResult.graph.coverage).toEqual({
status: "partial",
truncated: false,
omitted_count: null,
limits: [],
});
const complete = memberGraph(
buildManagedPeFixture({ ilBody: tinyIl([0x2a]) }),
);
for (const [graph, status, evidenceId] of [
[parserPartialResult.graph, "partial", parserPartialEvidence.evidence_id],
[complete.graph, "complete", complete.memberEvidence.evidence_id],
] as const) {
const expected = {
status,
truncated: false,
omitted_count: status === "partial" ? null : 0,
limits: [],
};
for (const kind of [
"managed-type",
"managed-method",
"managed-field",
] as const) {
const nodes = graph.nodes.filter((node) => node.kind === kind);
expect(nodes.length).toBeGreaterThan(0);
for (const node of nodes) {
expect(node.observations.length).toBeGreaterThan(0);
for (const observation of node.observations) {
expect(observation.evidence.coverage).toEqual(expected);
expect(observation.evidence.evidence_ids).toContain(evidenceId);
}
}
const ids = new Set(nodes.map(({ node_id }) => node_id));
const contains = graph.edges.filter(
(edge) =>
edge.relation === "contains" && ids.has(edge.target_node_id),
);
expect(contains).toHaveLength(nodes.length);
for (const edge of contains) {
expect(edge.evidence.coverage).toEqual(expected);
expect(edge.evidence.evidence_ids).toContain(evidenceId);
}
}
}
});
it("reports an unavailable source as unknown, not partial", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
const members = inspectManagedMembersBytes(bytes, binary);
const evidence = (state: "partial" | "unavailable") =>
createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: { ...members, coverage: { state, issues: [] } },
rawResult: null,
limitations: members.limitations,
});
const project = (state: "partial" | "unavailable") => {
const projection = projectManagedApplicationGraphEvidence({
managed_members: evidence(state),
});
if (!projection.ok) throw new Error("projection failed");
return managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
};
const unavailable = project("unavailable");
expect(unavailable.coverage).toEqual({ status: "unknown" });
expect(unavailable.graph.coverage).toEqual({
status: "unknown",
truncated: false,
omitted_count: null,
limits: [],
});
expect(unavailable.limitations).toContainEqual(
expect.stringMatching(/managed Evidence record is unavailable/iu),
);
expect(unavailable.limitations).not.toContainEqual(
expect.stringMatching(/partial coverage/iu),
);
// A known gap alone stays partial.
expect(project("partial").coverage).toEqual({ status: "partial" });
});
});
describe("managed application graph type selection", () => {
const project = (
result: Parameters<typeof createEvidence>[2]["result"],
bytes = buildManagedPeFixture(),
) => {
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
const projection = projectManagedApplicationGraphEvidence({
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result,
rawResult: null,
limitations: [],
}),
});
if (!projection.ok) throw new Error("projection failed");
return managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
};
it("marks a graph from selected member inspection partial", () => {
const bytes = buildManagedPeFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedInterop.exe");
const selected = inspectManagedMembersBytes(bytes, binary, {
typeNames: ["Fixture.Program"],
});
expect(selected.coverage.state).toBe("complete");
const result = project(selected);
expect(result.coverage).toMatchObject({ status: "partial" });
expect(result.limitations).toContainEqual(
expect.stringMatching(
/Fixture\.Program.*type, method, and field.*omitted, not absent/iu,
),
);
expect(result.limitations).not.toContainEqual(
expect.stringMatching(/managed Evidence.*partial coverage/iu),
);
});
});
describe("managed call and field-access relations", () => {
// Both fixture methods share this body: 0x06000001 and 0x06000002.
const relationsFixture = () =>
buildManagedPeFixture({
methodRowCount: 2,
ilBody: tinyIl([
...[0x28, 0x02, 0, 0, 0x06], // call 0x06000002
...[0x6f, 0x02, 0, 0, 0x06], // callvirt 0x06000002
...[0x28, 0x01, 0, 0, 0x06], // call 0x06000001
...[0xfe, 0x06, 0x02, 0, 0, 0x06], // ldftn 0x06000002
...[0x28, 0x01, 0, 0, 0x0a], // call MemberRef 0x0a000001
0x02,
...[0x7b, 0x01, 0, 0, 0x04], // ldfld 0x04000001
0x02,
0x02,
...[0x7d, 0x01, 0, 0, 0x04], // stfld 0x04000001
0x02,
...[0x7c, 0x01, 0, 0, 0x04], // ldflda 0x04000001
0x26,
0x2a,
]),
});
it("links in-module calls and field accesses once per source and target", () => {
const { members, memberEvidence, result, graph } =
memberGraph(relationsFixture());
expect(members.call_edges).toHaveLength(10);
expect(members.field_accesses).toHaveLength(6);
const memberTokens = new Map(
graph.nodes.map((node) => [
node.node_id,
node.observations[0]?.properties.token,
]),
);
const relation = (name: string) =>
graph.edges
.filter((edge) => edge.relation === name)
.map((edge) => ({
source: memberTokens.get(edge.source_node_id),
target: memberTokens.get(edge.target_node_id),
properties: edge.properties,
evidence_ids: edge.evidence.evidence_ids,
}));
// These distinct metadata methods intentionally have the same display name.
expect(
members.methods.map(
({ declaring_type, name }) => `${declaring_type}.${name}`,
),
).toEqual(["Fixture.Program.Main", "Fixture.Program.Main"]);
const [first, second] = members.methods.map(({ token }) => token);
expect(first).not.toBe(second);
const field = members.fields[0]?.token;
const evidence_ids = [memberEvidence.evidence_id];
const calls = relation("calls");
expect(calls).toHaveLength(2);
expect(calls).toEqual(
expect.arrayContaining([
{
source: first,
target: second,
properties: {
kind: "managed-call",
site_count: 2,
opcodes: { call: 1, callvirt: 1 },
},
evidence_ids,
},
{
source: second,
target: first,
properties: {
kind: "managed-call",
site_count: 1,
opcodes: { call: 1 },
},
evidence_ids,
},
]),
);
const accesses = relation("accesses");
expect(accesses).toHaveLength(2);
for (const source of [first, second])
expect(accesses).toContainEqual({
source,
target: field,
properties: {
kind: "managed-field-access",
access: ["address", "read", "write"],
site_count: 3,
opcodes: { ldfld: 1, ldflda: 1, stfld: 1 },
},
evidence_ids,
});
expect(result.summary).toMatchObject({ calls: 2, field_accesses: 2 });
// Two MemberRef calls, two ldftn loads and three self-calls stay counted.
expect(result.coverage).toEqual({ status: "partial" });
expect(result.graph.coverage).toEqual({
status: "partial",
truncated: false,
omitted_count: 7,
limits: [],
});
expect(result.limitations).toContainEqual(
expect.stringMatching(
/inspected module.*omitted: 2 member-reference, 2 function-pointer.*3 recursive self-call/iu,
),
);
});
it("refuses graph Evidence without relation counters", () => {
const { result } = memberGraph(relationsFixture());
const { calls: _calls, ...summary } = result.summary;
expect(
managedApplicationGraphResultSchema.safeParse({ ...result, summary })
.error?.issues[0]?.path,
).toEqual(["summary", "calls"]);
});
it("counts relations whose endpoints a type selection omitted", () => {
const bytes = relationsFixture();
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
const members = inspectManagedMembersBytes(bytes, binary);
const projection = projectManagedApplicationGraphEvidence({
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
// Drop the second method, as a narrower selection would.
result: { ...members, methods: members.methods.slice(0, 1) },
rawResult: null,
limitations: [],
}),
});
if (!projection.ok) throw new Error(JSON.stringify(projection.error));
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
expect(result.summary).toMatchObject({ calls: 0, field_accesses: 1 });
expect(
result.limitations.find((text) => text.startsWith("Calls edges")),
).toContain("missing-node");
expect(
result.limitations.find((text) => text.startsWith("Accesses edges")),
).toContain("3 missing-node");
});
});
it("keeps unfamiliar field opcodes unknown in managed graph Evidence", () => {
const bytes = buildManagedPeFixture({
ilBody: tinyIl([0x02, 0x7b, 0x01, 0, 0, 0x04, 0x26, 0x2a]),
});
const binary = managedPeFixtureTarget(bytes, "/fixture/ManagedCalls.exe");
const members = inspectManagedMembersBytes(bytes, binary);
const projection = projectManagedApplicationGraphEvidence({
managed_members: createEvidence(binary, MANAGED_STATIC_PROVIDER, {
operation: "inspect_managed_members",
parameters: {},
result: {
...members,
field_accesses: [
{ ...members.field_accesses[0]!, opcode: "constructor" },
],
},
limitations: [],
}),
});
if (!projection.ok) throw projection.error;
const result = managedApplicationGraphResultSchema.parse(
parseEvidence(projection.value).normalized_result,
);
expect(
result.graph.edges.find((edge) => edge.relation === "accesses")?.properties,
).toMatchObject({ access: ["unknown"], opcodes: { constructor: 1 } });
});