@rafter-security/cli 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,7 @@ import os from "os";
8
8
  import { execSync } from "child_process";
9
9
  import { fileURLToPath } from "url";
10
10
  import { createRequire } from "module";
11
- import { createInterface } from "readline";
11
+ import { askYesNo } from "../../utils/prompt.js";
12
12
  import { fmt } from "../../utils/formatter.js";
13
13
  import { injectInstructionFile } from "./instruction-block.js";
14
14
  import yaml from "js-yaml";
@@ -920,20 +920,6 @@ function registerGeminiSkills(skillsDir) {
920
920
  }
921
921
  }
922
922
  }
923
- async function askYesNo(question, defaultYes = true) {
924
- const rl = createInterface({ input: process.stdin, output: process.stderr });
925
- const suffix = defaultYes ? "[Y/n]" : "[y/N]";
926
- return new Promise((resolve) => {
927
- rl.question(` ${question} ${suffix} `, (answer) => {
928
- rl.close();
929
- const trimmed = answer.trim().toLowerCase();
930
- if (trimmed === "")
931
- resolve(defaultYes);
932
- else
933
- resolve(trimmed === "y" || trimmed === "yes");
934
- });
935
- });
936
- }
937
923
  export function createInitCommand() {
938
924
  return new Command("init")
939
925
  .description("Initialize agent security system")
@@ -1,6 +1,9 @@
1
1
  import { Command } from "commander";
2
2
  import { RegexScanner } from "../../scanners/regex-scanner.js";
3
3
  import { BetterleaksScanner } from "../../scanners/betterleaks.js";
4
+ import { unionScanResults } from "../../scanners/union.js";
5
+ import { BinaryManager, BETTERLEAKS_VERSION } from "../../utils/binary-manager.js";
6
+ import { askYesNo } from "../../utils/prompt.js";
4
7
  import { ConfigManager } from "../../core/config-manager.js";
5
8
  import { AuditLogger } from "../../core/audit-logger.js";
6
9
  import { applySuppressions, loadSuppressions, policyIgnoreToSuppressions, } from "../../core/custom-patterns.js";
@@ -75,7 +78,7 @@ function pathMatchesExcludePattern(relPath, pattern) {
75
78
  * write `components/common/Mermaid.tsx` in their `.rafter.yml`, not the
76
79
  * absolute path on the CI runner.
77
80
  */
78
- function applyExcludePaths(results, excludePaths, scanRoot) {
81
+ export function applyExcludePaths(results, excludePaths, scanRoot) {
79
82
  if (!excludePaths || excludePaths.length === 0)
80
83
  return results;
81
84
  const root = path.resolve(scanRoot).replace(/\\/g, "/");
@@ -117,6 +120,7 @@ export function createScanCommand() {
117
120
  .option("--watch", "Watch for file changes and re-scan on change")
118
121
  .option("--history", "Scan git history for secrets (requires betterleaks engine)")
119
122
  .option("--no-gitignore", "Scan files even if .gitignore would exclude them (default: respect .gitignore)")
123
+ .option("--no-auto-update", "Do not auto-update a stale managed betterleaks binary; fall back to the patterns engine instead")
120
124
  .action(async (scanPath, opts) => {
121
125
  // Validate flags before doing any work.
122
126
  const validEngines = ["auto", "betterleaks", "patterns"];
@@ -166,7 +170,7 @@ export function createScanCommand() {
166
170
  return;
167
171
  }
168
172
  // Determine scan engine
169
- const engine = await selectEngine(opts.engine || "auto", opts.quiet || false);
173
+ const engine = await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
170
174
  // Determine if path is file or directory
171
175
  const stats = fs.statSync(resolvedPath);
172
176
  let results;
@@ -201,7 +205,7 @@ export function createSecretsCommand() {
201
205
  * Order matters — first match wins, and policy rules are checked first so an
202
206
  * explicit reason wins over a bare .rafterignore line covering the same finding.
203
207
  */
204
- function collectSuppressions(policyIgnore) {
208
+ export function collectSuppressions(policyIgnore) {
205
209
  return [...policyIgnoreToSuppressions(policyIgnore), ...loadSuppressions()];
206
210
  }
207
211
  /**
@@ -279,6 +283,9 @@ function outputScanResults(results, opts, context, exitOnFindings = true, suppre
279
283
  line: m.line ?? null,
280
284
  column: m.column ?? null,
281
285
  redacted: m.redacted || "",
286
+ // sable-j85 — present only for `auto`-mode (both-engine) scans, so
287
+ // consumers can tell which engine surfaced each finding.
288
+ ...(m.engines ? { engines: m.engines } : {}),
282
289
  })),
283
290
  }));
284
291
  const out = {
@@ -360,7 +367,7 @@ async function scanDiffFiles(ref, opts, scanCfg, baselineEntries = [], scanPath,
360
367
  cwd,
361
368
  stdio: ["pipe", "pipe", "ignore"],
362
369
  }).trim();
363
- const engine = await selectEngine(opts.engine || "auto", opts.quiet || false);
370
+ const engine = await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
364
371
  const allResults = [];
365
372
  for (const file of changedFiles) {
366
373
  const filePath = path.resolve(repoRoot, file);
@@ -410,7 +417,7 @@ async function scanStagedFiles(opts, scanCfg, baselineEntries = [], scanPath, su
410
417
  cwd,
411
418
  stdio: ["pipe", "pipe", "ignore"],
412
419
  }).trim();
413
- const engine = await selectEngine(opts.engine || "auto", opts.quiet || false);
420
+ const engine = await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
414
421
  const allResults = [];
415
422
  for (const file of stagedFiles) {
416
423
  const filePath = path.resolve(repoRoot, file);
@@ -435,9 +442,22 @@ async function scanStagedFiles(opts, scanCfg, baselineEntries = [], scanPath, su
435
442
  }
436
443
  }
437
444
  /**
438
- * Select scan engine based on availability and user preference
445
+ * Select scan engine based on availability and user preference.
446
+ *
447
+ * `auto` (the default) resolves to `both` whenever betterleaks is available
448
+ * and usable — rafter then runs both engines and unions their findings
449
+ * (sable-j85), so a miss in one (betterleaks 1.1.x does not detect AWS access
450
+ * keys — sable-h2y) is still caught by the other. It degrades to
451
+ * `patterns`-only when betterleaks is absent or a stale binary can't be
452
+ * refreshed. Explicit `--engine betterleaks` / `--engine patterns` stay
453
+ * single-engine.
454
+ *
455
+ * `autoUpdate` (default true) controls sable-o4k behavior: when the resolved
456
+ * engine is betterleaks but the managed binary is stale, auto-update it before
457
+ * scanning. The caller resolves this from `--no-auto-update` and the
458
+ * `scan.auto_update_betterleaks` config key.
439
459
  */
440
- async function selectEngine(preference, quiet) {
460
+ async function selectEngine(preference, quiet, autoUpdate = true) {
441
461
  if (preference === "patterns") {
442
462
  return "patterns";
443
463
  }
@@ -450,21 +470,104 @@ async function selectEngine(preference, quiet) {
450
470
  }
451
471
  return "patterns";
452
472
  }
453
- return "betterleaks";
473
+ return ensureBetterleaksUsable(quiet, autoUpdate);
454
474
  }
455
475
  if (preference !== "auto") {
456
476
  console.error(`Invalid engine: ${preference}. Valid values: auto, betterleaks, patterns`);
457
477
  process.exit(2);
458
478
  }
459
- // Auto mode: try Betterleaks, fall back to patterns
479
+ // Auto mode: run BOTH engines when betterleaks is usable; otherwise patterns.
460
480
  const bl = new BetterleaksScanner();
461
481
  const available = await bl.isAvailable();
462
- return available ? "betterleaks" : "patterns";
482
+ if (!available)
483
+ return "patterns";
484
+ const usable = await ensureBetterleaksUsable(quiet, autoUpdate);
485
+ return usable === "betterleaks" ? "both" : "patterns";
486
+ }
487
+ /**
488
+ * sable-o4k — guard against a stale rafter-managed betterleaks binary silently
489
+ * returning zero findings (a leftover binary from an older rafter parses fine
490
+ * with `version` but emits a JSON shape the current parser rejects).
491
+ *
492
+ * When the managed binary is stale we auto-update it to the pinned version
493
+ * (default on; opt out with `--no-auto-update` or `scan.auto_update_betterleaks:
494
+ * false`). In an interactive TTY we confirm first. If the update is disabled,
495
+ * declined, or fails, we degrade to the patterns engine and print a CTA rather
496
+ * than scanning with a binary that yields nothing.
497
+ */
498
+ async function ensureBetterleaksUsable(quiet, autoUpdate) {
499
+ const bm = new BinaryManager();
500
+ if (!(await bm.isManagedBetterleaksStale()))
501
+ return "betterleaks";
502
+ const current = await bm.getBetterleaksVersion();
503
+ const cta = "Fix manually with: rafter agent update-betterleaks";
504
+ const stale = `Stale betterleaks binary (${current}; expected v${BETTERLEAKS_VERSION})`;
505
+ if (!autoUpdate) {
506
+ if (!quiet) {
507
+ console.error(fmt.warning(`${stale}. Auto-update is off — using the patterns engine for this scan. ${cta}`));
508
+ }
509
+ return "patterns";
510
+ }
511
+ // Only prompt when there's a human at a TTY; otherwise auto-update proceeds
512
+ // (default on) so non-interactive runs aren't left with a broken engine.
513
+ if (process.stdin.isTTY && !quiet) {
514
+ const ok = await askYesNo(`${stale}. Update it now?`, true);
515
+ if (!ok) {
516
+ console.error(fmt.warning(`Skipped — using the patterns engine for this scan. ${cta}`));
517
+ return "patterns";
518
+ }
519
+ }
520
+ if (!quiet)
521
+ console.error(fmt.info(`Updating betterleaks to v${BETTERLEAKS_VERSION}...`));
522
+ try {
523
+ await bm.downloadBetterleaks((m) => { if (!quiet)
524
+ console.error(` ${m}`); }, BETTERLEAKS_VERSION);
525
+ if (!quiet)
526
+ console.error(fmt.success(`Betterleaks updated to v${BETTERLEAKS_VERSION}.`));
527
+ return "betterleaks";
528
+ }
529
+ catch (e) {
530
+ if (!quiet) {
531
+ const msg = e instanceof Error ? e.message : String(e);
532
+ console.error(fmt.warning(`Betterleaks update failed (${msg}) — using the patterns engine for this scan. ${cta}`));
533
+ }
534
+ return "patterns";
535
+ }
536
+ }
537
+ /**
538
+ * Resolve whether stale-binary auto-update is enabled, from the CLI flag
539
+ * (`--no-auto-update` → `opts.autoUpdate === false`) and the
540
+ * `scan.auto_update_betterleaks` config key. Either opting out disables it.
541
+ */
542
+ export function autoUpdateEnabled(opts, scanCfg) {
543
+ return opts.autoUpdate !== false && scanCfg?.autoUpdateBetterleaks !== false;
463
544
  }
464
545
  /**
465
546
  * Scan a file with selected engine
466
547
  */
467
548
  async function scanFile(filePath, engine, scanCfg) {
549
+ const runPatterns = () => {
550
+ const scanner = new RegexScanner(scanCfg?.customPatterns);
551
+ const result = scanner.scanFile(filePath);
552
+ return result.matches.length > 0 ? [result] : [];
553
+ };
554
+ if (engine === "both") {
555
+ // sable-j85 — kick off betterleaks (async subprocess) first, run the
556
+ // synchronous regex scan while it works, then union. A betterleaks failure
557
+ // degrades to patterns-only rather than losing the scan.
558
+ const blPromise = (async () => {
559
+ try {
560
+ const result = await new BetterleaksScanner().scanFile(filePath);
561
+ return result.matches.length > 0 ? [result] : [];
562
+ }
563
+ catch {
564
+ console.error(fmt.warning("Betterleaks scan failed, using patterns only for this run"));
565
+ return [];
566
+ }
567
+ })();
568
+ const patternsResults = runPatterns();
569
+ return unionScanResults(await blPromise, patternsResults);
570
+ }
468
571
  if (engine === "betterleaks") {
469
572
  try {
470
573
  const bl = new BetterleaksScanner();
@@ -473,16 +576,10 @@ async function scanFile(filePath, engine, scanCfg) {
473
576
  }
474
577
  catch (e) {
475
578
  console.error(fmt.warning("Betterleaks scan failed, falling back to patterns"));
476
- const scanner = new RegexScanner(scanCfg?.customPatterns);
477
- const result = scanner.scanFile(filePath);
478
- return result.matches.length > 0 ? [result] : [];
579
+ return runPatterns();
479
580
  }
480
581
  }
481
- else {
482
- const scanner = new RegexScanner(scanCfg?.customPatterns);
483
- const result = scanner.scanFile(filePath);
484
- return result.matches.length > 0 ? [result] : [];
485
- }
582
+ return runPatterns();
486
583
  }
487
584
  /**
488
585
  * Scan a directory with selected engine
@@ -491,27 +588,42 @@ async function scanDirectory(dirPath, engine, scanCfg, history, respectGitignore
491
588
  // respectGitignore default is true. The patterns engine honors it via
492
589
  // RegexScanner.scanDirectory; betterleaks honors it natively (gitleaks
493
590
  // ancestry — reads .gitignore unless --no-git is set).
591
+ const runPatterns = () => {
592
+ const scanner = new RegexScanner(scanCfg?.customPatterns);
593
+ return scanner.scanDirectory(dirPath, {
594
+ excludePaths: scanCfg?.excludePaths,
595
+ respectGitignore,
596
+ });
597
+ };
494
598
  let results;
495
- if (engine === "betterleaks") {
599
+ if (engine === "both") {
600
+ // sable-j85 — start betterleaks (async subprocess) first, walk the tree
601
+ // with the synchronous patterns engine while it runs, then union. The
602
+ // exclude-paths post-filter below applies to the merged set.
603
+ const blPromise = (async () => {
604
+ try {
605
+ return await new BetterleaksScanner().scanDirectory(dirPath, { useGit: history ?? false });
606
+ }
607
+ catch {
608
+ console.error(fmt.warning("Betterleaks scan failed, using patterns only for this run"));
609
+ return [];
610
+ }
611
+ })();
612
+ const patternsResults = runPatterns();
613
+ results = unionScanResults(await blPromise, patternsResults);
614
+ }
615
+ else if (engine === "betterleaks") {
496
616
  try {
497
617
  const bl = new BetterleaksScanner();
498
618
  results = await bl.scanDirectory(dirPath, { useGit: history ?? false });
499
619
  }
500
620
  catch (e) {
501
621
  console.error(fmt.warning("Betterleaks scan failed, falling back to patterns"));
502
- const scanner = new RegexScanner(scanCfg?.customPatterns);
503
- results = scanner.scanDirectory(dirPath, {
504
- excludePaths: scanCfg?.excludePaths,
505
- respectGitignore,
506
- });
622
+ results = runPatterns();
507
623
  }
508
624
  }
509
625
  else {
510
- const scanner = new RegexScanner(scanCfg?.customPatterns);
511
- results = scanner.scanDirectory(dirPath, {
512
- excludePaths: scanCfg?.excludePaths,
513
- respectGitignore,
514
- });
626
+ results = runPatterns();
515
627
  }
516
628
  // sable-yz0 — post-filter by `.rafter.yml scan.exclude_paths`. Two bugs
517
629
  // motivated this chokepoint:
@@ -534,7 +646,7 @@ async function scanDirectory(dirPath, engine, scanCfg, history, respectGitignore
534
646
  async function watchAndScan(watchPath, opts, scanCfg, suppressions = []) {
535
647
  const { watch } = await import("chokidar");
536
648
  const logger = new AuditLogger();
537
- const engine = await selectEngine(opts.engine || "auto", opts.quiet || false);
649
+ const engine = await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
538
650
  if (!opts.quiet) {
539
651
  console.error(fmt.info(`Watching ${watchPath} for changes (${engine}). Press Ctrl+C to exit.`));
540
652
  }
@@ -2,7 +2,41 @@ import { Command } from "commander";
2
2
  import { CommandInterceptor } from "../../core/command-interceptor.js";
3
3
  import { RegexScanner } from "../../scanners/regex-scanner.js";
4
4
  import { AuditLogger } from "../../core/audit-logger.js";
5
+ import { ConfigManager } from "../../core/config-manager.js";
6
+ import { applySuppressions } from "../../core/custom-patterns.js";
7
+ import { collectSuppressions, applyExcludePaths } from "../agent/scan.js";
5
8
  import { execSync } from "child_process";
9
+ import fs from "fs";
10
+ import path from "path";
11
+ /**
12
+ * Load the policy-merged scan config + suppression list the same way scan.ts
13
+ * does, so hook decisions match `rafter secrets`. Policy discovery and
14
+ * `.rafterignore` loading key off `process.cwd()`; `cwd` lets tests point the
15
+ * lookup at a temp repo without a process-wide chdir leaking across tests.
16
+ */
17
+ function loadScanConfig(cwd = process.cwd()) {
18
+ const prev = process.cwd();
19
+ const restore = path.resolve(cwd) !== path.resolve(prev);
20
+ if (restore) {
21
+ try {
22
+ process.chdir(cwd);
23
+ }
24
+ catch { /* ignore — fall back to current cwd */ }
25
+ }
26
+ try {
27
+ const cfg = new ConfigManager().loadWithPolicy();
28
+ const scanCfg = cfg.agent?.scan;
29
+ return { scanCfg, suppressions: collectSuppressions(scanCfg?.ignore) };
30
+ }
31
+ finally {
32
+ if (restore) {
33
+ try {
34
+ process.chdir(prev);
35
+ }
36
+ catch { /* ignore */ }
37
+ }
38
+ }
39
+ }
6
40
  const RISK_LABELS = {
7
41
  critical: "CRITICAL", high: "HIGH", medium: "MEDIUM", low: "LOW",
8
42
  };
@@ -139,10 +173,15 @@ function evaluateBash(command) {
139
173
  if (trimmed.startsWith("git commit") || trimmed.startsWith("git push")) {
140
174
  const scanResult = scanStagedFiles();
141
175
  if (scanResult.secretsFound) {
142
- audit.logSecretDetected("staged files", `${scanResult.count} secret(s)`, "blocked");
176
+ // Audit per file so the log records WHICH file + pattern, not a bare count.
177
+ for (const r of scanResult.findings) {
178
+ const rel = path.relative(scanResult.repoRoot, r.file) || path.basename(r.file);
179
+ const names = [...new Set(r.matches.map((m) => m.pattern.name))];
180
+ audit.logSecretDetected(rel, names.join(", "), "blocked");
181
+ }
143
182
  return {
144
183
  decision: "deny",
145
- reason: `${scanResult.count} secret(s) detected in ${scanResult.files} staged file(s). Run 'rafter secrets --staged' for details.`,
184
+ reason: formatStagedSecretReason(scanResult),
146
185
  };
147
186
  }
148
187
  }
@@ -155,41 +194,112 @@ function evaluateWrite(toolInput) {
155
194
  if (!content) {
156
195
  return { decision: "allow" };
157
196
  }
158
- const scanner = new RegexScanner();
159
- if (scanner.hasSecrets(content)) {
160
- const matches = scanner.scanText(content);
161
- const names = [...new Set(matches.map(m => m.pattern.name))];
162
- const audit = new AuditLogger();
163
- audit.logSecretDetected(toolInput.file_path || "file content", names.join(", "), "blocked");
164
- return {
165
- decision: "deny",
166
- reason: `Secret detected in file content: ${names.join(", ")}`,
167
- };
197
+ // Route through the same config pipeline as scan.ts so the hook honors
198
+ // custom patterns, exclude_paths, and ignore rules from .rafter.yml.
199
+ const { scanCfg, suppressions } = loadScanConfig();
200
+ const scanner = new RegexScanner(scanCfg?.customPatterns);
201
+ const matches = scanner.scanText(content);
202
+ if (matches.length === 0) {
203
+ return { decision: "allow" };
168
204
  }
169
- return { decision: "allow" };
205
+ const filePath = toolInput.file_path || "file content";
206
+ // Apply exclude_paths + suppressions keyed on the target file path, so a
207
+ // write to a policy-excluded path is allowed (matching `rafter secrets`).
208
+ const afterExclude = applyExcludePaths([{ file: filePath, matches }], scanCfg?.excludePaths, process.cwd());
209
+ const { results: kept } = applySuppressions(afterExclude, suppressions);
210
+ const keptMatches = kept[0]?.matches ?? [];
211
+ if (keptMatches.length === 0) {
212
+ return { decision: "allow" };
213
+ }
214
+ const names = [...new Set(keptMatches.map((m) => m.pattern.name))];
215
+ const audit = new AuditLogger();
216
+ audit.logSecretDetected(filePath, names.join(", "), "blocked");
217
+ return {
218
+ decision: "deny",
219
+ reason: `Secret detected in ${filePath}: ${names.join(", ")}`,
220
+ };
170
221
  }
171
- function scanStagedFiles() {
222
+ /**
223
+ * Scan the git staged files for secrets, routed through the SAME config-aware
224
+ * pipeline as `rafter secrets --staged` (sable-55u). The hook previously ran
225
+ * RegexScanner directly on the raw staged list with no config — no custom
226
+ * patterns, no exclude_paths, no suppressions — so it phantom-blocked commits
227
+ * on findings the CLI would suppress. Now it loads `.rafter.yml`, applies
228
+ * exclude_paths and ignore rules, and reports which file + pattern matched.
229
+ *
230
+ * Still patterns-only (no betterleaks): the hook is on the hot path and must
231
+ * stay fast, and betterleaks version skew therefore cannot affect it.
232
+ *
233
+ * `cwd` exists for tests; production callers use the inherited process cwd.
234
+ */
235
+ export function scanStagedFiles(cwd = process.cwd()) {
236
+ const gitOpts = {
237
+ cwd, encoding: "utf-8", stdio: ["pipe", "pipe", "ignore"],
238
+ };
239
+ const empty = {
240
+ secretsFound: false, count: 0, files: 0, findings: [], repoRoot: cwd,
241
+ };
172
242
  try {
173
- const stagedOutput = execSync("git diff --cached --name-only --diff-filter=ACM", {
174
- encoding: "utf-8",
175
- stdio: ["pipe", "pipe", "ignore"],
176
- }).trim();
243
+ const repoRoot = execSync("git rev-parse --show-toplevel", gitOpts).trim() || cwd;
244
+ const stagedOutput = execSync("git diff --cached --name-only --diff-filter=ACM", gitOpts).trim();
177
245
  if (!stagedOutput) {
178
- return { secretsFound: false, count: 0, files: 0 };
246
+ return { ...empty, repoRoot };
247
+ }
248
+ const stagedFiles = stagedOutput.split("\n").filter((f) => f.trim());
249
+ const { scanCfg, suppressions } = loadScanConfig(cwd);
250
+ const scanner = new RegexScanner(scanCfg?.customPatterns);
251
+ const raw = [];
252
+ for (const file of stagedFiles) {
253
+ const filePath = path.resolve(repoRoot, file);
254
+ if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile())
255
+ continue;
256
+ const r = scanner.scanFile(filePath);
257
+ if (r.matches.length > 0)
258
+ raw.push(r);
179
259
  }
180
- const stagedFiles = stagedOutput.split("\n").filter(f => f.trim());
181
- const scanner = new RegexScanner();
182
- const results = scanner.scanFiles(stagedFiles);
183
- const totalMatches = results.reduce((sum, r) => sum + r.matches.length, 0);
260
+ const afterExclude = applyExcludePaths(raw, scanCfg?.excludePaths, repoRoot);
261
+ const { results: kept } = applySuppressions(afterExclude, suppressions);
262
+ const count = kept.reduce((sum, r) => sum + r.matches.length, 0);
184
263
  return {
185
- secretsFound: results.length > 0,
186
- count: totalMatches,
187
- files: results.length,
264
+ secretsFound: kept.length > 0,
265
+ count,
266
+ files: kept.length,
267
+ findings: kept,
268
+ repoRoot,
188
269
  };
189
270
  }
190
271
  catch {
191
- return { secretsFound: false, count: 0, files: 0 };
272
+ return empty;
273
+ }
274
+ }
275
+ /** Cap on individual findings listed in a deny reason before truncating. */
276
+ const MAX_REASON_FINDINGS = 10;
277
+ /**
278
+ * Render a deny reason that names each offending file + pattern (+ line when
279
+ * known) instead of a bare count, so the agent knows what to fix without a
280
+ * second `rafter secrets` run. Truncates long lists.
281
+ */
282
+ export function formatStagedSecretReason(scan) {
283
+ const lines = [];
284
+ let shown = 0;
285
+ outer: for (const r of scan.findings) {
286
+ const rel = path.relative(scan.repoRoot, r.file) || path.basename(r.file);
287
+ for (const m of r.matches) {
288
+ if (shown >= MAX_REASON_FINDINGS) {
289
+ lines.push(` …and ${scan.count - shown} more`);
290
+ break outer;
291
+ }
292
+ const loc = m.line ? `:${m.line}` : "";
293
+ lines.push(` ${rel}${loc} — ${m.pattern.name}`);
294
+ shown++;
295
+ }
192
296
  }
297
+ return [
298
+ `${scan.count} secret(s) detected in ${scan.files} staged file(s):`,
299
+ ...lines,
300
+ "Hook scan is pattern-only (betterleaks version is irrelevant). " +
301
+ "Run 'rafter secrets --staged' for full detail, or add an exclude_paths/ignore rule to .rafter.yml if this is a false positive.",
302
+ ].join("\n");
193
303
  }
194
304
  const STDIN_TIMEOUT_MS = 5000;
195
305
  function readStdin() {
@@ -4,6 +4,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
4
4
  import { CallToolRequestSchema, ListToolsRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
5
5
  import { RegexScanner } from "../../scanners/regex-scanner.js";
6
6
  import { BetterleaksScanner } from "../../scanners/betterleaks.js";
7
+ import { unionScanResults } from "../../scanners/union.js";
7
8
  import { CommandInterceptor } from "../../core/command-interceptor.js";
8
9
  import { AuditLogger } from "../../core/audit-logger.js";
9
10
  import { ConfigManager } from "../../core/config-manager.js";
@@ -19,6 +20,8 @@ function formatScanResults(results) {
19
20
  severity: m.pattern.severity,
20
21
  line: m.line,
21
22
  redacted: m.redacted || m.match.slice(0, 4) + "****",
23
+ // sable-j85 — engine attribution, set only for both-engine (auto) scans.
24
+ ...(m.engines ? { engines: m.engines } : {}),
22
25
  })),
23
26
  }));
24
27
  }
@@ -118,31 +121,44 @@ export function createServer() {
118
121
  case "scan_secrets": {
119
122
  const scanPath = args?.path;
120
123
  const engine = args?.engine || "auto";
121
- if (engine === "betterleaks" || engine === "auto") {
124
+ const runPatterns = () => {
125
+ const scanner = new RegexScanner();
126
+ try {
127
+ return scanner.scanDirectory(scanPath);
128
+ }
129
+ catch {
130
+ return [scanner.scanFile(scanPath)];
131
+ }
132
+ };
133
+ if (engine === "betterleaks") {
134
+ const bl = new BetterleaksScanner();
135
+ if (!(await bl.isAvailable()))
136
+ return errorResult("Betterleaks not installed");
137
+ try {
138
+ return textResult(formatScanResults(await bl.scanDirectory(scanPath)));
139
+ }
140
+ catch {
141
+ return errorResult("Betterleaks scan failed");
142
+ }
143
+ }
144
+ if (engine === "auto") {
145
+ // sable-j85 — run BOTH engines and union, so a betterleaks miss
146
+ // (e.g. AWS access keys) is still caught by patterns. Degrades to
147
+ // patterns-only when betterleaks is unavailable or errors.
122
148
  const bl = new BetterleaksScanner();
123
149
  if (await bl.isAvailable()) {
124
150
  try {
125
- const results = await bl.scanDirectory(scanPath);
126
- return textResult(formatScanResults(results));
151
+ const blResults = await bl.scanDirectory(scanPath);
152
+ return textResult(formatScanResults(unionScanResults(blResults, runPatterns())));
127
153
  }
128
154
  catch {
129
- if (engine === "betterleaks")
130
- return errorResult("Betterleaks scan failed");
155
+ // betterleaks failed — fall through to patterns-only.
131
156
  }
132
157
  }
133
- else if (engine === "betterleaks") {
134
- return errorResult("Betterleaks not installed");
135
- }
136
- }
137
- const scanner = new RegexScanner();
138
- let results;
139
- try {
140
- results = scanner.scanDirectory(scanPath);
141
- }
142
- catch {
143
- results = [scanner.scanFile(scanPath)];
158
+ return textResult(formatScanResults(runPatterns()));
144
159
  }
145
- return textResult(formatScanResults(results));
160
+ // patterns (and any unrecognized value — the tool schema enums this).
161
+ return textResult(formatScanResults(runPatterns()));
146
162
  }
147
163
  case "evaluate_command": {
148
164
  const command = args?.command;
@@ -93,6 +93,10 @@ function validateConfig(raw) {
93
93
  return true;
94
94
  });
95
95
  }
96
+ if (scan.autoUpdateBetterleaks !== undefined && typeof scan.autoUpdateBetterleaks !== "boolean") {
97
+ console.error('Warning: config "agent.scan.autoUpdateBetterleaks" must be a boolean — using default.');
98
+ delete scan.autoUpdateBetterleaks;
99
+ }
96
100
  }
97
101
  }
98
102
  return raw;
@@ -253,6 +257,9 @@ export class ConfigManager {
253
257
  if (policy.scan.customPatterns) {
254
258
  config.agent.scan.customPatterns = policy.scan.customPatterns;
255
259
  }
260
+ if (policy.scan.autoUpdateBetterleaks !== undefined) {
261
+ config.agent.scan.autoUpdateBetterleaks = policy.scan.autoUpdateBetterleaks;
262
+ }
256
263
  }
257
264
  // Ignore rules — top-level policy key, applied per finding at scan time
258
265
  if (policy.ignore && config.agent) {
@@ -96,6 +96,9 @@ function mapPolicy(raw) {
96
96
  severity: p.severity || "high",
97
97
  }));
98
98
  }
99
+ if (typeof raw.scan.auto_update_betterleaks === "boolean") {
100
+ policy.scan.autoUpdateBetterleaks = raw.scan.auto_update_betterleaks;
101
+ }
99
102
  }
100
103
  // sable-c1c — backend flat-shape compat. rafter-backend reads
101
104
  // exclude_paths / custom_patterns at the top level (no `scan:` nesting),
@@ -297,6 +300,13 @@ function validatePolicy(policy, raw) {
297
300
  }
298
301
  }
299
302
  }
303
+ // sable-o4k — only a boolean reaches policy.scan (mapping guards the type),
304
+ // so warn directly off the raw value when it's present but malformed.
305
+ if (raw.scan && typeof raw.scan === "object" &&
306
+ raw.scan.auto_update_betterleaks !== undefined &&
307
+ typeof raw.scan.auto_update_betterleaks !== "boolean") {
308
+ console.error(`Warning: "scan.auto_update_betterleaks" must be a boolean — ignoring.`);
309
+ }
300
310
  }
301
311
  if (policy.ignore !== undefined) {
302
312
  if (!Array.isArray(policy.ignore)) {
@@ -133,7 +133,11 @@ export class BetterleaksScanner {
133
133
  }
134
134
  const parsed = JSON.parse(content);
135
135
  if (!Array.isArray(parsed)) {
136
- console.error("[rafter] Warning: Betterleaks output is not an array — possible version mismatch");
136
+ // sable-o4k — a stale/incompatible binary emits a non-array shape and
137
+ // would otherwise silently yield zero findings. The managed binary is
138
+ // auto-updated upstream; this covers a stale binary on PATH, which we
139
+ // can't safely overwrite — so point the user at the fix.
140
+ console.error("[rafter] Warning: Betterleaks output is not an array — possible version mismatch. Run: rafter agent update-betterleaks");
137
141
  return [];
138
142
  }
139
143
  return parsed;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Merge findings from the betterleaks and patterns engines into one result
3
+ * set (sable-j85).
4
+ *
5
+ * `auto` mode now runs BOTH engines because each misses what the other
6
+ * catches: betterleaks 1.1.x does not detect AWS access keys (sable-h2y),
7
+ * while the regex patterns lack betterleaks's entropy/context heuristics.
8
+ * Running only one silently degrades coverage; the union restores it.
9
+ *
10
+ * Dedup: two findings are "the same secret" when they share
11
+ * `(file, line, column, matched-text)`. The key is deliberately conservative
12
+ * — it errs toward keeping both findings rather than collapsing two genuinely
13
+ * distinct secrets (e.g. the same token value pasted twice on one line at
14
+ * different columns), because dropping a real finding is a security
15
+ * regression for a scanner. The flip side: when the two engines extract
16
+ * slightly different text/columns for the same secret it is reported twice
17
+ * (once per engine) rather than merged — safe over-reporting, not a miss.
18
+ * When both engines DO agree on `(line, column, text)` we keep the betterleaks
19
+ * match — its `RuleID`-style `pattern.name` is the canonical id — and record
20
+ * both engines in `engines`. Ordering is deterministic: betterleaks findings
21
+ * first (in betterleaks's own order), then any patterns-only findings, grouped
22
+ * by the file each first appeared in.
23
+ */
24
+ export function unionScanResults(betterleaks, patterns) {
25
+ const fileOrder = [];
26
+ const byFile = new Map();
27
+ const ingest = (results, engine) => {
28
+ for (const r of results) {
29
+ let fileMap = byFile.get(r.file);
30
+ if (!fileMap) {
31
+ fileMap = new Map();
32
+ byFile.set(r.file, fileMap);
33
+ fileOrder.push(r.file);
34
+ }
35
+ for (const m of r.matches) {
36
+ // `line` and `column` are always digits (or "?"), so the first two
37
+ // spaces unambiguously delimit them from the matched secret — no key
38
+ // collisions even when the secret itself contains spaces. Column is
39
+ // part of the key so two distinct secrets sharing a line+text but
40
+ // sitting at different columns are NOT collapsed.
41
+ const key = `${m.line ?? "?"} ${m.column ?? "?"} ${m.match}`;
42
+ const existing = fileMap.get(key);
43
+ if (existing) {
44
+ if (!existing.engines.includes(engine))
45
+ existing.engines.push(engine);
46
+ }
47
+ else {
48
+ fileMap.set(key, { ...m, engines: [engine] });
49
+ }
50
+ }
51
+ }
52
+ };
53
+ ingest(betterleaks, "betterleaks");
54
+ ingest(patterns, "patterns");
55
+ return fileOrder.map((file) => ({
56
+ file,
57
+ matches: Array.from(byFile.get(file).values()),
58
+ }));
59
+ }
@@ -260,6 +260,26 @@ export class BinaryManager {
260
260
  return "unknown";
261
261
  }
262
262
  }
263
+ /**
264
+ * sable-o4k — detect a stale rafter-managed betterleaks binary. A leftover
265
+ * binary from an older rafter (a gitleaks-8.x install from before the rename,
266
+ * or an older betterleaks) runs `version` fine but emits a JSON report shape
267
+ * the current parser rejects, silently yielding zero findings on the default
268
+ * engine. We treat the managed binary as stale whenever its reported version
269
+ * doesn't contain the pinned BETTERLEAKS_VERSION rafter ships and parses
270
+ * against. PATH binaries are out of scope: we can't safely overwrite a
271
+ * user-managed install, and they may be a compatible different version.
272
+ * Read-only — never downloads.
273
+ */
274
+ async isManagedBetterleaksStale() {
275
+ if (!this.isBetterleaksInstalled())
276
+ return false;
277
+ const version = await this.getBetterleaksVersion();
278
+ // "unknown" (binary errored) / "not installed" — can't tell; don't churn.
279
+ if (version === "unknown" || version === "not installed")
280
+ return false;
281
+ return !version.includes(BETTERLEAKS_VERSION);
282
+ }
263
283
  /**
264
284
  * Get platform string for download URL
265
285
  */
@@ -0,0 +1,22 @@
1
+ import { createInterface } from "readline";
2
+ /**
3
+ * Ask a yes/no question on stderr (keeps stdout clean for JSON output) and
4
+ * resolve to a boolean. An empty answer resolves to `defaultYes`.
5
+ *
6
+ * Callers MUST gate this on `process.stdin.isTTY` — with no TTY the readline
7
+ * prompt has no interactive input and would hang waiting for a line.
8
+ */
9
+ export async function askYesNo(question, defaultYes = true) {
10
+ const rl = createInterface({ input: process.stdin, output: process.stderr });
11
+ const suffix = defaultYes ? "[Y/n]" : "[y/N]";
12
+ return new Promise((resolve) => {
13
+ rl.question(` ${question} ${suffix} `, (answer) => {
14
+ rl.close();
15
+ const trimmed = answer.trim().toLowerCase();
16
+ if (trimmed === "")
17
+ resolve(defaultYes);
18
+ else
19
+ resolve(trimmed === "y" || trimmed === "yes");
20
+ });
21
+ });
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rafter-security/cli",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: rafter-security
3
3
  description: Security toolkit for AI workflows. Use when scanning code or repos for vulnerabilities, auditing third-party skills/MCPs/agent configs before installing, evaluating shell commands before running them, or generating secure design questions for new features. Provides `rafter run` (remote SAST + SCA, needs RAFTER_API_KEY), `rafter secrets` (offline secrets-only), `rafter agent exec --dry-run` (command-risk classification), and `rafter skill review`.
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  homepage: https://rafter.so
6
6
  metadata:
7
7
  openclaw:
@@ -55,8 +55,15 @@ If the finding is a leaked secret that was committed:
55
55
 
56
56
  Suppress only when the finding is a real false positive *for this context*, with a written reason. Two mechanisms:
57
57
 
58
- - **Inline**: `// rafter-ignore: HARDCODED_API_KEY — test fixture, not a real key`
59
- - **Baseline**: `rafter agent baseline` snapshots current findings; only *new* findings fail future scans. Good for adopting Rafter on a legacy codebase without a big bang.
58
+ - **`.rafter.yml`**: add an `ignore:` rule naming the path(s), the rule(s), and a `reason` (your evidence). Repo-tied; persists across scans:
59
+ ```yaml
60
+ ignore:
61
+ - paths: ["tests/fixtures/**", "src/legacy/config.py"]
62
+ rules: ["AWS Access Key ID"] # omit `rules` to suppress every finding on those paths
63
+ reason: "fake test keys — no live path"
64
+ ```
65
+ Match `rules` on the finding's **rule name** (case-insensitive), not a hashed `R-…` id. On a local `rafter scan`, suppressed findings move into a `_suppressed[]` array and don't affect the exit code — you only fail on a *non-suppressed* finding. Docs: https://docs.rafter.so/suppression
66
+ - **Baseline**: `rafter agent baseline create` snapshots current findings; scan with `rafter scan --baseline` so only *new* findings surface. Good for adopting Rafter on a legacy codebase without a big bang.
60
67
 
61
68
  Never suppress by:
62
69
  - Commenting out the rule globally.