🛡️ 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.
JavaScript adversarial-aiclaude-codecursorelectron-securityllm-securitymcpmcp-serverobfuscationreverse-engineeringwebassembly
feat(cli): add verify-manifest with trusted-key pinning
DeryFerd52a1adae2bd0
diff --git a/src/cli.ts b/src/cli.tsindex b4e2e59..519069f 100644--- a/src/cli.ts+++ b/src/cli.ts@@ -5,6 +5,7 @@ import { BLA_IDENTITY } from "./identity.js";import { initBlaConfig, loadBlaConfig, resolveProjectSeed } from "./core/config.js";import { inspectReleaseExposure } from "./core/release-exposure.js";import { buildSarifReport } from "./core/sarif.js";+import { verifyManifestFile } from "./core/manifest-verify.js";import type { ProtectionIntensity } from "./core/types.js";import { auditTarget } from "./engine/auditor.js";import { protectTarget } from "./engine/protector.js";@@ -75,6 +76,7 @@ Commands:verify <target> [original] Verify Counter-REA defense invariants & token amplificationbenchmark Run the 14-target Counter-REA Showcase Benchmark suiteemit-headers Generate compile-time C/C++, Rust, or Go defense headers+ verify-manifest <file> Verify a signed .bla-manifest.json (exit 2 when invalid)doctor Check readiness of BLA engines & agent registrationsupdate Update global bla-shield installation & print setup refresh instructionsuninstall Remove BLA MCP configuration & skill files@@ -89,6 +91,7 @@ Options:--manifest Emit .bla-manifest.json next to protected artifacts (opt-in; unsigned by default)--sign-manifest Ed25519-sign the emitted manifest (implies --manifest; requires --manifest-key)--manifest-key <path> Ed25519 private key PEM file used by --sign-manifest+ --public-key <path> Trusted ed25519 public key PEM for verify-manifest--llm-directives Opt-in risky LLM directive layers (COPPA/medical bait, dual-use imitation, AI chat markers); OFF by default--weave-decoys Opt-in structural weave decoys (Python customer-def / .pyc co_consts weave; JS AST weave already on); OFF by default--self-seal Opt-in self-seal / sibling-module integrity binding for JS/Node targets (issue #40.5); OFF by default@@ -121,6 +124,7 @@ interface ParsedCliArgs {readonly manifest: boolean;readonly signManifest: boolean;readonly manifestKey?: string;+ readonly publicKey?: string;readonly llmDirectives: boolean;readonly weaveDecoys: boolean;readonly selfSeal: boolean;@@ -158,6 +162,7 @@ export function parseCliArgs(argv: readonly string[]): ParsedCliArgs {let manifest = false;let signManifest = false;let manifestKey: string | undefined;+ let publicKey: string | undefined;let llmDirectives = false;let weaveDecoys = false;let selfSeal = false;@@ -209,6 +214,8 @@ export function parseCliArgs(argv: readonly string[]): ParsedCliArgs {signManifest = true;} else if (arg === "--manifest-key" && i + 1 < argv.length) {manifestKey = argv[++i]!;+ } else if (arg === "--public-key" && i + 1 < argv.length) {+ publicKey = argv[++i]!;} else if (arg === "--llm-directives") {llmDirectives = true;} else if (arg === "--weave-decoys") {@@ -286,6 +293,7 @@ export function parseCliArgs(argv: readonly string[]): ParsedCliArgs {manifest,signManifest,...(manifestKey !== undefined ? { manifestKey } : {}),+ ...(publicKey !== undefined ? { publicKey } : {}),llmDirectives,weaveDecoys,selfSeal,@@ -907,6 +915,21 @@ export async function runCli(argv: readonly string[] = process.argv.slice(2)): Preturn 0;}+ case "verify-manifest": {+ const target = parsed.positionals[0];+ if (!target) {+ console.error("Error: Missing <manifest> path for `bla verify-manifest <manifest> [--public-key <pem>]`.");+ return 1;+ }+ const result = verifyManifestFile(target, parsed.publicKey);+ if (parsed.json) {+ console.log(JSON.stringify(result, null, 2));+ } else {+ console.log(`${result.ok ? "VALID" : "INVALID"}: ${result.reason}`);+ }+ return result.ok ? 0 : 2;+ }+default:console.error(`Unknown command: ${parsed.command}. Run "bla --help" for available commands.`);return 1;diff --git a/src/core/manifest-verify.ts b/src/core/manifest-verify.tsnew file mode 100644index 0000000..e7429be--- /dev/null+++ b/src/core/manifest-verify.ts@@ -0,0 +1,90 @@+import { createPublicKey } from "node:crypto";+import { readFileSync } from "node:fs";+import { resolve } from "node:path";+import { verifyManifestSignature } from "./manifest-signing.js";++export interface ManifestVerifyResult {+ /** Signature bytes are valid for the payload. */+ readonly ok: boolean;+ /** A signature object is present. */+ readonly signed: boolean;+ /** The caller supplied a trusted public key that matched the embedded key. */+ readonly pinned: boolean;+ readonly reason: string;+}++function normalizePublicKeyPem(pem: string): string {+ return createPublicKey(pem)+ .export({ type: "spki", format: "pem" })+ .toString("utf8");+}++/**+ * Verify a `.bla-manifest.json` file.+ *+ * Without `publicKeyPath`, this only proves the manifest was signed by the key+ * embedded in it (an attacker can re-sign their own edits). Supply+ * `publicKeyPath` to pin the trusted key.+ */+export function verifyManifestFile(+ manifestPath: string,+ publicKeyPath?: string,+): ManifestVerifyResult {+ let manifest: Record<string, unknown>;+ try {+ const parsed: unknown = JSON.parse(readFileSync(resolve(manifestPath), "utf8"));+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {+ return { ok: false, signed: false, pinned: false, reason: "manifest must be a JSON object" };+ }+ manifest = parsed as Record<string, unknown>;+ } catch (err) {+ const message = err instanceof Error ? err.message : String(err);+ return { ok: false, signed: false, pinned: false, reason: `cannot read manifest: ${message}` };+ }++ const rawSignature = manifest.signature;+ const signed =+ rawSignature !== undefined &&+ rawSignature !== null &&+ typeof rawSignature === "object" &&+ typeof (rawSignature as { value?: unknown }).value === "string";+ if (!signed) {+ return { ok: false, signed: false, pinned: false, reason: "manifest is unsigned" };+ }++ let pinned = false;+ if (publicKeyPath !== undefined) {+ const embedded = (rawSignature as { publicKey?: unknown }).publicKey;+ if (typeof embedded !== "string") {+ return { ok: false, signed: true, pinned: false, reason: "manifest signature has no public key" };+ }+ try {+ const expected = normalizePublicKeyPem(readFileSync(resolve(publicKeyPath), "utf8"));+ const actual = normalizePublicKeyPem(embedded);+ pinned = true;+ if (expected !== actual) {+ return {+ ok: false,+ signed: true,+ pinned: true,+ reason: "manifest public key does not match the pinned --public-key",+ };+ }+ } catch (err) {+ const message = err instanceof Error ? err.message : String(err);+ return { ok: false, signed: true, pinned: false, reason: `cannot read pinned public key: ${message}` };+ }+ }++ const ok = verifyManifestSignature(manifest);+ return {+ ok,+ signed: true,+ pinned,+ reason: ok+ ? pinned+ ? "signature valid (pinned key)"+ : "signature valid (embedded key only — pass --public-key to authenticate)"+ : "signature invalid",+ };+}\ No newline at end of filediff --git a/src/index.ts b/src/index.tsindex fb0fc1b..b4fc339 100644--- a/src/index.ts+++ b/src/index.ts@@ -296,6 +296,10 @@ export {type ManifestSignature,type ManifestSigningKeyPair,} from "./core/manifest-signing.js";+export {+ verifyManifestFile,+ type ManifestVerifyResult,+} from "./core/manifest-verify.js";export {resolveSupportedAgentClients,detectInstalledClients,diff --git a/tests/verify-manifest-command.test.ts b/tests/verify-manifest-command.test.tsnew file mode 100644index 0000000..21261e4--- /dev/null+++ b/tests/verify-manifest-command.test.ts@@ -0,0 +1,87 @@+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it } from "vitest";+import { generateManifestSigningKeyPair, signManifestPayload } from "../src/core/manifest-signing.js";+import { verifyManifestFile } from "../src/core/manifest-verify.js";+import { runCli } from "../src/cli.js";++const tempDirs: string[] = [];+function createTempDir(): string {+ const dir = mkdtempSync(join(tmpdir(), "bla-verify-manifest-"));+ tempDirs.push(dir);+ return dir;+}++afterEach(() => {+ for (const dir of tempDirs.splice(0)) {+ rmSync(dir, { recursive: true, force: true });+ }+});++describe("bla verify-manifest", () => {+ it("verifies a signed manifest with a pinned public key", () => {+ const dir = createTempDir();+ const pair = generateManifestSigningKeyPair();+ const manifestPath = join(dir, "manifest.json");+ const publicKeyPath = join(dir, "public.pem");+ const signed = signManifestPayload({ schema: "s", blaVersion: "2.1.0" }, pair.privateKeyPem);+ writeFileSync(manifestPath, JSON.stringify(signed, null, 2), "utf8");+ writeFileSync(publicKeyPath, pair.publicKeyPem, "utf8");++ const result = verifyManifestFile(manifestPath, publicKeyPath);+ expect(result).toMatchObject({ ok: true, signed: true, pinned: true });+ });++ it("rejects a manifest signed by a different key than the pinned one", () => {+ const dir = createTempDir();+ const trusted = generateManifestSigningKeyPair();+ const attacker = generateManifestSigningKeyPair();+ const manifestPath = join(dir, "manifest.json");+ const publicKeyPath = join(dir, "trusted.pem");+ writeFileSync(+ manifestPath,+ JSON.stringify(signManifestPayload({ a: 1 }, attacker.privateKeyPem)),+ "utf8",+ );+ writeFileSync(publicKeyPath, trusted.publicKeyPem, "utf8");++ const result = verifyManifestFile(manifestPath, publicKeyPath);+ expect(result.ok).toBe(false);+ expect(result.reason).toMatch(/does not match/);+ });++ it("rejects tampered and unsigned manifests", () => {+ const dir = createTempDir();+ const pair = generateManifestSigningKeyPair();+ const signed = signManifestPayload({ a: 1 }, pair.privateKeyPem);+ const tamperedPath = join(dir, "tampered.json");+ writeFileSync(tamperedPath, JSON.stringify({ ...signed, a: 2 }), "utf8");+ expect(verifyManifestFile(tamperedPath).ok).toBe(false);++ const unsignedPath = join(dir, "unsigned.json");+ writeFileSync(unsignedPath, JSON.stringify({ a: 1 }), "utf8");+ const unsigned = verifyManifestFile(unsignedPath);+ expect(unsigned).toMatchObject({ ok: false, signed: false });+ expect(unsigned.reason).toMatch(/unsigned/);+ });++ it("returns exit 0 for a valid manifest and 2 for an invalid one", async () => {+ const dir = createTempDir();+ const pair = generateManifestSigningKeyPair();+ const manifestPath = join(dir, "manifest.json");+ const publicKeyPath = join(dir, "public.pem");+ writeFileSync(+ manifestPath,+ JSON.stringify(signManifestPayload({ a: 1 }, pair.privateKeyPem)),+ "utf8",+ );+ writeFileSync(publicKeyPath, pair.publicKeyPem, "utf8");++ await expect(+ runCli(["verify-manifest", manifestPath, "--public-key", publicKeyPath, "--json"]),+ ).resolves.toBe(0);+ writeFileSync(manifestPath, JSON.stringify({ a: 1 }), "utf8");+ await expect(runCli(["verify-manifest", manifestPath, "--json"])).resolves.toBe(2);+ });+});\ No newline at end of file