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).
This commit is contained in:
DeryFerd
2026-11-17 00:25:56 +07:00
parent de982cb7d3
commit 474bb99a0a
2 changed files with 78 additions and 11 deletions
+16 -11
View File
@@ -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");
}
@@ -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:");
});
});