Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

refactor(managed): reuse canonical JSON digest owner

morluto87c8086fc1c9
diff --git a/src/domain/managed/managedApplicationGraph.ts b/src/domain/managed/managedApplicationGraph.ts
index 4ae40acd9..9ee2c40d9 100644
--- a/src/domain/managed/managedApplicationGraph.ts
+++ b/src/domain/managed/managedApplicationGraph.ts
@@ -1,6 +1,3 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, type Evidence } from "../evidence.js";
@@ -20,7 +17,7 @@ import {
type ManagedMemberInspection,
type ManagedNativeBoundaryInspection,
} from "./managedArtifact.js";
-import type { JsonValue } from "../jsonValue.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import {
assessManagedGraphOmissions,
managedGraphEvidenceCoverage,
@@ -120,13 +117,6 @@ export interface GraphBuildState {
readonly fieldNodes: Map<string, ApplicationNode>;
}
-const sha256 = (value: JsonValue): string => {
- const serialized = canonicalize(value);
- if (serialized === undefined)
- throw new TypeError("Managed application graph canonicalization failed");
- return createHash("sha256").update(serialized).digest("hex");
-};
-
/** Project authenticated managed Evidence records into the application graph. */
export const projectManagedApplicationGraph = (
input: ProjectManagedApplicationGraphInput,
@@ -194,7 +184,10 @@ export const projectManagedApplicationGraph = (
};
return managedApplicationGraphResultSchema.parse({
...withoutId,
- projection_id: `magp_${sha256(withoutId)}`,
+ projection_id: `magp_${digestCanonicalValue(
+ withoutId,
+ "Managed application graph",
+ )}`,
});
};
diff --git a/src/domain/managed/managedMemberComparison.ts b/src/domain/managed/managedMemberComparison.ts
index be268d7c6..c9545dfd3 100644
--- a/src/domain/managed/managedMemberComparison.ts
+++ b/src/domain/managed/managedMemberComparison.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import { emptyArraySchema } from "../emptyArraySchema.js";
import { evidenceSchema } from "../evidence.js";
import {
@@ -18,7 +19,7 @@ import {
buildFieldItems,
buildMethodItems,
} from "./managedMemberComparisonItems.js";
-import { keyMembers, sha256 } from "./managedMemberComparisonMatch.js";
+import { keyMembers } from "./managedMemberComparisonMatch.js";
import { hasSignatureResolution } from "./managedMemberSignatureIdentity.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
@@ -247,12 +248,15 @@ export const compareManagedMembers = (
});
const limitations = comparisonLimitations(left.result, right.result);
const result = {
- comparison_id: `mmc_${sha256({
- left: left.evidenceId,
- right: right.evidenceId,
- methods: [...methodItems],
- fields: [...fieldItems],
- })}`,
+ comparison_id: `mmc_${digestCanonicalValue(
+ {
+ left: left.evidenceId,
+ right: right.evidenceId,
+ methods: [...methodItems],
+ fields: [...fieldItems],
+ },
+ "Managed comparison",
+ )}`,
algorithm: {
name: "rea-managed-member-comparison" as const,
token_identity: "build-local" as const,
diff --git a/src/domain/managed/managedMemberComparisonItems.ts b/src/domain/managed/managedMemberComparisonItems.ts
index 1a1d1294b..ea44add11 100644
--- a/src/domain/managed/managedMemberComparisonItems.ts
+++ b/src/domain/managed/managedMemberComparisonItems.ts
@@ -1,11 +1,11 @@
import canonicalize from "canonicalize";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import type { ManagedMemberInspection } from "./managedArtifact.js";
import type { ManagedMemberComparisonResult } from "./managedMemberComparison.js";
-import {
- type ManagedFieldMatches,
- type ManagedMethodMatches,
- sha256,
+import type {
+ ManagedFieldMatches,
+ ManagedMethodMatches,
} from "./managedMemberComparisonMatch.js";
import { signatureIdentity } from "./managedMemberSignatureIdentity.js";
@@ -180,7 +180,7 @@ const methodOnlyItem = (
inventory: "method",
});
return {
- item_id: `mmc_method_${sha256({ token: member.left.token, side: "left" })}`,
+ item_id: `mmc_method_${digestCanonicalValue({ token: member.left.token, side: "left" }, "Managed comparison")}`,
status: limitations.length === 0 ? "removed" : "unknown",
left: methodIdentity(member.left),
right: null,
@@ -203,7 +203,7 @@ const methodOnlyItem = (
inventory: "method",
});
return {
- item_id: `mmc_method_${sha256({ token: member.right.token, side: "right" })}`,
+ item_id: `mmc_method_${digestCanonicalValue({ token: member.right.token, side: "right" }, "Managed comparison")}`,
status: limitations.length === 0 ? "added" : "unknown",
left: null,
right: methodIdentity(member.right),
@@ -377,7 +377,7 @@ const fieldOnlyItem = (
inventory: "field",
});
return {
- item_id: `mmc_field_${sha256({ token: member.left.token, side: "left" })}`,
+ item_id: `mmc_field_${digestCanonicalValue({ token: member.left.token, side: "left" }, "Managed comparison")}`,
status: limitations.length === 0 ? "removed" : "unknown",
left: fieldIdentity(member.left),
right: null,
@@ -399,7 +399,7 @@ const fieldOnlyItem = (
inventory: "field",
});
return {
- item_id: `mmc_field_${sha256({ token: member.right.token, side: "right" })}`,
+ item_id: `mmc_field_${digestCanonicalValue({ token: member.right.token, side: "right" }, "Managed comparison")}`,
status: limitations.length === 0 ? "added" : "unknown",
left: null,
right: fieldIdentity(member.right),
@@ -448,11 +448,14 @@ export const buildMethodItems = (
: observedDimensions;
const observedChange = observedDimensions.length > 0;
items.push({
- item_id: `mmc_method_${sha256({
- left: pair.left.item.token,
- right: pair.right.item.token,
- basis: pair.basis,
- })}`,
+ item_id: `mmc_method_${digestCanonicalValue(
+ {
+ left: pair.left.item.token,
+ right: pair.right.item.token,
+ basis: pair.basis,
+ },
+ "Managed comparison",
+ )}`,
status: observedChange
? "changed"
: bodyUnknown || signatureLimitations.length > 0
@@ -481,11 +484,14 @@ export const buildMethodItems = (
}
for (const ambiguous of matches.ambiguous) {
items.push({
- item_id: `mmc_method_${sha256({
- left: ambiguous.left.map(({ item }) => item.token),
- right: ambiguous.right.map(({ item }) => item.token),
- basis: ambiguous.basis,
- })}`,
+ item_id: `mmc_method_${digestCanonicalValue(
+ {
+ left: ambiguous.left.map(({ item }) => item.token),
+ right: ambiguous.right.map(({ item }) => item.token),
+ basis: ambiguous.basis,
+ },
+ "Managed comparison",
+ )}`,
status: "unknown",
left: null,
right: null,
@@ -551,10 +557,13 @@ export const buildFieldItems = (
signatureIdentity(pair.right.item.signature) ||
pair.left.item.flags !== pair.right.item.flags;
items.push({
- item_id: `mmc_field_${sha256({
- left: pair.left.item.token,
- right: pair.right.item.token,
- })}`,
+ item_id: `mmc_field_${digestCanonicalValue(
+ {
+ left: pair.left.item.token,
+ right: pair.right.item.token,
+ },
+ "Managed comparison",
+ )}`,
status: changed
? "changed"
: signatureLimitations.length > 0
@@ -575,10 +584,13 @@ export const buildFieldItems = (
}
for (const ambiguous of matches.ambiguous)
items.push({
- item_id: `mmc_field_${sha256({
- left: ambiguous.left.map(({ item }) => item.token),
- right: ambiguous.right.map(({ item }) => item.token),
- })}`,
+ item_id: `mmc_field_${digestCanonicalValue(
+ {
+ left: ambiguous.left.map(({ item }) => item.token),
+ right: ambiguous.right.map(({ item }) => item.token),
+ },
+ "Managed comparison",
+ )}`,
status: "unknown",
left: null,
right: null,
diff --git a/src/domain/managed/managedMemberComparisonMatch.ts b/src/domain/managed/managedMemberComparisonMatch.ts
index aa932500f..1ad3d5e99 100644
--- a/src/domain/managed/managedMemberComparisonMatch.ts
+++ b/src/domain/managed/managedMemberComparisonMatch.ts
@@ -1,7 +1,4 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
-
+import { digestCanonicalValue } from "../canonicalDigest.js";
import {
managedMemberInspectionSchema,
type ManagedMemberInspection,
@@ -15,15 +12,6 @@ import {
comparableSides,
signatureIdentity,
} from "./managedMemberSignatureIdentity.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];
@@ -69,49 +57,58 @@ const keyMethod = (item: Method, declared: DeclaredNameOf): Keyed<Method> => ({
item.body.status === "present" &&
item.body.il_sha256 !== null &&
item.body.normalized_il_sha256 !== null
- ? sha256([
- "method-exact",
- signatureIdentity(item.signature),
- item.body.il_sha256,
- item.body.normalized_il_sha256,
- ])
+ ? digestCanonicalValue(
+ [
+ "method-exact",
+ signatureIdentity(item.signature),
+ item.body.il_sha256,
+ item.body.normalized_il_sha256,
+ ],
+ "Managed comparison",
+ )
: null,
// The blob, with type operands named, is the exact signature; decoding
// only gates the shape keys.
signatureKey:
declared(item) === null
? null
- : sha256([
- "method-signature",
- declared(item),
- item.name,
- signatureIdentity(item.signature),
- ]),
+ : digestCanonicalValue(
+ [
+ "method-signature",
+ declared(item),
+ item.name,
+ signatureIdentity(item.signature),
+ ],
+ "Managed comparison",
+ ),
structuralKey:
item.signature.parse_status === "decoded" && item.body.status === "present"
- ? sha256([
- "method-structural",
- // Decoded type strings carry build-local rows; the identity names them.
- signatureIdentity(item.signature),
- 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, operand }) => [
- opcode,
- operand_kind,
- operand_kind === "constant" ? operand : null,
- ]),
- 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",
- ]),
- ])
+ ? digestCanonicalValue(
+ [
+ "method-structural",
+ // Decoded type strings carry build-local rows; the identity names them.
+ signatureIdentity(item.signature),
+ 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, operand }) => [
+ opcode,
+ operand_kind,
+ operand_kind === "constant" ? operand : null,
+ ]),
+ 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",
+ ]),
+ ],
+ "Managed comparison",
+ )
: null,
});
@@ -122,25 +119,34 @@ const keyField = (item: Field, declared: DeclaredNameOf): Keyed<Field> => ({
signatureId: signatureIdentity(item.signature),
exactKey:
item.signature.parse_status === "decoded"
- ? sha256(["field-exact", signatureIdentity(item.signature)])
+ ? digestCanonicalValue(
+ ["field-exact", signatureIdentity(item.signature)],
+ "Managed comparison",
+ )
: null,
// Only an undecoded field reaches this round: a decoded field with the
// same signature already met its counterpart in the exact round.
signatureKey:
declared(item) === null
? null
- : sha256([
- "field-signature",
- declared(item),
- item.name,
- signatureIdentity(item.signature),
- ]),
- structuralKey: sha256([
- "field-structural",
- item.signature.kind,
- item.signature.field_type,
- item.flags,
- ]),
+ : digestCanonicalValue(
+ [
+ "field-signature",
+ declared(item),
+ item.name,
+ signatureIdentity(item.signature),
+ ],
+ "Managed comparison",
+ ),
+ structuralKey: digestCanonicalValue(
+ [
+ "field-structural",
+ item.signature.kind,
+ item.signature.field_type,
+ item.flags,
+ ],
+ "Managed comparison",
+ ),
});
const matchMethods = (
@@ -237,7 +243,8 @@ export const identityOf = ({
}: {
readonly declaring_type: DeclaredName;
readonly name: string;
-}): string => sha256([declaring_type, name]);
+}): string =>
+ digestCanonicalValue([declaring_type, name], "Managed comparison");
/** Narrow a key to members of one declared type and name. */
const declaredAs =
@@ -246,7 +253,7 @@ const declaredAs =
const value = key(item);
return value === null || item.identityUnavailable
? null
- : sha256([item.identity, value]);
+ : digestCanonicalValue([item.identity, value], "Managed comparison");
};
const matchByKeys = <Item extends Declared>({
diff --git a/src/domain/managed/managedNativeVerification.ts b/src/domain/managed/managedNativeVerification.ts
index 11924b21e..85c968b13 100644
--- a/src/domain/managed/managedNativeVerification.ts
+++ b/src/domain/managed/managedNativeVerification.ts
@@ -1,14 +1,10 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
-
+import { digestCanonicalValue } from "../canonicalDigest.js";
import type { Evidence } from "../evidence.js";
import {
managedNativeBoundaryInspectionSchema,
type ManagedNativeBoundaryInspection,
} from "./managedArtifact.js";
import { parseManagedInspectionEvidence } from "./managedInspectionEvidence.js";
-import type { JsonValue } from "../jsonValue.js";
import {
managedNativeVerificationResultSchema,
type ManagedNativeVerificationInput,
@@ -20,13 +16,6 @@ import {
verifyPinvoke,
} from "./managedNativeVerificationMatch.js";
-const sha256 = (value: JsonValue): string => {
- const serialized = canonicalize(value);
- if (serialized === undefined)
- throw new TypeError("Managed/native verification canonicalization failed");
- return createHash("sha256").update(serialized).digest("hex");
-};
-
const parseManagedBoundary = (
input: ManagedNativeVerificationInput,
): {
@@ -65,6 +54,9 @@ export const verifyManagedNativeBoundaries = (
});
return managedNativeVerificationResultSchema.parse({
...withoutId,
- verification_id: `mnv_${sha256(withoutId)}`,
+ verification_id: `mnv_${digestCanonicalValue(
+ withoutId,
+ "Managed/native verification",
+ )}`,
});
};
diff --git a/src/domain/managed/managedNativeVerificationMatch.ts b/src/domain/managed/managedNativeVerificationMatch.ts
index 9bccc56e8..1b9754c40 100644
--- a/src/domain/managed/managedNativeVerificationMatch.ts
+++ b/src/domain/managed/managedNativeVerificationMatch.ts
@@ -1,13 +1,10 @@
-import { createHash } from "node:crypto";
import { basename } from "node:path";
-import canonicalize from "canonicalize";
-
import { parseEvidence } from "../evidence.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import { functionDossierSchema } from "../hopperValues.js";
import { inspectMachoSchema } from "../native/nativeInspection.js";
import type { ManagedNativeBoundaryInspection } from "./managedArtifact.js";
-import type { JsonValue } from "../jsonValue.js";
import {
pinvokeVerificationSchema,
type ManagedNativeVerificationInput,
@@ -16,13 +13,6 @@ import {
type PinvokeVerification,
} from "./managedNativeVerificationSchemas.js";
-const digest = (value: JsonValue): string => {
- const serialized = canonicalize(value);
- if (serialized === undefined)
- throw new TypeError("Managed/native verification canonicalization failed");
- return createHash("sha256").update(serialized).digest("hex");
-};
-
type PinvokeImport = ManagedNativeBoundaryInspection["pinvoke_imports"][number];
type VerifiedPinvoke = {
readonly verification: PinvokeVerification;
@@ -326,13 +316,16 @@ export const verifyPinvoke = ({
const limitations = pinvokeLimitations(selection.match, item);
return {
verification: pinvokeVerificationSchema.parse({
- item_id: `mnv_pinvoke_${digest({
- managedEvidenceId,
- token: item.token,
- importName: item.import_name,
- importScope: item.import_scope_name,
- candidates: selection.match.candidates,
- })}`,
+ item_id: `mnv_pinvoke_${digestCanonicalValue(
+ {
+ managedEvidenceId,
+ token: item.token,
+ importName: item.import_name,
+ importScope: item.import_scope_name,
+ candidates: selection.match.candidates,
+ },
+ "Managed/native verification",
+ )}`,
managed,
...selection.match,
evidence_links: [
diff --git a/src/domain/managed/managedReconstruction.ts b/src/domain/managed/managedReconstruction.ts
index d0fa71aaa..fd8a289c7 100644
--- a/src/domain/managed/managedReconstruction.ts
+++ b/src/domain/managed/managedReconstruction.ts
@@ -1,6 +1,5 @@
import { createHash } from "node:crypto";
-import canonicalize from "canonicalize";
import { z } from "zod";
import { evidenceSchema, type Evidence } from "../evidence.js";
@@ -10,7 +9,7 @@ import {
managedMemberInspectionSchema,
type ManagedMemberInspection,
} from "./managedArtifact.js";
-import type { JsonValue } from "../jsonValue.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import { digestSchema, prefixedDigestSchema } from "../digests.js";
import { parseManagedInspectionEvidence } from "./managedInspectionEvidence.js";
@@ -122,13 +121,6 @@ type StaticObservation =
const sha256Text = (value: string): string =>
createHash("sha256").update(value).digest("hex");
-const sha256Json = (value: JsonValue): string => {
- const serialized = canonicalize(value);
- if (serialized === undefined)
- throw new TypeError("Managed reconstruction canonicalization failed");
- return createHash("sha256").update(serialized).digest("hex");
-};
-
const countLines = (text: string): number =>
text.length === 0 ? 0 : text.split(/\r\n|\r|\n/u).length;
@@ -227,7 +219,10 @@ const buildDecompilerRecord = (
decompiler: ManagedReconstructionImportInput["decompiler"],
): ManagedReconstructionImportResult["decompiler"] => ({
...decompiler,
- options_sha256: sha256Json(decompiler.options),
+ options_sha256: digestCanonicalValue(
+ decompiler.options,
+ "Managed reconstruction",
+ ),
});
const RECONSTRUCTION_LIMITATIONS = [
@@ -282,6 +277,9 @@ export const importManagedReconstruction = (
);
return managedReconstructionImportResultSchema.parse({
...withoutId,
- reconstruction_id: `mre_${sha256Json(withoutId)}`,
+ reconstruction_id: `mre_${digestCanonicalValue(
+ withoutId,
+ "Managed reconstruction",
+ )}`,
});
};