@rafter-security/cli 0.11.2 → 0.11.3

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.
@@ -7,76 +7,97 @@ import path from "path";
7
7
  const execFileAsync = promisify(execFile);
8
8
  /** Per-invocation timeout for the betterleaks subprocess. */
9
9
  const SCAN_TIMEOUT_MS = 60000;
10
+ /** Config env vars betterleaks consults before the target's own .toml, in its order. */
11
+ const CONFIG_PATH_ENV_VARS = ["BETTERLEAKS_CONFIG", "GITLEAKS_CONFIG"];
12
+ const CONFIG_TOML_ENV_VARS = ["BETTERLEAKS_CONFIG_TOML", "GITLEAKS_CONFIG_TOML"];
10
13
  /**
11
- * Budget for the path arguments of one `betterleaks dir` call. Windows caps a
12
- * whole command line at 32,767 characters; elsewhere the limit is far larger.
14
+ * Paths under `dirPath` that a git-aware scan must skip, relative to it: what
15
+ * git ignores (a wholly ignored directory as one `dir/` entry), submodules (a
16
+ * gitlink, mode 160000, in the index) and untracked nested repositories (which
17
+ * `ls-files --others` lists as one `dir/` entry). Git lists no files inside
18
+ * either kind, so the file-list scan this replaced never scanned them. A
19
+ * tracked path that is now a directory on disk is NOT skipped: its files are
20
+ * untracked and get scanned. Returns null when `dirPath` is not inside a git
21
+ * work tree or git is unavailable, so the caller scans the directory whole.
13
22
  */
14
- const ARG_CHARS_PER_CALL = process.platform === "win32" ? 24000 : 500000;
23
+ export function gitSkippedPaths(dirPath) {
24
+ const ls = (...args) => {
25
+ const r = spawnSync("git", ["-C", dirPath, "ls-files", "-z", ...args, "--", "."], {
26
+ encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"], timeout: 30000, maxBuffer: 256 * 1024 * 1024,
27
+ });
28
+ if (r.status !== 0 || typeof r.stdout !== "string")
29
+ return null;
30
+ return r.stdout.split("\0").filter(Boolean);
31
+ };
32
+ const ignored = ls("--others", "--ignored", "--exclude-standard", "--directory");
33
+ const staged = ls("--cached", "--stage");
34
+ const untracked = ls("--others", "--exclude-standard");
35
+ if (ignored === null || staged === null || untracked === null)
36
+ return null;
37
+ // `--stage` lines read "<mode> <object> <stage>\t<path>".
38
+ const submodules = staged.filter((l) => l.startsWith("160000 ")).map((l) => l.slice(l.indexOf("\t") + 1));
39
+ const nestedRepos = untracked.filter((rel) => rel.endsWith("/"));
40
+ return [...ignored, ...submodules, ...nestedRepos];
41
+ }
15
42
  /**
16
- * Maximum number of file paths handed to one `betterleaks dir` invocation.
17
- *
18
- * sable-jiqb — betterleaks' run time scales with the length of the explicit
19
- * file list it is given (measured ~0.2s/file: 50 files ~10s, 300 files ~60s),
20
- * not with the size of the files. A whole git-tracked list on a few-hundred-file
21
- * repo therefore exceeds SCAN_TIMEOUT_MS in a single call and the engine falls
22
- * back to patterns. Capping each call at this many files keeps every invocation
23
- * well under the timeout (~0.2s * 64 ~= 13s, roughly a fifth of the 60s floor,
24
- * leaving headroom on a loaded box); the full tree is covered across as many
25
- * sequential calls as it takes, and their findings are unioned by file.
43
+ * Write `s` as a TOML basic string. JSON escapes every control character
44
+ * TOML forbids raw except DEL (U+007F), which would make the whole config
45
+ * unreadable and drop the scan to the patterns engine.
26
46
  */
27
- export const MAX_FILES_PER_CALL = 64;
28
- /** Config env vars betterleaks consults before the target's own .toml. */
29
- const CONFIG_ENV_VARS = [
30
- "BETTERLEAKS_CONFIG", "GITLEAKS_CONFIG", "BETTERLEAKS_CONFIG_TOML", "GITLEAKS_CONFIG_TOML",
31
- ];
47
+ function tomlString(s) {
48
+ return JSON.stringify(s).replace(/\x7f/g, "\\u007f");
49
+ }
32
50
  /**
33
- * Files under `dirPath` that git does not ignore (tracked, plus untracked and
34
- * not ignored), as absolute paths. Returns null when `dirPath` is not inside a
35
- * git work tree or git is unavailable, so the caller scans the directory whole.
51
+ * A regex (RE2, JS and Python read it alike) matching `p` and everything below
52
+ * it. Only on Windows is `\\` a separator; on POSIX it is an ordinary filename
53
+ * character and stays literal, or an ignored `a\\b` would also skip `a/b`.
36
54
  */
37
- export function gitVisibleFiles(dirPath) {
38
- const r = spawnSync("git", ["-C", dirPath, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "."], { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"], timeout: 30000, maxBuffer: 256 * 1024 * 1024 });
39
- if (r.status !== 0 || typeof r.stdout !== "string")
40
- return null;
41
- const files = new Set();
42
- for (const rel of r.stdout.split("\0")) {
43
- if (!rel)
44
- continue;
45
- const abs = path.join(dirPath, rel);
46
- try {
47
- // Skip deleted-but-tracked paths, submodule dirs and symlinks (betterleaks
48
- // does not follow symlinks by default either).
49
- if (fs.lstatSync(abs).isFile())
50
- files.add(abs);
51
- }
52
- catch {
53
- // deleted from the work tree
54
- }
55
- }
56
- return [...files];
55
+ function pathPrefixPattern(p) {
56
+ const slashed = (process.platform === "win32" ? p.replace(/\\/g, "/") : p).replace(/\/+$/, "");
57
+ return "^" + slashed.replace(/[\\.+*?()|[\]{}^$]/g, "\\$&") + "(?:/.*)?$";
57
58
  }
58
59
  /**
59
- * Split `paths` into groups that stay within both bounds: at most `maxCount`
60
- * paths per group (sable-jiqb — betterleaks' cost scales with the file count,
61
- * so this is what keeps each invocation under the timeout) and a joined length
62
- * under `budget` chars (the OS command-line cap, the original #262 constraint).
60
+ * The betterleaks config for one git-aware directory scan: the config the scan
61
+ * would otherwise use (`baseToml`, its text; betterleaks' default rules when
62
+ * null) plus a global path allowlist for `skipped`, so one
63
+ * `betterleaks dir <root>` walk passes over those paths without reading them.
64
+ *
65
+ * The base config is copied in, not extended: betterleaks 1.1.x stops
66
+ * following `[extend]` two levels down, so wrapping a repo config that itself
67
+ * extends a shared config (which extends the defaults) silently dropped the
68
+ * default rules. A relative `extend.path` in the copy still resolves as it
69
+ * did, against the working directory. The deprecated `[allowlist]` table is
70
+ * renamed to `[[allowlists]]`, its equivalent, since betterleaks rejects a
71
+ * config that has both.
72
+ *
73
+ * #262 — rafter used to hand betterleaks the git-visible files as path
74
+ * arguments, 64 per call. betterleaks 1.1.x rebuilds its config and detector
75
+ * for every path argument (~0.2s each with the default rules), and its
76
+ * `extend` depth counter is a process global it never resets, so with a config
77
+ * that extends another (`[extend] useDefault = true`) only the first two path
78
+ * arguments of a call got the extended rules: every later file was scanned
79
+ * with the repo's own rules alone. A single walk builds one detector and
80
+ * applies the full rule set to every file.
63
81
  */
64
- function chunkFiles(paths, budget, maxCount) {
65
- const chunks = [];
66
- let cur = [];
67
- let len = 0;
68
- for (const p of paths) {
69
- if (cur.length > 0 && (cur.length >= maxCount || len + p.length + 1 > budget)) {
70
- chunks.push(cur);
71
- cur = [];
72
- len = 0;
73
- }
74
- cur.push(p);
75
- len += p.length + 1;
76
- }
77
- if (cur.length > 0)
78
- chunks.push(cur);
79
- return chunks;
82
+ export function scanConfigToml(dirPath, skipped, baseToml) {
83
+ const patterns = [
84
+ "(?:^|/)\\.git(?:/.*)?$",
85
+ ...skipped.map((rel) => pathPrefixPattern(path.join(dirPath, rel))),
86
+ ];
87
+ const base = baseToml === null
88
+ ? ["[extend]", "useDefault = true"]
89
+ : [baseToml.replace(/^[ \t]*\[[ \t]*allowlist[ \t]*\][ \t]*(#.*)?$/gm, "[[allowlists]]")];
90
+ return [
91
+ "# Generated by rafter for one scan (scanConfigToml, rafter's betterleaks scanner).",
92
+ ...base,
93
+ "",
94
+ "[[allowlists]]",
95
+ 'description = "rafter: paths git ignores"',
96
+ "paths = [",
97
+ ...patterns.map((p) => ` ${tomlString(p)},`),
98
+ "]",
99
+ "",
100
+ ].join("\n");
80
101
  }
81
102
  /** Name why a betterleaks subprocess failed: a timeout reads differently from a crash. */
82
103
  function describeFailure(e) {
@@ -190,8 +211,8 @@ export class BetterleaksScanner {
190
211
  * walks node_modules, build output and local secret stores (.env) alike:
191
212
  * slow enough to hit the timeout on an ordinary checkout, and noisy (#262).
192
213
  * With respectGitignore (the default, matching the patterns engine) and a
193
- * target inside a git work tree, it is handed git's own list of non-ignored
194
- * files instead.
214
+ * target inside a git work tree, it walks the tree once under a generated
215
+ * config that skips what git ignores (see scanConfigToml).
195
216
  */
196
217
  async scanDirectory(dirPath, opts) {
197
218
  const blPath = await this.resolveBinary();
@@ -201,26 +222,36 @@ export class BetterleaksScanner {
201
222
  if (opts?.useGit) {
202
223
  return this.groupByFile(await this.runDir(blPath, "git", [dirPath], []));
203
224
  }
204
- const files = opts?.respectGitignore === false ? null : gitVisibleFiles(dirPath);
205
- if (files === null) {
225
+ const skipped = opts?.respectGitignore === false ? null : gitSkippedPaths(dirPath);
226
+ if (skipped === null) {
206
227
  return this.groupByFile(await this.runDir(blPath, "dir", [dirPath], []));
207
228
  }
208
- // Given file paths instead of a directory, betterleaks no longer finds the
209
- // target's own .betterleaks.toml / .gitleaks.toml, so pass it explicitly
210
- // unless an env var already chooses the config (it outranks the file).
211
- const configArgs = [];
212
- if (!CONFIG_ENV_VARS.some((v) => process.env[v])) {
213
- const cfg = [".betterleaks.toml", ".gitleaks.toml"]
229
+ // Private 0700 dir, atomically created — see scanFile for the TOCTOU rationale (CWE-367).
230
+ const cfgDir = fs.mkdtempSync(path.join(os.tmpdir(), "betterleaks-cfg-"));
231
+ try {
232
+ // The generated config is passed as -c, which outranks every other
233
+ // source, so it carries whichever config betterleaks would have chosen,
234
+ // in betterleaks' own order: env path, env content, the target's file.
235
+ const envPath = CONFIG_PATH_ENV_VARS.map((v) => process.env[v]).find(Boolean);
236
+ const envToml = CONFIG_TOML_ENV_VARS.map((v) => process.env[v]).find(Boolean);
237
+ const repoPath = [".betterleaks.toml", ".gitleaks.toml"]
214
238
  .map((n) => path.join(dirPath, n))
215
239
  .find((p) => fs.existsSync(p));
216
- if (cfg)
217
- configArgs.push("-c", cfg);
240
+ const baseToml = envPath
241
+ ? fs.readFileSync(envPath, "utf-8")
242
+ : envToml ?? (repoPath ? fs.readFileSync(repoPath, "utf-8") : null);
243
+ const cfg = path.join(cfgDir, "scan.toml");
244
+ fs.writeFileSync(cfg, scanConfigToml(dirPath, skipped, baseToml));
245
+ return this.groupByFile(await this.runDir(blPath, "dir", [dirPath], ["-c", cfg]));
218
246
  }
219
- const findings = [];
220
- for (const chunk of chunkFiles(files, ARG_CHARS_PER_CALL, MAX_FILES_PER_CALL)) {
221
- findings.push(...(await this.runDir(blPath, "dir", chunk, configArgs)));
247
+ finally {
248
+ try {
249
+ fs.rmSync(cfgDir, { recursive: true, force: true });
250
+ }
251
+ catch {
252
+ // ignore
253
+ }
222
254
  }
223
- return this.groupByFile(findings);
224
255
  }
225
256
  /** One betterleaks invocation; returns its findings or throws with the cause. */
226
257
  async runDir(blPath, subcommand, targets, extraArgs) {
@@ -82,7 +82,8 @@ export class RegexScanner {
82
82
  ".ruff_cache",
83
83
  "results",
84
84
  ".terraform",
85
- "bower_components"
85
+ "bower_components",
86
+ ".trigger", // Trigger.dev build output (bundled JS + source maps)
86
87
  ];
87
88
  // Merge policy excludePaths into the exclude list
88
89
  if (options?.excludePaths) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rafter-security/cli",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: rafter-security
3
3
  description: Security toolkit for AI workflows. Use when scanning code or repos for vulnerabilities, auditing third-party skills/MCPs/agent configs before installing, evaluating shell commands before running them, or generating secure design questions for new features. Provides `rafter run` (remote SAST + SCA, needs RAFTER_API_KEY), `rafter secrets` (offline secrets-only), `rafter agent exec --dry-run` (command-risk classification), and `rafter skill review`.
4
- version: 0.11.2
4
+ version: 0.11.3
5
5
  homepage: https://rafter.so
6
6
  metadata:
7
7
  openclaw:
@@ -113,6 +113,7 @@ rafter usage # check API quota
113
113
 
114
114
  Full CLI tree: **Read `docs/cli-reference.md`**. Full digest: `rafter brief commands`.
115
115
 
116
+ **Scan budget:** Remote scans spend the user's quota. `rafter run` scans the **pushed** branch, so re-running without a new push repeats it — run it once per task or before a PR and reuse `rafter get <scan-id>`; while iterating scan only changes (`rafter secrets --staged` / `--diff <ref>`); never loop, retry, or `--watch` from an agent session — exit `3` means quota exhausted, so stop, tell the user, and check `rafter usage`.
116
117
  ## Configuration
117
118
 
118
119
  `rafter run` (the full code scan) needs an API key: