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(js): keep class/new rename consistent + deterministic opaque seed (#44)

Timo Zhangd69255104c61
diff --git a/src/shields/javascript/ast-shield.ts b/src/shields/javascript/ast-shield.ts
index 028bd80..e0b9dcf 100644
--- a/src/shields/javascript/ast-shield.ts
+++ b/src/shields/javascript/ast-shield.ts
@@ -909,12 +909,16 @@ export function transformJavaScriptWithBlaShield(
// 2b. Rename non-exported local bindings (including function parameters and private helpers)
- // so original identifiers are not left pristine
+ // so original identifiers are not left pristine.
+ // Only rename bindings owned by the current scope. Babel mirrors ClassDeclaration
+ // names onto the class scope with the SAME binding object; renaming there a second
+ // time with a fresh PRNG name desyncs `class Foo` from `new Foo()` (issue #40).
const exportedNameSet = new Set(exportedSymbolsBefore);
const generatedNames = new Set<string>();
traverse(ast, {
Scope(path) {
for (const [name, binding] of Object.entries(path.scope.bindings)) {
+ if (binding.scope !== path.scope) continue;
if (RENAME_RESERVED.has(name)) continue;
if (exportedNameSet.has(name)) continue;
if (binding.kind === "module") continue;
@@ -1069,8 +1073,11 @@ export function transformJavaScriptWithBlaShield(
// 4. Build Decoy IPC Ambiguity & Route Mirage block + Cognitive-Armor seed entanglement
const mirageLines: string[] = [];
mirageLines.push(deceptionSuite.cognitiveArmor.code);
+ // Deterministic opaque-predicate seed (issue #40): never hrtime/Date.now.
+ // Keep the low bit entangled with cognitive-armor digest so tamper flips seed parity.
+ const opaqueSeedBase = (prng.nextInt(1, 0x1ffff) << 2) & 0x7fffc;
mirageLines.push(
- `const ${seedVarName} = (((typeof process !== "undefined" && typeof process?.hrtime?.bigint === "function" ? Number(process.hrtime.bigint() & 0x7fffcn) : (typeof Date !== "undefined" ? (Date.now() & 0x7fffc) : 4)) & 0x7fffc) | ((typeof ${deceptionSuite.cognitiveArmor.digestFunctionName} === "function" && ${deceptionSuite.cognitiveArmor.digestFunctionName}(0) === ${deceptionSuite.cognitiveArmor.expectedDigest}) ? 1 : 0));`,
+ `const ${seedVarName} = ((${opaqueSeedBase}) | ((typeof ${deceptionSuite.cognitiveArmor.digestFunctionName} === "function" && ${deceptionSuite.cognitiveArmor.digestFunctionName}(0) === ${deceptionSuite.cognitiveArmor.expectedDigest}) ? 1 : 0));`,
);
mirageLines.push(memoryDiffShield.code);
const channelsToMirror =
diff --git a/tests/class-rename-opaque-seed.test.ts b/tests/class-rename-opaque-seed.test.ts
new file mode 100644
index 0000000..b088e7a
--- /dev/null
+++ b/tests/class-rename-opaque-seed.test.ts
@@ -0,0 +1,132 @@
+import { describe, expect, it } from "vitest";
+import * as vm from "node:vm";
+import { transformJavaScriptWithBlaShield } from "../src/shields/javascript/ast-shield.js";
+
+function runCommonJs<T extends Record<string, unknown>>(
+ code: string,
+): T {
+ const mod = { exports: {} as T };
+ vm.runInContext(
+ code,
+ vm.createContext({
+ module: mod,
+ exports: mod.exports,
+ Math,
+ Object,
+ Array,
+ String,
+ Number,
+ Boolean,
+ Map,
+ Set,
+ WeakMap,
+ WeakSet,
+ Promise,
+ Error,
+ TypeError,
+ RangeError,
+ JSON,
+ Date,
+ parseInt,
+ parseFloat,
+ isNaN,
+ isFinite,
+ undefined,
+ NaN,
+ Infinity,
+ console,
+ process: undefined,
+ Buffer: undefined,
+ require: undefined,
+ globalThis: {},
+ }),
+ { timeout: 5000 },
+ );
+ return mod.exports;
+}
+
+describe("Issue #40: class/new rename consistency + deterministic opaque seed", () => {
+ it("keeps ClassDeclaration name in sync with new/identifier refs at bankrupt", () => {
+ const source = `
+ class Foo {
+ value() { return 42; }
+ static self() { return Foo; }
+ }
+ function make() {
+ return new Foo().value();
+ }
+ function sameRef() {
+ return Foo.self() === Foo;
+ }
+ module.exports = { make, sameRef };
+ `;
+ const result = transformJavaScriptWithBlaShield(source, {
+ intensity: "bankrupt",
+ seed: "issue-40-class-rename",
+ sourceType: "commonjs",
+ });
+
+ expect(result.code).not.toMatch(/\bclass\s+Foo\b/);
+ expect(result.code).not.toMatch(/\bnew\s+Foo\b/);
+ expect(result.code).not.toMatch(/hrtime/);
+ expect(result.code).not.toMatch(/Date\.now\s*\(/);
+
+ const orig = runCommonJs<{ make: () => number; sameRef: () => boolean }>(source);
+ const prot = runCommonJs<{ make: () => number; sameRef: () => boolean }>(
+ result.code,
+ );
+ expect(prot.make()).toBe(orig.make());
+ expect(prot.sameRef()).toBe(true);
+ });
+
+ it("keeps nested ClassDeclaration usable via new after rename", () => {
+ const source = `
+ function factory(n) {
+ class Bar {
+ constructor(x) { this.x = x; }
+ twice() { return this.x * 2; }
+ }
+ return new Bar(n).twice();
+ }
+ module.exports = { factory };
+ `;
+ const result = transformJavaScriptWithBlaShield(source, {
+ intensity: "bankrupt",
+ seed: "issue-40-nested-class",
+ sourceType: "commonjs",
+ });
+ const orig = runCommonJs<{ factory: (n: number) => number }>(source);
+ const prot = runCommonJs<{ factory: (n: number) => number }>(result.code);
+ for (const n of [0, 1, 7, 99]) {
+ expect(prot.factory(n)).toBe(orig.factory(n));
+ }
+ });
+
+ it("emits a deterministic opaque-predicate seed for the same harden seed", () => {
+ const source = `
+ function id(x) { return x ^ 1; }
+ module.exports = { id };
+ `;
+ const a = transformJavaScriptWithBlaShield(source, {
+ intensity: "maximum",
+ seed: "issue-40-opaque-seed-stable",
+ sourceType: "commonjs",
+ });
+ const b = transformJavaScriptWithBlaShield(source, {
+ intensity: "maximum",
+ seed: "issue-40-opaque-seed-stable",
+ sourceType: "commonjs",
+ });
+ expect(a.code).toBe(b.code);
+ expect(a.code).not.toMatch(/hrtime/);
+ expect(a.code).not.toMatch(/Date\.now\s*\(/);
+ // Seed initializer: deterministic numeric base OR'd with digest-check bit (Babel may drop parens).
+ expect(a.code).toMatch(
+ /const\s+\w+\s*=\s*\d+\s*\|\s*\(typeof\s+\w+\s*===\s*"function"/,
+ );
+
+ const orig = runCommonJs<{ id: (x: number) => number }>(source);
+ const prot = runCommonJs<{ id: (x: number) => number }>(a.code);
+ expect(prot.id(41)).toBe(orig.id(41));
+ });
+});