From 474bb99a0a4317e5f563d690b3fcab1e4661ec97 Mon Sep 17 00:00:00 2001 From: DeryFerd Date: Tue, 17 Nov 2026 00:25:56 +0700 Subject: [PATCH] fix(harden): sanitize artifact-derived text in the harden report formatHardenProjectReport interpolated directory/runId/written/relativePath/ outputPath/error/discoveryReadErrors/oneSentenceUsage verbatim, so a hostile artifact name could carry OSC/CSI control sequences into the operator's terminal. Route every artifact-derived field through sanitizeTerminalText like the other formatters (extends the PR #223 posture to the harden report). --- src/engine/harden.ts | 27 ++++---- .../harden-report-terminal-injection.test.ts | 62 +++++++++++++++++++ 2 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 tests/harden-report-terminal-injection.test.ts diff --git a/src/engine/harden.ts b/src/engine/harden.ts index b4e0b58..4887a1b 100644 --- a/src/engine/harden.ts +++ b/src/engine/harden.ts @@ -9,6 +9,7 @@ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "nod import { detectArtifactFormat } from "../core/detector.js"; import { generateProjectSeed, loadBlaConfig, type BlaProjectConfig } from "../core/config.js"; import { physicalPath } from "../core/output-path.js"; +import { sanitizeTerminalText } from "../core/terminal-text.js"; import { walkBounded } from "../core/walk.js"; import type { ArtifactFormat, ProtectionIntensity } from "../core/types.js"; import { BLA_IDENTITY } from "../identity.js"; @@ -1833,9 +1834,13 @@ export function hardenProject( } export function formatHardenProjectReport(report: HardenProjectReport): string { + // Artifact-derived strings (paths, errors, usage advice) pass through the + // shared terminal sanitizer like every other formatter — a hostile name + // must not carry OSC/CSI/C0 bytes into the operator's terminal. + const safe = sanitizeTerminalText; const lines: string[] = [ - `BLA harden ${report.blaVersion} — ${report.directory}${report.dryRun ? " (dry-run: nothing written)" : ""}`, - `Run: ${report.runId} (unsigned)`, + `BLA harden ${report.blaVersion} — ${safe(report.directory)}${report.dryRun ? " (dry-run: nothing written)" : ""}`, + `Run: ${safe(report.runId)} (unsigned)`, `Intensity: ${report.intensity} | discovered=${report.discoveredCount} hardened=${report.hardenedCount} failed=${report.failedCount}`, `Overall: ${ report.dryRun @@ -1856,30 +1861,30 @@ export function formatHardenProjectReport(report: HardenProjectReport): string { ` ⚠ cancelled after ${(report.written ?? []).length} written artifact(s); list is authoritative — no silent half-harden`, ); for (const w of report.written ?? []) { - lines.push(` · written: ${w}`); + lines.push(` · written: ${safe(w)}`); } } if (report.selfSealRecommended) { lines.push( - ` ⚠ self_seal recommended: ${report.selfSealRecommendation ?? "multi-artifact tree — pass --self-seal / self_seal:true"}`, + ` ⚠ self_seal recommended: ${report.selfSealRecommendation === undefined ? "multi-artifact tree — pass --self-seal / self_seal:true" : safe(report.selfSealRecommendation)}`, ); } if (report.sidecarEntangleError !== undefined) { lines.push( - ` ⚠ sidecar entanglement failed: ${report.sidecarEntangleError} — entry shipped WITHOUT the strip⇒fail binding`, + ` ⚠ sidecar entanglement failed: ${safe(report.sidecarEntangleError)} — entry shipped WITHOUT the strip⇒fail binding`, ); } for (const a of report.artifacts) { if (report.dryRun) { lines.push( - ` • ${a.relativePath} [${a.format}] → ${a.outputPath}${a.outputExisted ? " (exists; would overwrite)" : ""}`, + ` • ${safe(a.relativePath)} [${a.format}] → ${safe(a.outputPath)}${a.outputExisted ? " (exists; would overwrite)" : ""}`, ); continue; } lines.push( a.success - ? ` ✓ ${a.relativePath} [${a.format}] score=${a.defenseScore} tokens×${a.tokenAmplificationFactor.toFixed(1)} → ${a.outputPath}` - : ` ✗ ${a.relativePath} [${a.format}] ${a.error ?? "failed"}`, + ? ` ✓ ${safe(a.relativePath)} [${a.format}] score=${a.defenseScore} tokens×${a.tokenAmplificationFactor.toFixed(1)} → ${safe(a.outputPath)}` + : ` ✗ ${safe(a.relativePath)} [${a.format}] ${a.error === undefined ? "failed" : safe(a.error)}`, ); } if (report.truncated) { @@ -1892,7 +1897,7 @@ export function formatHardenProjectReport(report: HardenProjectReport): string { ` ⚠ discovery read errors (${report.discoveryReadErrors.length}) — inventory may be incomplete:`, ); for (const entry of report.discoveryReadErrors.slice(0, 5)) { - lines.push(` - ${entry}`); + lines.push(` - ${safe(entry)}`); } if (report.discoveryReadErrors.length > 5) { lines.push(` … ${report.discoveryReadErrors.length - 5} more`); @@ -1903,13 +1908,13 @@ export function formatHardenProjectReport(report: HardenProjectReport): string { " (no protectable artifacts found — put build outputs under dist/, build/, out/, or release/, or place .wasm/.sqlite/.safetensors/binaries at the project root)", ); } else { - lines.push(`Output: ${report.outputDirectory}`); + lines.push(`Output: ${safe(report.outputDirectory)}`); lines.push( report.dryRun ? "Next: re-run without --dry-run (MCP: dry_run=false) to write the hardened copies." : "Next: ship or point your release at the hardened copies above (originals are untouched). See DISCLAIMER.md.", ); } - lines.push(`Tip: ${report.oneSentenceUsage}`); + lines.push(`Tip: ${safe(report.oneSentenceUsage)}`); return lines.join("\n"); } diff --git a/tests/harden-report-terminal-injection.test.ts b/tests/harden-report-terminal-injection.test.ts new file mode 100644 index 0000000..83d28fb --- /dev/null +++ b/tests/harden-report-terminal-injection.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from "vitest"; +import { + formatHardenProjectReport, + type HardenProjectReport, +} from "../src/engine/harden.js"; + +/** Minimal valid report with hostile (OSC-bearing) artifact-derived strings. */ +function hostileReport(): HardenProjectReport { + const osc = "\u001b]0;pwned\u0007"; + return { + blaVersion: "test", + directory: `${osc}/project`, + intensity: "bankrupt", + overallSuccess: false, + discoveredCount: 1, + hardenedCount: 0, + failedCount: 1, + outputDirectory: `${osc}/project/.bla-hardened`, + artifacts: [ + { + inputPath: `${osc}/project/app.js`, + relativePath: `app${osc}.js`, + format: "javascript", + outputPath: `${osc}/project/.bla-hardened/app.js`, + success: false, + defenseScore: 0, + tokenAmplificationFactor: 1, + costAmplificationFactor: 1, + outputExisted: false, + error: `refused: hostile name ${osc}`, + }, + ], + oneSentenceUsage: `run bla harden ${osc} to finish`, + weaveDecoys: false, + selfSeal: false, + sidecarEntangle: false, + selfSealRecommended: false, + dryRun: false, + truncated: false, + maxArtifacts: 100, + wouldOverwrite: [], + discoveryReadErrors: [`unreadable${osc}:EACCES`], + runId: "run-0000", + cancelled: true, + written: [`${osc}/project/.bla-hardened/other.js`], + sidecarEntangleError: `bind failed for ${osc}`, + }; +} + +describe("harden report terminal hygiene", () => { + it("does not forward control characters from artifact-derived strings", () => { + const text = formatHardenProjectReport(hostileReport()); + expect(text).not.toContain("\u001b]0;"); + expect(text).not.toContain("\u0007"); + expect(text).not.toContain("\u001b"); + // The readable residue stays so operators still see the path shape. + expect(text).toContain("pwned"); + expect(text).toContain("app"); + expect(text).toContain(".js"); + expect(text).toContain("Output:"); + }); +});