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.

fix(harden): only claim entry self-coverage when self-digest verifies

qybaiheb5a90cce03ad
diff --git a/src/engine/harden.ts b/src/engine/harden.ts
index 34d83ef..1c27c35 100644
--- a/src/engine/harden.ts
+++ b/src/engine/harden.ts
@@ -2,6 +2,7 @@ import {
existsSync,
mkdirSync,
readdirSync,
+ readFileSync,
statSync,
writeFileSync,
} from "node:fs";
@@ -35,6 +36,8 @@ import {
evaluateHardenProjectPackagingBlocks,
} from "./packaging-gates.js";
import { planSidecarEntanglement } from "../shields/runtime/sidecar-entangle.js";
+import { verifySelfSealSelfDigest } from "../shields/runtime/self-seal.js";
+import { withoutHostSelfCoverage } from "./integrity-coverage.js";
import {
computeHardenRunId,
HARDEN_SUMMARY_FILE_NAME,
@@ -188,6 +191,12 @@ export interface HardenProjectReport {
readonly summaryPath?: string;
/** Absolute path of bla-integrity-coverage/v1 matrix when selfSeal wrote one. */
readonly integrityCoveragePath?: string;
+ /**
+ * True when the entry checker's canonical self-digest verifies on disk (entry
+ * self-tamper hard-fails). When false the written matrix drops the entry's
+ * self-coverage edges so packaging gates do not trust paper coverage.
+ */
+ readonly integrityCoverageSelfVerified?: boolean;
}
export interface HardenDiscoveryResult {
@@ -709,6 +718,7 @@ export function hardenProject(
}
let integrityCoveragePath: string | undefined;
+ let integrityCoverageSelfVerified: boolean | undefined;
if (options.selfSeal === true && !dryRun && summaries.some((s) => s.success)) {
try {
const jsLike = summaries.filter(
@@ -729,10 +739,17 @@ export function hardenProject(
seedTag: String(options.seed ?? runId),
baseDir: outputRoot,
});
+ const entryRel = entry.relativePath.replaceAll("\\", "/");
+ const entryAbs = join(outputRoot, entryRel);
+ integrityCoverageSelfVerified =
+ existsSync(entryAbs) && verifySelfSealSelfDigest(readFileSync(entryAbs)).verified;
+ const matrix = integrityCoverageSelfVerified
+ ? plan.coverageMatrix
+ : withoutHostSelfCoverage(plan.coverageMatrix, entryRel);
integrityCoveragePath = join(outputRoot, "integrity-coverage.json");
writeFileSync(
integrityCoveragePath,
- `${JSON.stringify(plan.coverageMatrix, null, 2)}\n`,
+ `${JSON.stringify(matrix, null, 2)}\n`,
"utf8",
);
}
@@ -753,6 +770,9 @@ export function hardenProject(
...(integrityCoveragePath !== undefined
? { integrityCoveragePath }
: {}),
+ ...(integrityCoverageSelfVerified !== undefined
+ ? { integrityCoverageSelfVerified }
+ : {}),
};
}
diff --git a/src/engine/integrity-coverage.ts b/src/engine/integrity-coverage.ts
index d6c9ff0..42405cc 100644
--- a/src/engine/integrity-coverage.ts
+++ b/src/engine/integrity-coverage.ts
@@ -221,6 +221,31 @@ export function buildInterleavedCoverageMatrix(
};
}
+/**
+ * Honesty downgrade: drop every edge by which `host#siteN` checkers claim to
+ * verify their own host file or sibling sites. Use when the host's runtime self
+ * check cannot hard-fail (e.g. self-digest not finalized) — editing the host
+ * removes all colocated sites at once, so the matrix must not count them as
+ * covering it. Re-evaluate afterwards; single-host trees will then refuse.
+ */
+export function withoutHostSelfCoverage(
+ matrix: CoverageMatrix,
+ host: string,
+): CoverageMatrix {
+ const prefix = `${host}#site`;
+ const isSelf = (n: string): boolean => n === host || n.startsWith(prefix);
+ const edges: Record<string, readonly string[]> = {};
+ for (const [checker, targets] of Object.entries(matrix.edges)) {
+ edges[checker] = isSelf(checker) ? targets.filter((t) => !isSelf(t)) : [...targets];
+ }
+ return {
+ schema: INTEGRITY_COVERAGE_SCHEMA,
+ nodes: matrix.nodes,
+ edges,
+ expectedSha256: matrix.expectedSha256,
+ };
+}
+
export function sha256Hex(buf: Buffer | string): string {
return createHash("sha256").update(buf).digest("hex");
}
diff --git a/tests/integrity-coverage-self-honesty.test.ts b/tests/integrity-coverage-self-honesty.test.ts
new file mode 100644
index 0000000..c77e011
--- /dev/null
+++ b/tests/integrity-coverage-self-honesty.test.ts
@@ -0,0 +1,87 @@
+/**
+ * Coverage-matrix honesty for entry self-coverage (CrackMe v8 red team 2026-11-05).
+ *
+ * The matrix may only claim `host#siteN → host` edges when the host's runtime
+ * self check can actually hard-fail (canonical self-digest verifies on disk).
+ */
+import { describe, expect, it } from "vitest";
+import { appendFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { join } from "node:path";
+import { tmpdir } from "node:os";
+import { spawnSync } from "node:child_process";
+import {
+ buildInterleavedCoverageMatrix,
+ evaluateCoverageMatrix,
+ finalizeCoverageMatrixHashes,
+ withoutHostSelfCoverage,
+} from "../src/engine/integrity-coverage.js";
+import { hardenProject } from "../src/engine/harden.js";
+
+function run(path: string) {
+ return spawnSync(process.execPath, [path], { encoding: "utf8" });
+}
+
+describe("integrity coverage honesty for host self-coverage", () => {
+ it("withoutHostSelfCoverage makes a single-host matrix refuse", () => {
+ const files = { "license.cjs": "x", "panel.html": "y" };
+ const matrix = finalizeCoverageMatrixHashes(
+ buildInterleavedCoverageMatrix({
+ fileNodes: Object.keys(files),
+ checkerHosts: ["license.cjs"],
+ expectedSha256: {},
+ colocatedSites: 2,
+ }),
+ Object.fromEntries(Object.entries(files).map(([k, v]) => [k, Buffer.from(v)])),
+ );
+ expect(evaluateCoverageMatrix(matrix).ok).toBe(true);
+ const honest = withoutHostSelfCoverage(matrix, "license.cjs");
+ for (const targets of Object.values(honest.edges)) {
+ expect(targets).not.toContain("license.cjs");
+ expect(targets.some((t) => t.startsWith("license.cjs#site"))).toBe(false);
+ expect(targets).toContain("panel.html");
+ }
+ const ev = evaluateCoverageMatrix(honest);
+ expect(ev.ok).toBe(false);
+ expect(ev.shouldRefuseBankrupt).toBe(true);
+ });
+
+ it("harden --self-seal reports a verified entry self-digest and the entry hard-fails on edit", () => {
+ const root = mkdtempSync(join(tmpdir(), "bla-selfdig-harden-"));
+ try {
+ const src = join(root, "app");
+ mkdirSync(src);
+ writeFileSync(
+ join(src, "license.cjs"),
+ ['"use strict";', "const helper = require('./helper.js');",
+ "if (require.main === module) console.log('sum', helper.add(2, 3));", ""].join("\n"),
+ );
+ writeFileSync(
+ join(src, "helper.js"),
+ ['"use strict";', "function add(a, b) { return a + b; }", "module.exports = { add };", ""].join("\n"),
+ );
+ const out = join(root, "out");
+ const report = hardenProject({
+ directory: src,
+ intensity: "bankrupt",
+ seed: "selfdig-harden",
+ outputDirectory: out,
+ selfSeal: true,
+ });
+ expect(report.overallSuccess).toBe(true);
+ expect(report.integrityCoverageSelfVerified).toBe(true);
+ const matrix = JSON.parse(readFileSync(report.integrityCoveragePath!, "utf8"));
+ expect(evaluateCoverageMatrix(matrix).ok).toBe(true);
+
+ const lic = join(out, "license.cjs");
+ const intact = run(lic);
+ expect(intact.status, intact.stderr).toBe(0);
+ expect(intact.stdout).toContain("sum 5");
+ appendFileSync(lic, "\n// patched\n");
+ const tampered = run(lic);
+ expect(tampered.status).not.toBe(0);
+ expect(`${tampered.stderr}${tampered.stdout}`).toMatch(/integrity check failed/);
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+ });
+});