Skip to content

armgit / rea

Public Imported from GitHub
0

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

refactor(domain): consolidate projection and verification digests

morlutoce5bc8cb0149
diff --git a/src/domain/android/androidApplication.ts b/src/domain/android/androidApplication.ts
index ea53a70d7..13c6e53be 100644
--- a/src/domain/android/androidApplication.ts
+++ b/src/domain/android/androidApplication.ts
@@ -10,6 +10,7 @@ import {
} from "../artifactInventoryEvidence.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import { nativeRuntimeConvention } from "../nativeConvention.js";
import {
applicationInventoryProjectionInputSchema,
@@ -19,7 +20,6 @@ import {
projectedInventoryEntries,
projectedFileComponents,
projectionCoverage,
- projectionDigest,
projectionEvidenceIdSchema,
unavailableComponentSchema,
} from "../mobileApplicationGraphProjection.js";
@@ -155,7 +155,7 @@ export const projectAndroidApplication = (
return {
projection: androidApplicationProjectionResultSchema.parse({
...withoutId,
- projection_id: `adp_${projectionDigest(withoutId, "Android application projection")}`,
+ projection_id: `adp_${digestCanonicalValue(withoutId, "Android application projection")}`,
}),
sources: evidence,
};
diff --git a/src/domain/apple/appleApplication.ts b/src/domain/apple/appleApplication.ts
index 6c2dfd049..fc70e7070 100644
--- a/src/domain/apple/appleApplication.ts
+++ b/src/domain/apple/appleApplication.ts
@@ -21,6 +21,7 @@ import {
import { evidenceSchema } from "../evidence.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import {
bridgeCandidateCoverageSchema,
projectCartesianCandidates,
@@ -30,7 +31,6 @@ import {
projectedComponentSchema,
projectedInventoryEntries,
projectedFileComponents,
- projectionDigest,
unavailableComponentSchema,
type ProjectedInventoryEntry,
} from "../mobileApplicationGraphProjection.js";
@@ -212,7 +212,7 @@ export const projectAppleApplication = (
/** Identify the complete projection, including source Evidence provenance. */
export const appleProjectionId = (projection: unknown): string =>
- `aap_${digest(projection)}`;
+ `aap_${digestCanonicalValue(projection, "Apple application projection")}`;
/**
* AppleDouble sidecars describe neighbouring files and are never components.
@@ -442,6 +442,3 @@ const projectionLimitations = (facts: {
"Bridge candidates are path-based hypotheses, not observed runtime calls.",
"A bridge basis is inferred from the native path and is repeated for every managed component.",
];
-
-const digest = (value: unknown): string =>
- projectionDigest(value, "Apple application projection");
diff --git a/src/domain/conformancePackage.ts b/src/domain/conformancePackage.ts
index 6734d2c2c..3f2a3ae1a 100644
--- a/src/domain/conformancePackage.ts
+++ b/src/domain/conformancePackage.ts
@@ -1,6 +1,3 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
import { z } from "zod";
import { IDENTIFIER_PATTERN } from "./stringPatterns.js";
@@ -9,6 +6,7 @@ import { evidenceSchema } from "./evidence.js";
import { evidenceBundleSchema } from "./evidenceBundle.js";
import { err, ok, type Result } from "./result.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
+import { digestCanonicalValue } from "./canonicalDigest.js";
const conformancePackageIdSchema = prefixedDigestSchema("cp");
@@ -317,8 +315,5 @@ const packageContents = (
});
const computePackageId = (contents: ConformancePackageContents): string => {
- const json = canonicalize(contents);
- if (json === undefined)
- throw new TypeError("Could not canonicalize parsed conformance package");
- return `cp_${createHash("sha256").update(json).digest("hex")}`;
+ return `cp_${digestCanonicalValue(contents, "Conformance package")}`;
};
diff --git a/src/domain/harmony/harmonyApplication.ts b/src/domain/harmony/harmonyApplication.ts
index 34ef9ee38..3f9c2b272 100644
--- a/src/domain/harmony/harmonyApplication.ts
+++ b/src/domain/harmony/harmonyApplication.ts
@@ -10,6 +10,7 @@ import {
} from "../artifactInventoryEvidence.js";
import { digestSchema } from "../digests.js";
import { prefixedDigestSchema } from "../digests.js";
+import { digestCanonicalValue } from "../canonicalDigest.js";
import { nativeRuntimeConvention } from "../nativeConvention.js";
import {
applicationInventoryProjectionInputSchema,
@@ -19,7 +20,6 @@ import {
projectedInventoryEntries,
projectedFileComponents,
projectionCoverage,
- projectionDigest,
projectionEvidenceIdSchema,
unavailableComponentSchema,
} from "../mobileApplicationGraphProjection.js";
@@ -151,7 +151,7 @@ export const projectHarmonyApplication = (
return {
projection: harmonyApplicationProjectionResultSchema.parse({
...withoutId,
- projection_id: `hmp_${projectionDigest(withoutId, "HarmonyOS application projection")}`,
+ projection_id: `hmp_${digestCanonicalValue(withoutId, "HarmonyOS application projection")}`,
}),
sources: evidence,
};
diff --git a/src/domain/mobileApplicationGraphProjection.ts b/src/domain/mobileApplicationGraphProjection.ts
index bd0ee2b10..ea3ca4548 100644
--- a/src/domain/mobileApplicationGraphProjection.ts
+++ b/src/domain/mobileApplicationGraphProjection.ts
@@ -1,6 +1,3 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
import { z } from "zod";
import {
@@ -141,11 +138,3 @@ export const projectedFileComponents = (
? [component]
: [],
);
-
-/** Digest a canonical JSON projection payload for its stable identifier. */
-export const projectionDigest = (value: unknown, label: string): string => {
- const encoded = canonicalize(value);
- if (encoded === undefined)
- throw new TypeError(`${label} is not canonical JSON`);
- return createHash("sha256").update(encoded).digest("hex");
-};
diff --git a/src/domain/reconstructionCoverage.ts b/src/domain/reconstructionCoverage.ts
index 715d0c6ba..2a6ef77b2 100644
--- a/src/domain/reconstructionCoverage.ts
+++ b/src/domain/reconstructionCoverage.ts
@@ -1,6 +1,3 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
import { z } from "zod";
import {
@@ -23,6 +20,7 @@ import {
} from "./reconstructionCoverageEvaluation.js";
import { digestSchema } from "./../domain/digests.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
+import { digestCanonicalValue } from "./canonicalDigest.js";
import {
offsetDateTimeInstantFromEpochMilliseconds,
offsetDateTimeSchema,
@@ -289,7 +287,7 @@ export const createReconstructionVerifierContract = (
const parsed = verifierContractBaseSchema.parse(input);
return verifierContractSchema.parse({
...parsed,
- contract_sha256: digest(parsed),
+ contract_sha256: digestCanonicalValue(parsed, "Reconstruction coverage"),
});
};
@@ -354,7 +352,13 @@ const validateCoverageReferences = (
indexUnique(workspace.boundaries, "boundary_id");
for (const contract of workspace.verifier_contracts) {
const { contract_sha256: contractSha256, ...base } = contract;
- if (contractSha256 !== digest(verifierContractBaseSchema.parse(base)))
+ if (
+ contractSha256 !==
+ digestCanonicalValue(
+ verifierContractBaseSchema.parse(base),
+ "Reconstruction coverage",
+ )
+ )
throw new TypeError(
`Verifier contract digest is invalid: ${contract.verifier_id}`,
);
@@ -462,10 +466,3 @@ const reasonOrder = (left: ClosureReason, right: ClosureReason): number =>
compositeKey([left.code, left.subject_id]),
compositeKey([right.code, right.subject_id]),
);
-
-const digest = (value: unknown): string => {
- const encoded = canonicalize(value);
- if (encoded === undefined)
- throw new TypeError("Reconstruction coverage value is not canonical JSON");
- return createHash("sha256").update(encoded).digest("hex");
-};
diff --git a/src/domain/reconstructionVerification.ts b/src/domain/reconstructionVerification.ts
index 59a456424..b0e3c8f11 100644
--- a/src/domain/reconstructionVerification.ts
+++ b/src/domain/reconstructionVerification.ts
@@ -1,6 +1,3 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
import { z } from "zod";
import { artifactComparisonResultSchema } from "./artifactComparison.js";
@@ -19,6 +16,7 @@ import { functionDossierSchema } from "./hopperValues.js";
import { assessFunctionCallCoverage } from "./functionCallCoverage.js";
import { processCaptureComparisonSchema } from "./process/processComparison.js";
import { parseProcessCapture } from "./process/processCaptureParsing.js";
+import { digestCanonicalValue } from "./canonicalDigest.js";
import {
reconstructionClaimResultSchema,
reconstructionSpecificationSchema,
@@ -72,12 +70,15 @@ export const verifyReconstruction = (
);
const output = {
status: failed > 0 ? "fail" : unresolved > 0 ? "unknown" : "pass",
- specification_sha256: digest({
- ...specification,
- claims: [...specification.claims].sort((left, right) =>
- compareUnicodeCodePoints(left.claim_id, right.claim_id),
- ),
- }),
+ specification_sha256: digestCanonicalValue(
+ {
+ ...specification,
+ claims: [...specification.claims].sort((left, right) =>
+ compareUnicodeCodePoints(left.claim_id, right.claim_id),
+ ),
+ },
+ "Reconstruction specification",
+ ),
summary: {
total: results.length,
passed: count(results, "pass"),
@@ -377,13 +378,6 @@ const classify = (
return "pass";
};
-const digest = (value: unknown): string => {
- const encoded = canonicalize(value);
- if (encoded === undefined)
- throw new TypeError("Specification canonicalization failed");
- return createHash("sha256").update(encoded).digest("hex");
-};
-
const count = (
items: readonly ClaimResult[],
status: ClaimResult["status"],
diff --git a/src/domain/webInventory.ts b/src/domain/webInventory.ts
index 2cde0a18b..ebce288c3 100644
--- a/src/domain/webInventory.ts
+++ b/src/domain/webInventory.ts
@@ -1,6 +1,4 @@
-import { createHash } from "node:crypto";
-
-import canonicalize from "canonicalize";
+import { digestCanonicalValue } from "./canonicalDigest.js";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
@@ -36,7 +34,7 @@ export const stableWebResources = (
): StableWebResource[] => {
const keyed = resources.map((resource) => ({
...resource,
- resource_key: `res_${digest(resource)}`,
+ resource_key: `res_${digestCanonicalValue(resource, "Web inventory")}`,
}));
return [
...new Map(
@@ -114,23 +112,19 @@ export const createCapturedWebScriptReconciler = (
/** Stable script identity excludes target IDs, CDP script IDs, and timestamps. */
export const stableWebScriptKey = (script: WebInventoryScriptInput): string =>
- `scr_${digest({
- url: script.url,
- cdp_hash: script.cdp_hash,
- length: script.length,
- is_module: script.is_module,
- language: script.language,
- source_map_url: script.source_map_url,
- })}`;
+ `scr_${digestCanonicalValue(
+ {
+ url: script.url,
+ cdp_hash: script.cdp_hash,
+ length: script.length,
+ is_module: script.is_module,
+ language: script.language,
+ source_map_url: script.source_map_url,
+ },
+ "Web inventory",
+ )}`;
const uniqueResourceKeys = (
resources: readonly StableWebResource[],
): string[] =>
[...new Set(resources.map(({ resource_key }) => resource_key))].sort();
-
-const digest = (value: unknown): string => {
- const encoded = canonicalize(value);
- if (encoded === undefined)
- throw new TypeError("Web inventory value is not canonical JSON");
- return createHash("sha256").update(encoded).digest("hex");
-};