Select exact declaring types before managed signatures and CIL bodies are decoded, preserving caller choices in CLI/MCP Evidence. Keep unmatched names and selected coverage explicit; comparisons and graphs retain unknowns outside their observed scope. Reuse existing metadata ownership/accounting and advertise unique selections through the actual SDK Schema. Validated with 229 focused tests across 21 files, 11 managed conformance controls, fullstatic/docs checks and real Linux CLI and fresh MCP inspection of two unchanged DLLs. All 118 selected CIL hashes matched original PE-mapped bytes; result/caller-argument parity and unmatched controls passed. Full artifact/native-boundary metadata is complete; whole-assembly members retain the unchanged 64 MiB resource constraint. No target code executes. Refs #1878 Co-authored-by: skared <190618464+skoodster@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com> Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
338 lines
9.4 KiB
TypeScript
338 lines
9.4 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import canonicalize from "canonicalize";
|
|
|
|
import {
|
|
managedMemberInspectionSchema,
|
|
type ManagedMemberInspection,
|
|
} from "./managedArtifact.js";
|
|
import { parseManagedInspectionEvidence } from "./managedInspectionEvidence.js";
|
|
import type {
|
|
ManagedMemberComparisonResult,
|
|
ManagedMemberComparisonSide,
|
|
} from "./managedMemberComparison.js";
|
|
import type { JsonValue } from "../jsonValue.js";
|
|
|
|
export const sha256 = (value: JsonValue): string => {
|
|
const serialized = canonicalize(value);
|
|
if (serialized === undefined)
|
|
throw new TypeError("Managed comparison canonicalization failed");
|
|
return createHash("sha256").update(serialized).digest("hex");
|
|
};
|
|
|
|
type Method = ManagedMemberInspection["methods"][number];
|
|
type Field = ManagedMemberInspection["fields"][number];
|
|
type MatchBasis =
|
|
ManagedMemberComparisonResult["methods"][number]["match"]["basis"];
|
|
type ConcreteMatchBasis = Exclude<MatchBasis, "none">;
|
|
|
|
interface Keyed<Item> {
|
|
readonly item: Item;
|
|
readonly exactKey: string | null;
|
|
readonly signatureKey: string | null;
|
|
readonly structuralKey: string | null;
|
|
}
|
|
|
|
interface MatchedPair<Item> {
|
|
readonly left: Keyed<Item>;
|
|
readonly right: Keyed<Item>;
|
|
readonly basis: ConcreteMatchBasis;
|
|
readonly confidence: "exact" | "high";
|
|
}
|
|
|
|
interface Ambiguous<Item> {
|
|
readonly left: readonly Keyed<Item>[];
|
|
readonly right: readonly Keyed<Item>[];
|
|
readonly basis: ConcreteMatchBasis;
|
|
}
|
|
|
|
const keyMethod = (item: Method): Keyed<Method> => ({
|
|
item,
|
|
exactKey:
|
|
item.body.status === "present" && item.body.normalized_il_sha256 !== null
|
|
? sha256([
|
|
"method-exact",
|
|
item.signature.raw_sha256,
|
|
item.body.normalized_il_sha256,
|
|
])
|
|
: null,
|
|
// The raw blob is the exact signature; decoding only feeds shape keys.
|
|
signatureKey: sha256([
|
|
"method-signature",
|
|
item.declaring_type,
|
|
item.name,
|
|
item.signature.raw_sha256,
|
|
]),
|
|
structuralKey:
|
|
item.signature.parse_status === "decoded" && item.body.status === "present"
|
|
? sha256([
|
|
"method-structural",
|
|
item.signature.kind,
|
|
item.signature.calling_convention,
|
|
item.signature.generic_parameter_count,
|
|
item.signature.parameter_count,
|
|
item.signature.return_type,
|
|
item.signature.parameter_types,
|
|
item.body.status,
|
|
item.body.header_format,
|
|
item.body.il_size,
|
|
item.body.max_stack,
|
|
item.body.init_locals,
|
|
item.body.opcode_counts,
|
|
item.body.anchors.map(({ opcode, operand_kind }) => [
|
|
opcode,
|
|
operand_kind,
|
|
]),
|
|
item.body.exception_regions.map((region) => [
|
|
region.flags,
|
|
region.try_length,
|
|
region.handler_length,
|
|
region.class_token === null ? null : "type-token",
|
|
region.filter_offset === null ? null : "filter",
|
|
]),
|
|
])
|
|
: null,
|
|
});
|
|
|
|
const keyField = (item: Field): Keyed<Field> => ({
|
|
item,
|
|
exactKey:
|
|
item.signature.parse_status === "decoded"
|
|
? sha256(["field-exact", item.signature.raw_sha256])
|
|
: null,
|
|
// Only an undecoded field reaches this round: a decoded field with the
|
|
// same raw signature already met its counterpart in the exact round.
|
|
signatureKey: sha256([
|
|
"field-signature",
|
|
item.declaring_type,
|
|
item.name,
|
|
item.signature.raw_sha256,
|
|
]),
|
|
structuralKey: sha256([
|
|
"field-structural",
|
|
item.signature.kind,
|
|
item.signature.field_type,
|
|
item.flags,
|
|
]),
|
|
});
|
|
|
|
const matchMethods = (
|
|
left: readonly Keyed<Method>[],
|
|
right: readonly Keyed<Method>[],
|
|
) =>
|
|
matchByKeys({
|
|
left,
|
|
right,
|
|
exactBasis: "exact-il-signature",
|
|
fallbackBases: [
|
|
{
|
|
basis: "exact-signature",
|
|
key: ({ signatureKey }) => signatureKey,
|
|
},
|
|
{
|
|
basis: "structural-method-shape",
|
|
key: ({ structuralKey }) => structuralKey,
|
|
},
|
|
],
|
|
});
|
|
|
|
const matchFields = (
|
|
left: readonly Keyed<Field>[],
|
|
right: readonly Keyed<Field>[],
|
|
) =>
|
|
matchByKeys({
|
|
left,
|
|
right,
|
|
exactBasis: "field-signature",
|
|
fallbackBases: [
|
|
{
|
|
basis: "exact-signature",
|
|
key: ({ signatureKey }) => signatureKey,
|
|
},
|
|
],
|
|
});
|
|
|
|
export type ManagedMethodMatches = ReturnType<typeof matchMethods>;
|
|
export type ManagedFieldMatches = ReturnType<typeof matchFields>;
|
|
|
|
interface MatchByKeysInput<Item> {
|
|
readonly left: readonly Keyed<Item>[];
|
|
readonly right: readonly Keyed<Item>[];
|
|
readonly exactBasis: ConcreteMatchBasis;
|
|
readonly fallbackBases: readonly {
|
|
readonly basis: ConcreteMatchBasis;
|
|
readonly key: (item: Keyed<Item>) => string | null;
|
|
}[];
|
|
}
|
|
|
|
const matchByKeys = <Item>({
|
|
left,
|
|
right,
|
|
exactBasis,
|
|
fallbackBases,
|
|
}: MatchByKeysInput<Item>): {
|
|
readonly pairs: readonly MatchedPair<Item>[];
|
|
readonly ambiguous: readonly Ambiguous<Item>[];
|
|
readonly leftOnly: readonly Keyed<Item>[];
|
|
readonly rightOnly: readonly Keyed<Item>[];
|
|
} => {
|
|
const usedLeft = new Set<Keyed<Item>>();
|
|
const usedRight = new Set<Keyed<Item>>();
|
|
const pairs: MatchedPair<Item>[] = [];
|
|
const ambiguous: Ambiguous<Item>[] = [];
|
|
const rounds: readonly {
|
|
readonly basis: ConcreteMatchBasis;
|
|
readonly key: (item: Keyed<Item>) => string | null;
|
|
}[] = [
|
|
{ basis: exactBasis, key: ({ exactKey }) => exactKey },
|
|
...fallbackBases,
|
|
];
|
|
for (const round of rounds) {
|
|
const leftGroups = groupBy(
|
|
left.filter((item) => !usedLeft.has(item)),
|
|
round.key,
|
|
);
|
|
const rightGroups = groupBy(
|
|
right.filter((item) => !usedRight.has(item)),
|
|
round.key,
|
|
);
|
|
for (const [key, leftItems] of leftGroups) {
|
|
const rightItems = rightGroups.get(key);
|
|
if (rightItems === undefined) continue;
|
|
if (leftItems.length === 1 && rightItems.length === 1) {
|
|
const [leftItem] = leftItems;
|
|
const [rightItem] = rightItems;
|
|
if (leftItem === undefined || rightItem === undefined) continue;
|
|
usedLeft.add(leftItem);
|
|
usedRight.add(rightItem);
|
|
pairs.push({
|
|
left: leftItem,
|
|
right: rightItem,
|
|
basis: round.basis,
|
|
confidence:
|
|
round.basis === "exact-il-signature" ||
|
|
round.basis === "exact-signature" ||
|
|
round.basis === "field-signature"
|
|
? "exact"
|
|
: "high",
|
|
});
|
|
} else {
|
|
for (const item of leftItems) usedLeft.add(item);
|
|
for (const item of rightItems) usedRight.add(item);
|
|
ambiguous.push({
|
|
left: leftItems,
|
|
right: rightItems,
|
|
basis: round.basis,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
pairs,
|
|
ambiguous,
|
|
leftOnly: left.filter((item) => !usedLeft.has(item)),
|
|
rightOnly: right.filter((item) => !usedRight.has(item)),
|
|
};
|
|
};
|
|
|
|
const groupBy = <Item>(
|
|
items: readonly Item[],
|
|
keyOf: (item: Item) => string | null,
|
|
): Map<string, readonly Item[]> => {
|
|
const grouped = new Map<string, Item[]>();
|
|
for (const item of items) {
|
|
const key = keyOf(item);
|
|
if (key === null) continue;
|
|
const bucket = grouped.get(key) ?? [];
|
|
bucket.push(item);
|
|
grouped.set(key, bucket);
|
|
}
|
|
return grouped;
|
|
};
|
|
|
|
/** Whether a side's inventory observed members of this declaring type. */
|
|
const observedBy =
|
|
(side: ManagedMemberComparisonSide) =>
|
|
({ declaring_type }: { readonly declaring_type: string | null }): boolean =>
|
|
side.result.selection === null ||
|
|
(declaring_type !== null &&
|
|
side.result.selection.type_names.includes(declaring_type));
|
|
|
|
/**
|
|
* Pair only members whose declaring type both sides observed. A member outside
|
|
* the other side's selection stays one-sided, so a type selection never yields
|
|
* a remap to a member of a type the other side did not inspect.
|
|
*/
|
|
const matchObserved = <Item extends { readonly declaring_type: string | null }>(
|
|
left: readonly Item[],
|
|
right: readonly Item[],
|
|
sides: {
|
|
readonly inLeft: (item: Item) => boolean;
|
|
readonly inRight: (item: Item) => boolean;
|
|
},
|
|
match: (
|
|
left: readonly Keyed<Item>[],
|
|
right: readonly Keyed<Item>[],
|
|
) => ReturnType<typeof matchByKeys<Item>>,
|
|
key: (item: Item) => Keyed<Item>,
|
|
): ReturnType<typeof matchByKeys<Item>> => {
|
|
const matched = match(
|
|
left.filter(sides.inRight).map(key),
|
|
right.filter(sides.inLeft).map(key),
|
|
);
|
|
return {
|
|
...matched,
|
|
leftOnly: [
|
|
...matched.leftOnly,
|
|
...left.filter((item) => !sides.inRight(item)).map(key),
|
|
],
|
|
rightOnly: [
|
|
...matched.rightOnly,
|
|
...right.filter((item) => !sides.inLeft(item)).map(key),
|
|
],
|
|
};
|
|
};
|
|
|
|
export const keyMembers = (
|
|
left: ManagedMemberComparisonSide,
|
|
right: ManagedMemberComparisonSide,
|
|
): {
|
|
readonly methodMatches: ReturnType<typeof matchMethods>;
|
|
readonly fieldMatches: ReturnType<typeof matchFields>;
|
|
} => {
|
|
const sides = { inLeft: observedBy(left), inRight: observedBy(right) };
|
|
return {
|
|
methodMatches: matchObserved(
|
|
left.result.methods,
|
|
right.result.methods,
|
|
sides,
|
|
matchMethods,
|
|
keyMethod,
|
|
),
|
|
fieldMatches: matchObserved(
|
|
left.result.fields,
|
|
right.result.fields,
|
|
sides,
|
|
matchFields,
|
|
keyField,
|
|
),
|
|
};
|
|
};
|
|
|
|
export const parseManagedMemberEvidence = (
|
|
evidence: unknown,
|
|
): {
|
|
readonly evidenceId: string;
|
|
readonly result: ManagedMemberInspection;
|
|
} => {
|
|
const parsed = parseManagedInspectionEvidence(
|
|
evidence,
|
|
"inspect_managed_members",
|
|
managedMemberInspectionSchema,
|
|
);
|
|
return {
|
|
evidenceId: parsed.evidence.evidence_id,
|
|
result: parsed.result,
|
|
};
|
|
};
|