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(self-seal): keep opaque seed before top-level const uses

qybaihe36b1641a26b6
diff --git a/src/shields/javascript/ast-shield.ts b/src/shields/javascript/ast-shield.ts
index 3416178..6f3ab12 100644
--- a/src/shields/javascript/ast-shield.ts
+++ b/src/shields/javascript/ast-shield.ts
@@ -1277,11 +1277,26 @@ export function transformJavaScriptWithBlaShield(
break;
}
- const [corpusDecl, digestFnDecl, seedDecl, heapDecl, ...restInjected] =
- sanitizedInjectedStatements;
- const topDecls = [corpusDecl, digestFnDecl, seedDecl, heapDecl].filter(
- (s): s is t.Statement => s !== undefined,
- );
+ // Prepend every injected statement through the opaque-predicate seed decl
+ // (cognitive armor + optional self-seal preamble + seed). Self-seal inserts
+ // extra statements before the seed; a fixed 4-stmt split pushed the seed to
+ // the program end and caused TDZ crashes when top-level consts referenced it
+ // (red-blue round-4). Also keep the immediate following shadow-heap decl up top.
+ const seedInjectIndex = sanitizedInjectedStatements.findIndex((stmt) => {
+ if (!t.isVariableDeclaration(stmt)) return false;
+ return stmt.declarations.some(
+ (d) => t.isIdentifier(d.id) && d.id.name === seedVarName,
+ );
+ });
+ const topEnd =
+ seedInjectIndex >= 0
+ ? Math.min(
+ sanitizedInjectedStatements.length,
+ seedInjectIndex + 1 /* seed */ + 1 /* shadow heap usually follows */,
+ )
+ : Math.min(4, sanitizedInjectedStatements.length);
+ const topDecls = sanitizedInjectedStatements.slice(0, topEnd);
+ const restInjected = sanitizedInjectedStatements.slice(topEnd);
if (topDecls.length > 0) {
ast.program.body.splice(insertIndex, 0, ...topDecls);
}
diff --git a/tests/self-seal.test.ts b/tests/self-seal.test.ts
index f8d2310..6d5f7a7 100644
--- a/tests/self-seal.test.ts
+++ b/tests/self-seal.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import { mkdtempSync, writeFileSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
+import { spawnSync } from "node:child_process";
import { SeededPrng } from "../src/core/prng.js";
import {
generateSelfSealSnippet,
@@ -56,6 +57,49 @@ describe("self-seal helper (#40.5)", () => {
expect(c).not.toBe(a);
});
+ it("self-seal keeps opaque seed before top-level const uses (no TDZ)", () => {
+ // Bankrupt-style string splitting references seedVar in top-level consts.
+ // Self-seal used to push the seed decl to the program end → TDZ ReferenceError.
+ const src = [
+ '"use strict";',
+ 'const HONEYPOT = Object.freeze(["P4#kR9$vM2@xL7!q", "W9$mK3#pT7!vQ2@z"]);',
+ 'const LABEL = "bla-redblue-r4|wrap";',
+ 'function main(k) { return HONEYPOT.includes(k) ? "hp" : LABEL + ":" + String(k).length; }',
+ 'module.exports = { main, HONEYPOT };',
+ 'if (require.main === module) console.log(main(process.argv[2] || ""));',
+ "",
+ ].join("\n");
+ const on = transformJavaScriptWithBlaShield(src, {
+ intensity: "bankrupt",
+ seed: "self-seal-tdz-r4",
+ selfSeal: true,
+ });
+ expect(on.selfSealEnabled).toBe(true);
+ expect(on.selfSealBindingName).toBeTruthy();
+ // Seed const must appear before the first top-level Object.freeze / LABEL split use.
+ const seedDecl = on.code.match(/const\s+([A-Za-z_$][\w$]*)\s*=\s*(?:\(\(\d+|\d+\s*\^)/);
+ expect(seedDecl).toBeTruthy();
+ const seedName = seedDecl![1]!;
+ const seedPos = on.code.indexOf(seedDecl![0]!);
+ const freezePos = on.code.indexOf("Object.freeze");
+ expect(seedPos).toBeGreaterThanOrEqual(0);
+ if (freezePos >= 0) {
+ expect(seedPos).toBeLessThan(freezePos);
+ }
+
+ const dir = mkdtempSync(join(tmpdir(), "bla-selfseal-tdz-"));
+ try {
+ const file = join(dir, "gate.cjs");
+ writeFileSync(file, on.code, "utf8");
+ const run = spawnSync(process.execPath, [file, "abc"], { encoding: "utf8" });
+ expect(run.status, run.stderr).toBe(0);
+ expect(run.stderr || "").not.toMatch(/before initialization/i);
+ expect(run.stdout).toMatch(/bla-redblue-r4\|wrap:3/);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it("AST shield injects self-seal only when opted in", () => {
const src = "export function pay(n) { return n * 2; }\n";
const off = transformJavaScriptWithBlaShield(src, {