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(archive): reject entry names that escape the archive root or alias the reserved namespace (#50)

Dery Ferdika7e51e422ca78
diff --git a/src/shields/formats/archive.ts b/src/shields/formats/archive.ts
index 5e910c6..00cda46 100644
--- a/src/shields/formats/archive.ts
+++ b/src/shields/formats/archive.ts
@@ -1,4 +1,5 @@
import { deflateRawSync, inflateRawSync } from "node:zlib";
+import { posix } from "node:path";
import { SeededPrng } from "../../core/prng.js";
import type { ProtectionIntensity } from "../../core/types.js";
import { generateChaffPayloads } from "../chaff/chaff-generator.js";
@@ -227,14 +228,31 @@ export function validateZipProtectionInput(
const reservedExact = options.allowUnsignedApk
? ["lib/arm64-v8a/libbla_enclave_jni.so"]
: [];
+ // Normalize every entry name before the reserved-prefix check so a
+ // traversal (`../evil`) or an aliased name (`./assets/bla-shield/x`) cannot
+ // escape the archive root or collide with the reserved output namespace.
+ const normalizedNames = existingEntries.map((e) => {
+ const normalized = posix.normalize(e.name);
+ if (
+ normalized.startsWith("/") ||
+ normalized.startsWith("../") ||
+ normalized === ".." ||
+ e.name.includes("\\")
+ ) {
+ throw new UnsupportedArchiveError(
+ `entry name resolves outside the archive root: ${e.name}`,
+ );
+ }
+ return normalized;
+ });
if (
- existingEntries.some(
- (e) =>
- reservedPrefixes.includes(e.name) ||
- reservedExact.includes(e.name) ||
- e.name.startsWith("assets/bla-shield/") ||
+ normalizedNames.some(
+ (name) =>
+ reservedPrefixes.includes(name) ||
+ reservedExact.includes(name) ||
+ name.startsWith("assets/bla-shield/") ||
(!options.allowUnsignedApk &&
- (e.name.startsWith("assets/enclave/") || e.name.startsWith("enclave/"))),
+ (name.startsWith("assets/enclave/") || name.startsWith("enclave/"))),
)
) {
throw new UnsupportedArchiveError("existing entries overlap the reserved output namespace");
diff --git a/tests/archive-entry-traversal.test.ts b/tests/archive-entry-traversal.test.ts
new file mode 100644
index 0000000..2dd4d0a
--- /dev/null
+++ b/tests/archive-entry-traversal.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from "vitest";
+import {
+ UnsupportedArchiveError,
+ validateZipProtectionInput,
+ writeZipArchive,
+} from "../src/shields/formats/archive.js";
+
+const data = Buffer.from("harmless content ".repeat(8));
+
+describe("archive entry-name traversal admission", () => {
+ const cases: [string, string][] = [
+ ["../evil.txt", "path traversal above the archive root"],
+ ["../../evil.txt", "deep path traversal above the archive root"],
+ ["/etc/passwd", "absolute path"],
+ ["./assets/bla-shield/x.js", "aliased reserved prefix (dot-slash)"],
+ ["assets/./bla-shield/x.js", "aliased reserved prefix (mid-path dot)"],
+ ["assets/bla-shield/x.js", "direct reserved prefix"],
+ ];
+
+ for (const [name, label] of cases) {
+ it(`rejects ${label}: ${name}`, () => {
+ const zip = writeZipArchive([{ name, data }]);
+ expect(() => validateZipProtectionInput(zip)).toThrow(UnsupportedArchiveError);
+ });
+ }
+
+ it("still accepts a normal entry name", () => {
+ const zip = writeZipArchive([{ name: "file.txt", data }]);
+ expect(() => validateZipProtectionInput(zip)).not.toThrow();
+ });
+});