@rafter-security/cli 0.8.4 → 0.8.6
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.
- package/dist/commands/agent/init.js +1 -15
- package/dist/commands/agent/scan.js +142 -30
- package/dist/commands/agent/status.js +30 -0
- package/dist/commands/hook/pretool.js +173 -48
- package/dist/commands/mcp/server.js +33 -17
- package/dist/core/config-manager.js +7 -0
- package/dist/core/hook-control.js +75 -0
- package/dist/core/policy-loader.js +10 -0
- package/dist/scanners/betterleaks.js +33 -11
- package/dist/scanners/union.js +59 -0
- package/dist/utils/binary-manager.js +20 -0
- package/dist/utils/prompt.js +22 -0
- package/package.json +1 -1
- package/resources/rafter-security-skill.md +1 -1
- package/resources/skills/rafter/docs/finding-triage.md +9 -2
|
@@ -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 {
|
|
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
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
477
|
-
const result = scanner.scanFile(filePath);
|
|
478
|
-
return result.matches.length > 0 ? [result] : [];
|
|
579
|
+
return runPatterns();
|
|
479
580
|
}
|
|
480
581
|
}
|
|
481
|
-
|
|
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 === "
|
|
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
|
-
|
|
503
|
-
results = scanner.scanDirectory(dirPath, {
|
|
504
|
-
excludePaths: scanCfg?.excludePaths,
|
|
505
|
-
respectGitignore,
|
|
506
|
-
});
|
|
622
|
+
results = runPatterns();
|
|
507
623
|
}
|
|
508
624
|
}
|
|
509
625
|
else {
|
|
510
|
-
|
|
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
|
}
|
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from "url";
|
|
|
7
7
|
import { getRafterDir, getAuditLogPath, getBinDir } from "../../core/config-defaults.js";
|
|
8
8
|
import { AuditLogger } from "../../core/audit-logger.js";
|
|
9
9
|
import { ConfigManager } from "../../core/config-manager.js";
|
|
10
|
+
import { resolveHookControl } from "../../core/hook-control.js";
|
|
10
11
|
import { BinaryManager } from "../../utils/binary-manager.js";
|
|
11
12
|
import { SkillManager } from "../../utils/skill-manager.js";
|
|
12
13
|
export function createStatusCommand() {
|
|
@@ -38,6 +39,22 @@ export function createStatusCommand() {
|
|
|
38
39
|
else {
|
|
39
40
|
console.log(`\nConfig: not found — run: rafter agent init`);
|
|
40
41
|
}
|
|
42
|
+
// --- Hook off-switch (trusted-source only: env / global config) ---
|
|
43
|
+
{
|
|
44
|
+
const c = resolveHookControl();
|
|
45
|
+
const describe = (on, src) => on ? "active" : `DISABLED (via ${src === "env" ? "RAFTER_DISABLE_* env" : "global config"})`;
|
|
46
|
+
if (!c.hookEnabled) {
|
|
47
|
+
console.log(`Hooks: ${describe(false, c.source.hook)}`);
|
|
48
|
+
}
|
|
49
|
+
else if (!c.secretScanEnabled || !c.commandPolicyEnabled) {
|
|
50
|
+
console.log(`Hooks: active (partial)`);
|
|
51
|
+
console.log(` secret scan: ${describe(c.secretScanEnabled, c.source.secretScan)}`);
|
|
52
|
+
console.log(` command policy: ${describe(c.commandPolicyEnabled, c.source.commandPolicy)}`);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
console.log(`Hooks: active`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
41
58
|
// --- Betterleaks ---
|
|
42
59
|
const exeExt = process.platform === "win32" ? ".exe" : "";
|
|
43
60
|
const localBetterleaks = path.join(getBinDir(), `betterleaks${exeExt}`);
|
|
@@ -208,6 +225,19 @@ function buildStatusJson(home, configPath, auditPath) {
|
|
|
208
225
|
betterleaks_available: isBetterleaksAvailable(),
|
|
209
226
|
config_path: formatHomePath(configPath, home),
|
|
210
227
|
audit_log_path: formatHomePath(auditPath, home),
|
|
228
|
+
hook_control: (() => {
|
|
229
|
+
const c = resolveHookControl();
|
|
230
|
+
return {
|
|
231
|
+
hook_enabled: c.hookEnabled,
|
|
232
|
+
secret_scan_enabled: c.secretScanEnabled,
|
|
233
|
+
command_policy_enabled: c.commandPolicyEnabled,
|
|
234
|
+
source: {
|
|
235
|
+
hook: c.source.hook,
|
|
236
|
+
secret_scan: c.source.secretScan,
|
|
237
|
+
command_policy: c.source.commandPolicy,
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
})(),
|
|
211
241
|
};
|
|
212
242
|
}
|
|
213
243
|
function detectAgents(home) {
|
|
@@ -2,7 +2,42 @@ 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 { resolveHookControl } from "../../core/hook-control.js";
|
|
8
|
+
import { collectSuppressions, applyExcludePaths } from "../agent/scan.js";
|
|
5
9
|
import { execSync } from "child_process";
|
|
10
|
+
import fs from "fs";
|
|
11
|
+
import path from "path";
|
|
12
|
+
/**
|
|
13
|
+
* Load the policy-merged scan config + suppression list the same way scan.ts
|
|
14
|
+
* does, so hook decisions match `rafter secrets`. Policy discovery and
|
|
15
|
+
* `.rafterignore` loading key off `process.cwd()`; `cwd` lets tests point the
|
|
16
|
+
* lookup at a temp repo without a process-wide chdir leaking across tests.
|
|
17
|
+
*/
|
|
18
|
+
function loadScanConfig(cwd = process.cwd()) {
|
|
19
|
+
const prev = process.cwd();
|
|
20
|
+
const restore = path.resolve(cwd) !== path.resolve(prev);
|
|
21
|
+
if (restore) {
|
|
22
|
+
try {
|
|
23
|
+
process.chdir(cwd);
|
|
24
|
+
}
|
|
25
|
+
catch { /* ignore — fall back to current cwd */ }
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const cfg = new ConfigManager().loadWithPolicy();
|
|
29
|
+
const scanCfg = cfg.agent?.scan;
|
|
30
|
+
return { scanCfg, suppressions: collectSuppressions(scanCfg?.ignore) };
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
if (restore) {
|
|
34
|
+
try {
|
|
35
|
+
process.chdir(prev);
|
|
36
|
+
}
|
|
37
|
+
catch { /* ignore */ }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
6
41
|
const RISK_LABELS = {
|
|
7
42
|
critical: "CRITICAL", high: "HIGH", medium: "MEDIUM", low: "LOW",
|
|
8
43
|
};
|
|
@@ -106,43 +141,62 @@ function normalizeInput(raw, format) {
|
|
|
106
141
|
}
|
|
107
142
|
function evaluateToolCall(payload) {
|
|
108
143
|
const { tool_name, tool_input } = payload;
|
|
144
|
+
// Honor the (trusted-source-only) hook off-switch before doing any work.
|
|
145
|
+
// Master switch off → allow everything; otherwise the two concerns are gated
|
|
146
|
+
// independently inside evaluateBash (command policy + git-commit secret scan).
|
|
147
|
+
const control = resolveHookControl();
|
|
148
|
+
if (!control.hookEnabled)
|
|
149
|
+
return { decision: "allow" };
|
|
109
150
|
if (tool_name === "Bash") {
|
|
110
|
-
return evaluateBash(tool_input?.command || "");
|
|
151
|
+
return evaluateBash(tool_input?.command || "", control);
|
|
111
152
|
}
|
|
112
153
|
if (tool_name === "Write" || tool_name === "Edit") {
|
|
154
|
+
if (!control.secretScanEnabled)
|
|
155
|
+
return { decision: "allow" };
|
|
113
156
|
return evaluateWrite(tool_input || {});
|
|
114
157
|
}
|
|
115
158
|
return { decision: "allow" };
|
|
116
159
|
}
|
|
117
|
-
function evaluateBash(command) {
|
|
118
|
-
const interceptor = new CommandInterceptor();
|
|
160
|
+
function evaluateBash(command, control) {
|
|
119
161
|
const audit = new AuditLogger();
|
|
120
|
-
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
162
|
+
// Command-risk interception — gated by commandPolicy. When disabled, skip the
|
|
163
|
+
// block/approval logic but still fall through to the staged-secret scan below
|
|
164
|
+
// (a user may keep secret scanning while silencing command prompts).
|
|
165
|
+
if (control.commandPolicyEnabled) {
|
|
166
|
+
const interceptor = new CommandInterceptor();
|
|
167
|
+
const evaluation = interceptor.evaluate(command);
|
|
168
|
+
// Blocked — hard deny
|
|
169
|
+
if (!evaluation.allowed && !evaluation.requiresApproval) {
|
|
170
|
+
audit.logCommandIntercepted(command, false, "blocked", evaluation.reason);
|
|
171
|
+
return {
|
|
172
|
+
decision: "deny",
|
|
173
|
+
reason: formatBlockedMessage(command, evaluation),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
// Requires approval — deny (agent can't provide interactive approval)
|
|
177
|
+
if (evaluation.requiresApproval) {
|
|
178
|
+
audit.logCommandIntercepted(command, false, "blocked", evaluation.reason);
|
|
179
|
+
return {
|
|
180
|
+
decision: "deny",
|
|
181
|
+
reason: formatApprovalMessage(command, evaluation),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
136
184
|
}
|
|
137
|
-
// Git commit/push — scan staged files for secrets
|
|
185
|
+
// Git commit/push — scan staged files for secrets. Gated by secretScan so the
|
|
186
|
+
// git-commit secret check survives `commandPolicy` being disabled on its own.
|
|
138
187
|
const trimmed = command.trim();
|
|
139
|
-
if (trimmed.startsWith("git commit") || trimmed.startsWith("git push")) {
|
|
188
|
+
if (control.secretScanEnabled && (trimmed.startsWith("git commit") || trimmed.startsWith("git push"))) {
|
|
140
189
|
const scanResult = scanStagedFiles();
|
|
141
190
|
if (scanResult.secretsFound) {
|
|
142
|
-
|
|
191
|
+
// Audit per file so the log records WHICH file + pattern, not a bare count.
|
|
192
|
+
for (const r of scanResult.findings) {
|
|
193
|
+
const rel = path.relative(scanResult.repoRoot, r.file) || path.basename(r.file);
|
|
194
|
+
const names = [...new Set(r.matches.map((m) => m.pattern.name))];
|
|
195
|
+
audit.logSecretDetected(rel, names.join(", "), "blocked");
|
|
196
|
+
}
|
|
143
197
|
return {
|
|
144
198
|
decision: "deny",
|
|
145
|
-
reason:
|
|
199
|
+
reason: formatStagedSecretReason(scanResult),
|
|
146
200
|
};
|
|
147
201
|
}
|
|
148
202
|
}
|
|
@@ -155,41 +209,112 @@ function evaluateWrite(toolInput) {
|
|
|
155
209
|
if (!content) {
|
|
156
210
|
return { decision: "allow" };
|
|
157
211
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
decision: "deny",
|
|
166
|
-
reason: `Secret detected in file content: ${names.join(", ")}`,
|
|
167
|
-
};
|
|
212
|
+
// Route through the same config pipeline as scan.ts so the hook honors
|
|
213
|
+
// custom patterns, exclude_paths, and ignore rules from .rafter.yml.
|
|
214
|
+
const { scanCfg, suppressions } = loadScanConfig();
|
|
215
|
+
const scanner = new RegexScanner(scanCfg?.customPatterns);
|
|
216
|
+
const matches = scanner.scanText(content);
|
|
217
|
+
if (matches.length === 0) {
|
|
218
|
+
return { decision: "allow" };
|
|
168
219
|
}
|
|
169
|
-
|
|
220
|
+
const filePath = toolInput.file_path || "file content";
|
|
221
|
+
// Apply exclude_paths + suppressions keyed on the target file path, so a
|
|
222
|
+
// write to a policy-excluded path is allowed (matching `rafter secrets`).
|
|
223
|
+
const afterExclude = applyExcludePaths([{ file: filePath, matches }], scanCfg?.excludePaths, process.cwd());
|
|
224
|
+
const { results: kept } = applySuppressions(afterExclude, suppressions);
|
|
225
|
+
const keptMatches = kept[0]?.matches ?? [];
|
|
226
|
+
if (keptMatches.length === 0) {
|
|
227
|
+
return { decision: "allow" };
|
|
228
|
+
}
|
|
229
|
+
const names = [...new Set(keptMatches.map((m) => m.pattern.name))];
|
|
230
|
+
const audit = new AuditLogger();
|
|
231
|
+
audit.logSecretDetected(filePath, names.join(", "), "blocked");
|
|
232
|
+
return {
|
|
233
|
+
decision: "deny",
|
|
234
|
+
reason: `Secret detected in ${filePath}: ${names.join(", ")}`,
|
|
235
|
+
};
|
|
170
236
|
}
|
|
171
|
-
|
|
237
|
+
/**
|
|
238
|
+
* Scan the git staged files for secrets, routed through the SAME config-aware
|
|
239
|
+
* pipeline as `rafter secrets --staged` (sable-55u). The hook previously ran
|
|
240
|
+
* RegexScanner directly on the raw staged list with no config — no custom
|
|
241
|
+
* patterns, no exclude_paths, no suppressions — so it phantom-blocked commits
|
|
242
|
+
* on findings the CLI would suppress. Now it loads `.rafter.yml`, applies
|
|
243
|
+
* exclude_paths and ignore rules, and reports which file + pattern matched.
|
|
244
|
+
*
|
|
245
|
+
* Still patterns-only (no betterleaks): the hook is on the hot path and must
|
|
246
|
+
* stay fast, and betterleaks version skew therefore cannot affect it.
|
|
247
|
+
*
|
|
248
|
+
* `cwd` exists for tests; production callers use the inherited process cwd.
|
|
249
|
+
*/
|
|
250
|
+
export function scanStagedFiles(cwd = process.cwd()) {
|
|
251
|
+
const gitOpts = {
|
|
252
|
+
cwd, encoding: "utf-8", stdio: ["pipe", "pipe", "ignore"],
|
|
253
|
+
};
|
|
254
|
+
const empty = {
|
|
255
|
+
secretsFound: false, count: 0, files: 0, findings: [], repoRoot: cwd,
|
|
256
|
+
};
|
|
172
257
|
try {
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
stdio: ["pipe", "pipe", "ignore"],
|
|
176
|
-
}).trim();
|
|
258
|
+
const repoRoot = execSync("git rev-parse --show-toplevel", gitOpts).trim() || cwd;
|
|
259
|
+
const stagedOutput = execSync("git diff --cached --name-only --diff-filter=ACM", gitOpts).trim();
|
|
177
260
|
if (!stagedOutput) {
|
|
178
|
-
return {
|
|
261
|
+
return { ...empty, repoRoot };
|
|
262
|
+
}
|
|
263
|
+
const stagedFiles = stagedOutput.split("\n").filter((f) => f.trim());
|
|
264
|
+
const { scanCfg, suppressions } = loadScanConfig(cwd);
|
|
265
|
+
const scanner = new RegexScanner(scanCfg?.customPatterns);
|
|
266
|
+
const raw = [];
|
|
267
|
+
for (const file of stagedFiles) {
|
|
268
|
+
const filePath = path.resolve(repoRoot, file);
|
|
269
|
+
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile())
|
|
270
|
+
continue;
|
|
271
|
+
const r = scanner.scanFile(filePath);
|
|
272
|
+
if (r.matches.length > 0)
|
|
273
|
+
raw.push(r);
|
|
179
274
|
}
|
|
180
|
-
const
|
|
181
|
-
const
|
|
182
|
-
const
|
|
183
|
-
const totalMatches = results.reduce((sum, r) => sum + r.matches.length, 0);
|
|
275
|
+
const afterExclude = applyExcludePaths(raw, scanCfg?.excludePaths, repoRoot);
|
|
276
|
+
const { results: kept } = applySuppressions(afterExclude, suppressions);
|
|
277
|
+
const count = kept.reduce((sum, r) => sum + r.matches.length, 0);
|
|
184
278
|
return {
|
|
185
|
-
secretsFound:
|
|
186
|
-
count
|
|
187
|
-
files:
|
|
279
|
+
secretsFound: kept.length > 0,
|
|
280
|
+
count,
|
|
281
|
+
files: kept.length,
|
|
282
|
+
findings: kept,
|
|
283
|
+
repoRoot,
|
|
188
284
|
};
|
|
189
285
|
}
|
|
190
286
|
catch {
|
|
191
|
-
return
|
|
287
|
+
return empty;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/** Cap on individual findings listed in a deny reason before truncating. */
|
|
291
|
+
const MAX_REASON_FINDINGS = 10;
|
|
292
|
+
/**
|
|
293
|
+
* Render a deny reason that names each offending file + pattern (+ line when
|
|
294
|
+
* known) instead of a bare count, so the agent knows what to fix without a
|
|
295
|
+
* second `rafter secrets` run. Truncates long lists.
|
|
296
|
+
*/
|
|
297
|
+
export function formatStagedSecretReason(scan) {
|
|
298
|
+
const lines = [];
|
|
299
|
+
let shown = 0;
|
|
300
|
+
outer: for (const r of scan.findings) {
|
|
301
|
+
const rel = path.relative(scan.repoRoot, r.file) || path.basename(r.file);
|
|
302
|
+
for (const m of r.matches) {
|
|
303
|
+
if (shown >= MAX_REASON_FINDINGS) {
|
|
304
|
+
lines.push(` …and ${scan.count - shown} more`);
|
|
305
|
+
break outer;
|
|
306
|
+
}
|
|
307
|
+
const loc = m.line ? `:${m.line}` : "";
|
|
308
|
+
lines.push(` ${rel}${loc} — ${m.pattern.name}`);
|
|
309
|
+
shown++;
|
|
310
|
+
}
|
|
192
311
|
}
|
|
312
|
+
return [
|
|
313
|
+
`${scan.count} secret(s) detected in ${scan.files} staged file(s):`,
|
|
314
|
+
...lines,
|
|
315
|
+
"Hook scan is pattern-only (betterleaks version is irrelevant). " +
|
|
316
|
+
"Run 'rafter secrets --staged' for full detail, or add an exclude_paths/ignore rule to .rafter.yml if this is a false positive.",
|
|
317
|
+
].join("\n");
|
|
193
318
|
}
|
|
194
319
|
const STDIN_TIMEOUT_MS = 5000;
|
|
195
320
|
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
|
-
|
|
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
|
|
126
|
-
return textResult(formatScanResults(
|
|
151
|
+
const blResults = await bl.scanDirectory(scanPath);
|
|
152
|
+
return textResult(formatScanResults(unionScanResults(blResults, runPatterns())));
|
|
127
153
|
}
|
|
128
154
|
catch {
|
|
129
|
-
|
|
130
|
-
return errorResult("Betterleaks scan failed");
|
|
155
|
+
// betterleaks failed — fall through to patterns-only.
|
|
131
156
|
}
|
|
132
157
|
}
|
|
133
|
-
|
|
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
|
-
|
|
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) {
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ConfigManager } from "./config-manager.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parse a tri-state from an env var. Returns true (disable), false (force-enable),
|
|
4
|
+
* or undefined (unset / unrecognized → defer to config). Deliberately strict:
|
|
5
|
+
* only explicit, well-known tokens count, so a stray value fails safe to "defer"
|
|
6
|
+
* rather than silently disabling a security control (secure-design D2).
|
|
7
|
+
*/
|
|
8
|
+
function envTriState(raw) {
|
|
9
|
+
if (raw == null)
|
|
10
|
+
return undefined;
|
|
11
|
+
const v = raw.trim().toLowerCase();
|
|
12
|
+
if (v === "1" || v === "true" || v === "yes" || v === "on")
|
|
13
|
+
return true; // disable
|
|
14
|
+
if (v === "0" || v === "false" || v === "no" || v === "off")
|
|
15
|
+
return false; // force-enable
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Resolve whether the hook (and its sub-parts) should act.
|
|
20
|
+
*
|
|
21
|
+
* SECURITY (secure-design D1): the disable signal is honored ONLY from trusted,
|
|
22
|
+
* machine-owner-owned sources — the global `~/.rafter/config.json` and the
|
|
23
|
+
* `RAFTER_DISABLE_*` env vars. It is NEVER read from project-local `.rafter.yml`,
|
|
24
|
+
* so cloning a hostile repo cannot silently disable a victim's secret scanning or
|
|
25
|
+
* command interception. That is enforced structurally: this function calls
|
|
26
|
+
* `ConfigManager.load()` (global config only), NOT `loadWithPolicy()` (which
|
|
27
|
+
* merges `.rafter.yml`), and `hooks` is absent from the PolicyFile schema.
|
|
28
|
+
*
|
|
29
|
+
* Precedence (D5): env var overrides global config. Default (D2): enabled; an
|
|
30
|
+
* unreadable config or unrecognized value fails safe to enabled.
|
|
31
|
+
*/
|
|
32
|
+
export function resolveHookControl(opts) {
|
|
33
|
+
const env = opts?.env ?? process.env;
|
|
34
|
+
let cfg = opts?.config;
|
|
35
|
+
if (!cfg) {
|
|
36
|
+
try {
|
|
37
|
+
cfg = new ConfigManager().load();
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// Unreadable/corrupt global config must not disable the hook — fail safe.
|
|
41
|
+
cfg = undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const h = cfg?.agent?.hooks;
|
|
45
|
+
// Resolve one axis: env wins over global; absent → default `true` (enabled).
|
|
46
|
+
// `globalDisabled` is the config saying `<key>: false` (disabled) or
|
|
47
|
+
// `hooks.<sub>: true` meaning "disable this sub-part".
|
|
48
|
+
const resolve = (envVal, globalDisabled) => {
|
|
49
|
+
if (envVal !== undefined)
|
|
50
|
+
return { enabled: !envVal, source: "env" };
|
|
51
|
+
if (globalDisabled === true)
|
|
52
|
+
return { enabled: false, source: "global-config" };
|
|
53
|
+
return { enabled: true, source: "default" };
|
|
54
|
+
};
|
|
55
|
+
// Master switch. Global form: `agent.hooks.enabled === false` disables.
|
|
56
|
+
const hook = resolve(envTriState(env.RAFTER_DISABLE_HOOKS), h?.enabled === false ? true : undefined);
|
|
57
|
+
// Sub-parts. Global form: `agent.hooks.secretScan === false` disables that part.
|
|
58
|
+
// A disabled master switch forces every sub-part off regardless of its own setting.
|
|
59
|
+
const secretScan = hook.enabled
|
|
60
|
+
? resolve(envTriState(env.RAFTER_DISABLE_SECRET_SCAN), h?.secretScan === false ? true : undefined)
|
|
61
|
+
: { enabled: false, source: hook.source };
|
|
62
|
+
const commandPolicy = hook.enabled
|
|
63
|
+
? resolve(envTriState(env.RAFTER_DISABLE_COMMAND_POLICY), h?.commandPolicy === false ? true : undefined)
|
|
64
|
+
: { enabled: false, source: hook.source };
|
|
65
|
+
return {
|
|
66
|
+
hookEnabled: hook.enabled,
|
|
67
|
+
secretScanEnabled: secretScan.enabled,
|
|
68
|
+
commandPolicyEnabled: commandPolicy.enabled,
|
|
69
|
+
source: {
|
|
70
|
+
hook: hook.source,
|
|
71
|
+
secretScan: secretScan.source,
|
|
72
|
+
commandPolicy: commandPolicy.source,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -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)) {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { execFile } from "child_process";
|
|
2
2
|
import { promisify } from "util";
|
|
3
|
-
import { randomBytes } from "crypto";
|
|
4
3
|
import { BinaryManager } from "../utils/binary-manager.js";
|
|
5
4
|
import fs from "fs";
|
|
6
5
|
import os from "os";
|
|
@@ -48,7 +47,11 @@ export class BetterleaksScanner {
|
|
|
48
47
|
if (!blPath) {
|
|
49
48
|
throw new Error("Betterleaks not available");
|
|
50
49
|
}
|
|
51
|
-
|
|
50
|
+
// mkdtempSync atomically creates a private 0700 dir, so the report path
|
|
51
|
+
// can't be pre-created/symlinked by another user on the shared tmpdir —
|
|
52
|
+
// avoids the TOCTOU race a predictable Date.now() filename allowed (CWE-367).
|
|
53
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "betterleaks-"));
|
|
54
|
+
const tmpReport = path.join(tmpDir, "report.json");
|
|
52
55
|
try {
|
|
53
56
|
// `--` ensures a target path beginning with `-` isn't parsed as a flag by betterleaks.
|
|
54
57
|
await execFileAsync(blPath, ["dir", "-f", "json", "--report-path", tmpReport, "--", filePath], { timeout: 60000 });
|
|
@@ -56,7 +59,6 @@ export class BetterleaksScanner {
|
|
|
56
59
|
return { file: filePath, matches: [] };
|
|
57
60
|
}
|
|
58
61
|
const results = this.parseResults(tmpReport);
|
|
59
|
-
fs.unlinkSync(tmpReport);
|
|
60
62
|
return {
|
|
61
63
|
file: filePath,
|
|
62
64
|
matches: results.map(r => this.convertToPatternMatch(r))
|
|
@@ -66,17 +68,23 @@ export class BetterleaksScanner {
|
|
|
66
68
|
// Betterleaks exits with --exit-code (default 1) when leaks found — read report before cleanup
|
|
67
69
|
if (e.code === 1 && fs.existsSync(tmpReport)) {
|
|
68
70
|
const results = this.parseResults(tmpReport);
|
|
69
|
-
fs.unlinkSync(tmpReport);
|
|
70
71
|
return {
|
|
71
72
|
file: filePath,
|
|
72
73
|
matches: results.map(r => this.convertToPatternMatch(r))
|
|
73
74
|
};
|
|
74
75
|
}
|
|
75
|
-
if (fs.existsSync(tmpReport)) {
|
|
76
|
-
fs.unlinkSync(tmpReport);
|
|
77
|
-
}
|
|
78
76
|
throw new Error(`Betterleaks scan failed: ${e.message}`);
|
|
79
77
|
}
|
|
78
|
+
finally {
|
|
79
|
+
// Best-effort cleanup — a cleanup failure must never mask the scan's
|
|
80
|
+
// real result or its original error.
|
|
81
|
+
try {
|
|
82
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// ignore
|
|
86
|
+
}
|
|
87
|
+
}
|
|
80
88
|
}
|
|
81
89
|
async scanFiles(filePaths) {
|
|
82
90
|
const results = [];
|
|
@@ -102,7 +110,9 @@ export class BetterleaksScanner {
|
|
|
102
110
|
if (!blPath) {
|
|
103
111
|
throw new Error("Betterleaks not available");
|
|
104
112
|
}
|
|
105
|
-
|
|
113
|
+
// Private 0700 dir, atomically created — see scanFile for the TOCTOU rationale (CWE-367).
|
|
114
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "betterleaks-"));
|
|
115
|
+
const tmpReport = path.join(tmpDir, "report.json");
|
|
106
116
|
const subcommand = opts?.useGit ? "git" : "dir";
|
|
107
117
|
try {
|
|
108
118
|
// `--` ensures a target path beginning with `-` isn't parsed as a flag by betterleaks.
|
|
@@ -111,19 +121,27 @@ export class BetterleaksScanner {
|
|
|
111
121
|
return [];
|
|
112
122
|
}
|
|
113
123
|
const results = this.parseResults(tmpReport);
|
|
114
|
-
fs.unlinkSync(tmpReport);
|
|
115
124
|
return this.groupByFile(results);
|
|
116
125
|
}
|
|
117
126
|
catch (e) {
|
|
118
127
|
if (fs.existsSync(tmpReport)) {
|
|
119
128
|
const results = this.parseResults(tmpReport);
|
|
120
|
-
fs.unlinkSync(tmpReport);
|
|
121
129
|
if (e.code === 1) {
|
|
122
130
|
return this.groupByFile(results);
|
|
123
131
|
}
|
|
124
132
|
}
|
|
125
133
|
throw new Error(`Betterleaks scan failed: ${e.message}`);
|
|
126
134
|
}
|
|
135
|
+
finally {
|
|
136
|
+
// Best-effort cleanup — a cleanup failure must never mask the scan's
|
|
137
|
+
// real result or its original error.
|
|
138
|
+
try {
|
|
139
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// ignore
|
|
143
|
+
}
|
|
144
|
+
}
|
|
127
145
|
}
|
|
128
146
|
parseResults(reportPath) {
|
|
129
147
|
try {
|
|
@@ -133,7 +151,11 @@ export class BetterleaksScanner {
|
|
|
133
151
|
}
|
|
134
152
|
const parsed = JSON.parse(content);
|
|
135
153
|
if (!Array.isArray(parsed)) {
|
|
136
|
-
|
|
154
|
+
// sable-o4k — a stale/incompatible binary emits a non-array shape and
|
|
155
|
+
// would otherwise silently yield zero findings. The managed binary is
|
|
156
|
+
// auto-updated upstream; this covers a stale binary on PATH, which we
|
|
157
|
+
// can't safely overwrite — so point the user at the fix.
|
|
158
|
+
console.error("[rafter] Warning: Betterleaks output is not an array — possible version mismatch. Run: rafter agent update-betterleaks");
|
|
137
159
|
return [];
|
|
138
160
|
}
|
|
139
161
|
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,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
|
+
version: 0.8.6
|
|
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
|
-
-
|
|
59
|
-
|
|
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.
|