@rafter-security/cli 0.10.5 → 0.11.1

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.
Files changed (41) hide show
  1. package/dist/commands/agent/exec.js +26 -9
  2. package/dist/commands/agent/init.js +286 -225
  3. package/dist/commands/agent/install-hook.js +19 -8
  4. package/dist/commands/agent/scan.js +270 -70
  5. package/dist/commands/agent/status.js +2 -2
  6. package/dist/commands/agent/update-skill-scanner.js +1 -1
  7. package/dist/commands/agent/verify.js +39 -15
  8. package/dist/commands/backend/get.js +6 -2
  9. package/dist/commands/backend/run.js +25 -2
  10. package/dist/commands/backend/scan-status.js +34 -4
  11. package/dist/commands/hook/pretool.js +124 -36
  12. package/dist/commands/mcp/server.js +184 -129
  13. package/dist/commands/report.js +6 -2
  14. package/dist/core/audit-logger.js +47 -17
  15. package/dist/core/config-manager.js +46 -8
  16. package/dist/core/custom-patterns.js +28 -8
  17. package/dist/core/docs-loader.js +33 -3
  18. package/dist/core/pattern-engine.js +23 -5
  19. package/dist/core/policy-loader.js +15 -4
  20. package/dist/core/risk-rules.js +391 -9
  21. package/dist/index.js +7 -1
  22. package/dist/scanners/betterleaks.js +127 -14
  23. package/dist/scanners/git-history-scan.js +75 -0
  24. package/dist/scanners/regex-scanner.js +16 -2
  25. package/dist/scanners/secret-patterns.js +5 -1
  26. package/dist/scanners/union.js +31 -19
  27. package/dist/utils/api.js +35 -1
  28. package/dist/utils/env-guard.js +14 -14
  29. package/dist/utils/git-diff.js +36 -20
  30. package/dist/utils/git.js +164 -52
  31. package/dist/utils/skill-manager.js +4 -3
  32. package/package.json +16 -15
  33. package/resources/continue-rules/rafter.md +1 -1
  34. package/resources/cursor-rules/rafter.mdc +1 -1
  35. package/resources/rafter-security-skill.md +3 -1
  36. package/resources/skills/rafter/SKILL.md +1 -2
  37. package/resources/skills/rafter/docs/guardrails.md +1 -1
  38. package/resources/skills/rafter-code-review/SKILL.md +0 -1
  39. package/resources/skills/rafter-secure-design/SKILL.md +0 -1
  40. package/resources/skills/rafter-skill-review/SKILL.md +0 -1
  41. package/resources/windsurf-rules/rafter.md +1 -1
@@ -0,0 +1,75 @@
1
+ import { execFileSync, spawn } from "child_process";
2
+ import path from "path";
3
+ import { createAddedLineParser } from "../utils/git-diff.js";
4
+ import { RegexScanner } from "./regex-scanner.js";
5
+ /**
6
+ * The repository root containing `dirPath`. Throws when `dirPath` is not
7
+ * inside a git work tree, so `--history` never reports a clean scan of a
8
+ * history it could not read.
9
+ */
10
+ export function gitRepoRoot(dirPath) {
11
+ try {
12
+ return execFileSync("git", ["rev-parse", "--show-toplevel"], {
13
+ cwd: dirPath, encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"],
14
+ }).trim();
15
+ }
16
+ catch {
17
+ throw new Error(`--history: not a git repository: ${dirPath}`);
18
+ }
19
+ }
20
+ /**
21
+ * Scan every line ever added in the repository's history with the patterns
22
+ * engine (sable-28lv). Reads the same commits as `betterleaks git`
23
+ * (`git log -p -U0 --full-history --all`), streamed line by line, so memory
24
+ * holds one line plus the findings, whatever the size of the history.
25
+ *
26
+ * Files are reported as absolute paths under the repo root and lines as the
27
+ * line number in the commit that added them, the same shape betterleaks
28
+ * history results take after `absolutize`, so the union merges the two.
29
+ */
30
+ export async function scanGitHistory(dirPath, customPatterns) {
31
+ const repoRoot = gitRepoRoot(dirPath);
32
+ const scanner = new RegexScanner(customPatterns);
33
+ const feed = createAddedLineParser();
34
+ const byFile = new Map();
35
+ const onLine = (rawLine) => {
36
+ const added = feed(rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine);
37
+ if (!added)
38
+ return;
39
+ for (const m of scanner.scanLine(added.text, added.line)) {
40
+ const file = path.join(repoRoot, added.file);
41
+ let found = byFile.get(file);
42
+ if (!found)
43
+ byFile.set(file, (found = new Map()));
44
+ // The same secret re-added at the same place in a later commit is one finding.
45
+ const key = `${m.line} ${m.column} ${m.match}`;
46
+ if (!found.has(key))
47
+ found.set(key, m);
48
+ }
49
+ };
50
+ await new Promise((resolve, reject) => {
51
+ const child = spawn("git", ["-c", "core.quotePath=false", "log", "-p", "-U0", "--no-color", "--no-ext-diff", "--no-textconv",
52
+ "--full-history", "--all", "--format=commit %H"], { cwd: repoRoot, stdio: ["ignore", "pipe", "pipe"] });
53
+ let pending = "";
54
+ let stderr = "";
55
+ child.stdout.setEncoding("utf-8");
56
+ child.stdout.on("data", (chunk) => {
57
+ const lines = (pending + chunk).split("\n");
58
+ pending = lines.pop();
59
+ for (const l of lines)
60
+ onLine(l);
61
+ });
62
+ child.stderr.on("data", (d) => { stderr += d; });
63
+ child.on("error", (e) => reject(new Error(`--history: git log failed: ${e.message}`)));
64
+ child.on("close", (code) => {
65
+ if (pending)
66
+ onLine(pending);
67
+ // An empty repository has no history to read; git says so with exit 128.
68
+ if (code === 0 || /does not have any commits/.test(stderr))
69
+ resolve();
70
+ else
71
+ reject(new Error(`--history: git log failed (exit ${code}): ${stderr.trim()}`));
72
+ });
73
+ });
74
+ return [...byFile.entries()].map(([file, found]) => ({ file, matches: [...found.values()] }));
75
+ }
@@ -4,6 +4,14 @@ import path from "path";
4
4
  import { PatternEngine } from "../core/pattern-engine.js";
5
5
  import { DEFAULT_SECRET_PATTERNS } from "./secret-patterns.js";
6
6
  import { loadCustomPatterns } from "../core/custom-patterns.js";
7
+ /** A human-readable reason for a file read failure. */
8
+ function readErrorReason(e) {
9
+ if (e && e.code === "EACCES")
10
+ return "permission denied";
11
+ if (e && e.code)
12
+ return String(e.code);
13
+ return e instanceof Error ? e.message : "could not read file";
14
+ }
7
15
  export class RegexScanner {
8
16
  constructor(customPatterns) {
9
17
  const patterns = [...DEFAULT_SECRET_PATTERNS, ...loadCustomPatterns()];
@@ -29,7 +37,9 @@ export class RegexScanner {
29
37
  return { file: filePath, matches };
30
38
  }
31
39
  catch (e) {
32
- return { file: filePath, matches: [] };
40
+ // A file we could not read is not a clean scan — surface it distinctly
41
+ // (sable-ilbx) so "no findings" cannot mean "could not open".
42
+ return { file: filePath, matches: [], readError: readErrorReason(e) };
33
43
  }
34
44
  }
35
45
  /**
@@ -39,7 +49,9 @@ export class RegexScanner {
39
49
  const results = [];
40
50
  for (const filePath of filePaths) {
41
51
  const result = this.scanFile(filePath);
42
- if (result.matches.length > 0) {
52
+ // Keep a result that has findings, and also one that failed to read, so
53
+ // a permission-denied file is not silently dropped (sable-ilbx).
54
+ if (result.matches.length > 0 || result.readError) {
43
55
  results.push(result);
44
56
  }
45
57
  }
@@ -49,6 +61,7 @@ export class RegexScanner {
49
61
  * Scan a directory recursively
50
62
  */
51
63
  scanDirectory(dirPath, options) {
64
+ // Keep in step with DEFAULT_EXCLUDE in python/rafter_cli/scanners/regex_scanner.py.
52
65
  const exclude = options?.exclude || [
53
66
  "node_modules",
54
67
  ".git",
@@ -66,6 +79,7 @@ export class RegexScanner {
66
79
  ".tox",
67
80
  ".mypy_cache",
68
81
  ".pytest_cache",
82
+ ".ruff_cache",
69
83
  "results",
70
84
  ".terraform",
71
85
  "bower_components"
@@ -6,7 +6,11 @@ export const DEFAULT_SECRET_PATTERNS = [
6
6
  // AWS
7
7
  {
8
8
  name: "AWS Access Key ID",
9
- regex: "(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)[A-Z0-9]{16}",
9
+ // Negative lookahead excludes the canonical AWS-docs example key
10
+ // (AKIAIOSFODNN7EXAMPLE and its siblings, which all end in EXAMPLE).
11
+ // betterleaks skips these too (sable/#230); matching that keeps the two
12
+ // engines from disagreeing on a well-known public non-secret.
13
+ regex: "(A3T[A-Z0-9]|AKIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASIA)(?![A-Z0-9]{9}EXAMPLE)[A-Z0-9]{16}",
10
14
  severity: "critical",
11
15
  description: "AWS Access Key ID detected"
12
16
  },
@@ -8,14 +8,11 @@
8
8
  * Running only one silently degrades coverage; the union restores it.
9
9
  *
10
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
11
+ * `(file, line, column)`, or `(file, line, column, matched-text)` when the
12
+ * position is unknown. Distinct secrets on one line (the same token pasted
13
+ * twice) sit at different columns and are both kept, because dropping a real
14
+ * finding is a security regression for a scanner. When both engines report a
15
+ * position we keep the betterleaks
19
16
  * match — its `RuleID`-style `pattern.name` is the canonical id — and record
20
17
  * both engines in `engines`. Ordering is deterministic: betterleaks findings
21
18
  * first (in betterleaks's own order), then any patterns-only findings, grouped
@@ -24,6 +21,9 @@
24
21
  export function unionScanResults(betterleaks, patterns) {
25
22
  const fileOrder = [];
26
23
  const byFile = new Map();
24
+ // Carry a read failure through the union (sable-ilbx): a file one engine
25
+ // could not open must still be reported as unreadable, not dropped.
26
+ const readErrorByFile = new Map();
27
27
  const ingest = (results, engine) => {
28
28
  for (const r of results) {
29
29
  let fileMap = byFile.get(r.file);
@@ -32,28 +32,40 @@ export function unionScanResults(betterleaks, patterns) {
32
32
  byFile.set(r.file, fileMap);
33
33
  fileOrder.push(r.file);
34
34
  }
35
+ if (r.readError && !readErrorByFile.has(r.file)) {
36
+ readErrorByFile.set(r.file, r.readError);
37
+ }
35
38
  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}`;
39
+ // Two secrets cannot start at the same line and column, so a known
40
+ // position alone identifies the secret: the engines extract different
41
+ // text for it (betterleaks a whole PEM block, the regex pattern only
42
+ // its header). Without a position, fall back to the matched text.
43
+ const key = m.line != null && m.column != null
44
+ ? `${m.line} ${m.column}`
45
+ : `${m.line ?? "?"} ${m.column ?? "?"} ${m.match}`;
42
46
  const existing = fileMap.get(key);
43
47
  if (existing) {
44
48
  if (!existing.engines.includes(engine))
45
49
  existing.engines.push(engine);
50
+ if (!existing.ruleNames.includes(m.pattern.name))
51
+ existing.ruleNames.push(m.pattern.name);
46
52
  }
47
53
  else {
48
- fileMap.set(key, { ...m, engines: [engine] });
54
+ fileMap.set(key, { ...m, engines: [engine], ruleNames: [m.pattern.name] });
49
55
  }
50
56
  }
51
57
  }
52
58
  };
53
59
  ingest(betterleaks, "betterleaks");
54
60
  ingest(patterns, "patterns");
55
- return fileOrder.map((file) => ({
56
- file,
57
- matches: Array.from(byFile.get(file).values()),
58
- }));
61
+ return fileOrder.map((file) => {
62
+ const out = {
63
+ file,
64
+ matches: Array.from(byFile.get(file).values()),
65
+ };
66
+ const readError = readErrorByFile.get(file);
67
+ if (readError)
68
+ out.readError = readError;
69
+ return out;
70
+ });
59
71
  }
package/dist/utils/api.js CHANGED
@@ -1,6 +1,40 @@
1
1
  import axios from "axios";
2
2
  import { ConfigManager } from "../core/config-manager.js";
3
3
  export const API = "https://rafter.so/api/";
4
+ /**
5
+ * The API base in effect right now.
6
+ *
7
+ * `RAFTER_API_URL`, read fresh on every call rather than cached at import
8
+ * time, overrides the production default in `API`. This is how the test
9
+ * suite points the CLI at a local, non-routable port instead of the live
10
+ * backend — see `node/tests/env-isolation-setup.ts`. Production never sets
11
+ * the var, so this is a no-op outside tests.
12
+ *
13
+ * Every request carries the API key, so the override must be https, or plain
14
+ * http to a loopback host. Anything else is ignored with a warning. A remote
15
+ * https override is honored with a one-time notice, since the key goes there.
16
+ */
17
+ const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
18
+ let warnedApiOverride = false;
19
+ function resolveApiBase() {
20
+ const override = process.env.RAFTER_API_URL;
21
+ if (!override)
22
+ return API;
23
+ let url = null;
24
+ try {
25
+ url = new URL(override);
26
+ }
27
+ catch { /* reported below */ }
28
+ const loopback = url !== null && LOOPBACK_HOSTS.has(url.hostname);
29
+ const allowed = url !== null && (url.protocol === "https:" || (url.protocol === "http:" && loopback));
30
+ if (!loopback && !warnedApiOverride) {
31
+ warnedApiOverride = true;
32
+ console.error(allowed
33
+ ? `rafter: RAFTER_API_URL is set; sending requests and your API key to ${url.origin}`
34
+ : `rafter: ignoring RAFTER_API_URL (must be https, or http to localhost); using ${API}`);
35
+ }
36
+ return allowed ? override : API;
37
+ }
4
38
  /**
5
39
  * sable-2s6p — the HTTP client for every authenticated Rafter API call.
6
40
  *
@@ -45,7 +79,7 @@ apiClient.interceptors.response.use(undefined, (error) => {
45
79
  });
46
80
  /** Join API with a path segment without producing a double slash, regardless of leading/trailing slashes on either side. */
47
81
  export function apiUrl(path) {
48
- return `${API.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
82
+ return `${resolveApiBase().replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
49
83
  }
50
84
  // Exit codes
51
85
  /**
@@ -1,22 +1,22 @@
1
1
  /**
2
- * Security-control env vars must never be settable by a project `.env`.
2
+ * A project `.env` must never supply a rafter setting.
3
3
  *
4
4
  * `dotenv.config()` runs at CLI startup (index.ts) and, with no path, loads
5
- * `$CWD/.env` — which, when rafter runs inside an agent hook on a cloned repo,
6
- * is a file IN THE UNTRUSTED REPOSITORY. dotenv does not override a variable
7
- * already present in the real environment, but it DOES introduce one that was
8
- * unset — so a repo shipping `RAFTER_DISABLE_HOOKS=1` (or any `RAFTER_DISABLE_*`
9
- * / `RAFTER_HOOK_*` value) could switch off the victim's command policy and
10
- * secret scanning. That defeats the control whose own contract (hook-control.ts)
11
- * says the disable signal is honored only from the machine owner's environment.
5
+ * `$CWD/.env` — which, when rafter runs inside an agent hook or a scan on a
6
+ * cloned repo, is a file IN THE UNTRUSTED REPOSITORY. dotenv does not override
7
+ * a variable already present in the real environment, but it DOES introduce one
8
+ * that was unset. Every `RAFTER_*` variable is an operator setting: the disable
9
+ * switches and hook timeouts, but also the API key (which outranks the key the
10
+ * operator stored in ~/.rafter/config.json), the GitHub token, the notify
11
+ * webhook and the paid-scan confirmation. None of them may come from the repo
12
+ * being scanned.
12
13
  *
13
- * This runs dotenv, then drops any `RAFTER_DISABLE_*` / `RAFTER_HOOK_*` variable
14
- * that was NOT already set in the real environment before dotenv ran. The
15
- * owner's real values are preserved untouched; legitimate `.env` keys that do
16
- * not match those prefixes (RAFTER_API_KEY, RAFTER_GITHUB_TOKEN, …) are
17
- * unaffected. rf-7dda / sable-nz4y sibling.
14
+ * This runs dotenv, then drops any `RAFTER_*` variable that was NOT already set
15
+ * in the real environment before dotenv ran. The owner's real values are
16
+ * preserved untouched. This matches the Python runtime, which never reads the
17
+ * working directory's `.env`.
18
18
  */
19
- const PROTECTED_PREFIX = /^RAFTER_(DISABLE_|HOOK_)/;
19
+ const PROTECTED_PREFIX = /^RAFTER_/;
20
20
  export function guardSecurityEnvFromDotenv(applyDotenv, env = process.env) {
21
21
  const present = new Set();
22
22
  for (const k of Object.keys(env)) {
@@ -2,6 +2,17 @@
2
2
  * Parse unified git diff output and extract added/modified lines (+ side only).
3
3
  * Dependency-free — expects `git diff -U0 --no-color` output (zero context lines).
4
4
  */
5
+ /**
6
+ * `maxBuffer` for reading a `git diff` with execSync/execFileSync. Node's
7
+ * default (1 MiB) throws ENOBUFS on ordinary large diffs (lockfiles, generated
8
+ * or vendored files), which crashed the scan or made it skip silently (#274).
9
+ * 256 MiB is far above any real diff. RAFTER_GIT_DIFF_MAX_BUFFER (bytes)
10
+ * overrides it for constrained hosts or to exercise the error path in tests.
11
+ */
12
+ export function gitDiffMaxBuffer() {
13
+ const raw = Number(process.env.RAFTER_GIT_DIFF_MAX_BUFFER);
14
+ return Number.isInteger(raw) && raw > 0 ? raw : 256 * 1024 * 1024;
15
+ }
5
16
  const HUNK_HEADER_RE = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/;
6
17
  const NO_NEWLINE_RE = /^\$/;
7
18
  /**
@@ -21,21 +32,35 @@ export function normalizeDiffPath(raw) {
21
32
  * only the `+` side is returned.
22
33
  */
23
34
  export function parseUnifiedDiffAddedLines(patch) {
35
+ const feed = createAddedLineParser();
24
36
  const results = [];
37
+ for (const rawLine of patch.split(/\r?\n/)) {
38
+ const added = feed(rawLine);
39
+ if (added)
40
+ results.push(added);
41
+ }
42
+ return results;
43
+ }
44
+ /**
45
+ * The same parser, one line at a time, for diffs too large to hold in memory
46
+ * (`git log -p` over a whole history). Returns the added line, or null.
47
+ * Lines must arrive without their trailing newline.
48
+ */
49
+ export function createAddedLineParser() {
25
50
  let currentFile = null;
26
51
  let newLine = 0;
27
- for (const rawLine of patch.split(/\r?\n/)) {
52
+ return (rawLine) => {
28
53
  if (NO_NEWLINE_RE.test(rawLine))
29
- continue;
54
+ return null;
30
55
  if (rawLine.startsWith("diff --git ")) {
31
56
  currentFile = null;
32
57
  newLine = 0;
33
- continue;
58
+ return null;
34
59
  }
35
60
  if (rawLine.startsWith("Binary files ") && rawLine.endsWith(" differ")) {
36
61
  currentFile = null;
37
62
  newLine = 0;
38
- continue;
63
+ return null;
39
64
  }
40
65
  // `+++ `/`--- ` are file headers ONLY in the file-header region (before the
41
66
  // first `@@`, where newLine is still 0). Inside a hunk body (newLine > 0) a
@@ -50,36 +75,27 @@ export function parseUnifiedDiffAddedLines(patch) {
50
75
  currentFile = normalizeDiffPath(pathPart);
51
76
  }
52
77
  newLine = 0;
53
- continue;
78
+ return null;
54
79
  }
55
80
  if (newLine <= 0 && rawLine.startsWith("--- ")) {
56
- continue;
81
+ return null;
57
82
  }
58
83
  const hunk = HUNK_HEADER_RE.exec(rawLine);
59
84
  if (hunk) {
60
85
  newLine = parseInt(hunk[1], 10);
61
- continue;
86
+ return null;
62
87
  }
63
88
  if (!currentFile || newLine <= 0)
64
- continue;
89
+ return null;
65
90
  // Any '+' line here is added content (real headers were consumed above
66
91
  // while newLine <= 0). Do NOT exclude '+++...': an added line whose content
67
92
  // starts with '++' would otherwise be dropped, missing a secret.
68
93
  if (rawLine.startsWith("+")) {
69
- results.push({
70
- file: currentFile,
71
- line: newLine,
72
- text: rawLine.slice(1),
73
- });
74
- newLine++;
75
- continue;
76
- }
77
- if (rawLine.startsWith("-")) {
78
- continue;
94
+ return { file: currentFile, line: newLine++, text: rawLine.slice(1) };
79
95
  }
80
96
  if (rawLine.startsWith(" ")) {
81
97
  newLine++;
82
98
  }
83
- }
84
- return results;
99
+ return null;
100
+ };
85
101
  }