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.

feat(core): add assertSafeOutputPath admission checks for file and directory protection

qybaiheb97409318b37
diff --git a/src/core/output-path.ts b/src/core/output-path.ts
new file mode 100644
index 0000000..fa0c7b3
--- /dev/null
+++ b/src/core/output-path.ts
@@ -0,0 +1,59 @@
+import { lstatSync, realpathSync, statSync } from "node:fs";
+import { basename, dirname, isAbsolute, join, relative, sep } from "node:path";
+
+function exists(path: string): boolean {
+ try {
+ lstatSync(path);
+ return true;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
+ throw error;
+ }
+}
+
+function physicalPath(path: string): string {
+ const suffix: string[] = [];
+ let ancestor = path;
+ while (!exists(ancestor)) {
+ suffix.unshift(basename(ancestor));
+ const parent = dirname(ancestor);
+ if (parent === ancestor) throw new Error(`Cannot resolve output path: ${path}`);
+ ancestor = parent;
+ }
+ return join(realpathSync(ancestor), ...suffix);
+}
+
+/** Admission checks before any output is created. */
+export function assertSafeOutputPath(
+ input: string,
+ output: string,
+ directory: boolean,
+ inPlace = false,
+): void {
+ const physicalInput = realpathSync(input);
+ const physicalOutput = physicalPath(output);
+ if (directory) {
+ const rel = relative(physicalInput, physicalOutput);
+ const withinInput =
+ rel === "" || (!isAbsolute(rel) && rel !== ".." && !rel.startsWith(`..${sep}`));
+ if (inPlace || withinInput) {
+ throw new Error(
+ "Directory output must be outside the input directory; in-place directory protection is not supported.",
+ );
+ }
+ if (exists(output)) {
+ throw new Error("Directory output must be a new path; existing outputs are not overwritten.");
+ }
+ return;
+ }
+
+ let sameFile = physicalInput === physicalOutput;
+ if (!sameFile && exists(output)) {
+ const source = statSync(input);
+ const destination = statSync(output);
+ sameFile = source.dev === destination.dev && source.ino === destination.ino;
+ }
+ if (sameFile && !inPlace) {
+ throw new Error("Output refers to the input file; explicitly enable inPlace to overwrite it.");
+ }
+}