@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
package/dist/core/risk-rules.js
CHANGED
|
@@ -16,6 +16,75 @@ const CRITICAL_DIRS = "home|etc|usr|boot|root|sys|proc|lib|lib64|bin|sbin|opt";
|
|
|
16
16
|
* so the default policy deny-list (`DEFAULT_BLOCKED_PATTERNS`) is exactly this
|
|
17
17
|
* set, byte for byte, and can never drift from it.
|
|
18
18
|
*/
|
|
19
|
+
/**
|
|
20
|
+
* Reverse shells — an interactive shell handed to an attacker over a socket.
|
|
21
|
+
* In an agent-security product these must never rate low/allowed (sable-0ki7:
|
|
22
|
+
* evaluate_command rated them low). Folded into CRITICAL so they are hard-
|
|
23
|
+
* blocked with no policy opt-out, exactly like the other catastrophes.
|
|
24
|
+
*
|
|
25
|
+
* Each source is matched against the *sanitized, lowercased* command (see
|
|
26
|
+
* assessCommandRisk), so quoted prose and an eval's `-c`/`-e` argument are
|
|
27
|
+
* already inlined — `echo /dev/tcp` is redacted to `echo`, while
|
|
28
|
+
* `python3 -c '…pty.spawn("/bin/sh")'` exposes its payload. Every signature is
|
|
29
|
+
* deliberately narrow: each was checked against benign look-alikes that share a
|
|
30
|
+
* keyword (`nc -z`, OpenBSD `nc -C host 25`, `socat - TCP:h:p`,
|
|
31
|
+
* `systemctl … docker.socket`, `node … --socket-path … --shell /bin/bash`,
|
|
32
|
+
* `strace -e trace=socket /bin/sh`, `ls config/dev/tcp/`,
|
|
33
|
+
* `import socket; print(...)`) with no false positive — see
|
|
34
|
+
* risk-rules-reverse-shell.test.ts.
|
|
35
|
+
*
|
|
36
|
+
* Known static-analysis limits. These fail OPEN (rate low) because the payload
|
|
37
|
+
* is unreadable after sanitization, not because a pattern is loose; they are
|
|
38
|
+
* tracked for a follow-up (sable-czo1 sibling), not papered over with a broad
|
|
39
|
+
* rule that would also hard-block benign commands:
|
|
40
|
+
* - a payload piped into a non-shell interpreter — `echo '<code>' | python3` —
|
|
41
|
+
* whose stdin the sanitizer redacts (the broadest gap);
|
|
42
|
+
* - a payload split across shell variables (`a=/dev/tc;b=p/…`) or hidden in an
|
|
43
|
+
* unreadable `$(…)`/base64 blob;
|
|
44
|
+
* - the quoted-operand forms of `php -r`, `socat exec:'/bin/sh'` (the program
|
|
45
|
+
* half quoted), `gawk /inet/`, `telnet`, and the non-standard `rcat`.
|
|
46
|
+
* `… | sh` and base64-into-shell still degrade to high via shellProgramFromStream.
|
|
47
|
+
*/
|
|
48
|
+
const REVERSE_SHELL_PATTERN_SOURCES = [
|
|
49
|
+
// bash/zsh net redirect `>& /dev/tcp/host/port`: output to the socket or fd
|
|
50
|
+
// duplication onto it (`>`, `>&`, `&>`, `<>`, `<&`), or a shell reading its
|
|
51
|
+
// program from it (`bash < /dev/tcp/…`; `-c` excluded so the inlined
|
|
52
|
+
// payload is not mistaken for the shell's stdin). A read-only redirect —
|
|
53
|
+
// the wait-for-port idiom `bash -c '</dev/tcp/h/p'`, `cat < /dev/tcp/…` —
|
|
54
|
+
// and a path component (`ls config/dev/tcp/`) do not match.
|
|
55
|
+
`((>|<>|[<>]&)\\s*|\\b(ba|z|da|k|a)?sh(\\s+-[abd-z]+)*\\s*<\\s*)/dev/(tcp|udp)/`,
|
|
56
|
+
// …or an interactive shell anywhere beside the pseudo-device.
|
|
57
|
+
`^(?=[\\s\\S]*(^|[\\s<>&;|])/dev/(tcp|udp)/)(?=[\\s\\S]*\\b(ba|z|da|k|a)?sh\\s+-[a-z]*i)`,
|
|
58
|
+
// netcat with an exec flag: `-e` anywhere in a short-flag cluster (`-e`,
|
|
59
|
+
// `-ve`, `-nve`) or the long `--exec`/`--sh-exec`/`--lua-exec`. `-c` is
|
|
60
|
+
// intentionally omitted — after lowercasing it is indistinguishable from
|
|
61
|
+
// OpenBSD `nc -C` (CRLF), and no benign nc cluster carries `e`. Plain
|
|
62
|
+
// `nc -z`/`-l`/`-v`/`-nv` do not match.
|
|
63
|
+
`\\b(nc|ncat|netcat)\\b[^|;&]*\\s(-[a-z]*e\\b|--(sh-|lua-)?exec\\b)`,
|
|
64
|
+
// named-pipe relay `mkfifo /tmp/f; … nc …; … /bin/sh`. Requires a fifo AND a
|
|
65
|
+
// netcat AND a shell (any order), so a fifo beside a benign `nc -z`/`nc -l`
|
|
66
|
+
// health check does not match.
|
|
67
|
+
`^(?=[\\s\\S]*\\b(mkfifo|mknod)\\b)(?=[\\s\\S]*\\b(nc|ncat|netcat)\\b)(?=[\\s\\S]*(/bin/(ba)?sh|\\b(ba)?sh\\b[^\\n]*-i))`,
|
|
68
|
+
// socat with a program endpoint `EXEC:`/`SYSTEM:`. A plain relay
|
|
69
|
+
// (`socat - TCP:host:port`) has neither and does not match.
|
|
70
|
+
`\\bsocat\\b[\\s\\S]*\\b(exec|system):`,
|
|
71
|
+
// interpreter socket one-liner, part A: a socket *call* (not the bare word,
|
|
72
|
+
// which is in `--socket-path`/`docker.socket`/`trace=socket`) together with a
|
|
73
|
+
// spawn that NAMES a shell. Order-independent anchored lookaheads — so a
|
|
74
|
+
// payload that opens the socket after the spawn (the canonical Node.js form)
|
|
75
|
+
// is still caught — and linear (no `prefix[\\s\\S]*suffix` backtracking).
|
|
76
|
+
`^(?=[\\s\\S]*(socket\\.socket|\\bsocket\\s*\\(|tcpsocket\\.|io::socket|pf_inet|sock_stream|net\\.socket))(?=[\\s\\S]*(pty\\.spawn|/bin/(ba)?sh|(os\\.system|system|exec|popen|spawnsync|spawn|subprocess\\.(call|run|popen|check_output|check_call))\\s*[\\s(\\[{"']*/?(bin/)?(ba|z|da|a|k)?sh\\b))`,
|
|
77
|
+
// interpreter socket one-liner, part B: a socket call + a generic exec/system
|
|
78
|
+
// + socket-fd wiring (dup2/fileno/pipe/redirect/read-loop). Catches the forms
|
|
79
|
+
// whose shell name arrives from the socket at runtime (perl `system $_ while
|
|
80
|
+
// <$s>`, a node `.pipe()` bridge), without hard-blocking a benign socket
|
|
81
|
+
// script that merely shells out (which has no such wiring).
|
|
82
|
+
`^(?=[\\s\\S]*(socket\\.socket|\\bsocket\\s*\\(|tcpsocket\\.|io::socket|pf_inet|sock_stream|net\\.socket))(?=[\\s\\S]*(\\bexec\\b|\\bsystem\\b|os\\.system|subprocess\\.|\\bpopen\\s*\\(|\\bspawn\\s*\\())(?=[\\s\\S]*(dup2|fileno|\\.pipe\\s*\\(|<&|>&|fdopen|while\\s*<))`,
|
|
83
|
+
// zsh TCP-module reverse shell (`zmodload zsh/net/tcp; ztcp host port; …`).
|
|
84
|
+
`\\bzmodload\\b[\\s\\S]*zsh/net/tcp`,
|
|
85
|
+
// powershell/pwsh TCPClient reverse shell (cross-platform pwsh on Linux too).
|
|
86
|
+
`system\\.net\\.sockets\\.tcpclient`,
|
|
87
|
+
];
|
|
19
88
|
const CRITICAL_PATTERN_SOURCES = [
|
|
20
89
|
// rm -rf / (root only, any flag order)
|
|
21
90
|
`rm\\s+(-[a-z]*r[a-z]*\\s+)*-[a-z]*f[a-z]*\\s+/(\\s|$)`,
|
|
@@ -29,6 +98,7 @@ const CRITICAL_PATTERN_SOURCES = [
|
|
|
29
98
|
`mkfs`,
|
|
30
99
|
`fdisk`,
|
|
31
100
|
`parted`,
|
|
101
|
+
...REVERSE_SHELL_PATTERN_SOURCES,
|
|
32
102
|
];
|
|
33
103
|
export const CRITICAL_PATTERNS = CRITICAL_PATTERN_SOURCES.map((s) => new RegExp(s));
|
|
34
104
|
export const HIGH_PATTERNS = [
|
|
@@ -1004,31 +1074,342 @@ export function shellProgramFromStream(command) {
|
|
|
1004
1074
|
}
|
|
1005
1075
|
return false;
|
|
1006
1076
|
}
|
|
1077
|
+
// ---------------------------------------------------------------------------
|
|
1078
|
+
// Obfuscation normalizer + evasion-construct detector (sable-jtxp)
|
|
1079
|
+
// ---------------------------------------------------------------------------
|
|
1080
|
+
//
|
|
1081
|
+
// The risk patterns match the SPELLING of a command, so an attacker can keep a
|
|
1082
|
+
// catastrophic command's effect while changing its spelling past the patterns:
|
|
1083
|
+
// long flags (`rm --recursive --force /`), `$IFS` word-splitting
|
|
1084
|
+
// (`rm${IFS}-rf${IFS}/`), brace expansion (`{rm,-rf,/}`), ANSI-C quoting
|
|
1085
|
+
// (`bash -c $'rm\x20-rf\x20/'`), or process substitution into a shell
|
|
1086
|
+
// (`sh <(curl …)`). Two layers answer this, applied in assessCommandRisk:
|
|
1087
|
+
//
|
|
1088
|
+
// 1. normalizeForMatching() un-obfuscates a bounded set of these spellings —
|
|
1089
|
+
// WITHOUT executing anything — and the patterns are matched against the
|
|
1090
|
+
// normalized form as well as the raw/sanitized one (highest risk wins).
|
|
1091
|
+
// 2. evasionConstructRisk() independently rates the obfuscation constructs
|
|
1092
|
+
// themselves as approval-grade, so a construct whose decoded payload the
|
|
1093
|
+
// classifier still cannot read is gated rather than allowed.
|
|
1094
|
+
//
|
|
1095
|
+
// LIMITATION — READ BEFORE RELYING ON THIS. It recognizes a fixed set of
|
|
1096
|
+
// SPELLINGS; it is DEFENCE IN DEPTH, NOT a containment boundary. It does not and
|
|
1097
|
+
// cannot stop an adversary who INDIRECTS the command so the dangerous token
|
|
1098
|
+
// never appears literally — assembled through a shell variable (`X=rm; $X -rf
|
|
1099
|
+
// /`), produced by a command substitution (`$(…)` in command position), or
|
|
1100
|
+
// handed to an interpreter whose argument the classifier cannot read (a payload
|
|
1101
|
+
// piped into a non-shell `python`/`perl`, a base64 blob decoded at runtime).
|
|
1102
|
+
// Those are deliberately NOT chased here: a pass that tried to gate variable
|
|
1103
|
+
// indirection or `$(…)` in command position also catches legitimate `$CC` /
|
|
1104
|
+
// `$(which python)` shapes, so the real answer is a deny-by-default execution
|
|
1105
|
+
// redesign, tracked separately (hoare's epic sable-75st; this change is
|
|
1106
|
+
// sable-jtxp). Everything here only ever EXPOSES more to the patterns — the
|
|
1107
|
+
// safe direction — and the quoted-data redaction in sanitizeCommandForMatching
|
|
1108
|
+
// still runs on the normalized form, so decoding a quoted commit message does
|
|
1109
|
+
// not make it match.
|
|
1110
|
+
/** Bounds so a crafted input cannot hang or exhaust memory. */
|
|
1111
|
+
const MAX_NORMALIZE_INPUT = 20000;
|
|
1112
|
+
const MAX_NORMALIZE_OUTPUT = 40000;
|
|
1113
|
+
const MAX_BRACE_PASSES = 24;
|
|
1114
|
+
const ANSI_C_SIMPLE = {
|
|
1115
|
+
t: "\t", n: "\n", r: "\r", "\\": "\\", "'": "'", '"': '"',
|
|
1116
|
+
a: "\x07", b: "\b", f: "\f", v: "\v", e: "\x1b", E: "\x1b",
|
|
1117
|
+
};
|
|
1118
|
+
/**
|
|
1119
|
+
* Decode one ANSI-C `$'…'` string whose opening `$'` is at `i`, returning the
|
|
1120
|
+
* decoded content and the index just past the closing quote. Parses `$'…'` with
|
|
1121
|
+
* its own escape rules (a `\'` does NOT terminate), which is why plain
|
|
1122
|
+
* single-quote handling in the tokenizer cannot see through it.
|
|
1123
|
+
*/
|
|
1124
|
+
function decodeAnsiCStringAt(s, i) {
|
|
1125
|
+
i += 2;
|
|
1126
|
+
let decoded = "";
|
|
1127
|
+
while (i < s.length && s[i] !== "'") {
|
|
1128
|
+
if (s[i] === "\\" && i + 1 < s.length) {
|
|
1129
|
+
i++;
|
|
1130
|
+
const c = s[i];
|
|
1131
|
+
if (c === "x" || c === "X") {
|
|
1132
|
+
i++;
|
|
1133
|
+
let hex = "";
|
|
1134
|
+
while (i < s.length && hex.length < 2 && /[0-9a-fA-F]/.test(s[i])) {
|
|
1135
|
+
hex += s[i];
|
|
1136
|
+
i++;
|
|
1137
|
+
}
|
|
1138
|
+
decoded += hex ? String.fromCharCode(parseInt(hex, 16)) : "x";
|
|
1139
|
+
continue;
|
|
1140
|
+
}
|
|
1141
|
+
if (c >= "0" && c <= "7") {
|
|
1142
|
+
let oct = "";
|
|
1143
|
+
while (i < s.length && oct.length < 3 && s[i] >= "0" && s[i] <= "7") {
|
|
1144
|
+
oct += s[i];
|
|
1145
|
+
i++;
|
|
1146
|
+
}
|
|
1147
|
+
decoded += String.fromCharCode(parseInt(oct, 8) & 0xff);
|
|
1148
|
+
continue;
|
|
1149
|
+
}
|
|
1150
|
+
decoded += ANSI_C_SIMPLE[c] ?? c;
|
|
1151
|
+
i++;
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
decoded += s[i];
|
|
1155
|
+
i++;
|
|
1156
|
+
}
|
|
1157
|
+
if (i < s.length)
|
|
1158
|
+
i++; // consume the closing quote
|
|
1159
|
+
return { decoded, next: i };
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Decode the escapes of every ANSI-C `$'…'` string, keeping each result ONE
|
|
1163
|
+
* quoted word so a shell wrapper still sees a single `-c` argument.
|
|
1164
|
+
*/
|
|
1165
|
+
function decodeAnsiCQuotes(s) {
|
|
1166
|
+
if (!s.includes("$'"))
|
|
1167
|
+
return s;
|
|
1168
|
+
let out = "";
|
|
1169
|
+
let i = 0;
|
|
1170
|
+
while (i < s.length) {
|
|
1171
|
+
if (s[i] === "$" && s[i + 1] === "'") {
|
|
1172
|
+
const r = decodeAnsiCStringAt(s, i);
|
|
1173
|
+
// Re-wrap as a single-quoted word, escaping embedded quotes, so the
|
|
1174
|
+
// sanitizer still treats it as one argument rather than splitting it.
|
|
1175
|
+
out += "'" + r.decoded.replace(/'/g, "'\\''") + "'";
|
|
1176
|
+
i = r.next;
|
|
1177
|
+
continue;
|
|
1178
|
+
}
|
|
1179
|
+
out += s[i];
|
|
1180
|
+
i++;
|
|
1181
|
+
}
|
|
1182
|
+
return out;
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* True if some `$'…'` decodes to content that HIDES A WORD BOUNDARY — interior
|
|
1186
|
+
* whitespace separating two or more tokens (`$'rm\x20-rf\x20/'`), the shape
|
|
1187
|
+
* that reconstructs a multi-word command inside one quoted word. A `$'…'` that
|
|
1188
|
+
* decodes to a lone separator (`$'\t'`, `$'\n'`) or to a single token
|
|
1189
|
+
* (`$'hi'`, a color escape) is a legitimate data literal and is NOT flagged —
|
|
1190
|
+
* matching on bare presence over-blocked idioms like `grep -P $'\t'`.
|
|
1191
|
+
*/
|
|
1192
|
+
function ansiCHidesWordBoundary(s) {
|
|
1193
|
+
if (!s.includes("$'"))
|
|
1194
|
+
return false;
|
|
1195
|
+
let i = 0;
|
|
1196
|
+
while ((i = s.indexOf("$'", i)) !== -1) {
|
|
1197
|
+
const r = decodeAnsiCStringAt(s, i);
|
|
1198
|
+
if (r.decoded.trim().split(/\s+/).filter(Boolean).length > 1)
|
|
1199
|
+
return true;
|
|
1200
|
+
i = r.next;
|
|
1201
|
+
}
|
|
1202
|
+
return false;
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Expand simple brace lists `{a,b,c}` to space-separated `a b c`. Space-join,
|
|
1206
|
+
* NOT the shell's cartesian product, so adjacent groups cannot blow up — the
|
|
1207
|
+
* over-approximation only exposes more tokens to the patterns. `(?<!\$)` leaves
|
|
1208
|
+
* `${VAR}` parameter expansion alone; a comma is required so `${IFS}` and plain
|
|
1209
|
+
* `{}` are untouched. Innermost-first over bounded passes handles nesting.
|
|
1210
|
+
*/
|
|
1211
|
+
function expandBraceLists(s) {
|
|
1212
|
+
if (!s.includes("{"))
|
|
1213
|
+
return s;
|
|
1214
|
+
const re = /(?<!\$)\{([^{}]*,[^{}]*)\}/g;
|
|
1215
|
+
let passes = 0;
|
|
1216
|
+
let prev;
|
|
1217
|
+
do {
|
|
1218
|
+
prev = s;
|
|
1219
|
+
s = s.replace(re, (_m, inner) => inner.split(",").join(" "));
|
|
1220
|
+
passes++;
|
|
1221
|
+
if (s.length > MAX_NORMALIZE_OUTPUT) {
|
|
1222
|
+
s = s.slice(0, MAX_NORMALIZE_OUTPUT);
|
|
1223
|
+
break;
|
|
1224
|
+
}
|
|
1225
|
+
} while (s !== prev && passes < MAX_BRACE_PASSES);
|
|
1226
|
+
return s;
|
|
1227
|
+
}
|
|
1228
|
+
/**
|
|
1229
|
+
* Reduce an `rm` whose flags carry BOTH recursive and force intent — in any
|
|
1230
|
+
* long, short or `--no-preserve-root` spelling, adjacent or not — to the
|
|
1231
|
+
* `rm -rf` form the existing patterns catch. Scoped to `rm`, and the collapse
|
|
1232
|
+
* only ever adds `-rf`, so it cannot turn a benign command destructive.
|
|
1233
|
+
*/
|
|
1234
|
+
function canonicalizeRmFlags(s) {
|
|
1235
|
+
if (!/\brm\b/i.test(s))
|
|
1236
|
+
return s;
|
|
1237
|
+
const re = /(^|[\s;&|()<>])((?:[\w./-]*\/)?rm)((?:[ \t]+--?[A-Za-z][\w-]*)*)/gi;
|
|
1238
|
+
return s.replace(re, (whole, pre, cmd, flags) => {
|
|
1239
|
+
if (!flags.trim())
|
|
1240
|
+
return whole;
|
|
1241
|
+
let recursive = false;
|
|
1242
|
+
let force = false;
|
|
1243
|
+
for (const tok of flags.trim().split(/\s+/)) {
|
|
1244
|
+
const low = tok.toLowerCase();
|
|
1245
|
+
if (low.startsWith("--")) {
|
|
1246
|
+
if (low === "--recursive")
|
|
1247
|
+
recursive = true;
|
|
1248
|
+
else if (low === "--force" || low === "--no-preserve-root")
|
|
1249
|
+
force = true;
|
|
1250
|
+
}
|
|
1251
|
+
else {
|
|
1252
|
+
if (low.includes("r"))
|
|
1253
|
+
recursive = true;
|
|
1254
|
+
if (low.includes("f"))
|
|
1255
|
+
force = true;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
return recursive && force ? `${pre}${cmd} -rf` : whole;
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
1262
|
+
* A spelling-normalized view of a command for pattern matching. Decodes ANSI-C
|
|
1263
|
+
* quoting, substitutes `$IFS`/`${IFS}`, expands brace lists, canonicalizes
|
|
1264
|
+
* destructive-flag spellings, and collapses whitespace runs. Executes nothing;
|
|
1265
|
+
* bounded against hostile input.
|
|
1266
|
+
*/
|
|
1267
|
+
export function normalizeForMatching(command) {
|
|
1268
|
+
if (!command)
|
|
1269
|
+
return command;
|
|
1270
|
+
let s = command.length > MAX_NORMALIZE_INPUT ? command.slice(0, MAX_NORMALIZE_INPUT) : command;
|
|
1271
|
+
s = decodeAnsiCQuotes(s);
|
|
1272
|
+
s = s.replace(/\$\{IFS\}/g, " ").replace(/\$IFS(?![A-Za-z0-9_])/g, " ");
|
|
1273
|
+
s = expandBraceLists(s);
|
|
1274
|
+
s = canonicalizeRmFlags(s);
|
|
1275
|
+
// Collapse runs of spaces/tabs; keep newlines/CRs as statement separators.
|
|
1276
|
+
s = s.replace(/[^\S\n\r]+/g, " ");
|
|
1277
|
+
if (s.length > MAX_NORMALIZE_OUTPUT)
|
|
1278
|
+
s = s.slice(0, MAX_NORMALIZE_OUTPUT);
|
|
1279
|
+
return s;
|
|
1280
|
+
}
|
|
1281
|
+
/** The exec piece of a segment (skipping env assignments and prefix wrappers). */
|
|
1282
|
+
function resolveExecPiece(seg) {
|
|
1283
|
+
const isRedirectTarget = new Array(seg.length).fill(false);
|
|
1284
|
+
for (let i = 1; i < seg.length; i++) {
|
|
1285
|
+
const prev = seg[i - 1];
|
|
1286
|
+
if (prev.op && REDIRECT_OPS.has(prev.op) && !seg[i].op)
|
|
1287
|
+
isRedirectTarget[i] = true;
|
|
1288
|
+
}
|
|
1289
|
+
for (let i = 0; i < seg.length; i++) {
|
|
1290
|
+
const p = seg[i];
|
|
1291
|
+
if (p.op || isRedirectTarget[i])
|
|
1292
|
+
continue;
|
|
1293
|
+
if (!p.quoted && ENV_ASSIGNMENT.test(p.text))
|
|
1294
|
+
continue;
|
|
1295
|
+
if (!p.quoted && TAIL_WRAPPERS.has(execName(p.text))) {
|
|
1296
|
+
let j = i + 1;
|
|
1297
|
+
while (j < seg.length) {
|
|
1298
|
+
const q = seg[j];
|
|
1299
|
+
if (q.op || isRedirectTarget[j]) {
|
|
1300
|
+
j++;
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
if (q.text.startsWith("-") || NUMERIC_ARG.test(q.text)) {
|
|
1304
|
+
j++;
|
|
1305
|
+
continue;
|
|
1306
|
+
}
|
|
1307
|
+
break;
|
|
1308
|
+
}
|
|
1309
|
+
i = j - 1;
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
return p;
|
|
1313
|
+
}
|
|
1314
|
+
return null;
|
|
1315
|
+
}
|
|
1316
|
+
/**
|
|
1317
|
+
* True if the command uses a shell OBFUSCATION construct that should require
|
|
1318
|
+
* approval even when the normalized payload still cannot be read: `$IFS`
|
|
1319
|
+
* splitting, ANSI-C `$'…'`, a brace list in COMMAND position, a process
|
|
1320
|
+
* substitution feeding a shell/eval, or a base64 decode piped into a shell.
|
|
1321
|
+
* Narrow by design — a brace ARGUMENT or a process substitution into a
|
|
1322
|
+
* non-shell (`diff <(a) <(b)`) is routine and not flagged.
|
|
1323
|
+
*/
|
|
1324
|
+
export function evasionConstructRisk(command) {
|
|
1325
|
+
if (!command)
|
|
1326
|
+
return false;
|
|
1327
|
+
const s = command.length > MAX_NORMALIZE_INPUT ? command.slice(0, MAX_NORMALIZE_INPUT) : command;
|
|
1328
|
+
if (/\$\{IFS\}|\$IFS\b/.test(s))
|
|
1329
|
+
return true;
|
|
1330
|
+
if (ansiCHidesWordBoundary(s))
|
|
1331
|
+
return true;
|
|
1332
|
+
if (/\bbase64\b[\s\S]*\|\s*(ba|z|da|k|a)?sh\b/i.test(s))
|
|
1333
|
+
return true;
|
|
1334
|
+
const { pieces, unterminated } = tokenize(s);
|
|
1335
|
+
if (unterminated)
|
|
1336
|
+
return false; // a broken parse already fails closed elsewhere
|
|
1337
|
+
const segments = [];
|
|
1338
|
+
let seg = [];
|
|
1339
|
+
for (const p of pieces) {
|
|
1340
|
+
if (p.op !== null && CHAIN_OPS.has(p.op)) {
|
|
1341
|
+
segments.push(seg);
|
|
1342
|
+
seg = [];
|
|
1343
|
+
continue;
|
|
1344
|
+
}
|
|
1345
|
+
seg.push(p);
|
|
1346
|
+
}
|
|
1347
|
+
segments.push(seg);
|
|
1348
|
+
for (const segment of segments) {
|
|
1349
|
+
const exec = resolveExecPiece(segment);
|
|
1350
|
+
if (!exec)
|
|
1351
|
+
continue;
|
|
1352
|
+
if (exec.text && /^\{[^{}]*,[^{}]*\}/.test(exec.text))
|
|
1353
|
+
return true;
|
|
1354
|
+
const name = execName(exec.text);
|
|
1355
|
+
if (SHELL_EXECS.has(name) || EVAL_EXECS.has(name)) {
|
|
1356
|
+
for (const p of segment) {
|
|
1357
|
+
if (p.op === null && p.text === "" && p.substs.length > 0) {
|
|
1358
|
+
const open = s.slice(p.start, p.start + 2);
|
|
1359
|
+
if (open === "<(" || open === ">(")
|
|
1360
|
+
return true;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
return false;
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* The distinct lowercased, sanitized views a command's risk is judged against:
|
|
1369
|
+
* the raw command and — when normalization changed it — the spelling-normalized
|
|
1370
|
+
* command. Matching the union defeats a spelling that evades the patterns.
|
|
1371
|
+
*/
|
|
1372
|
+
function matchVariants(command) {
|
|
1373
|
+
const raw = sanitizeCommandForMatching(command).toLowerCase().trim();
|
|
1374
|
+
const variants = [];
|
|
1375
|
+
if (raw)
|
|
1376
|
+
variants.push(raw);
|
|
1377
|
+
const normalized = normalizeForMatching(command);
|
|
1378
|
+
if (normalized !== command) {
|
|
1379
|
+
const normSan = sanitizeCommandForMatching(normalized).toLowerCase().trim();
|
|
1380
|
+
if (normSan && normSan !== raw)
|
|
1381
|
+
variants.push(normSan);
|
|
1382
|
+
}
|
|
1383
|
+
return variants;
|
|
1384
|
+
}
|
|
1007
1385
|
export function assessCommandRisk(command) {
|
|
1008
1386
|
// rf-vnxs: checked on the RAW command, before sanitization, because the
|
|
1009
1387
|
// disarm is an ordinary well-formed invocation — there is nothing malformed
|
|
1010
1388
|
// for the sanitizer to normalise, and redaction could hide the operand.
|
|
1011
1389
|
if (modifiesRafterSecurityConfig(command))
|
|
1012
1390
|
return "critical";
|
|
1013
|
-
|
|
1014
|
-
|
|
1391
|
+
// sable-jtxp: match against the raw AND the spelling-normalized view, so a
|
|
1392
|
+
// command whose spelling evades the patterns is still caught.
|
|
1393
|
+
const variants = matchVariants(command);
|
|
1394
|
+
if (variants.length === 0)
|
|
1015
1395
|
return "low";
|
|
1016
1396
|
for (const pattern of CRITICAL_PATTERNS) {
|
|
1017
|
-
if (pattern.test(
|
|
1397
|
+
if (variants.some((v) => pattern.test(v)))
|
|
1018
1398
|
return "critical";
|
|
1019
1399
|
}
|
|
1020
|
-
// rf-zvll B2: a shell fed its program from a channel we cannot
|
|
1400
|
+
// rf-zvll B2 + sable-jtxp: a shell fed its program from a channel we cannot
|
|
1401
|
+
// read, or an obfuscation construct whose payload we cannot read, requires
|
|
1021
1402
|
// approval. Checked AFTER critical so `echo 'rm -rf /' | sh` — where the
|
|
1022
1403
|
// payload IS readable and matches — keeps its hard block rather than being
|
|
1023
1404
|
// softened to approval.
|
|
1024
|
-
if (shellProgramFromStream(command))
|
|
1405
|
+
if (shellProgramFromStream(command) || evasionConstructRisk(command))
|
|
1025
1406
|
return "high";
|
|
1026
1407
|
for (const pattern of HIGH_PATTERNS) {
|
|
1027
|
-
if (pattern.test(
|
|
1408
|
+
if (variants.some((v) => pattern.test(v)))
|
|
1028
1409
|
return "high";
|
|
1029
1410
|
}
|
|
1030
1411
|
for (const pattern of MEDIUM_PATTERNS) {
|
|
1031
|
-
if (pattern.test(
|
|
1412
|
+
if (variants.some((v) => pattern.test(v)))
|
|
1032
1413
|
return "medium";
|
|
1033
1414
|
}
|
|
1034
1415
|
return "low";
|
|
@@ -1043,9 +1424,10 @@ export function matchedCriticalPattern(command) {
|
|
|
1043
1424
|
if (modifiesRafterSecurityConfig(command)) {
|
|
1044
1425
|
return "rafter security configuration is protected (rf-vnxs)";
|
|
1045
1426
|
}
|
|
1046
|
-
|
|
1427
|
+
// Mirror assessCommandRisk: a critical match may live in the normalized view.
|
|
1428
|
+
const variants = matchVariants(command);
|
|
1047
1429
|
for (const pattern of CRITICAL_PATTERNS) {
|
|
1048
|
-
if (pattern.test(
|
|
1430
|
+
if (variants.some((v) => pattern.test(v)))
|
|
1049
1431
|
return pattern.source;
|
|
1050
1432
|
}
|
|
1051
1433
|
return null;
|
package/dist/index.js
CHANGED
|
@@ -23,6 +23,12 @@ import { createSitesCommand } from "./commands/sites/index.js";
|
|
|
23
23
|
import { checkForUpdate } from "./utils/update-checker.js";
|
|
24
24
|
import { setAgentMode } from "./utils/formatter.js";
|
|
25
25
|
import { createRequire } from "module";
|
|
26
|
+
// Commands call process.exit() right after writing results. When stdout is a
|
|
27
|
+
// pipe, Node writes asynchronously and exit drops whatever is still queued, so
|
|
28
|
+
// `rafter secrets --json . | jq` got only the first 64 KiB. Write synchronously.
|
|
29
|
+
// Only stdout: a blocking stderr stalls the process (including `mcp serve`)
|
|
30
|
+
// when a parent pipes stderr and never reads it.
|
|
31
|
+
process.stdout._handle?.setBlocking?.(true);
|
|
26
32
|
// A repo `.env` must not be able to set any RAFTER_* variable (key, token, disables).
|
|
27
33
|
guardSecurityEnvFromDotenv(() => dotenv.config());
|
|
28
34
|
const require = createRequire(import.meta.url);
|
|
@@ -1,10 +1,91 @@
|
|
|
1
|
-
import { execFile } from "child_process";
|
|
1
|
+
import { execFile, spawnSync } from "child_process";
|
|
2
2
|
import { promisify } from "util";
|
|
3
3
|
import { BinaryManager } from "../utils/binary-manager.js";
|
|
4
4
|
import fs from "fs";
|
|
5
5
|
import os from "os";
|
|
6
6
|
import path from "path";
|
|
7
7
|
const execFileAsync = promisify(execFile);
|
|
8
|
+
/** Per-invocation timeout for the betterleaks subprocess. */
|
|
9
|
+
const SCAN_TIMEOUT_MS = 60000;
|
|
10
|
+
/**
|
|
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.
|
|
13
|
+
*/
|
|
14
|
+
const ARG_CHARS_PER_CALL = process.platform === "win32" ? 24000 : 500000;
|
|
15
|
+
/**
|
|
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.
|
|
26
|
+
*/
|
|
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
|
+
];
|
|
32
|
+
/**
|
|
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.
|
|
36
|
+
*/
|
|
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];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
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).
|
|
63
|
+
*/
|
|
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;
|
|
80
|
+
}
|
|
81
|
+
/** Name why a betterleaks subprocess failed: a timeout reads differently from a crash. */
|
|
82
|
+
function describeFailure(e) {
|
|
83
|
+
if (e?.killed && e?.signal)
|
|
84
|
+
return `timed out after ${SCAN_TIMEOUT_MS / 1000}s`;
|
|
85
|
+
if (typeof e?.code === "number")
|
|
86
|
+
return `exit ${e.code}`;
|
|
87
|
+
return e?.message ?? String(e);
|
|
88
|
+
}
|
|
8
89
|
export class BetterleaksScanner {
|
|
9
90
|
constructor() {
|
|
10
91
|
this.resolvedPath = null;
|
|
@@ -54,7 +135,7 @@ export class BetterleaksScanner {
|
|
|
54
135
|
const tmpReport = path.join(tmpDir, "report.json");
|
|
55
136
|
try {
|
|
56
137
|
// `--` ensures a target path beginning with `-` isn't parsed as a flag by betterleaks.
|
|
57
|
-
await execFileAsync(blPath, ["dir", "-f", "json", "--report-path", tmpReport, "--", filePath], { timeout:
|
|
138
|
+
await execFileAsync(blPath, ["dir", "-f", "json", "--report-path", tmpReport, "--", filePath], { timeout: SCAN_TIMEOUT_MS });
|
|
58
139
|
if (!fs.existsSync(tmpReport)) {
|
|
59
140
|
return { file: filePath, matches: [] };
|
|
60
141
|
}
|
|
@@ -104,33 +185,61 @@ export class BetterleaksScanner {
|
|
|
104
185
|
/**
|
|
105
186
|
* Scan a directory. With useGit=true, scans git history (`betterleaks git`);
|
|
106
187
|
* otherwise scans the filesystem (`betterleaks dir`).
|
|
188
|
+
*
|
|
189
|
+
* `betterleaks dir` has no notion of .gitignore, so given a directory it
|
|
190
|
+
* walks node_modules, build output and local secret stores (.env) alike:
|
|
191
|
+
* slow enough to hit the timeout on an ordinary checkout, and noisy (#262).
|
|
192
|
+
* 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.
|
|
107
195
|
*/
|
|
108
196
|
async scanDirectory(dirPath, opts) {
|
|
109
197
|
const blPath = await this.resolveBinary();
|
|
110
198
|
if (!blPath) {
|
|
111
199
|
throw new Error("Betterleaks not available");
|
|
112
200
|
}
|
|
201
|
+
if (opts?.useGit) {
|
|
202
|
+
return this.groupByFile(await this.runDir(blPath, "git", [dirPath], []));
|
|
203
|
+
}
|
|
204
|
+
const files = opts?.respectGitignore === false ? null : gitVisibleFiles(dirPath);
|
|
205
|
+
if (files === null) {
|
|
206
|
+
return this.groupByFile(await this.runDir(blPath, "dir", [dirPath], []));
|
|
207
|
+
}
|
|
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"]
|
|
214
|
+
.map((n) => path.join(dirPath, n))
|
|
215
|
+
.find((p) => fs.existsSync(p));
|
|
216
|
+
if (cfg)
|
|
217
|
+
configArgs.push("-c", cfg);
|
|
218
|
+
}
|
|
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)));
|
|
222
|
+
}
|
|
223
|
+
return this.groupByFile(findings);
|
|
224
|
+
}
|
|
225
|
+
/** One betterleaks invocation; returns its findings or throws with the cause. */
|
|
226
|
+
async runDir(blPath, subcommand, targets, extraArgs) {
|
|
113
227
|
// Private 0700 dir, atomically created — see scanFile for the TOCTOU rationale (CWE-367).
|
|
114
228
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "betterleaks-"));
|
|
115
229
|
const tmpReport = path.join(tmpDir, "report.json");
|
|
116
|
-
const subcommand = opts?.useGit ? "git" : "dir";
|
|
117
230
|
try {
|
|
118
231
|
// `--` ensures a target path beginning with `-` isn't parsed as a flag by betterleaks.
|
|
119
|
-
await execFileAsync(blPath, [subcommand, "-f", "json", "--report-path", tmpReport, "--",
|
|
232
|
+
await execFileAsync(blPath, [subcommand, ...extraArgs, "-f", "json", "--report-path", tmpReport, "--", ...targets], { timeout: SCAN_TIMEOUT_MS, maxBuffer: 64 * 1024 * 1024 });
|
|
120
233
|
if (!fs.existsSync(tmpReport)) {
|
|
121
234
|
return [];
|
|
122
235
|
}
|
|
123
|
-
|
|
124
|
-
return this.groupByFile(results);
|
|
236
|
+
return this.parseResults(tmpReport);
|
|
125
237
|
}
|
|
126
238
|
catch (e) {
|
|
127
|
-
if (fs.existsSync(tmpReport)) {
|
|
128
|
-
|
|
129
|
-
if (e.code === 1) {
|
|
130
|
-
return this.groupByFile(results);
|
|
131
|
-
}
|
|
239
|
+
if (e.code === 1 && fs.existsSync(tmpReport)) {
|
|
240
|
+
return this.parseResults(tmpReport);
|
|
132
241
|
}
|
|
133
|
-
throw new Error(`Betterleaks scan failed
|
|
242
|
+
throw new Error(`Betterleaks scan failed (${describeFailure(e)})`);
|
|
134
243
|
}
|
|
135
244
|
finally {
|
|
136
245
|
// Best-effort cleanup — a cleanup failure must never mask the scan's
|
|
@@ -183,7 +292,10 @@ export class BetterleaksScanner {
|
|
|
183
292
|
},
|
|
184
293
|
match: result.Secret || result.Match,
|
|
185
294
|
line: result.StartLine,
|
|
186
|
-
|
|
295
|
+
// betterleaks 1.1.x reports StartColumn one past the 1-based column on
|
|
296
|
+
// every line but the first. Report the true column, as the regex engine
|
|
297
|
+
// does, so the union can merge the two engines' findings.
|
|
298
|
+
column: result.StartLine > 1 && result.StartColumn > 1 ? result.StartColumn - 1 : result.StartColumn,
|
|
187
299
|
redacted: this.redact(result.Secret || result.Match)
|
|
188
300
|
};
|
|
189
301
|
}
|
|
@@ -217,7 +329,8 @@ export class BetterleaksScanner {
|
|
|
217
329
|
const visibleChars = 4;
|
|
218
330
|
const start = match.substring(0, visibleChars);
|
|
219
331
|
const end = match.substring(match.length - visibleChars);
|
|
220
|
-
|
|
332
|
+
// Cap the mask: betterleaks returns whole PEM blocks as the secret.
|
|
333
|
+
const middle = "*".repeat(Math.min(match.length - (visibleChars * 2), 16));
|
|
221
334
|
return start + middle + end;
|
|
222
335
|
}
|
|
223
336
|
groupByFile(results) {
|