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(manifest): pin verification to a trusted key and canonicalize JSON

DeryFerdd70f53e157d3
diff --git a/src/core/manifest-signing.ts b/src/core/manifest-signing.ts
index 47c50e2..965ca14 100644
--- a/src/core/manifest-signing.ts
+++ b/src/core/manifest-signing.ts
@@ -26,6 +26,22 @@ export interface ManifestSignature {
readonly publicKey: string;
/** base64 ed25519 signature over the canonical payload JSON. */
readonly value: string;
+ /**
+ * Canonicalization used for `value`. `"sorted-keys-v1"` sorts object keys
+ * recursively so a re-serialized manifest still verifies. Older manifests
+ * without this field are verified using insertion-order JSON (legacy).
+ */
+ readonly canonicalization?: "sorted-keys-v1";
+}
+
+/** Deterministic JSON with recursively sorted object keys (arrays keep order). */
+export function canonicalManifestJson(value: unknown): string {
+ if (value === null || typeof value !== "object") return JSON.stringify(value) ?? "null";
+ if (Array.isArray(value)) return `[${value.map(canonicalManifestJson).join(",")}]`;
+ const entries = Object.entries(value as Record<string, unknown>)
+ .filter(([, v]) => v !== undefined)
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
+ return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalManifestJson(v)}`).join(",")}}`;
}
export interface ManifestSigningKeyPair {
@@ -52,23 +68,32 @@ export function signManifestPayload<T extends object>(
privateKeyPem: string,
): T & { readonly signature: ManifestSignature } {
const privateKey = createPrivateKey(privateKeyPem);
- const canonical = JSON.stringify(payload);
+ const canonical = canonicalManifestJson(payload);
const value = cryptoSign(null, Buffer.from(canonical, "utf8"), privateKey).toString("base64");
const publicKeyPem = createPublicKey(privateKey)
.export({ type: "spki", format: "pem" })
.toString("utf8");
return {
...payload,
- signature: { algorithm: "ed25519", publicKey: publicKeyPem, value },
+ signature: { algorithm: "ed25519", publicKey: publicKeyPem, value, canonicalization: "sorted-keys-v1" },
};
}
/**
* Verify a manifest object's embedded ed25519 `signature`.
- * Returns false for unsigned manifests, malformed signatures, or mismatches —
- * never throws.
+ *
+ * When `expectedPublicKeyPem` is supplied, the embedded key must be the same
+ * key (compared as normalized SPKI PEM). Without a pinned key, verification
+ * only proves that whoever produced the manifest also produced the signature —
+ * an attacker can re-sign a modified manifest with their own keypair.
+ *
+ * Returns false for unsigned manifests, malformed signatures, mismatches, or a
+ * pinned-key mismatch — never throws.
*/
-export function verifyManifestSignature(manifest: Record<string, unknown>): boolean {
+export function verifyManifestSignature(
+ manifest: Record<string, unknown>,
+ expectedPublicKeyPem?: string,
+): boolean {
const rawSignature = manifest.signature;
if (rawSignature === undefined || rawSignature === null || typeof rawSignature !== "object") {
return false;
@@ -83,8 +108,17 @@ export function verifyManifestSignature(manifest: Record<string, unknown>): bool
}
try {
const publicKey = createPublicKey(signature.publicKey);
+ if (expectedPublicKeyPem !== undefined) {
+ const pinned = createPublicKey(expectedPublicKeyPem);
+ const embeddedSpki = publicKey.export({ type: "spki", format: "pem" }).toString("utf8");
+ const pinnedSpki = pinned.export({ type: "spki", format: "pem" }).toString("utf8");
+ if (embeddedSpki !== pinnedSpki) return false;
+ }
const { signature: _ignored, ...rest } = manifest;
- const canonical = JSON.stringify(rest);
+ const canonical =
+ signature.canonicalization === "sorted-keys-v1"
+ ? canonicalManifestJson(rest)
+ : JSON.stringify(rest);
return cryptoVerify(null, Buffer.from(canonical, "utf8"), publicKey, Buffer.from(signature.value, "base64"));
} catch {
return false;
diff --git a/tests/manifest-signing.test.ts b/tests/manifest-signing.test.ts
index 0ad5499..8540deb 100644
--- a/tests/manifest-signing.test.ts
+++ b/tests/manifest-signing.test.ts
@@ -1,6 +1,7 @@
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
+import { createPrivateKey, sign as cryptoSign } from "node:crypto";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
generateManifestSigningKeyPair,
@@ -145,4 +146,51 @@ describe("manifest ed25519 signing", () => {
}),
).toThrow(/not readable/);
});
+
+ it("pins verification to an expected public key", () => {
+ const trusted = generateManifestSigningKeyPair();
+ const attacker = generateManifestSigningKeyPair();
+ const payload = { schema: "https://bla.tools/schemas/defense-manifest-v1.json", items: [1, 2] };
+
+ const trustedManifest = signManifestPayload(payload, trusted.privateKeyPem);
+ expect(verifyManifestSignature({ ...trustedManifest }, trusted.publicKeyPem)).toBe(true);
+ expect(verifyManifestSignature({ ...trustedManifest }, attacker.publicKeyPem)).toBe(false);
+
+ // A full re-sign with an attacker keypair must not pass a pinned check,
+ // even though the embedded key verifies its own signature.
+ const forged = signManifestPayload({ ...payload, items: [9, 9] }, attacker.privateKeyPem);
+ expect(verifyManifestSignature({ ...forged })).toBe(true);
+ expect(verifyManifestSignature({ ...forged }, trusted.publicKeyPem)).toBe(false);
+ });
+
+ it("verifies after object key reordering (sorted-key canonicalization)", () => {
+ const pair = generateManifestSigningKeyPair();
+ const signed = signManifestPayload(
+ { schema: "s", blaVersion: "2.1.0", nested: { z: 1, a: 2 } },
+ pair.privateKeyPem,
+ ) as Record<string, unknown>;
+ const reordered = {
+ nested: { a: 2, z: 1 },
+ blaVersion: "2.1.0",
+ schema: "s",
+ signature: signed.signature,
+ };
+ expect(verifyManifestSignature(reordered)).toBe(true);
+ });
+
+ it("still verifies legacy insertion-order signatures", () => {
+ const pair = generateManifestSigningKeyPair();
+ const privateKey = createPrivateKey(pair.privateKeyPem);
+ const payload = { a: 1, b: { c: 2 } };
+ const value = cryptoSign(
+ null,
+ Buffer.from(JSON.stringify(payload), "utf8"),
+ privateKey,
+ ).toString("base64");
+ const legacy = {
+ ...payload,
+ signature: { algorithm: "ed25519", publicKey: pair.publicKeyPem, value },
+ };
+ expect(verifyManifestSignature(legacy)).toBe(true);
+ });
});