Skip to content

armgit / bla

Public Imported from GitHub
0

🛡️ BLA (Blind LLM Agents) — Adversarial defense shield that protects binaries, Electron apps, JS bundles, Python, and WebAssembly against AI reverse-engineering agents like REA. Poison -> Exhaust -> Hallucinate.

feat(engine): unsigned harden runId and bla-harden-summary/v2

qybaihe5bab150c5b1e
diff --git a/src/engine/harden.ts b/src/engine/harden.ts
index 797bb8c..59da2c4 100644
--- a/src/engine/harden.ts
+++ b/src/engine/harden.ts
@@ -13,6 +13,11 @@ import {
defendTarget,
type OneClickDefenseReport,
} from "./defender.js";
+import {
+ computeHardenRunId,
+ HARDEN_SUMMARY_FILE_NAME,
+ HARDEN_SUMMARY_SCHEMA,
+} from "./run-evidence.js";
/** Preferred build-output directory names, searched first under a project root. */
export const BLA_BUILD_OUTPUT_DIR_NAMES = Object.freeze([
@@ -128,6 +133,13 @@ export interface HardenProjectReport {
readonly maxArtifacts: number;
/** Planned/actual outputs that already existed before this run. */
readonly wouldOverwrite: readonly string[];
+ /**
+ * Content-addressed id for this harden run (unsigned). Stable across identical
+ * inputs/planned outputs; present even on dry-run so agents can correlate later.
+ */
+ readonly runId: string;
+ /** Absolute path of the unsigned summary when written; omitted on dry-run. */
+ readonly summaryPath?: string;
}
export interface HardenDiscoveryResult {
@@ -326,6 +338,19 @@ export function hardenProject(
});
const wouldOverwrite = plannedOutputs.filter((p) => p.outputExisted).map((p) => p.outputPath);
+ const runId = computeHardenRunId({
+ blaVersion: BLA_IDENTITY.version,
+ directory: absDir,
+ intensity,
+ weaveDecoys: options.weaveDecoys === true,
+ selfSeal: options.selfSeal === true,
+ artifacts: plannedOutputs.map(({ artifact, outputPath }) => ({
+ relativePath: artifact.relativePath,
+ format: artifact.format,
+ outputPath,
+ })),
+ });
+
const reportBase = {
blaVersion: BLA_IDENTITY.version,
directory: absDir,
@@ -340,6 +365,7 @@ export function hardenProject(
truncated: discovery.truncated,
maxArtifacts: discovery.maxArtifacts,
wouldOverwrite,
+ runId,
} as const;
if (dryRun) {
@@ -419,14 +445,18 @@ export function hardenProject(
}
}
- // Persist a project-level summary for agents to read back.
+ // Persist a project-level summary for agents to read back (unsigned).
+ let summaryPath: string | undefined;
if (discovered.length > 0) {
+ summaryPath = join(outputRoot, HARDEN_SUMMARY_FILE_NAME);
try {
writeFileSync(
- join(outputRoot, "bla-harden-summary.json"),
+ summaryPath,
JSON.stringify(
{
- schema: "bla-harden-summary/v1",
+ schema: HARDEN_SUMMARY_SCHEMA,
+ runId,
+ signature: "unsigned",
directory: absDir,
intensity,
weaveDecoys: options.weaveDecoys === true,
@@ -439,7 +469,7 @@ export function hardenProject(
"utf8",
);
} catch {
- /* best-effort */
+ summaryPath = undefined;
}
}
@@ -451,12 +481,14 @@ export function hardenProject(
hardenedCount,
failedCount,
artifacts: summaries,
+ ...(summaryPath !== undefined ? { summaryPath } : {}),
};
}
export function formatHardenProjectReport(report: HardenProjectReport): string {
const lines: string[] = [
`BLA harden ${report.blaVersion} — ${report.directory}${report.dryRun ? " (dry-run: nothing written)" : ""}`,
+ `Run: ${report.runId} (unsigned)`,
`Intensity: ${report.intensity} | discovered=${report.discoveredCount} hardened=${report.hardenedCount} failed=${report.failedCount}`,
`Overall: ${report.overallSuccess ? "SUCCESS" : report.discoveredCount === 0 ? "NO ARTIFACTS" : "PARTIAL/FAILED"}`,
];
diff --git a/src/engine/run-evidence.ts b/src/engine/run-evidence.ts
new file mode 100644
index 0000000..48d600c
--- /dev/null
+++ b/src/engine/run-evidence.ts
@@ -0,0 +1,139 @@
+import { createHash } from "node:crypto";
+import { existsSync, readFileSync, statSync } from "node:fs";
+import { join, resolve } from "node:path";
+
+/** File name of the project-level summary `hardenProject` writes. */
+export const HARDEN_SUMMARY_FILE_NAME = "bla-harden-summary.json";
+
+/** Current summary schema; v2 adds `runId`. */
+export const HARDEN_SUMMARY_SCHEMA = "bla-harden-summary/v2";
+
+/** Stable inputs that identify one harden run's planned work. */
+export interface HardenRunIdentity {
+ readonly blaVersion: string;
+ readonly directory: string;
+ readonly intensity: string;
+ readonly weaveDecoys: boolean;
+ readonly selfSeal: boolean;
+ readonly artifacts: readonly {
+ readonly relativePath: string;
+ readonly format: string;
+ readonly outputPath: string;
+ }[];
+}
+
+/**
+ * Deterministic, content-addressed run id: identical inputs and planned outputs
+ * always yield the same id, so it can be recomputed and cross-checked later.
+ * Unsigned — it proves consistency, not authorship.
+ */
+export function computeHardenRunId(identity: HardenRunIdentity): string {
+ const artifacts = [...identity.artifacts]
+ .map((a) => ({
+ relativePath: a.relativePath,
+ format: a.format,
+ outputPath: a.outputPath,
+ }))
+ .sort((x, y) => (x.relativePath < y.relativePath ? -1 : x.relativePath > y.relativePath ? 1 : 0));
+ const canonical = JSON.stringify({
+ blaVersion: identity.blaVersion,
+ directory: identity.directory,
+ intensity: identity.intensity,
+ weaveDecoys: identity.weaveDecoys,
+ selfSeal: identity.selfSeal,
+ artifacts,
+ });
+ return `bla-run-${createHash("sha256").update(canonical).digest("hex").slice(0, 24)}`;
+}
+
+export interface HardenRunSummary {
+ readonly schema: string;
+ readonly runId: string;
+ readonly [key: string]: unknown;
+}
+
+export interface ReadHardenRunSummaryResult {
+ readonly summaryPath: string;
+ readonly runId: string;
+ readonly runIdMatches: boolean | null;
+ readonly summary: HardenRunSummary;
+}
+
+/**
+ * Read back a persisted harden summary (file, or the directory holding it) and
+ * optionally assert it belongs to `expectRunId`. Throws on a missing/unreadable
+ * file, a non-summary JSON, a pre-v2 summary without `runId`, or an id mismatch,
+ * so a bad lookup can never read as a successful correlation.
+ */
+export function readHardenRunSummary(
+ location: string,
+ expectRunId?: string,
+): ReadHardenRunSummaryResult {
+ const abs = resolve(location);
+ if (!existsSync(abs)) {
+ throw new Error(`Run summary not found: ${abs}`);
+ }
+ const summaryPath = statSync(abs).isDirectory() ? join(abs, HARDEN_SUMMARY_FILE_NAME) : abs;
+ if (!existsSync(summaryPath)) {
+ throw new Error(`Run summary not found: ${summaryPath}`);
+ }
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(readFileSync(summaryPath, "utf8"));
+ } catch (err) {
+ throw new Error(
+ `Run summary is not valid JSON: ${summaryPath} (${err instanceof Error ? err.message : String(err)})`,
+ );
+ }
+ if (
+ typeof parsed !== "object" ||
+ parsed === null ||
+ Array.isArray(parsed) ||
+ typeof (parsed as { schema?: unknown }).schema !== "string" ||
+ !(parsed as { schema: string }).schema.startsWith("bla-harden-summary/")
+ ) {
+ throw new Error(`Not a BLA harden summary: ${summaryPath}`);
+ }
+ const record = parsed as { schema: string; runId?: unknown };
+ if (typeof record.runId !== "string" || record.runId.length === 0) {
+ throw new Error(
+ `Harden summary has no runId (schema ${record.schema}); re-run bla_harden to produce ${HARDEN_SUMMARY_SCHEMA}: ${summaryPath}`,
+ );
+ }
+ const runId = record.runId;
+ if (expectRunId !== undefined && expectRunId !== runId) {
+ throw new Error(
+ `Run id mismatch: expected ${expectRunId} but ${summaryPath} records ${runId}`,
+ );
+ }
+ return {
+ summaryPath,
+ runId,
+ runIdMatches: expectRunId === undefined ? null : true,
+ summary: parsed as HardenRunSummary,
+ };
+}
+
+/** sha256 hex of a file's bytes, for content-addressing single-artifact outputs. */
+export function sha256File(path: string): string {
+ return createHash("sha256").update(readFileSync(path)).digest("hex");
+}
+
+/**
+ * Deterministic run id for a single-artifact defend/protect run, bound to the
+ * produced output bytes so a later re-hash can confirm the output is unchanged.
+ */
+export function computeArtifactRunId(input: {
+ readonly tool: string;
+ readonly inputPath: string;
+ readonly outputPath: string;
+ readonly outputSha256: string;
+}): string {
+ const canonical = JSON.stringify({
+ tool: input.tool,
+ inputPath: resolve(input.inputPath),
+ outputPath: resolve(input.outputPath),
+ outputSha256: input.outputSha256,
+ });
+ return `bla-run-${createHash("sha256").update(canonical).digest("hex").slice(0, 24)}`;
+}
diff --git a/src/index.ts b/src/index.ts
index fb29e3f..267f641 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -175,6 +175,16 @@ export {
type DiscoveredArtifact,
type HardenDiscoveryResult,
} from "./engine/harden.js";
+export {
+ computeHardenRunId,
+ computeArtifactRunId,
+ readHardenRunSummary,
+ HARDEN_SUMMARY_FILE_NAME,
+ HARDEN_SUMMARY_SCHEMA,
+ type HardenRunIdentity,
+ type HardenRunSummary,
+ type ReadHardenRunSummaryResult,
+} from "./engine/run-evidence.js";
export * from "./shields/exhaustion/index.js";
export * from "./shields/deception/index.js";
export {