🛡️ 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.
JavaScript adversarial-aiclaude-codecursorelectron-securityllm-securitymcpmcp-serverobfuscationreverse-engineeringwebassembly
feat(core): add bla.config.json project config loader and SARIF 2.1.0 security report generator
qybaihed921475d36d6
diff --git a/src/core/config.ts b/src/core/config.tsnew file mode 100644index 0000000..1651a9c--- /dev/null+++ b/src/core/config.ts@@ -0,0 +1,115 @@+import { existsSync, readFileSync, writeFileSync } from "node:fs";+import { join, resolve } from "node:path";+import type { ProtectionIntensity } from "./types.js";++export interface BlaProjectConfig {+ readonly $schema?: string;+ readonly input?: string;+ readonly output?: string;+ readonly intensity?: ProtectionIntensity;+ readonly seed?: string;+ readonly emitManifest?: boolean;+ readonly honeySourceMaps?: boolean;+ readonly minDefenseScore?: number;+}++export const DEFAULT_BLA_CONFIG: BlaProjectConfig = Object.freeze({+ $schema: "https://bla.tools/schemas/bla.config.v1.json",+ input: "./dist",+ intensity: "bankrupt",+ seed: "bla-project-seed-v1",+ emitManifest: true,+ honeySourceMaps: true,+ minDefenseScore: 90,+});++const CONFIG_FILENAMES = ["bla.config.json", ".blarc.json", ".blarc"] as const;++/**+ * Loads `bla.config.json`, `.blarc.json`, or the `"bla"` field from `package.json`+ * in `cwd` (or an explicit config file path).+ */+export function loadBlaConfig(+ cwd: string = process.cwd(),+ explicitConfigPath?: string,+): { config: BlaProjectConfig; sourcePath: string | null } {+ const root = resolve(cwd);++ if (explicitConfigPath) {+ const absExplicit = resolve(root, explicitConfigPath);+ if (!existsSync(absExplicit)) {+ throw new Error(`BLA config file not found: ${absExplicit}`);+ }+ const parsed = JSON.parse(readFileSync(absExplicit, "utf8")) as BlaProjectConfig;+ return {+ config: { ...DEFAULT_BLA_CONFIG, ...parsed },+ sourcePath: absExplicit,+ };+ }++ for (const name of CONFIG_FILENAMES) {+ const candidate = join(root, name);+ if (existsSync(candidate)) {+ try {+ const parsed = JSON.parse(readFileSync(candidate, "utf8")) as BlaProjectConfig;+ return {+ config: { ...DEFAULT_BLA_CONFIG, ...parsed },+ sourcePath: candidate,+ };+ } catch {+ // ignore malformed optional config+ }+ }+ }++ const pkgPath = join(root, "package.json");+ if (existsSync(pkgPath)) {+ try {+ const pkg = JSON.parse(readFileSync(pkgPath, "utf8")) as {+ bla?: BlaProjectConfig;+ };+ if (pkg.bla && typeof pkg.bla === "object") {+ return {+ config: { ...DEFAULT_BLA_CONFIG, ...pkg.bla },+ sourcePath: pkgPath,+ };+ }+ } catch {+ // ignore+ }+ }++ return {+ config: DEFAULT_BLA_CONFIG,+ sourcePath: null,+ };+}++/**+ * Writes a starter `bla.config.json` in `targetDir`.+ */+export function initBlaConfig(+ targetDir: string = process.cwd(),+ overwrite = false,+): { configPath: string; created: boolean; config: BlaProjectConfig } {+ const configPath = join(resolve(targetDir), "bla.config.json");+ if (existsSync(configPath) && !overwrite) {+ const existing = JSON.parse(readFileSync(configPath, "utf8")) as BlaProjectConfig;+ return {+ configPath,+ created: false,+ config: { ...DEFAULT_BLA_CONFIG, ...existing },+ };+ }++ writeFileSync(+ configPath,+ JSON.stringify(DEFAULT_BLA_CONFIG, null, 2) + "\n",+ "utf8",+ );+ return {+ configPath,+ created: true,+ config: DEFAULT_BLA_CONFIG,+ };+}diff --git a/src/core/sarif.ts b/src/core/sarif.tsnew file mode 100644index 0000000..c39b50d--- /dev/null+++ b/src/core/sarif.ts@@ -0,0 +1,151 @@+import { BLA_IDENTITY } from "../identity.js";+import type { AuditReport } from "./types.js";+import type { ReleaseExposureReport } from "./release-exposure.js";++export interface SarifLog {+ readonly $schema: string;+ readonly version: "2.1.0";+ readonly runs: readonly [+ {+ readonly tool: {+ readonly driver: {+ readonly name: string;+ readonly fullName: string;+ readonly version: string;+ readonly informationUri: string;+ readonly rules: readonly {+ readonly id: string;+ readonly name: string;+ readonly shortDescription: { readonly text: string };+ readonly defaultConfiguration: {+ readonly level: "error" | "warning" | "note";+ };+ }[];+ };+ };+ readonly results: readonly {+ readonly ruleId: string;+ readonly level: "error" | "warning" | "note";+ readonly message: { readonly text: string };+ readonly locations: readonly [+ {+ readonly physicalLocation: {+ readonly artifactLocation: { readonly uri: string };+ readonly region?: { readonly startLine: number };+ };+ },+ ];+ }[];+ },+ ];+}++/**+ * Formats an `AuditReport` (and optional `ReleaseExposureReport`) as a SARIF 2.1.0 JSON document+ * compatible with GitHub Code Scanning (`github/codeql-action/upload-sarif`).+ */+export function buildSarifReport(+ audit: AuditReport,+ releaseExposure?: ReleaseExposureReport | null,+): SarifLog {+ const rulesMap = new Map<+ string,+ {+ id: string;+ name: string;+ shortDescription: { text: string };+ defaultConfiguration: { level: "error" | "warning" | "note" };+ }+ >();++ const results: SarifLog["runs"][0]["results"][number][] = [];++ for (const item of audit.exposedItems) {+ const ruleId = `BLA-AUDIT-${item.kind.toUpperCase()}`;+ const level: "error" | "warning" | "note" =+ item.riskLevel === "critical"+ ? "error"+ : item.riskLevel === "high"+ ? "warning"+ : "note";++ if (!rulesMap.has(ruleId)) {+ rulesMap.set(ruleId, {+ id: ruleId,+ name: `ExposedArtifact_${item.kind}`,+ shortDescription: {+ text: `Artifact surface (${item.kind}) exposed to automated reverse-engineering tool (${item.reaToolUsed})`,+ },+ defaultConfiguration: { level },+ });+ }++ const [uriPart, linePart] = item.location.split(":");+ const startLine = linePart ? Number.parseInt(linePart, 10) : undefined;++ results.push({+ ruleId,+ level,+ message: {+ text: `${item.name}: ${item.reason} (Est. reconstruction: ${item.estimatedTokensToReconstruct} tokens / $${item.estimatedCostUsd.toFixed(2)})`,+ },+ locations: [+ {+ physicalLocation: {+ artifactLocation: { uri: uriPart || audit.targetPath },+ ...(startLine && Number.isFinite(startLine) ? { region: { startLine } } : {}),+ },+ },+ ],+ });+ }++ if (releaseExposure) {+ for (const f of releaseExposure.findings) {+ const ruleId = `BLA-RELEASE-${f.kind.toUpperCase()}`;+ const level: "error" | "warning" | "note" =+ f.kind === "embedded-source-text" ? "error" : "warning";+ if (!rulesMap.has(ruleId)) {+ rulesMap.set(ruleId, {+ id: ruleId,+ name: `ReleaseExposure_${f.kind}`,+ shortDescription: { text: `Release source-map exposure (${f.kind}, ${f.representation})` },+ defaultConfiguration: { level },+ });+ }+ results.push({+ ruleId,+ level,+ message: {+ text: `${f.kind} (${f.representation}) in ${f.file} — Run "npx bla-agents defend" to replace with decoy source maps.`,+ },+ locations: [+ {+ physicalLocation: {+ artifactLocation: { uri: f.file },+ },+ },+ ],+ });+ }+ }++ return {+ $schema: "https://json.schemastore.org/sarif-2.1.0.json",+ version: "2.1.0",+ runs: [+ {+ tool: {+ driver: {+ name: BLA_IDENTITY.packageName,+ fullName: BLA_IDENTITY.fullName,+ version: BLA_IDENTITY.version,+ informationUri: "https://bla.tools",+ rules: [...rulesMap.values()],+ },+ },+ results,+ },+ ],+ };+}