@rafter-security/cli 0.10.0 → 0.10.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/exec.js +63 -11
- package/dist/commands/agent/init.js +94 -19
- package/dist/commands/agent/scan.js +6 -2
- package/dist/commands/agent/verify.js +128 -6
- package/dist/commands/backend/get.js +9 -4
- package/dist/commands/backend/run.js +3 -4
- package/dist/commands/backend/scan-status.js +211 -26
- package/dist/commands/backend/usage.js +2 -3
- package/dist/commands/issues/from-scan.js +26 -6
- package/dist/commands/issues/from-text.js +1 -1
- package/dist/commands/issues/issue-builder.js +1 -1
- package/dist/commands/mcp/server.js +5 -6
- package/dist/commands/notify.js +2 -3
- package/dist/commands/sites/create.js +2 -3
- package/dist/commands/sites/get.js +2 -3
- package/dist/commands/sites/list.js +2 -3
- package/dist/commands/sites/scan.js +2 -3
- package/dist/core/config-manager.js +105 -10
- package/dist/core/risk-rules.js +321 -18
- package/dist/index.js +3 -1
- package/dist/scanners/betterleaks.js +7 -0
- package/dist/utils/api.js +48 -0
- package/dist/utils/env-guard.js +32 -0
- package/package.json +1 -1
- package/resources/rafter-security-skill.md +1 -1
- package/resources/skills/rafter/docs/cli-reference.md +1 -1
- package/resources/skills/rafter/docs/guardrails.md +2 -2
package/dist/core/risk-rules.js
CHANGED
|
@@ -128,7 +128,9 @@ const REDIRECT_OPS = new Set([">", ">>", "<", "<<"]);
|
|
|
128
128
|
/** Bound on recursion through nested shell wrappers / substitutions. */
|
|
129
129
|
const MAX_SANITIZE_DEPTH = 8;
|
|
130
130
|
function isOpChar(c) {
|
|
131
|
-
|
|
131
|
+
// \n and \r are statement separators (rf-6pqx): a newline ends a command
|
|
132
|
+
// exactly as ";" does, so a payload on a later line is classified on its own.
|
|
133
|
+
return c === ";" || c === "&" || c === "|" || c === ">" || c === "<" || c === "\n" || c === "\r";
|
|
132
134
|
}
|
|
133
135
|
/** Read a `$(…)` substitution starting at `i`; returns its contents and the next index. */
|
|
134
136
|
function readSubst(s, i) {
|
|
@@ -161,18 +163,32 @@ function readBacktick(s, i) {
|
|
|
161
163
|
}
|
|
162
164
|
return { inner, next: Math.min(j + 1, s.length) };
|
|
163
165
|
}
|
|
164
|
-
/**
|
|
166
|
+
/**
|
|
167
|
+
* Split a command line into words and operators, respecting quotes and substitutions.
|
|
168
|
+
* `unterminated` is set when a quote is never closed — the parse is then unreliable
|
|
169
|
+
* and the caller must FAIL CLOSED (match the raw string) rather than trust a
|
|
170
|
+
* desynchronized sanitization (se-y6vo: `$'a\'b'` swallows a trailing payload).
|
|
171
|
+
*/
|
|
165
172
|
function tokenize(s) {
|
|
166
173
|
const pieces = [];
|
|
174
|
+
let unterminated = false;
|
|
167
175
|
let i = 0;
|
|
168
176
|
while (i < s.length) {
|
|
169
177
|
const c = s[i];
|
|
170
|
-
|
|
178
|
+
// Whitespace EXCEPT newlines is skipped; a newline falls through to the
|
|
179
|
+
// operator branch below so it becomes a statement separator (rf-6pqx).
|
|
180
|
+
if (/\s/.test(c) && c !== "\n" && c !== "\r") {
|
|
171
181
|
i++;
|
|
172
182
|
continue;
|
|
173
183
|
}
|
|
174
184
|
if (isOpChar(c)) {
|
|
175
185
|
const start = i;
|
|
186
|
+
if (c === "\n" || c === "\r") {
|
|
187
|
+
// Normalize a line break (incl. CRLF) to a ";" separator piece.
|
|
188
|
+
i += 1;
|
|
189
|
+
pieces.push({ start, end: i, op: ";", text: ";", quoted: false, substs: [] });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
176
192
|
const two = s.slice(i, i + 2);
|
|
177
193
|
const op = (two === "&&" || two === "||" || two === ">>" || two === "<<") ? two : c;
|
|
178
194
|
i += op.length;
|
|
@@ -188,6 +204,20 @@ function tokenize(s) {
|
|
|
188
204
|
if (/\s/.test(ch) || isOpChar(ch))
|
|
189
205
|
break;
|
|
190
206
|
if (ch === "\\") {
|
|
207
|
+
// Line continuation: `\` immediately before a newline (incl. CRLF) is
|
|
208
|
+
// deleted by the shell — `r\<NL>m` is `rm`, so the newline must not be
|
|
209
|
+
// absorbed into the word (rf-6pqx/se-y6vo).
|
|
210
|
+
const nxt = s[i + 1] ?? "";
|
|
211
|
+
if (nxt === "\n") {
|
|
212
|
+
i += 2;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (nxt === "\r") {
|
|
216
|
+
i += 2;
|
|
217
|
+
if (s[i] === "\n")
|
|
218
|
+
i++;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
191
221
|
i++;
|
|
192
222
|
if (i < s.length) {
|
|
193
223
|
text += s[i];
|
|
@@ -199,19 +229,43 @@ function tokenize(s) {
|
|
|
199
229
|
if (ch === "'") {
|
|
200
230
|
i++;
|
|
201
231
|
quoted = true;
|
|
202
|
-
|
|
232
|
+
let closed = false;
|
|
233
|
+
while (i < s.length) {
|
|
234
|
+
if (s[i] === "'") {
|
|
235
|
+
closed = true;
|
|
236
|
+
i++;
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
203
239
|
text += s[i];
|
|
204
240
|
i++;
|
|
205
241
|
}
|
|
206
|
-
|
|
242
|
+
if (!closed)
|
|
243
|
+
unterminated = true;
|
|
207
244
|
continue;
|
|
208
245
|
}
|
|
209
246
|
// Double quotes are data, but `$( )` / backticks inside them DO execute.
|
|
210
247
|
if (ch === '"') {
|
|
211
248
|
i++;
|
|
212
249
|
quoted = true;
|
|
213
|
-
|
|
250
|
+
let closed = false;
|
|
251
|
+
while (i < s.length) {
|
|
252
|
+
if (s[i] === '"') {
|
|
253
|
+
closed = true;
|
|
254
|
+
i++;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
214
257
|
if (s[i] === "\\") {
|
|
258
|
+
const nxt = s[i + 1] ?? "";
|
|
259
|
+
if (nxt === "\n") {
|
|
260
|
+
i += 2;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (nxt === "\r") {
|
|
264
|
+
i += 2;
|
|
265
|
+
if (s[i] === "\n")
|
|
266
|
+
i++;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
215
269
|
i++;
|
|
216
270
|
if (i < s.length) {
|
|
217
271
|
text += s[i];
|
|
@@ -234,7 +288,8 @@ function tokenize(s) {
|
|
|
234
288
|
text += s[i];
|
|
235
289
|
i++;
|
|
236
290
|
}
|
|
237
|
-
|
|
291
|
+
if (!closed)
|
|
292
|
+
unterminated = true;
|
|
238
293
|
continue;
|
|
239
294
|
}
|
|
240
295
|
if (ch === "$" && s[i + 1] === "(") {
|
|
@@ -254,7 +309,7 @@ function tokenize(s) {
|
|
|
254
309
|
}
|
|
255
310
|
pieces.push({ start, end: i, op: null, text, quoted, substs });
|
|
256
311
|
}
|
|
257
|
-
return pieces;
|
|
312
|
+
return { pieces, unterminated };
|
|
258
313
|
}
|
|
259
314
|
/** `/usr/bin/rm` → `rm`; used to classify the executable of a segment. */
|
|
260
315
|
function execName(text) {
|
|
@@ -264,11 +319,58 @@ function execName(text) {
|
|
|
264
319
|
const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
265
320
|
const SHELL_C_FLAG = /^-[a-z]*c$/;
|
|
266
321
|
const LONG_FLAG_WITH_VALUE = /^(--[a-z][a-z-]*)=/;
|
|
322
|
+
const NUMERIC_ARG = /^\d+[a-z]*$/i;
|
|
323
|
+
/**
|
|
324
|
+
* A heredoc introducer: `<<`, optional `-`/`~` (indented-terminator forms), an
|
|
325
|
+
* optional quote around the delimiter, and the delimiter word. Group 1 is the
|
|
326
|
+
* dash/tilde, group 3 is the delimiter name. `g` so we can find all on a line.
|
|
327
|
+
*/
|
|
328
|
+
const HEREDOC_START = /(?<!<)<<(?!<)([-~]?)\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\2/g;
|
|
329
|
+
/** Chain/pipe operators, used to find the statement that owns a heredoc. */
|
|
330
|
+
const CHAIN_SPLIT = /\|\||&&|[;|&]/;
|
|
267
331
|
/**
|
|
268
332
|
* Decide, for one segment (a single command in a chain), which spans are DATA
|
|
269
333
|
* and which are code, appending the resulting replacements.
|
|
270
334
|
*/
|
|
271
|
-
|
|
335
|
+
/**
|
|
336
|
+
* The effective executable of a segment (`sudo rm -rf /` -> `rm`), used to ask
|
|
337
|
+
* what the NEXT stage of a pipeline does with this stage's output.
|
|
338
|
+
*/
|
|
339
|
+
function segmentExec(pieces) {
|
|
340
|
+
const isRedirectTarget = new Array(pieces.length).fill(false);
|
|
341
|
+
for (let i = 1; i < pieces.length; i++) {
|
|
342
|
+
const prev = pieces[i - 1];
|
|
343
|
+
if (prev.op && REDIRECT_OPS.has(prev.op) && !pieces[i].op)
|
|
344
|
+
isRedirectTarget[i] = true;
|
|
345
|
+
}
|
|
346
|
+
for (let i = 0; i < pieces.length; i++) {
|
|
347
|
+
const p = pieces[i];
|
|
348
|
+
if (p.op || isRedirectTarget[i])
|
|
349
|
+
continue;
|
|
350
|
+
if (!p.quoted && ENV_ASSIGNMENT.test(p.text))
|
|
351
|
+
continue;
|
|
352
|
+
if (!p.quoted && TAIL_WRAPPERS.has(execName(p.text))) {
|
|
353
|
+
let j = i + 1;
|
|
354
|
+
while (j < pieces.length) {
|
|
355
|
+
const q = pieces[j];
|
|
356
|
+
if (q.op || isRedirectTarget[j]) {
|
|
357
|
+
j++;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (q.text.startsWith("-") || /^\d+[a-z]*$/i.test(q.text)) {
|
|
361
|
+
j++;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
i = j - 1;
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
return execName(p.text);
|
|
370
|
+
}
|
|
371
|
+
return "";
|
|
372
|
+
}
|
|
373
|
+
function processSegment(pieces, depth, out, pipedIntoShell = false, outputExecuted = false) {
|
|
272
374
|
// A word is a redirect target when the piece before it is `>`/`>>`/`<`.
|
|
273
375
|
const isRedirectTarget = new Array(pieces.length).fill(false);
|
|
274
376
|
for (let i = 1; i < pieces.length; i++) {
|
|
@@ -323,6 +425,14 @@ function processSegment(pieces, depth, out) {
|
|
|
323
425
|
hasEvalFlag = true;
|
|
324
426
|
}
|
|
325
427
|
const codeCarrying = hasShellExec || hasEvalFlag || EVAL_EXECS.has(exec);
|
|
428
|
+
// sable-c6an. Two questions the code conflated, and conflating them gets one
|
|
429
|
+
// of them wrong:
|
|
430
|
+
// codeCarrying — this segment RUNS a command string it was handed
|
|
431
|
+
// executesOutput — this segment's STDOUT becomes code somewhere else
|
|
432
|
+
// (`… | bash`, or a substitution used as a -c script)
|
|
433
|
+
// `bash -c "echo 'rm -rf /'"` is the first and not the second, so its operand
|
|
434
|
+
// stays data; `bash -c "$(echo rm -rf /)"` is the second, so it is code.
|
|
435
|
+
const executesOutput = pipedIntoShell || outputExecuted;
|
|
326
436
|
let seenShell = false;
|
|
327
437
|
let pendingScript = false;
|
|
328
438
|
let prevTextFlag = false;
|
|
@@ -338,7 +448,17 @@ function processSegment(pieces, depth, out) {
|
|
|
338
448
|
seenShell = true;
|
|
339
449
|
// `bash -c <script>` — the next word is a command string, not data.
|
|
340
450
|
if (pendingScript && !p.text.startsWith("-")) {
|
|
341
|
-
|
|
451
|
+
// The script can be entirely a substitution — `bash -c "$(…)"` — where
|
|
452
|
+
// `text` is EMPTY and the substitution IS the script. Reading only `text`
|
|
453
|
+
// dropped it, so `bash -c "$(echo rm -rf /)"` classified low: a bypass of
|
|
454
|
+
// the hard block needing no policy file (sable-c6an). The substitution's
|
|
455
|
+
// OUTPUT is the script, so it sanitizes as output-executed.
|
|
456
|
+
const parts = [];
|
|
457
|
+
if (p.text !== "")
|
|
458
|
+
parts.push(sanitize(p.text, depth + 1));
|
|
459
|
+
for (const sub of p.substs)
|
|
460
|
+
parts.push(sanitize(sub, depth + 1, true));
|
|
461
|
+
out.push({ start: p.start, end: p.end, with: parts.join(" ") });
|
|
342
462
|
pendingScript = false;
|
|
343
463
|
prevTextFlag = false;
|
|
344
464
|
continue;
|
|
@@ -350,7 +470,9 @@ function processSegment(pieces, depth, out) {
|
|
|
350
470
|
}
|
|
351
471
|
// `$(…)` / backticks execute — scan their contents, drop the literal wrapper.
|
|
352
472
|
if (p.substs.length > 0) {
|
|
353
|
-
const inner = p.substs
|
|
473
|
+
const inner = p.substs
|
|
474
|
+
.map((s) => sanitize(s, depth + 1, codeCarrying || executesOutput))
|
|
475
|
+
.join(" ");
|
|
354
476
|
out.push({ start: p.start, end: p.end, with: inner });
|
|
355
477
|
prevTextFlag = false;
|
|
356
478
|
continue;
|
|
@@ -386,7 +508,7 @@ function processSegment(pieces, depth, out) {
|
|
|
386
508
|
}
|
|
387
509
|
prevTextFlag = false;
|
|
388
510
|
// Operands of a text command (`echo`, `grep`, `printf`) are never executed.
|
|
389
|
-
if (isTextExec && i > execIdx) {
|
|
511
|
+
if (isTextExec && i > execIdx && !executesOutput) {
|
|
390
512
|
out.push({ start: p.start, end: p.end, with: " " });
|
|
391
513
|
continue;
|
|
392
514
|
}
|
|
@@ -397,7 +519,7 @@ function processSegment(pieces, depth, out) {
|
|
|
397
519
|
// hide a flag or a path from the patterns (`rm "-rf" "/"`).
|
|
398
520
|
out.push({ start: p.start, end: p.end, with: p.text });
|
|
399
521
|
}
|
|
400
|
-
else if (codeCarrying) {
|
|
522
|
+
else if (codeCarrying || executesOutput) {
|
|
401
523
|
// This segment executes a command string (`ssh host "…"`, `mysql -e "…"`).
|
|
402
524
|
// The quoted argument is code — unquote and scan it, recursively.
|
|
403
525
|
out.push({ start: p.start, end: p.end, with: sanitize(p.text, depth + 1) });
|
|
@@ -409,21 +531,93 @@ function processSegment(pieces, depth, out) {
|
|
|
409
531
|
}
|
|
410
532
|
}
|
|
411
533
|
}
|
|
412
|
-
|
|
534
|
+
/**
|
|
535
|
+
* Remove `\<newline>` line continuations the way a shell does BEFORE parsing.
|
|
536
|
+
* `r\<NL>m -rf /` is `rm -rf /` to bash, so the backslash-newline must be deleted
|
|
537
|
+
* or the token `rm` never forms and the pattern misses it (se-y6vo). Removed when
|
|
538
|
+
* unquoted or inside double quotes; PRESERVED inside single quotes.
|
|
539
|
+
*/
|
|
540
|
+
function stripLineContinuations(s) {
|
|
541
|
+
if (!s.includes("\\"))
|
|
542
|
+
return s;
|
|
543
|
+
let out = "";
|
|
544
|
+
let i = 0;
|
|
545
|
+
let inSingle = false;
|
|
546
|
+
let inDouble = false;
|
|
547
|
+
while (i < s.length) {
|
|
548
|
+
const c = s[i];
|
|
549
|
+
if (c === "'" && !inDouble) {
|
|
550
|
+
inSingle = !inSingle;
|
|
551
|
+
out += c;
|
|
552
|
+
i++;
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (c === '"' && !inSingle) {
|
|
556
|
+
inDouble = !inDouble;
|
|
557
|
+
out += c;
|
|
558
|
+
i++;
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
if (c === "\\" && !inSingle) {
|
|
562
|
+
const nxt = s[i + 1] ?? "";
|
|
563
|
+
if (nxt === "\n") {
|
|
564
|
+
i += 2;
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
if (nxt === "\r") {
|
|
568
|
+
i += 2;
|
|
569
|
+
if (s[i] === "\n")
|
|
570
|
+
i++;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
// Normal escape (`\"`, `\$`, …): keep both chars so an escaped quote does
|
|
574
|
+
// not flip the quote state above.
|
|
575
|
+
out += c;
|
|
576
|
+
if (i + 1 < s.length) {
|
|
577
|
+
out += s[i + 1];
|
|
578
|
+
i += 2;
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
i++;
|
|
582
|
+
}
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
out += c;
|
|
586
|
+
i++;
|
|
587
|
+
}
|
|
588
|
+
return out;
|
|
589
|
+
}
|
|
590
|
+
function sanitize(command, depth, outputExecuted = false) {
|
|
413
591
|
if (!command || depth > MAX_SANITIZE_DEPTH)
|
|
414
592
|
return command;
|
|
415
|
-
|
|
593
|
+
command = stripLineContinuations(command);
|
|
594
|
+
const { pieces, unterminated } = tokenize(command);
|
|
595
|
+
// Fail closed: an unterminated quote means the parse desynchronized from the
|
|
596
|
+
// shell, so a sanitized view could hide an executable payload. Match the raw
|
|
597
|
+
// command instead (se-y6vo).
|
|
598
|
+
if (unterminated)
|
|
599
|
+
return command;
|
|
416
600
|
const replacements = [];
|
|
601
|
+
// Segments keep the operator that ends them, so a segment can be asked
|
|
602
|
+
// whether its output feeds a shell (`echo "rm -rf /" | bash`).
|
|
603
|
+
const segments = [];
|
|
417
604
|
let segment = [];
|
|
418
605
|
for (const p of pieces) {
|
|
419
606
|
if (p.op && CHAIN_OPS.has(p.op)) {
|
|
420
|
-
|
|
607
|
+
segments.push({ pieces: segment, endOp: p.op });
|
|
421
608
|
segment = [];
|
|
422
609
|
continue;
|
|
423
610
|
}
|
|
424
611
|
segment.push(p);
|
|
425
612
|
}
|
|
426
|
-
|
|
613
|
+
segments.push({ pieces: segment, endOp: null });
|
|
614
|
+
for (let k = 0; k < segments.length; k++) {
|
|
615
|
+
const next = segments[k + 1];
|
|
616
|
+
const pipedIntoShell = segments[k].endOp === "|" &&
|
|
617
|
+
next !== undefined &&
|
|
618
|
+
SHELL_EXECS.has(segmentExec(next.pieces));
|
|
619
|
+
processSegment(segments[k].pieces, depth, replacements, pipedIntoShell, outputExecuted);
|
|
620
|
+
}
|
|
427
621
|
if (replacements.length === 0)
|
|
428
622
|
return command;
|
|
429
623
|
replacements.sort((a, b) => a.start - b.start);
|
|
@@ -443,8 +637,117 @@ function sanitize(command, depth) {
|
|
|
443
637
|
* command consumes as DATA is redacted; text a shell or eval wrapper EXECUTES
|
|
444
638
|
* is preserved (and recursively sanitized). Everything else is untouched.
|
|
445
639
|
*/
|
|
640
|
+
/**
|
|
641
|
+
* Does the command that owns a heredoc EXECUTE its body? `cat > f <<EOF` /
|
|
642
|
+
* `grep -q x <<EOF` consume the body as DATA; `bash <<EOF` / `sh <<'EOF'` /
|
|
643
|
+
* `ssh host <<EOF` EXECUTE it. Only a data body may be stripped; an executed
|
|
644
|
+
* body must stay visible to the patterns (else the newline fix opens a fresh
|
|
645
|
+
* bypass — se-y6vo).
|
|
646
|
+
*/
|
|
647
|
+
function heredocOwnerExecutes(line, ltPos) {
|
|
648
|
+
const head = line.slice(0, ltPos);
|
|
649
|
+
const segments = head.split(CHAIN_SPLIT);
|
|
650
|
+
const seg = segments.length ? segments[segments.length - 1] : head;
|
|
651
|
+
const tokens = seg.split(/\s+/).filter((t) => t.length > 0);
|
|
652
|
+
let idx = 0;
|
|
653
|
+
while (idx < tokens.length) {
|
|
654
|
+
const tok = tokens[idx];
|
|
655
|
+
if (ENV_ASSIGNMENT.test(tok)) {
|
|
656
|
+
idx++;
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
const name = execName(tok);
|
|
660
|
+
if (TAIL_WRAPPERS.has(name)) {
|
|
661
|
+
idx++;
|
|
662
|
+
while (idx < tokens.length && (tokens[idx].startsWith("-") || NUMERIC_ARG.test(tokens[idx])))
|
|
663
|
+
idx++;
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
return SHELL_EXECS.has(name) || EVAL_EXECS.has(name);
|
|
667
|
+
}
|
|
668
|
+
return false;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Blank heredoc BODIES that a command consumes as DATA, before tokenizing.
|
|
672
|
+
* A body written to a file (`cat > doc.md <<EOF … EOF`) or searched
|
|
673
|
+
* (`grep <<EOF … EOF`) is data, never executed. Leaving it in place makes the
|
|
674
|
+
* newline-as-separator fix (rf-6pqx) classify ordinary documentation writes as
|
|
675
|
+
* CRITICAL (rf-3rsj) — the same over-block public #230 reported. Stripping it
|
|
676
|
+
* fixes both. A body EXECUTED by a shell/eval owner is KEPT so it is still
|
|
677
|
+
* scanned. Co-designed with kerckhoffs (se-ijzs) and achebe (#230).
|
|
678
|
+
*/
|
|
679
|
+
/**
|
|
680
|
+
* `cat <<EOF | bash` — the body is data to `cat`, but `cat`'s OUTPUT is the
|
|
681
|
+
* script, so the body is executed after all. The owner check above only reads
|
|
682
|
+
* the text BEFORE the introducer and never sees the pipe, so without this a
|
|
683
|
+
* heredoc piped into a shell is stripped and the hard block is silently lost.
|
|
684
|
+
* Keeping a body is the safe direction: it can only over-block, and only for a
|
|
685
|
+
* shape that should block anyway.
|
|
686
|
+
*/
|
|
687
|
+
function heredocOutputPipedToShell(line, ltPos) {
|
|
688
|
+
const stages = line.slice(ltPos).split("|").slice(1);
|
|
689
|
+
for (const stage of stages) {
|
|
690
|
+
const tokens = stage.trim().split(/\s+/).filter((t) => t.length > 0);
|
|
691
|
+
let idx = 0;
|
|
692
|
+
while (idx < tokens.length) {
|
|
693
|
+
const tok = tokens[idx];
|
|
694
|
+
if (ENV_ASSIGNMENT.test(tok)) {
|
|
695
|
+
idx++;
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
const name = execName(tok);
|
|
699
|
+
if (TAIL_WRAPPERS.has(name)) {
|
|
700
|
+
idx++;
|
|
701
|
+
while (idx < tokens.length && (tokens[idx].startsWith("-") || NUMERIC_ARG.test(tokens[idx])))
|
|
702
|
+
idx++;
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
if (SHELL_EXECS.has(name) || EVAL_EXECS.has(name))
|
|
706
|
+
return true;
|
|
707
|
+
break;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
712
|
+
function stripHeredocBodies(command) {
|
|
713
|
+
if (!command.includes("<<"))
|
|
714
|
+
return command;
|
|
715
|
+
const lines = command.split("\n");
|
|
716
|
+
const out = [];
|
|
717
|
+
let k = 0;
|
|
718
|
+
while (k < lines.length) {
|
|
719
|
+
const line = lines[k];
|
|
720
|
+
out.push(line);
|
|
721
|
+
HEREDOC_START.lastIndex = 0;
|
|
722
|
+
const matches = [...line.matchAll(HEREDOC_START)];
|
|
723
|
+
k += 1;
|
|
724
|
+
if (matches.length === 0)
|
|
725
|
+
continue;
|
|
726
|
+
const ltPos = matches[0].index ?? 0;
|
|
727
|
+
const keep = heredocOwnerExecutes(line, ltPos) || heredocOutputPipedToShell(line, ltPos);
|
|
728
|
+
for (const m of matches) {
|
|
729
|
+
const delim = m[3];
|
|
730
|
+
const dash = m[1]; // '-'/'~': a tab-indented terminator is allowed
|
|
731
|
+
while (k < lines.length) {
|
|
732
|
+
// bash terminates only on a line EXACTLY equal to the delimiter — for
|
|
733
|
+
// plain `<<` an indented delimiter is body; `<<-` strips leading TABS.
|
|
734
|
+
let cand = lines[k].replace(/\r$/, "");
|
|
735
|
+
if (dash)
|
|
736
|
+
cand = cand.replace(/^\t+/, "");
|
|
737
|
+
if (cand === delim) {
|
|
738
|
+
k += 1;
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
if (keep)
|
|
742
|
+
out.push(lines[k]);
|
|
743
|
+
k += 1;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return out.join("\n");
|
|
748
|
+
}
|
|
446
749
|
export function sanitizeCommandForMatching(command) {
|
|
447
|
-
return sanitize(command, 0);
|
|
750
|
+
return sanitize(stripHeredocBodies(command), 0);
|
|
448
751
|
}
|
|
449
752
|
/**
|
|
450
753
|
* Assess risk level of a command string.
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from "commander";
|
|
3
3
|
import * as dotenv from "dotenv";
|
|
4
|
+
import { guardSecurityEnvFromDotenv } from "./utils/env-guard.js";
|
|
4
5
|
import { createRunCommand } from "./commands/backend/run.js";
|
|
5
6
|
import { createGetCommand } from "./commands/backend/get.js";
|
|
6
7
|
import { createUsageCommand } from "./commands/backend/usage.js";
|
|
@@ -22,7 +23,8 @@ import { createSitesCommand } from "./commands/sites/index.js";
|
|
|
22
23
|
import { checkForUpdate } from "./utils/update-checker.js";
|
|
23
24
|
import { setAgentMode } from "./utils/formatter.js";
|
|
24
25
|
import { createRequire } from "module";
|
|
25
|
-
|
|
26
|
+
// rf-7dda: a repo `.env` must not be able to disable the hook or its timeouts.
|
|
27
|
+
guardSecurityEnvFromDotenv(() => dotenv.config());
|
|
26
28
|
const require = createRequire(import.meta.url);
|
|
27
29
|
const { version: VERSION } = require("../package.json");
|
|
28
30
|
// Set agent mode early from argv — preAction hooks may not propagate to nested
|
|
@@ -150,6 +150,13 @@ export class BetterleaksScanner {
|
|
|
150
150
|
return [];
|
|
151
151
|
}
|
|
152
152
|
const parsed = JSON.parse(content);
|
|
153
|
+
// #217 — betterleaks >=1.1.2 writes the literal `null` (not `[]`) for a
|
|
154
|
+
// clean scan via the `dir`/`git` subcommands we invoke. That is a valid
|
|
155
|
+
// empty result, not a version mismatch, so it must not reach the warning
|
|
156
|
+
// branch below — otherwise every clean file scanned emits warning noise.
|
|
157
|
+
if (parsed === null) {
|
|
158
|
+
return [];
|
|
159
|
+
}
|
|
153
160
|
if (!Array.isArray(parsed)) {
|
|
154
161
|
// sable-o4k — a stale/incompatible binary emits a non-array shape and
|
|
155
162
|
// would otherwise silently yield zero findings. The managed binary is
|
package/dist/utils/api.js
CHANGED
|
@@ -1,10 +1,58 @@
|
|
|
1
|
+
import axios from "axios";
|
|
1
2
|
import { ConfigManager } from "../core/config-manager.js";
|
|
2
3
|
export const API = "https://rafter.so/api/";
|
|
4
|
+
/**
|
|
5
|
+
* sable-2s6p — the HTTP client for every authenticated Rafter API call.
|
|
6
|
+
*
|
|
7
|
+
* `maxRedirects: 0` is the point of it. axios (via follow-redirects) replays
|
|
8
|
+
* request headers on a redirect, and unlike `Authorization` the custom
|
|
9
|
+
* `x-api-key` header is not stripped when the host changes. Since the API base
|
|
10
|
+
* is user-settable (`--rafter-url`, self-hosted installs), a 302 from a
|
|
11
|
+
* misconfigured or hostile endpoint would walk the caller's API key to another
|
|
12
|
+
* host. Nothing in this CLI needs to follow a redirect, so none of them do.
|
|
13
|
+
*
|
|
14
|
+
* Use this for anything that sends `x-api-key`. Plain `axios` is fine for
|
|
15
|
+
* user-supplied webhooks and other unauthenticated calls.
|
|
16
|
+
*/
|
|
17
|
+
export const apiClient = axios.create({
|
|
18
|
+
maxRedirects: 0,
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* A redirect target is attacker-controlled if the endpoint is. Header values
|
|
22
|
+
* cannot contain CR/LF, but ESC is a legal byte, so an unsanitized Location can
|
|
23
|
+
* emit ANSI sequences that rewrite the user's terminal. Strip anything
|
|
24
|
+
* non-printable and cap the length.
|
|
25
|
+
*/
|
|
26
|
+
function safeForTerminal(value) {
|
|
27
|
+
if (typeof value !== "string")
|
|
28
|
+
return "";
|
|
29
|
+
// eslint-disable-next-line no-control-regex
|
|
30
|
+
const printable = value.replace(/[\u0000-\u001f\u007f-\u009f]/g, "");
|
|
31
|
+
return printable.length > 200 ? `${printable.slice(0, 200)}…` : printable;
|
|
32
|
+
}
|
|
33
|
+
// A refused redirect otherwise surfaces as a bare "Request failed with status
|
|
34
|
+
// code 302", which tells the user nothing about why. Name the cause.
|
|
35
|
+
apiClient.interceptors.response.use(undefined, (error) => {
|
|
36
|
+
const status = error?.response?.status;
|
|
37
|
+
if (status >= 300 && status < 400) {
|
|
38
|
+
const target = safeForTerminal(error?.response?.headers?.location) || "another host";
|
|
39
|
+
error.message =
|
|
40
|
+
`The Rafter API redirected to ${target}, and Rafter does not follow redirects ` +
|
|
41
|
+
`on authenticated requests — your API key would be sent to the redirect target. ` +
|
|
42
|
+
`If you are pointing Rafter at a self-hosted instance, use its final URL.`;
|
|
43
|
+
}
|
|
44
|
+
return Promise.reject(error);
|
|
45
|
+
});
|
|
3
46
|
/** Join API with a path segment without producing a double slash, regardless of leading/trailing slashes on either side. */
|
|
4
47
|
export function apiUrl(path) {
|
|
5
48
|
return `${API.replace(/\/+$/, "")}/${path.replace(/^\/+/, "")}`;
|
|
6
49
|
}
|
|
7
50
|
// Exit codes
|
|
51
|
+
/**
|
|
52
|
+
* Read timeout for short-lived API calls (status polls and the like), in ms.
|
|
53
|
+
* Mirrors the read half of Python's `API_TIMEOUT_SHORT`.
|
|
54
|
+
*/
|
|
55
|
+
export const API_TIMEOUT_SHORT_MS = 30000;
|
|
8
56
|
export const EXIT_SUCCESS = 0;
|
|
9
57
|
export const EXIT_GENERAL_ERROR = 1;
|
|
10
58
|
export const EXIT_SCAN_NOT_FOUND = 2;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security-control env vars must never be settable by a project `.env`.
|
|
3
|
+
*
|
|
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.
|
|
12
|
+
*
|
|
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.
|
|
18
|
+
*/
|
|
19
|
+
const PROTECTED_PREFIX = /^RAFTER_(DISABLE_|HOOK_)/;
|
|
20
|
+
export function guardSecurityEnvFromDotenv(applyDotenv, env = process.env) {
|
|
21
|
+
const present = new Set();
|
|
22
|
+
for (const k of Object.keys(env)) {
|
|
23
|
+
if (PROTECTED_PREFIX.test(k))
|
|
24
|
+
present.add(k);
|
|
25
|
+
}
|
|
26
|
+
applyDotenv();
|
|
27
|
+
for (const k of Object.keys(env)) {
|
|
28
|
+
if (PROTECTED_PREFIX.test(k) && !present.has(k)) {
|
|
29
|
+
delete env[k];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: rafter-security
|
|
3
3
|
description: Security toolkit for AI workflows. Use when scanning code or repos for vulnerabilities, auditing third-party skills/MCPs/agent configs before installing, evaluating shell commands before running them, or generating secure design questions for new features. Provides `rafter run` (remote SAST + SCA, needs RAFTER_API_KEY), `rafter secrets` (offline secrets-only), `rafter agent exec --dry-run` (command-risk classification), and `rafter skill review`.
|
|
4
|
-
version: 0.10.
|
|
4
|
+
version: 0.10.2
|
|
5
5
|
homepage: https://rafter.so
|
|
6
6
|
metadata:
|
|
7
7
|
openclaw:
|
|
@@ -64,7 +64,7 @@ When: before firing multiple remote scans, or when the user asks about limits.
|
|
|
64
64
|
|
|
65
65
|
Classify and optionally run a shell command through Rafter's risk tiers (critical / high / medium / low).
|
|
66
66
|
|
|
67
|
-
When: any time a destructive-looking command is about to be executed by an agent. Use `--dry-run` to classify without running.
|
|
67
|
+
When: any time a destructive-looking command is about to be executed by an agent. Use `--dry-run` to classify without running: exit 0 = allowed, 1 = blocked, 2 = needs a person's approval. Without `--dry-run`, a command that needs approval is only ever approved by a person at an interactive terminal; from an agent's shell it is denied.
|
|
68
68
|
|
|
69
69
|
Example: `rafter agent exec --dry-run -- rm -rf $WORK_DIR`
|
|
70
70
|
|
|
@@ -10,7 +10,7 @@ Rafter exposes two hook handlers over stdio:
|
|
|
10
10
|
- `rafter hook posttool` — read a JSON event after a tool ran; log to audit trail, optionally rescan written files for secrets.
|
|
11
11
|
|
|
12
12
|
For platforms without hooks, the same classifier is reachable as:
|
|
13
|
-
- `rafter agent exec --dry-run -- <command>` (
|
|
13
|
+
- `rafter agent exec --dry-run -- <command>` (prints the risk tier and runs nothing; exits 0 allowed, 1 blocked, 2 needs a person's approval)
|
|
14
14
|
- `rafter mcp serve` → MCP tool `evaluate_command`
|
|
15
15
|
|
|
16
16
|
## Risk Tiers
|
|
@@ -66,7 +66,7 @@ If the block is a false positive **for this specific context**, the right path i
|
|
|
66
66
|
allow:
|
|
67
67
|
- "^terraform destroy -target=module\\.sandbox"
|
|
68
68
|
```
|
|
69
|
-
2. Or
|
|
69
|
+
2. Or have a person run it: `rafter agent exec -- <command>` prompts for approval only at an interactive terminal and logs the override to the audit trail. There is no acknowledgement flag — `--force` no longer skips the prompt and a piped `yes` is not an approval — because any flag or input an agent can supply is not a person's decision.
|
|
70
70
|
3. Never disable the hook globally to get past one command — that silently drops protection for every future call.
|
|
71
71
|
|
|
72
72
|
## Audit Trail
|