@rafter-security/cli 0.10.6 → 0.11.2
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/baseline.js +9 -50
- package/dist/commands/agent/components.js +7 -2
- package/dist/commands/agent/exec.js +26 -9
- package/dist/commands/agent/init.js +275 -220
- package/dist/commands/agent/install-hook.js +17 -6
- package/dist/commands/agent/scan.js +309 -71
- package/dist/commands/agent/update-skill-scanner.js +1 -1
- package/dist/commands/agent/verify.js +39 -15
- package/dist/commands/backend/get.js +6 -2
- package/dist/commands/backend/scan-status.js +34 -4
- package/dist/commands/ci/init.js +4 -1
- package/dist/commands/hook/posttool.js +11 -0
- package/dist/commands/hook/pretool.js +124 -36
- package/dist/commands/mcp/server.js +184 -129
- package/dist/commands/report.js +6 -2
- package/dist/core/audit-logger.js +47 -17
- package/dist/core/config-manager.js +46 -8
- package/dist/core/custom-patterns.js +28 -8
- package/dist/core/docs-loader.js +33 -3
- package/dist/core/pattern-engine.js +23 -5
- package/dist/core/policy-loader.js +15 -4
- package/dist/core/risk-rules.js +391 -9
- package/dist/index.js +6 -0
- package/dist/scanners/betterleaks.js +127 -14
- package/dist/scanners/git-history-scan.js +75 -0
- package/dist/scanners/regex-scanner.js +16 -2
- package/dist/scanners/secret-patterns.js +11 -2
- package/dist/scanners/union.js +31 -19
- package/dist/utils/api.js +35 -1
- package/dist/utils/git-diff.js +36 -20
- package/dist/utils/skill-manager.js +4 -3
- package/package.json +16 -15
- package/resources/continue-rules/rafter.md +1 -1
- package/resources/cursor-rules/rafter.mdc +1 -1
- package/resources/rafter-security-skill.md +3 -1
- package/resources/skills/rafter/SKILL.md +1 -2
- package/resources/skills/rafter/docs/guardrails.md +1 -1
- package/resources/skills/rafter-code-review/SKILL.md +0 -1
- package/resources/skills/rafter-secure-design/SKILL.md +0 -1
- package/resources/skills/rafter-skill-review/SKILL.md +0 -1
- package/resources/windsurf-rules/rafter.md +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import { RegexScanner } from "../../scanners/regex-scanner.js";
|
|
3
3
|
import { scanAddedDiffLines } from "../../scanners/git-diff-scan.js";
|
|
4
|
-
import {
|
|
4
|
+
import { gitRepoRoot, scanGitHistory } from "../../scanners/git-history-scan.js";
|
|
5
|
+
import { parseUnifiedDiffAddedLines, gitDiffMaxBuffer } from "../../utils/git-diff.js";
|
|
5
6
|
import { BetterleaksScanner } from "../../scanners/betterleaks.js";
|
|
6
7
|
import { unionScanResults } from "../../scanners/union.js";
|
|
7
8
|
import { BinaryManager, BETTERLEAKS_VERSION } from "../../utils/binary-manager.js";
|
|
@@ -106,7 +107,9 @@ function applyBaseline(results, entries) {
|
|
|
106
107
|
e.pattern === m.pattern.name &&
|
|
107
108
|
(e.line == null || e.line === (m.line ?? null)))),
|
|
108
109
|
}))
|
|
109
|
-
|
|
110
|
+
// Keep a read-error entry even with no matches (sable-ilbx), so an
|
|
111
|
+
// unreadable file survives a baseline filter.
|
|
112
|
+
.filter((r) => r.matches.length > 0 || r.readError);
|
|
110
113
|
}
|
|
111
114
|
export function createScanCommand() {
|
|
112
115
|
return new Command("scan")
|
|
@@ -117,10 +120,11 @@ export function createScanCommand() {
|
|
|
117
120
|
.option("--format <format>", "Output format: text, json, sarif", "text")
|
|
118
121
|
.option("--staged", "Scan only git staged files")
|
|
119
122
|
.option("--diff <ref>", "Scan files changed since a git ref")
|
|
123
|
+
.option("--range <range>", "Scan only files changed in a git commit range, e.g. main..HEAD")
|
|
120
124
|
.option("--engine <engine>", "Scan engine: betterleaks or patterns", "auto")
|
|
121
125
|
.option("--baseline", "Filter findings present in the saved baseline")
|
|
122
126
|
.option("--watch", "Watch for file changes and re-scan on change")
|
|
123
|
-
.option("--history", "Scan
|
|
127
|
+
.option("--history", "Scan every commit on every ref instead of the working tree")
|
|
124
128
|
.option("--no-gitignore", "Scan files even if .gitignore would exclude them (default: respect .gitignore)")
|
|
125
129
|
.option("--no-auto-update", "Do not auto-update a stale managed betterleaks binary; fall back to the patterns engine instead")
|
|
126
130
|
.action(async (scanPath, opts) => {
|
|
@@ -144,17 +148,31 @@ export function createScanCommand() {
|
|
|
144
148
|
if (isAgentScan) {
|
|
145
149
|
process.stderr.write("Warning: rafter agent scan is deprecated and will be removed in a future major version. Use rafter secrets instead.\n");
|
|
146
150
|
}
|
|
147
|
-
// Load policy-merged config for excludePaths/customPatterns/ignore
|
|
151
|
+
// Load policy-merged config for excludePaths/customPatterns/ignore.
|
|
152
|
+
// Discover and anchor the policy from the scan TARGET, not the process
|
|
153
|
+
// cwd (sable-lu0j), so an ambient `.rafter.yml` in cwd cannot suppress
|
|
154
|
+
// real secrets in a scan of a different tree. For `--staged`/`--diff` the
|
|
155
|
+
// default target is "." (cwd), which is the repo the command runs in.
|
|
156
|
+
const scanBase = path.resolve(scanPath);
|
|
157
|
+
const rafterignoreDir = fs.existsSync(scanBase) && fs.statSync(scanBase).isDirectory()
|
|
158
|
+
? scanBase
|
|
159
|
+
: path.dirname(scanBase);
|
|
148
160
|
const manager = new ConfigManager();
|
|
149
|
-
const cfg = manager.loadWithPolicy();
|
|
161
|
+
const cfg = manager.loadWithPolicy(scanBase);
|
|
150
162
|
const scanCfg = cfg.agent?.scan;
|
|
151
|
-
const suppressions = collectSuppressions(scanCfg?.ignore);
|
|
163
|
+
const suppressions = collectSuppressions(scanCfg?.ignore, scanCfg?.ignoreBaseDir, rafterignoreDir);
|
|
152
164
|
const baselineEntries = opts.baseline ? loadBaselineEntries() : [];
|
|
153
165
|
// Handle --diff flag
|
|
154
166
|
if (opts.diff) {
|
|
155
167
|
await scanDiffFiles(opts.diff, opts, scanCfg, baselineEntries, path.resolve(scanPath), suppressions);
|
|
156
168
|
return;
|
|
157
169
|
}
|
|
170
|
+
// Handle --range flag (a commit range a..b; reuses the diff plumbing so a
|
|
171
|
+
// user can scope the scan to the changed files on a huge repo).
|
|
172
|
+
if (opts.range) {
|
|
173
|
+
await scanRangeFiles(opts.range, opts, scanCfg, baselineEntries, path.resolve(scanPath), suppressions);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
158
176
|
// Handle --staged flag
|
|
159
177
|
if (opts.staged) {
|
|
160
178
|
await scanStagedFiles(opts, scanCfg, baselineEntries, path.resolve(scanPath), suppressions);
|
|
@@ -172,23 +190,28 @@ export function createScanCommand() {
|
|
|
172
190
|
return;
|
|
173
191
|
}
|
|
174
192
|
// Determine scan engine
|
|
175
|
-
const
|
|
193
|
+
const requestedEngine = opts.engine || "auto";
|
|
194
|
+
const selected = await selectEngine(requestedEngine, opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
|
|
195
|
+
const engine = selected.engine;
|
|
196
|
+
// Threaded through the scan so a scan-time fall-back (e.g. a betterleaks
|
|
197
|
+
// timeout) updates it before the output reports the engine actually used.
|
|
198
|
+
const report = buildEngineReport(requestedEngine, engine, selected.reason);
|
|
176
199
|
// Determine if path is file or directory
|
|
177
200
|
const stats = fs.statSync(resolvedPath);
|
|
178
201
|
let results;
|
|
179
202
|
if (stats.isDirectory()) {
|
|
180
203
|
if (!opts.quiet) {
|
|
181
|
-
console.error(`Scanning directory: ${resolvedPath} (${engine})`);
|
|
204
|
+
console.error(`Scanning directory: ${resolvedPath} (${engineLabel(engine)})`);
|
|
182
205
|
}
|
|
183
|
-
results = await scanDirectory(resolvedPath, engine, scanCfg, opts.history, opts.gitignore);
|
|
206
|
+
results = await scanDirectory(resolvedPath, engine, scanCfg, opts.history, opts.gitignore, report);
|
|
184
207
|
}
|
|
185
208
|
else {
|
|
186
209
|
if (!opts.quiet) {
|
|
187
|
-
console.error(`Scanning file: ${resolvedPath} (${engine})`);
|
|
210
|
+
console.error(`Scanning file: ${resolvedPath} (${engineLabel(engine)})`);
|
|
188
211
|
}
|
|
189
|
-
results = await scanFile(resolvedPath, engine, scanCfg);
|
|
212
|
+
results = await scanFile(resolvedPath, engine, scanCfg, report);
|
|
190
213
|
}
|
|
191
|
-
outputScanResults(applyBaseline(results, baselineEntries), opts, undefined, true, suppressions);
|
|
214
|
+
outputScanResults(applyBaseline(results, baselineEntries), opts, undefined, true, suppressions, report);
|
|
192
215
|
});
|
|
193
216
|
}
|
|
194
217
|
/**
|
|
@@ -207,13 +230,19 @@ export function createSecretsCommand() {
|
|
|
207
230
|
* Order matters — first match wins, and policy rules are checked first so an
|
|
208
231
|
* explicit reason wins over a bare .rafterignore line covering the same finding.
|
|
209
232
|
*/
|
|
210
|
-
export function collectSuppressions(policyIgnore) {
|
|
211
|
-
|
|
233
|
+
export function collectSuppressions(policyIgnore, policyBaseDir, rafterignoreDir = process.cwd()) {
|
|
234
|
+
// Anchor policy globs to the `.rafter.yml`'s directory and `.rafterignore`
|
|
235
|
+
// globs to the scanned tree (sable-lu0j), so neither can suppress findings in
|
|
236
|
+
// a different tree.
|
|
237
|
+
return [
|
|
238
|
+
...policyIgnoreToSuppressions(policyIgnore, policyBaseDir),
|
|
239
|
+
...loadSuppressions(rafterignoreDir),
|
|
240
|
+
];
|
|
212
241
|
}
|
|
213
242
|
/**
|
|
214
243
|
* Emit SARIF 2.1.0 JSON for GitHub/GitLab security tab integration
|
|
215
244
|
*/
|
|
216
|
-
function outputSarif(results) {
|
|
245
|
+
function outputSarif(results, emptyExitCode = 0) {
|
|
217
246
|
const rules = new Map();
|
|
218
247
|
const sarifResults = [];
|
|
219
248
|
for (const r of results) {
|
|
@@ -259,22 +288,50 @@ function outputSarif(results) {
|
|
|
259
288
|
],
|
|
260
289
|
};
|
|
261
290
|
console.log(JSON.stringify(sarif, null, 2));
|
|
262
|
-
|
|
291
|
+
// A read error (sable-ilbx) makes an otherwise-empty scan non-clean; the
|
|
292
|
+
// caller passes that exit code in.
|
|
293
|
+
process.exit(results.length > 0 ? 1 : emptyExitCode);
|
|
263
294
|
}
|
|
264
295
|
/**
|
|
265
296
|
* Shared output logic for scan results
|
|
266
297
|
*/
|
|
267
|
-
function outputScanResults(results, opts, context, exitOnFindings = true, suppressions = []) {
|
|
298
|
+
function outputScanResults(results, opts, context, exitOnFindings = true, suppressions = [], report) {
|
|
268
299
|
const format = opts.format ?? (opts.json ? "json" : "text");
|
|
269
300
|
if (!["text", "json", "sarif"].includes(format)) {
|
|
270
301
|
console.error(`Invalid format: ${format}. Valid values: text, json, sarif`);
|
|
271
302
|
process.exit(2);
|
|
272
303
|
}
|
|
304
|
+
// Partition off files that could not be read (sable-ilbx). A read failure is
|
|
305
|
+
// not a finding and not a clean pass — surface it so "no secrets detected"
|
|
306
|
+
// cannot stand in for "could not open". Warn on stderr (so stdout stays
|
|
307
|
+
// parseable), report it in the JSON/text body, and let it set the exit code.
|
|
308
|
+
const readErrors = results
|
|
309
|
+
.filter((r) => r.readError)
|
|
310
|
+
.map((r) => ({ file: r.file, error: r.readError }));
|
|
311
|
+
const scanResults = results.filter((r) => !r.readError);
|
|
312
|
+
if (readErrors.length > 0 && !opts.quiet) {
|
|
313
|
+
console.error(fmt.warning(`Could not read ${readErrors.length} file(s); results may be incomplete.`));
|
|
314
|
+
}
|
|
315
|
+
// No silent downgrade (sable-jiqb): when betterleaks was requested/auto but
|
|
316
|
+
// patterns ran instead, say so on stderr so it is visible in every format
|
|
317
|
+
// without corrupting a JSON/SARIF stdout payload. The JSON body below also
|
|
318
|
+
// carries it as structured fields.
|
|
319
|
+
if (report?.degraded && !opts.quiet) {
|
|
320
|
+
console.error(fmt.warning(`Engine: requested ${report.requested}, used ${engineLabel(report.used)}` +
|
|
321
|
+
(report.reason ? ` (${report.reason})` : "")));
|
|
322
|
+
}
|
|
273
323
|
// Split suppressed findings off the main result list. Both engines feed
|
|
274
324
|
// through here, so policy-driven suppression applies regardless of source.
|
|
275
|
-
const { results: keptResults, suppressed } = applySuppressions(
|
|
325
|
+
const { results: keptResults, suppressed } = applySuppressions(scanResults, suppressions);
|
|
326
|
+
// Exit code: findings dominate (1); otherwise an unreadable file is a scan
|
|
327
|
+
// that could not complete cleanly (2); a clean, complete scan is 0.
|
|
328
|
+
const scanExitCode = keptResults.length > 0 ? 1 : readErrors.length > 0 ? 2 : 0;
|
|
329
|
+
// Before the format branches, so --json and SARIF runs are recorded too. A
|
|
330
|
+
// clean scan is recorded as well: "we looked on this date and found nothing"
|
|
331
|
+
// is what a postmortem needs.
|
|
332
|
+
logScanToAudit(keptResults, context);
|
|
276
333
|
if (format === "sarif") {
|
|
277
|
-
outputSarif(keptResults);
|
|
334
|
+
outputSarif(keptResults, scanExitCode);
|
|
278
335
|
return;
|
|
279
336
|
}
|
|
280
337
|
if (format === "json" || opts.json) {
|
|
@@ -299,12 +356,26 @@ function outputScanResults(results, opts, context, exitOnFindings = true, suppre
|
|
|
299
356
|
triage_applied: false,
|
|
300
357
|
results: filesOut,
|
|
301
358
|
};
|
|
359
|
+
// sable-jiqb — report the engine actually used and any fall-back, so a
|
|
360
|
+
// json/MCP/CI consumer is never left thinking betterleaks ran when it did not.
|
|
361
|
+
if (report) {
|
|
362
|
+
out.engine_requested = report.requested;
|
|
363
|
+
out.engine_used = report.used;
|
|
364
|
+
out.degraded = report.degraded;
|
|
365
|
+
if (report.degraded && report.reason)
|
|
366
|
+
out.degraded_reason = report.reason;
|
|
367
|
+
}
|
|
302
368
|
if (suppressed.length > 0) {
|
|
303
369
|
out._suppressed = suppressed;
|
|
304
370
|
}
|
|
371
|
+
// Files that could not be read (sable-ilbx) — a reader can tell an empty
|
|
372
|
+
// `results` from an incomplete scan.
|
|
373
|
+
if (readErrors.length > 0) {
|
|
374
|
+
out.unreadable = readErrors;
|
|
375
|
+
}
|
|
305
376
|
console.log(JSON.stringify(out, null, 2));
|
|
306
377
|
if (exitOnFindings)
|
|
307
|
-
process.exit(
|
|
378
|
+
process.exit(scanExitCode);
|
|
308
379
|
return;
|
|
309
380
|
}
|
|
310
381
|
// Text output — note suppression on stderr so stdout remains parseable.
|
|
@@ -312,12 +383,21 @@ function outputScanResults(results, opts, context, exitOnFindings = true, suppre
|
|
|
312
383
|
console.error(fmt.info(`(${suppressed.length} finding(s) hidden by .rafter.yml)`));
|
|
313
384
|
}
|
|
314
385
|
if (keptResults.length === 0) {
|
|
315
|
-
if (
|
|
386
|
+
if (readErrors.length > 0) {
|
|
387
|
+
// Not a clean pass — the scan could not read some files (sable-ilbx).
|
|
388
|
+
if (!opts.quiet) {
|
|
389
|
+
console.log(`\n${fmt.warning(`${readErrors.length} file(s) could not be read:`)}`);
|
|
390
|
+
for (const re of readErrors)
|
|
391
|
+
console.log(` ${re.file} (${re.error})`);
|
|
392
|
+
console.log();
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else if (!opts.quiet) {
|
|
316
396
|
const msg = context ? `No secrets detected in ${context}` : "No secrets detected";
|
|
317
397
|
console.log(`\n${fmt.success(msg)}\n`);
|
|
318
398
|
}
|
|
319
399
|
if (exitOnFindings)
|
|
320
|
-
process.exit(
|
|
400
|
+
process.exit(scanExitCode);
|
|
321
401
|
return;
|
|
322
402
|
}
|
|
323
403
|
console.log(`\n${fmt.warning(`Found secrets in ${keptResults.length} file(s):`)}\n`);
|
|
@@ -336,6 +416,12 @@ function outputScanResults(results, opts, context, exitOnFindings = true, suppre
|
|
|
336
416
|
}
|
|
337
417
|
}
|
|
338
418
|
console.log(`\n${fmt.warning(`Total: ${totalMatches} secret(s) detected in ${keptResults.length} file(s)`)}\n`);
|
|
419
|
+
if (readErrors.length > 0 && !opts.quiet) {
|
|
420
|
+
console.log(`${fmt.warning(`${readErrors.length} file(s) could not be read:`)}`);
|
|
421
|
+
for (const re of readErrors)
|
|
422
|
+
console.log(` ${re.file} (${re.error})`);
|
|
423
|
+
console.log();
|
|
424
|
+
}
|
|
339
425
|
if (context === "staged files") {
|
|
340
426
|
console.log(`${fmt.error("Commit blocked. Remove secrets before committing.")}\n`);
|
|
341
427
|
}
|
|
@@ -343,7 +429,7 @@ function outputScanResults(results, opts, context, exitOnFindings = true, suppre
|
|
|
343
429
|
console.log(`Run 'rafter agent audit' to see the security log.\n`);
|
|
344
430
|
}
|
|
345
431
|
if (exitOnFindings)
|
|
346
|
-
process.exit(
|
|
432
|
+
process.exit(scanExitCode);
|
|
347
433
|
}
|
|
348
434
|
/**
|
|
349
435
|
* Scan files changed since a git ref (+ lines only in the unified diff).
|
|
@@ -356,6 +442,23 @@ async function scanDiffFiles(ref, opts, scanCfg, baselineEntries = [], scanPath,
|
|
|
356
442
|
}
|
|
357
443
|
await runGitAddedLineScan(["diff", "-U0", "--no-color", "--diff-filter=ACM", ref], opts, scanCfg, baselineEntries, scanPath, suppressions, `files changed since ${ref}`, `No files changed since ${ref}`);
|
|
358
444
|
}
|
|
445
|
+
/**
|
|
446
|
+
* Scan only the files changed in a git commit range (e.g. `main..HEAD`).
|
|
447
|
+
*
|
|
448
|
+
* sable-jiqb — on a repo large enough that a full betterleaks scan is slow, a
|
|
449
|
+
* user can scope the scan to a range's changed files. Reuses the `--diff`
|
|
450
|
+
* added-line plumbing (patterns engine; git line scope is incompatible with
|
|
451
|
+
* betterleaks' whole-file `dir` scan), with the range passed to `git diff` as a
|
|
452
|
+
* single revision argument.
|
|
453
|
+
*/
|
|
454
|
+
async function scanRangeFiles(range, opts, scanCfg, baselineEntries = [], scanPath, suppressions = []) {
|
|
455
|
+
// git would parse a leading "-" as one of its own options, not a revision range.
|
|
456
|
+
if (range.startsWith("-")) {
|
|
457
|
+
console.error(`Error: invalid range "${range}" (a git range cannot start with "-")`);
|
|
458
|
+
process.exit(2);
|
|
459
|
+
}
|
|
460
|
+
await runGitAddedLineScan(["diff", "-U0", "--no-color", "--diff-filter=ACM", range], opts, scanCfg, baselineEntries, scanPath, suppressions, `files changed in ${range}`, `No files changed in ${range}`);
|
|
461
|
+
}
|
|
359
462
|
/**
|
|
360
463
|
* Scan git staged files for secrets (+ lines only in the staged diff).
|
|
361
464
|
*/
|
|
@@ -368,11 +471,17 @@ async function scanStagedFiles(opts, scanCfg, baselineEntries = [], scanPath, su
|
|
|
368
471
|
*/
|
|
369
472
|
async function runGitAddedLineScan(gitArgs, opts, scanCfg, baselineEntries, scanPath, suppressions, contextLabel, emptyMessage, errorOpts) {
|
|
370
473
|
const cwd = scanPath && fs.existsSync(scanPath) && fs.statSync(scanPath).isDirectory() ? scanPath : undefined;
|
|
474
|
+
const diffReport = diffModeReport(opts);
|
|
371
475
|
try {
|
|
372
476
|
const patch = execFileSync("git", gitArgs, {
|
|
373
477
|
encoding: "utf-8",
|
|
374
478
|
cwd,
|
|
375
479
|
stdio: ["pipe", "pipe", "ignore"],
|
|
480
|
+
// The default 1 MiB cap makes `git diff` throw ENOBUFS on ordinary large
|
|
481
|
+
// staged diffs (lockfiles, generated or vendored files). That crash used
|
|
482
|
+
// to exit 1 — indistinguishable from a real finding, so a commit gate
|
|
483
|
+
// reported a broken scan as a possible secret (#274). Read the whole diff.
|
|
484
|
+
maxBuffer: gitDiffMaxBuffer(),
|
|
376
485
|
});
|
|
377
486
|
if (!patch.trim()) {
|
|
378
487
|
if (!opts.quiet) {
|
|
@@ -380,7 +489,7 @@ async function runGitAddedLineScan(gitArgs, opts, scanCfg, baselineEntries, scan
|
|
|
380
489
|
// --json, and outputScanResults owns the single stdout success line.
|
|
381
490
|
console.error(fmt.success(emptyMessage));
|
|
382
491
|
}
|
|
383
|
-
outputScanResults([], opts, contextLabel, true, suppressions);
|
|
492
|
+
outputScanResults([], opts, contextLabel, true, suppressions, diffReport);
|
|
384
493
|
return;
|
|
385
494
|
}
|
|
386
495
|
const addedLines = parseUnifiedDiffAddedLines(patch);
|
|
@@ -390,7 +499,7 @@ async function runGitAddedLineScan(gitArgs, opts, scanCfg, baselineEntries, scan
|
|
|
390
499
|
// --json, and outputScanResults owns the single stdout success line.
|
|
391
500
|
console.error(fmt.success(emptyMessage));
|
|
392
501
|
}
|
|
393
|
-
outputScanResults([], opts, contextLabel, true, suppressions);
|
|
502
|
+
outputScanResults([], opts, contextLabel, true, suppressions, diffReport);
|
|
394
503
|
return;
|
|
395
504
|
}
|
|
396
505
|
const repoRoot = execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
@@ -404,16 +513,73 @@ async function runGitAddedLineScan(gitArgs, opts, scanCfg, baselineEntries, scan
|
|
|
404
513
|
}
|
|
405
514
|
const allResults = scanAddedDiffLines(addedLines, repoRoot, scanCfg?.customPatterns);
|
|
406
515
|
const filtered = applyExcludePaths(allResults, scanCfg?.excludePaths, repoRoot);
|
|
407
|
-
outputScanResults(applyBaseline(filtered, baselineEntries), opts, contextLabel, true, suppressions);
|
|
516
|
+
outputScanResults(applyBaseline(filtered, baselineEntries), opts, contextLabel, true, suppressions, diffReport);
|
|
408
517
|
}
|
|
409
518
|
catch (error) {
|
|
410
519
|
if (error.status === 128) {
|
|
411
520
|
console.error(errorOpts?.notRepoMessage ?? "Error: Not in a git repository or invalid ref");
|
|
412
521
|
process.exit(2);
|
|
413
522
|
}
|
|
414
|
-
|
|
523
|
+
// A scan that could not run is a scan ERROR, never a finding. Exit 2 (per
|
|
524
|
+
// CLI_SPEC: invalid input / scan error) so a commit gate keying on the exit
|
|
525
|
+
// code never mistakes a broken scan for a detected secret (#274). ENOBUFS is
|
|
526
|
+
// the known case (a diff past the buffer), but any failure to read/parse the
|
|
527
|
+
// diff belongs here rather than surfacing as exit 1.
|
|
528
|
+
const detail = error?.code === "ENOBUFS"
|
|
529
|
+
? "staged diff too large for the scanner to read"
|
|
530
|
+
: (error?.message ?? String(error));
|
|
531
|
+
console.error(`Error: secret scan could not run (${detail}); no scan was performed`);
|
|
532
|
+
process.exit(2);
|
|
415
533
|
}
|
|
416
534
|
}
|
|
535
|
+
/** Self-explanatory display label for a resolved engine — "both" alone
|
|
536
|
+
* doesn't say which two engines ran (issue #231 item 6). */
|
|
537
|
+
export function engineLabel(engine) {
|
|
538
|
+
return engine === "both" ? "both: betterleaks + patterns" : engine;
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Build the initial engine report from the requested engine and the engine
|
|
542
|
+
* `selectEngine` resolved to. A resolution of `patterns` when betterleaks was
|
|
543
|
+
* wanted (explicit or via `auto`) is a degradation; `selectReason` explains it.
|
|
544
|
+
* Scan-time failures (a timeout mid-scan) further mutate this report in the
|
|
545
|
+
* scanners below.
|
|
546
|
+
*/
|
|
547
|
+
export function buildEngineReport(requested, resolved, selectReason = null) {
|
|
548
|
+
const wantedBetterleaks = requested === "auto" || requested === "betterleaks";
|
|
549
|
+
const degraded = wantedBetterleaks && resolved === "patterns";
|
|
550
|
+
return {
|
|
551
|
+
requested,
|
|
552
|
+
used: resolved,
|
|
553
|
+
degraded,
|
|
554
|
+
reason: degraded ? (selectReason ?? "betterleaks unavailable; used the patterns engine") : undefined,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
/** Record a scan-time fall-back to the patterns engine on the report. */
|
|
558
|
+
function markDegraded(report, reason) {
|
|
559
|
+
if (!report)
|
|
560
|
+
return;
|
|
561
|
+
report.used = "patterns";
|
|
562
|
+
report.degraded = true;
|
|
563
|
+
report.reason = reason;
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* The engine report for the git-diff modes (`--diff` / `--range` / `--staged`),
|
|
567
|
+
* which scan added lines with the patterns engine by design (betterleaks'
|
|
568
|
+
* whole-file `dir` scan is incompatible with a line scope). Only an explicit
|
|
569
|
+
* `--engine betterleaks` is a degradation here; `auto` is satisfied by patterns.
|
|
570
|
+
*/
|
|
571
|
+
function diffModeReport(opts) {
|
|
572
|
+
const requested = opts.engine || "auto";
|
|
573
|
+
const degraded = requested === "betterleaks";
|
|
574
|
+
return {
|
|
575
|
+
requested,
|
|
576
|
+
used: "patterns",
|
|
577
|
+
degraded,
|
|
578
|
+
reason: degraded
|
|
579
|
+
? "diff/range/staged modes scan changed lines with the patterns engine"
|
|
580
|
+
: undefined,
|
|
581
|
+
};
|
|
582
|
+
}
|
|
417
583
|
/**
|
|
418
584
|
* Select scan engine based on availability and user preference.
|
|
419
585
|
*
|
|
@@ -430,9 +596,9 @@ async function runGitAddedLineScan(gitArgs, opts, scanCfg, baselineEntries, scan
|
|
|
430
596
|
* scanning. The caller resolves this from `--no-auto-update` and the
|
|
431
597
|
* `scan.auto_update_betterleaks` config key.
|
|
432
598
|
*/
|
|
433
|
-
async function selectEngine(preference, quiet, autoUpdate = true) {
|
|
599
|
+
export async function selectEngine(preference, quiet, autoUpdate = true) {
|
|
434
600
|
if (preference === "patterns") {
|
|
435
|
-
return "patterns";
|
|
601
|
+
return { engine: "patterns", reason: null };
|
|
436
602
|
}
|
|
437
603
|
if (preference === "betterleaks") {
|
|
438
604
|
const bl = new BetterleaksScanner();
|
|
@@ -441,7 +607,7 @@ async function selectEngine(preference, quiet, autoUpdate = true) {
|
|
|
441
607
|
if (!quiet) {
|
|
442
608
|
console.error(fmt.warning("Betterleaks requested but not available, using patterns"));
|
|
443
609
|
}
|
|
444
|
-
return "patterns";
|
|
610
|
+
return { engine: "patterns", reason: "betterleaks requested but not available" };
|
|
445
611
|
}
|
|
446
612
|
return ensureBetterleaksUsable(quiet, autoUpdate);
|
|
447
613
|
}
|
|
@@ -453,9 +619,11 @@ async function selectEngine(preference, quiet, autoUpdate = true) {
|
|
|
453
619
|
const bl = new BetterleaksScanner();
|
|
454
620
|
const available = await bl.isAvailable();
|
|
455
621
|
if (!available)
|
|
456
|
-
return "patterns";
|
|
622
|
+
return { engine: "patterns", reason: "betterleaks not available" };
|
|
457
623
|
const usable = await ensureBetterleaksUsable(quiet, autoUpdate);
|
|
458
|
-
return usable === "betterleaks"
|
|
624
|
+
return usable.engine === "betterleaks"
|
|
625
|
+
? { engine: "both", reason: null }
|
|
626
|
+
: { engine: "patterns", reason: usable.reason };
|
|
459
627
|
}
|
|
460
628
|
/**
|
|
461
629
|
* sable-o4k — guard against a stale rafter-managed betterleaks binary silently
|
|
@@ -471,7 +639,7 @@ async function selectEngine(preference, quiet, autoUpdate = true) {
|
|
|
471
639
|
async function ensureBetterleaksUsable(quiet, autoUpdate) {
|
|
472
640
|
const bm = new BinaryManager();
|
|
473
641
|
if (!(await bm.isManagedBetterleaksStale()))
|
|
474
|
-
return "betterleaks";
|
|
642
|
+
return { engine: "betterleaks", reason: null };
|
|
475
643
|
const current = await bm.getBetterleaksVersion();
|
|
476
644
|
const cta = "Fix manually with: rafter agent update-betterleaks";
|
|
477
645
|
const stale = `Stale betterleaks binary (${current}; expected v${BETTERLEAKS_VERSION})`;
|
|
@@ -479,7 +647,7 @@ async function ensureBetterleaksUsable(quiet, autoUpdate) {
|
|
|
479
647
|
if (!quiet) {
|
|
480
648
|
console.error(fmt.warning(`${stale}. Auto-update is off — using the patterns engine for this scan. ${cta}`));
|
|
481
649
|
}
|
|
482
|
-
return "patterns";
|
|
650
|
+
return { engine: "patterns", reason: `${stale}; auto-update off` };
|
|
483
651
|
}
|
|
484
652
|
// Only prompt when there's a human at a TTY; otherwise auto-update proceeds
|
|
485
653
|
// (default on) so non-interactive runs aren't left with a broken engine.
|
|
@@ -487,7 +655,7 @@ async function ensureBetterleaksUsable(quiet, autoUpdate) {
|
|
|
487
655
|
const ok = await askYesNo(`${stale}. Update it now?`, true);
|
|
488
656
|
if (!ok) {
|
|
489
657
|
console.error(fmt.warning(`Skipped — using the patterns engine for this scan. ${cta}`));
|
|
490
|
-
return "patterns";
|
|
658
|
+
return { engine: "patterns", reason: `${stale}; update declined` };
|
|
491
659
|
}
|
|
492
660
|
}
|
|
493
661
|
if (!quiet)
|
|
@@ -497,14 +665,14 @@ async function ensureBetterleaksUsable(quiet, autoUpdate) {
|
|
|
497
665
|
console.error(` ${m}`); }, BETTERLEAKS_VERSION);
|
|
498
666
|
if (!quiet)
|
|
499
667
|
console.error(fmt.success(`Betterleaks updated to v${BETTERLEAKS_VERSION}.`));
|
|
500
|
-
return "betterleaks";
|
|
668
|
+
return { engine: "betterleaks", reason: null };
|
|
501
669
|
}
|
|
502
670
|
catch (e) {
|
|
671
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
503
672
|
if (!quiet) {
|
|
504
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
505
673
|
console.error(fmt.warning(`Betterleaks update failed (${msg}) — using the patterns engine for this scan. ${cta}`));
|
|
506
674
|
}
|
|
507
|
-
return "patterns";
|
|
675
|
+
return { engine: "patterns", reason: `betterleaks update failed (${msg})` };
|
|
508
676
|
}
|
|
509
677
|
}
|
|
510
678
|
/**
|
|
@@ -518,11 +686,13 @@ export function autoUpdateEnabled(opts, scanCfg) {
|
|
|
518
686
|
/**
|
|
519
687
|
* Scan a file with selected engine
|
|
520
688
|
*/
|
|
521
|
-
async function scanFile(filePath, engine, scanCfg) {
|
|
689
|
+
export async function scanFile(filePath, engine, scanCfg, report) {
|
|
522
690
|
const runPatterns = () => {
|
|
523
691
|
const scanner = new RegexScanner(scanCfg?.customPatterns);
|
|
524
692
|
const result = scanner.scanFile(filePath);
|
|
525
|
-
|
|
693
|
+
// Keep the result when it has findings OR could not be read (sable-ilbx),
|
|
694
|
+
// so a permission-denied file is not reported as a clean scan.
|
|
695
|
+
return result.matches.length > 0 || result.readError ? [result] : [];
|
|
526
696
|
};
|
|
527
697
|
if (engine === "both") {
|
|
528
698
|
// sable-j85 — kick off betterleaks (async subprocess) first, run the
|
|
@@ -533,8 +703,9 @@ async function scanFile(filePath, engine, scanCfg) {
|
|
|
533
703
|
const result = await new BetterleaksScanner().scanFile(filePath);
|
|
534
704
|
return result.matches.length > 0 ? [result] : [];
|
|
535
705
|
}
|
|
536
|
-
catch {
|
|
706
|
+
catch (e) {
|
|
537
707
|
console.error(fmt.warning("Betterleaks scan failed, using patterns only for this run"));
|
|
708
|
+
markDegraded(report, errorText(e));
|
|
538
709
|
return [];
|
|
539
710
|
}
|
|
540
711
|
})();
|
|
@@ -549,54 +720,82 @@ async function scanFile(filePath, engine, scanCfg) {
|
|
|
549
720
|
}
|
|
550
721
|
catch (e) {
|
|
551
722
|
console.error(fmt.warning("Betterleaks scan failed, falling back to patterns"));
|
|
723
|
+
markDegraded(report, errorText(e));
|
|
552
724
|
return runPatterns();
|
|
553
725
|
}
|
|
554
726
|
}
|
|
555
727
|
return runPatterns();
|
|
556
728
|
}
|
|
729
|
+
/** An engine failure's message, e.g. "Betterleaks scan failed (timed out after 60s)". */
|
|
730
|
+
function errorText(e) {
|
|
731
|
+
return e instanceof Error ? e.message : String(e);
|
|
732
|
+
}
|
|
557
733
|
/**
|
|
558
734
|
* Scan a directory with selected engine
|
|
559
735
|
*/
|
|
560
|
-
async function scanDirectory(dirPath, engine, scanCfg, history, respectGitignore) {
|
|
561
|
-
// respectGitignore default is true.
|
|
562
|
-
// RegexScanner.scanDirectory
|
|
563
|
-
//
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
736
|
+
export async function scanDirectory(dirPath, engine, scanCfg, history, respectGitignore, report) {
|
|
737
|
+
// respectGitignore default is true. Both engines honor it: the patterns
|
|
738
|
+
// engine via RegexScanner.scanDirectory, betterleaks by scanning only the
|
|
739
|
+
// files git does not ignore (BetterleaksScanner.scanDirectory, #262).
|
|
740
|
+
// sable-28lv — with --history BOTH engines read the commit history. The
|
|
741
|
+
// patterns engine used to scan the working tree instead, so without
|
|
742
|
+
// betterleaks `--history` reported a clean history it had never read.
|
|
743
|
+
let repoRoot = null;
|
|
744
|
+
if (history) {
|
|
745
|
+
try {
|
|
746
|
+
repoRoot = gitRepoRoot(dirPath);
|
|
747
|
+
}
|
|
748
|
+
catch (e) {
|
|
749
|
+
console.error(`Error: ${errorText(e)}`);
|
|
750
|
+
process.exit(2);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
const runPatterns = async () => {
|
|
754
|
+
if (!history) {
|
|
755
|
+
return new RegexScanner(scanCfg?.customPatterns).scanDirectory(dirPath, {
|
|
756
|
+
excludePaths: scanCfg?.excludePaths,
|
|
757
|
+
respectGitignore,
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
try {
|
|
761
|
+
return await scanGitHistory(dirPath, scanCfg?.customPatterns);
|
|
762
|
+
}
|
|
763
|
+
catch (e) {
|
|
764
|
+
console.error(`Error: ${errorText(e)}`);
|
|
765
|
+
process.exit(2);
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
// `betterleaks git` reports paths relative to the repo root; make them
|
|
769
|
+
// absolute like every other scan, so the union merges the two engines.
|
|
770
|
+
const runBetterleaks = async () => {
|
|
771
|
+
const found = await new BetterleaksScanner().scanDirectory(dirPath, { useGit: history ?? false, respectGitignore });
|
|
772
|
+
return repoRoot ? found.map((r) => ({ ...r, file: path.resolve(repoRoot, r.file) })) : found;
|
|
570
773
|
};
|
|
571
774
|
let results;
|
|
572
775
|
if (engine === "both") {
|
|
573
|
-
// sable-j85 — start betterleaks (async subprocess) first,
|
|
574
|
-
//
|
|
575
|
-
//
|
|
576
|
-
const blPromise = (
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
return [];
|
|
583
|
-
}
|
|
584
|
-
})();
|
|
585
|
-
const patternsResults = runPatterns();
|
|
776
|
+
// sable-j85 — start betterleaks (async subprocess) first, run the patterns
|
|
777
|
+
// engine while it runs, then union. The exclude-paths post-filter below
|
|
778
|
+
// applies to the merged set.
|
|
779
|
+
const blPromise = runBetterleaks().catch((e) => {
|
|
780
|
+
console.error(fmt.warning(`${errorText(e)}; using patterns only for this run`));
|
|
781
|
+
markDegraded(report, errorText(e));
|
|
782
|
+
return [];
|
|
783
|
+
});
|
|
784
|
+
const patternsResults = await runPatterns();
|
|
586
785
|
results = unionScanResults(await blPromise, patternsResults);
|
|
587
786
|
}
|
|
588
787
|
else if (engine === "betterleaks") {
|
|
589
788
|
try {
|
|
590
|
-
|
|
591
|
-
results = await bl.scanDirectory(dirPath, { useGit: history ?? false });
|
|
789
|
+
results = await runBetterleaks();
|
|
592
790
|
}
|
|
593
791
|
catch (e) {
|
|
594
|
-
console.error(fmt.warning(
|
|
595
|
-
|
|
792
|
+
console.error(fmt.warning(`${errorText(e)}; falling back to patterns`));
|
|
793
|
+
markDegraded(report, errorText(e));
|
|
794
|
+
results = await runPatterns();
|
|
596
795
|
}
|
|
597
796
|
}
|
|
598
797
|
else {
|
|
599
|
-
results = runPatterns();
|
|
798
|
+
results = await runPatterns();
|
|
600
799
|
}
|
|
601
800
|
// sable-yz0 — post-filter by `.rafter.yml scan.exclude_paths`. Two bugs
|
|
602
801
|
// motivated this chokepoint:
|
|
@@ -619,9 +818,9 @@ async function scanDirectory(dirPath, engine, scanCfg, history, respectGitignore
|
|
|
619
818
|
async function watchAndScan(watchPath, opts, scanCfg, suppressions = []) {
|
|
620
819
|
const { watch } = await import("chokidar");
|
|
621
820
|
const logger = new AuditLogger();
|
|
622
|
-
const engine = await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg));
|
|
821
|
+
const engine = (await selectEngine(opts.engine || "auto", opts.quiet || false, autoUpdateEnabled(opts, scanCfg))).engine;
|
|
623
822
|
if (!opts.quiet) {
|
|
624
|
-
console.error(fmt.info(`Watching ${watchPath} for changes (${engine}). Press Ctrl+C to exit.`));
|
|
823
|
+
console.error(fmt.info(`Watching ${watchPath} for changes (${engineLabel(engine)}). Press Ctrl+C to exit.`));
|
|
625
824
|
}
|
|
626
825
|
// Do an initial scan
|
|
627
826
|
const stats = fs.statSync(watchPath);
|
|
@@ -689,6 +888,45 @@ async function watchAndScan(watchPath, opts, scanCfg, suppressions = []) {
|
|
|
689
888
|
});
|
|
690
889
|
});
|
|
691
890
|
}
|
|
891
|
+
/**
|
|
892
|
+
* Record that a scan ran, so `rafter agent audit` has something in it.
|
|
893
|
+
*
|
|
894
|
+
* `rafter secrets` told the user "Run 'rafter agent audit' to see the security
|
|
895
|
+
* log" and the log was always empty, because only command interception ever
|
|
896
|
+
* wrote to it — three of seven beta users followed that hint and hit a dead
|
|
897
|
+
* end, one of them immediately after a correct and useful scan result. The
|
|
898
|
+
* first thing a careful person does with a security finding is look for the
|
|
899
|
+
* record of it.
|
|
900
|
+
*
|
|
901
|
+
* Counts and pattern NAMES only — never a matched value, not even redacted.
|
|
902
|
+
* The audit log is a postmortem artifact that outlives the terminal, so it
|
|
903
|
+
* must not become a second place secrets live.
|
|
904
|
+
*/
|
|
905
|
+
function logScanToAudit(results, context) {
|
|
906
|
+
try {
|
|
907
|
+
const totalMatches = results.reduce((n, r) => n + r.matches.length, 0);
|
|
908
|
+
const patterns = [...new Set(results.flatMap((r) => r.matches.map((m) => m.pattern.name)))].sort();
|
|
909
|
+
new AuditLogger().log({
|
|
910
|
+
eventType: "scan_executed",
|
|
911
|
+
securityCheck: {
|
|
912
|
+
passed: totalMatches === 0,
|
|
913
|
+
reason: totalMatches === 0
|
|
914
|
+
? "Secret scan completed with no findings"
|
|
915
|
+
: `Secret scan found ${totalMatches} secret(s) in ${results.length} file(s)`,
|
|
916
|
+
details: {
|
|
917
|
+
filesWithFindings: results.length,
|
|
918
|
+
totalMatches,
|
|
919
|
+
patterns,
|
|
920
|
+
...(context ? { context } : {}),
|
|
921
|
+
},
|
|
922
|
+
},
|
|
923
|
+
resolution: { actionTaken: totalMatches === 0 ? "allowed" : "blocked" },
|
|
924
|
+
});
|
|
925
|
+
}
|
|
926
|
+
catch {
|
|
927
|
+
// Never let audit logging break a scan — the scan result is the product.
|
|
928
|
+
}
|
|
929
|
+
}
|
|
692
930
|
/**
|
|
693
931
|
* Log watch findings to audit log
|
|
694
932
|
*/
|