mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 01:46:39 +00:00
refactor(verifiers): share whole-file SHA-256 hashing (#14475)
* refactor(verifiers): share whole-file SHA-256 hashing * fix(verifiers): type the streamed digest input
This commit is contained in:
1 parent
a0390ba877
commit
60af9ef6b3
12 files changed
+41
-68
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { link, mkdir, open, rm } from "node:fs/promises";
|
||||
import { resolve, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -16,11 +16,6 @@ const directory = resolve(
|
||||
),
|
||||
);
|
||||
await mkdir(directory, { recursive: true });
|
||||
const hashFile = async (path) => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
for (const item of [
|
||||
{
|
||||
url: JADX_RELEASE.url,
|
||||
@@ -31,7 +26,7 @@ for (const item of [
|
||||
]) {
|
||||
const path = join(directory, item.filename);
|
||||
try {
|
||||
const digest = await hashFile(path);
|
||||
const digest = await sha256File(path);
|
||||
if (digest !== item.sha256)
|
||||
throw new Error(
|
||||
`Existing ${path} has a different SHA-256; it was not overwritten`,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { link, mkdir, open, rm } from "node:fs/promises";
|
||||
import { resolve, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -15,14 +15,9 @@ const directory = resolve(
|
||||
),
|
||||
);
|
||||
await mkdir(directory, { recursive: true });
|
||||
const hashFile = async (path) => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
const path = join(directory, fixture.filename);
|
||||
try {
|
||||
const digest = await hashFile(path);
|
||||
const digest = await sha256File(path);
|
||||
if (digest !== fixture.sha256)
|
||||
throw new Error(
|
||||
`Existing ${path} has a different SHA-256; it was not overwritten`,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
|
||||
/** Verify artifact identity without depending on private runtime snapshot helpers. */
|
||||
export const hashAndroidFile = async (path) => {
|
||||
/**
|
||||
* Stream a verifier input into a lowercase hexadecimal SHA-256 digest.
|
||||
* @param {import("node:fs").PathLike} path
|
||||
*/
|
||||
export const sha256File = async (path) => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const bytes of createReadStream(path)) hash.update(bytes);
|
||||
return hash.digest("hex");
|
||||
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env node
|
||||
import { sha256File } from "./lib/verifier-file-sha256.mjs";
|
||||
import {
|
||||
assertFeatureTrace,
|
||||
findMappedNonFunction,
|
||||
@@ -12,7 +13,6 @@ import {
|
||||
import { withVerifierResourceOwner } from "./lib/verifier-file-resources.mjs";
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { access, readdir, writeFile } from "node:fs/promises";
|
||||
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
||||
@@ -133,11 +133,6 @@ let phase = "target admission";
|
||||
const workspaces = new Set();
|
||||
let targetIsDatabase = false;
|
||||
const databases = new Set();
|
||||
const digest = async () => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(target)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
const observe = (input) => {
|
||||
const evidence = evidenceSchema.parse(input);
|
||||
assert.equal(evidence.provider.id, "ida");
|
||||
@@ -547,7 +542,7 @@ const cleanup = async () => {
|
||||
await upstream.connect();
|
||||
if (config.mode === "attached") {
|
||||
const metadata = await upstream.readResource("ida://idb/metadata");
|
||||
assert.equal(metadata.sha256.toLowerCase(), await digest());
|
||||
assert.equal(metadata.sha256.toLowerCase(), await sha256File(target));
|
||||
report.checks.push(
|
||||
"existing GUI target remains open with the original input identity",
|
||||
);
|
||||
@@ -583,13 +578,13 @@ const cleanup = async () => {
|
||||
}
|
||||
};
|
||||
try {
|
||||
const before = await digest();
|
||||
const before = await sha256File(target);
|
||||
for await (const head of createReadStream(target, { end: 3 }))
|
||||
targetIsDatabase = hasIdaDatabaseSignature(head);
|
||||
const siblings = (await readdir(dirname(target))).sort();
|
||||
await mcp(await cli());
|
||||
await cleanup();
|
||||
assert.equal(await digest(), before);
|
||||
assert.equal(await sha256File(target), before);
|
||||
// Engines write databases beside their input; only the private copy may get one.
|
||||
assert.deepEqual((await readdir(dirname(target))).sort(), siblings);
|
||||
report.checks.push(
|
||||
|
||||
@@ -8,7 +8,7 @@ import { promisify } from "node:util";
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { parseEvidence } from "../../../dist/domain/evidence.js";
|
||||
import { hashAndroidFile } from "./digest.mjs";
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import { JADX_RELEASE } from "../../../dist/android/JadxRelease.js";
|
||||
import fixture from "../../fixtures/android/apidemos.json" with { type: "json" };
|
||||
import { verifyAndroidCliCancellation } from "./cli-cancellation.mjs";
|
||||
@@ -31,12 +31,12 @@ if (apk === undefined || jar === undefined)
|
||||
await access(apk);
|
||||
await access(jar);
|
||||
assert.equal(
|
||||
await hashAndroidFile(apk),
|
||||
await sha256File(apk),
|
||||
fixture.sha256,
|
||||
"Real Android lane requires the fixed ApiDemos release fixture",
|
||||
);
|
||||
assert.equal(
|
||||
await hashAndroidFile(jar),
|
||||
await sha256File(jar),
|
||||
JADX_RELEASE.sha256,
|
||||
"Real Android lane requires the audited headless JADX release JAR",
|
||||
);
|
||||
@@ -312,7 +312,7 @@ await verifyAndroidFailureRecovery({
|
||||
fixture,
|
||||
});
|
||||
assert.equal(
|
||||
await hashAndroidFile(apk),
|
||||
await sha256File(apk),
|
||||
fixture.sha256,
|
||||
"Original APK must remain unchanged",
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { parseEvidence } from "../../../dist/domain/evidence.js";
|
||||
import { hashAndroidFile } from "./digest.mjs";
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
|
||||
/** The public result keeps one using node and exposes its two raw DEX invokes. */
|
||||
export const verifyAndroidDirectInvokes = async ({
|
||||
@@ -13,7 +13,7 @@ export const verifyAndroidDirectInvokes = async ({
|
||||
environment,
|
||||
execute,
|
||||
}) => {
|
||||
const digest = await hashAndroidFile(apk);
|
||||
const digest = await sha256File(apk);
|
||||
const check = (value) => {
|
||||
const evidence = parseEvidence(value);
|
||||
assert.equal(evidence.subject.identity.sha256, digest);
|
||||
@@ -102,7 +102,7 @@ export const verifyAndroidDirectInvokes = async ({
|
||||
await client.close();
|
||||
}
|
||||
assert.equal(
|
||||
await hashAndroidFile(apk),
|
||||
await sha256File(apk),
|
||||
digest,
|
||||
"Static inspection changed fixture bytes",
|
||||
);
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
requireMcpOutput,
|
||||
requireMcpToolError,
|
||||
} from "../../lib/mcp-verifier-results.mjs";
|
||||
import { hashAndroidFile } from "./digest.mjs";
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
|
||||
const operation = "trace_android_references";
|
||||
const className = "android.app.NotificationManager";
|
||||
@@ -94,7 +94,7 @@ const cli = async (options, path, index, selector = input(path, index)) => {
|
||||
};
|
||||
|
||||
const verifyApk = async (options, path, dexEntry) => {
|
||||
const digest = await hashAndroidFile(path);
|
||||
const digest = await sha256File(path);
|
||||
const cliResults = [];
|
||||
for (const index of [0, 1])
|
||||
cliResults.push(
|
||||
@@ -172,7 +172,7 @@ const verifyApk = async (options, path, dexEntry) => {
|
||||
assert.deepEqual(replay.normalized_result, cliResults[0].normalized_result);
|
||||
}, [() => client.close(), () => transport.close()]);
|
||||
assert.equal(
|
||||
await hashAndroidFile(path),
|
||||
await sha256File(path),
|
||||
digest,
|
||||
"Static references changed APK bytes",
|
||||
);
|
||||
@@ -187,7 +187,7 @@ const verifyLimit = async (
|
||||
stage = "caller_inventory",
|
||||
selector,
|
||||
) => {
|
||||
const digest = await hashAndroidFile(path);
|
||||
const digest = await sha256File(path);
|
||||
try {
|
||||
await cli(options, path, 0, selector);
|
||||
assert.fail("Oversized external reference inventory was accepted");
|
||||
@@ -198,7 +198,7 @@ const verifyLimit = async (
|
||||
assert.equal(failure.details.reported_limits.stage, stage);
|
||||
assert.equal(failure.retryable, false);
|
||||
}
|
||||
assert.equal(await hashAndroidFile(path), digest);
|
||||
assert.equal(await sha256File(path), digest);
|
||||
console.log(
|
||||
`PASS real external ${stage} representation refusal retains typed resource constraint and immutable APK`,
|
||||
);
|
||||
@@ -247,7 +247,7 @@ const checkSiteBudget = (value, digest) => {
|
||||
|
||||
/** Optional instruction positions cannot displace the complete caller inventory. */
|
||||
const verifySiteBudget = async (options, path, control) => {
|
||||
const digest = await hashAndroidFile(path);
|
||||
const digest = await sha256File(path);
|
||||
const first = checkSiteBudget(await cli(options, path, 0), digest);
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
@@ -298,7 +298,7 @@ const verifySiteBudget = async (options, path, control) => {
|
||||
);
|
||||
check(
|
||||
requireMcpOutput(await call(input(control, 0)), operation),
|
||||
await hashAndroidFile(control),
|
||||
await sha256File(control),
|
||||
0,
|
||||
"classes.dex",
|
||||
);
|
||||
@@ -308,7 +308,7 @@ const verifySiteBudget = async (options, path, control) => {
|
||||
);
|
||||
assert.deepEqual(replay.normalized_result, first.normalized_result);
|
||||
}, [() => client.close(), () => transport.close()]);
|
||||
assert.equal(await hashAndroidFile(path), digest);
|
||||
assert.equal(await sha256File(path), digest);
|
||||
console.log(
|
||||
"PASS site-heavy CLI/MCP complete11 callers, partial positions, bounded output, target replacement/recovery and awaited cleanup",
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { parseEvidence } from "../../../dist/domain/evidence.js";
|
||||
import { hashAndroidFile } from "./digest.mjs";
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
|
||||
const initializers = [
|
||||
{
|
||||
@@ -98,7 +98,7 @@ export const verifyAndroidMethodBodies = async ({
|
||||
environment,
|
||||
execute,
|
||||
}) => {
|
||||
const digest = await hashAndroidFile(apk);
|
||||
const digest = await sha256File(apk);
|
||||
const check = (value, scenario) => {
|
||||
const evidence = parseEvidence(value);
|
||||
assert.equal(evidence.subject.identity.sha256, digest);
|
||||
@@ -198,7 +198,7 @@ export const verifyAndroidMethodBodies = async ({
|
||||
await client.close();
|
||||
}
|
||||
assert.equal(
|
||||
await hashAndroidFile(apk),
|
||||
await sha256File(apk),
|
||||
digest,
|
||||
"Static inspection changed fixture bytes",
|
||||
);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import { requireMcpOutput } from "../../lib/mcp-verifier-results.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { access, writeFile } from "node:fs/promises";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -26,13 +25,8 @@ if (hap === undefined)
|
||||
"verify:harmony requires REA_HARMONY_TEST_HAP. Run npm run fixtures:harmony and set it to the downloaded file; see docs/guides/harmony/harmony-analysis.md.",
|
||||
);
|
||||
await access(hap);
|
||||
const hashFile = async (path) => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
assert.equal(
|
||||
await hashFile(hap),
|
||||
await sha256File(hap),
|
||||
fixture.sha256,
|
||||
"Real HarmonyOS lane requires the pinned Harmony pipeline release fixture",
|
||||
);
|
||||
@@ -216,7 +210,7 @@ await withVerifierFixture(workspaceFixture, async () => {
|
||||
assert.notEqual(empty.isError, true, JSON.stringify(empty));
|
||||
assert.equal(empty.structuredContent?.result?.open, false);
|
||||
assert.equal(
|
||||
await hashFile(path),
|
||||
await sha256File(path),
|
||||
fixture.sha256,
|
||||
"MCP workflow must preserve source bytes",
|
||||
);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { performance } from "node:perf_hooks";
|
||||
@@ -104,11 +104,6 @@ const client = new Client({
|
||||
const serialize = (value) => JSON.stringify(value);
|
||||
const digest = (value) =>
|
||||
createHash("sha256").update(serialize(value)).digest("hex");
|
||||
const fileDigest = async (path) => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) hash.update(chunk);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
const simulatedNativeEvidence = (path, sha256) =>
|
||||
createEvidence(
|
||||
{ path, sha256, format: "raw-binary" },
|
||||
@@ -151,11 +146,11 @@ const ownedCommand = async (command, arguments_, timeoutMs = 60_000) => {
|
||||
}
|
||||
};
|
||||
await withVerifierCleanup(async () => {
|
||||
const archiveSha256 = await fileDigest(values.artifact);
|
||||
const archiveSha256 = await sha256File(values.artifact);
|
||||
const companion = join(`${values.artifact}.unpacked`, values["addon-path"]);
|
||||
let companionSha256 = null;
|
||||
try {
|
||||
companionSha256 = await fileDigest(companion);
|
||||
companionSha256 = await sha256File(companion);
|
||||
} catch (cause) {
|
||||
if (cause?.code !== "ENOENT") throw cause;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { constants } from "node:buffer";
|
||||
import { createHash } from "node:crypto";
|
||||
@@ -157,9 +158,7 @@ await withVerifierFixture(fixture, async () => {
|
||||
assert.equal(output.error, undefined);
|
||||
assert.equal(unexpectedOutputBytes, 0);
|
||||
|
||||
const actualHash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(path)) actualHash.update(chunk);
|
||||
const digest = actualHash.digest("hex");
|
||||
const digest = await sha256File(path);
|
||||
assert.equal(digest, expectedHash.digest("hex"));
|
||||
assert.equal((await stat(path)).size, expectedBytes);
|
||||
let verifiedTokenCount;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { sha256File } from "../../lib/verifier-file-sha256.mjs";
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream } from "node:fs";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { performance } from "node:perf_hooks";
|
||||
@@ -111,10 +111,7 @@ const resourceSnapshot = async () => {
|
||||
|
||||
await withVerifierCleanup(async () => {
|
||||
const start = performance.now();
|
||||
const artifactHash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(values.artifact))
|
||||
artifactHash.update(chunk);
|
||||
const artifactSha256 = artifactHash.digest("hex");
|
||||
const artifactSha256 = await sha256File(values.artifact);
|
||||
temporary = await VerifierFixture.open({
|
||||
prefix: "rea-retained-facts-verifier-",
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user