@rafter-security/cli 0.11.6 → 0.11.7

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.
@@ -87,9 +87,10 @@ const REVERSE_SHELL_PATTERN_SOURCES = [
87
87
  `system\\.net\\.sockets\\.tcpclient`,
88
88
  ];
89
89
  const CRITICAL_PATTERN_SOURCES = [
90
- // rm -rf / (root only, any flag order)
91
- `rm\\s+(-[a-z]*r[a-z]*\\s+)*-[a-z]*f[a-z]*\\s+/(\\s|$)`,
92
- `rm\\s+(-[a-z]*f[a-z]*\\s+)*-[a-z]*r[a-z]*\\s+/(\\s|$)`,
90
+ // rm -rf / (root only, any flag order). `/*` is the root's every entry, so it
91
+ // is the same catastrophe as `/` (sable-e7ju).
92
+ `rm\\s+(-[a-z]*r[a-z]*\\s+)*-[a-z]*f[a-z]*\\s+/\\*?(\\s|$)`,
93
+ `rm\\s+(-[a-z]*f[a-z]*\\s+)*-[a-z]*r[a-z]*\\s+/\\*?(\\s|$)`,
93
94
  // rm -rf on critical top-level directories
94
95
  `rm\\s+(-[a-z]*r[a-z]*\\s+)*-[a-z]*f[a-z]*\\s+/(${CRITICAL_DIRS})(/|\\s|$)`,
95
96
  `rm\\s+(-[a-z]*f[a-z]*\\s+)*-[a-z]*r[a-z]*\\s+/(${CRITICAL_DIRS})(/|\\s|$)`,
@@ -327,6 +328,7 @@ function tokenize(s) {
327
328
  const start = i;
328
329
  let text = "";
329
330
  let quoted = false;
331
+ let escaped = false;
330
332
  const substs = [];
331
333
  while (i < s.length) {
332
334
  const ch = s[i];
@@ -348,6 +350,7 @@ function tokenize(s) {
348
350
  continue;
349
351
  }
350
352
  i++;
353
+ escaped = true;
351
354
  if (i < s.length) {
352
355
  text += s[i];
353
356
  i++;
@@ -436,7 +439,7 @@ function tokenize(s) {
436
439
  text += ch;
437
440
  i++;
438
441
  }
439
- pieces.push({ start, end: i, op: null, text, quoted, substs });
442
+ pieces.push({ start, end: i, op: null, text, quoted, escaped, substs });
440
443
  }
441
444
  return { pieces, unterminated };
442
445
  }
@@ -619,7 +622,9 @@ function processSegment(pieces, depth, out, pipedIntoShell = false, outputExecut
619
622
  // Unquote a quoted executable so quoting the *command name* cannot break
620
623
  // the pattern anchors (`"rm" -rf /`, `r"m" -rf /`, `watch "rm -rf /"`).
621
624
  // Unquoting only ever exposes more to the patterns — the safe direction.
622
- if (p.quoted)
625
+ // An escaped one is the same: the shell drops the backslash, so `r\m`
626
+ // runs `rm` and the patterns must see `rm` (sable-e7ju).
627
+ if (p.quoted || p.escaped)
623
628
  out.push({ start: p.start, end: p.end, with: p.text });
624
629
  prevTextFlag = false;
625
630
  continue;
@@ -1516,38 +1521,222 @@ function expandBraceLists(s) {
1516
1521
  } while (s !== prev && passes < MAX_BRACE_PASSES);
1517
1522
  return s;
1518
1523
  }
1524
+ /** GNU rm's long options, against which an abbreviation is resolved. */
1525
+ const RM_LONG_OPTIONS = [
1526
+ "dir", "force", "help", "interactive", "no-preserve-root", "one-file-system",
1527
+ "preserve-root", "recursive", "verbose", "version",
1528
+ ];
1529
+ /**
1530
+ * The rm long option `name` (without `--` or `=value`) stands for, or null.
1531
+ * GNU getopt_long accepts an exact name or any UNIQUE prefix of one: `--recur`
1532
+ * and `--r` are `--recursive`, `--forc` is `--force`, while `--ver` (verbose or
1533
+ * version) is ambiguous and rejected, so rm runs nothing. (sable-e7ju)
1534
+ */
1535
+ function resolveRmLongOption(name) {
1536
+ if (!name)
1537
+ return null;
1538
+ if (RM_LONG_OPTIONS.includes(name))
1539
+ return name;
1540
+ const hits = RM_LONG_OPTIONS.filter((o) => o.startsWith(name));
1541
+ return hits.length === 1 ? hits[0] : null;
1542
+ }
1543
+ /** The rm command word, at the start of the string or after a separator. */
1544
+ const RM_WORD = /(^|[\s;&|()<>])((?:[\w./-]*\/)?rm)(?=[ \t])/gi;
1545
+ /** A word rm's getopt reads as an option: `-rf`, `--recur`, `--interactive=never`. */
1546
+ const RM_FLAG_WORD = /^--?[A-Za-z][\w-]*(?:=.*)?$/;
1547
+ /** A file redirect inside an argv (`2>/dev/null`, `>> log`, `<in`): not an argument. */
1548
+ const RM_REDIRECT = /\d*(?:>>|>&|<&|>\||<>|>|<)(?![(<])/y;
1549
+ /**
1550
+ * The words of one rm argv, read from `s` at `from` up to the first unquoted
1551
+ * separator (`;`, `&`, `|`, `(`, `)`, a heredoc or process substitution, a
1552
+ * newline) or comment. Each word keeps its source text and its value with
1553
+ * quotes and escapes removed. A file redirect and its target are one word
1554
+ * marked `redirect`, since the shell strips them from the argv rm permutes:
1555
+ * `rm 2>/dev/null / -rf` is still `rm -rf /`.
1556
+ */
1557
+ function rmArgWords(s, from) {
1558
+ const skipBlanks = (k) => {
1559
+ while (k < s.length && (s[k] === " " || s[k] === "\t"))
1560
+ k++;
1561
+ return k;
1562
+ };
1563
+ const words = [];
1564
+ let i = from;
1565
+ for (;;) {
1566
+ const j = skipBlanks(i);
1567
+ RM_REDIRECT.lastIndex = j;
1568
+ const redir = RM_REDIRECT.exec(s);
1569
+ if (redir) {
1570
+ const t = readRmWord(s, skipBlanks(j + redir[0].length));
1571
+ if (!t)
1572
+ return { words, end: i };
1573
+ words.push({ src: s.slice(j, t.end), val: "", redirect: true });
1574
+ i = t.end;
1575
+ continue;
1576
+ }
1577
+ const w = readRmWord(s, j);
1578
+ if (!w)
1579
+ return { words, end: i };
1580
+ words.push({ src: s.slice(j, w.end), val: w.val });
1581
+ i = w.end;
1582
+ }
1583
+ }
1584
+ /** One shell word of `s` starting at `j`, or null at a separator, comment or end. */
1585
+ function readRmWord(s, j) {
1586
+ if (j >= s.length || /[;&|()<>\n\r#]/.test(s[j]))
1587
+ return null;
1588
+ let val = "";
1589
+ while (j < s.length && !/[\s;&|()<>]/.test(s[j])) {
1590
+ const c = s[j];
1591
+ if (c === "\\") {
1592
+ if (j + 1 < s.length)
1593
+ val += s[j + 1];
1594
+ j += 2;
1595
+ continue;
1596
+ }
1597
+ if (c === "'" || c === '"') {
1598
+ let k = j + 1;
1599
+ while (k < s.length && s[k] !== c) {
1600
+ if (c === '"' && s[k] === "\\" && k + 1 < s.length)
1601
+ k++;
1602
+ val += s[k];
1603
+ k++;
1604
+ }
1605
+ j = k + 1;
1606
+ continue;
1607
+ }
1608
+ val += c;
1609
+ j++;
1610
+ }
1611
+ return { val, end: Math.min(j, s.length) };
1612
+ }
1519
1613
  /**
1520
- * Reduce an `rm` whose flags carry BOTH recursive and force intent — in any
1521
- * long, short or `--no-preserve-root` spelling, adjacent or not — to the
1522
- * `rm -rf` form the existing patterns catch. Scoped to `rm`, and the collapse
1523
- * only ever adds `-rf`, so it cannot turn a benign command destructive.
1614
+ * Reduce an `rm`'s flags — in any long, abbreviated-long, short or
1615
+ * `--no-preserve-root` spelling, adjacent or not, before or AFTER its operands
1616
+ * — to the short form the patterns read, `-rf`, `-r` or `-f`, placed before
1617
+ * the operands, with any redirect moved after them. GNU rm permutes its argv, so `rm / -rf` is `rm -rf /`, and a
1618
+ * long or trailing spelling gets exactly the tier of its short twin. Option
1619
+ * parsing stops at the first `--`: after it `-rf` is a file name, so it stays
1620
+ * an operand, written `./-rf` so no pattern reads it as a flag. Scoped to
1621
+ * `rm`; it only rewrites flags rm would honor, so it cannot turn a benign
1622
+ * command destructive. (sable-e7ju)
1524
1623
  */
1525
1624
  function canonicalizeRmFlags(s) {
1526
1625
  if (!/\brm\b/i.test(s))
1527
1626
  return s;
1528
- const re = /(^|[\s;&|()<>])((?:[\w./-]*\/)?rm)((?:[ \t]+--?[A-Za-z][\w-]*)*)/gi;
1529
- return s.replace(re, (whole, pre, cmd, flags) => {
1530
- if (!flags.trim())
1531
- return whole;
1627
+ let out = "";
1628
+ let last = 0;
1629
+ RM_WORD.lastIndex = 0;
1630
+ for (let m = RM_WORD.exec(s); m; m = RM_WORD.exec(s)) {
1631
+ const cmdEnd = m.index + m[0].length;
1632
+ const { words, end } = rmArgWords(s, cmdEnd);
1532
1633
  let recursive = false;
1533
1634
  let force = false;
1534
- for (const tok of flags.trim().split(/\s+/)) {
1535
- const low = tok.toLowerCase();
1536
- if (low.startsWith("--")) {
1537
- if (low === "--recursive")
1538
- recursive = true;
1539
- else if (low === "--force" || low === "--no-preserve-root")
1540
- force = true;
1635
+ let optsDone = false;
1636
+ const operands = [];
1637
+ const redirects = [];
1638
+ for (const w of words) {
1639
+ if (w.redirect) {
1640
+ redirects.push(w.src);
1641
+ continue;
1541
1642
  }
1542
- else {
1543
- if (low.includes("r"))
1544
- recursive = true;
1545
- if (low.includes("f"))
1546
- force = true;
1643
+ if (!optsDone && w.val === "--") {
1644
+ optsDone = true;
1645
+ continue;
1646
+ }
1647
+ if (!optsDone && RM_FLAG_WORD.test(w.val)) {
1648
+ const low = w.val.toLowerCase();
1649
+ if (low.startsWith("--")) {
1650
+ const opt = resolveRmLongOption(low.slice(2).split("=")[0]);
1651
+ if (opt === "recursive")
1652
+ recursive = true;
1653
+ else if (opt === "force" || opt === "no-preserve-root")
1654
+ force = true;
1655
+ }
1656
+ else {
1657
+ if (low.includes("r"))
1658
+ recursive = true;
1659
+ if (low.includes("f"))
1660
+ force = true;
1661
+ }
1662
+ continue;
1663
+ }
1664
+ operands.push(optsDone && w.val.startsWith("-") ? `./${w.src}` : w.src);
1665
+ }
1666
+ // Resume after this argv either way: an `rm` inside it is an operand, and
1667
+ // rescanning from the command word would make the walk quadratic.
1668
+ RM_WORD.lastIndex = Math.max(end, cmdEnd);
1669
+ if (!recursive && !force)
1670
+ continue;
1671
+ const flags = `-${recursive ? "r" : ""}${force ? "f" : ""}`;
1672
+ out += `${s.slice(last, cmdEnd)} ${[flags, ...operands, ...redirects].join(" ")}`;
1673
+ last = end;
1674
+ }
1675
+ return out + s.slice(last);
1676
+ }
1677
+ /** Find actions that delete what the walk visits. */
1678
+ const FIND_DELETE_EXECS = new Set(["-exec", "-execdir", "-ok", "-okdir"]);
1679
+ /** A find start path that IS a critical root: `/`, `/*`, `/etc`, `/home/`, `/usr/*`. */
1680
+ const FIND_CRITICAL_ROOT = new RegExp(`^/+(?:(?:${CRITICAL_DIRS})/*)?\\*?$`, "i");
1681
+ /** A find start path that IS the home directory: `~`, `~/`, `$HOME`, `${HOME}/`. */
1682
+ const FIND_HOME_ROOT = /^(?:~|\$home|\$\{home\})\/*\*?$/i;
1683
+ /**
1684
+ * The `rm -rf <start path>` equivalent of every `find` that deletes what it
1685
+ * walks (`-delete`, or `-exec`/`-execdir`/`-ok`/`-okdir rm`) from a critical
1686
+ * root or the home directory, or "" if none. Matched against the CRITICAL and
1687
+ * HIGH patterns, never MEDIUM: `find / -delete` is as catastrophic as
1688
+ * `rm -rf /`, and `find ~ -delete` needs approval like `rm -rf ~`, while
1689
+ * `find . -name '*.pyc' -delete` must keep its own tier rather than inherit
1690
+ * `rm -rf .`'s approval gate. Only a start path that is itself such a root
1691
+ * counts, not one below it: `find /home/me/proj -name '*.pyc' -delete` is
1692
+ * routine cleanup, and a find filters what it deletes. (sable-e7ju)
1693
+ */
1694
+ function findDeletionAsRm(view) {
1695
+ if (!/\bfind\b/i.test(view))
1696
+ return "";
1697
+ const { pieces, unterminated } = tokenize(view);
1698
+ if (unterminated)
1699
+ return "";
1700
+ const out = [];
1701
+ let seg = [];
1702
+ const flush = () => {
1703
+ const at = seg.findIndex((p) => p.op === null && execName(p.text) === "find");
1704
+ if (at !== -1) {
1705
+ let i = at + 1;
1706
+ // Leading options: -H -L -P, -D <debugopts>, -O<level>.
1707
+ while (i < seg.length && /^-([HLP]|D|O\d*)$/i.test(seg[i].text)) {
1708
+ i += seg[i].text.toLowerCase() === "-d" ? 2 : 1;
1709
+ }
1710
+ const paths = [];
1711
+ while (i < seg.length && seg[i].op === null && !/^[-(!),]/.test(seg[i].text)) {
1712
+ if (FIND_CRITICAL_ROOT.test(seg[i].text))
1713
+ paths.push(seg[i].text.replace(/^\/+/, "/"));
1714
+ else if (FIND_HOME_ROOT.test(seg[i].text))
1715
+ paths.push("~");
1716
+ i++;
1717
+ }
1718
+ let deletes = false;
1719
+ for (let j = i; j < seg.length; j++) {
1720
+ const t = seg[j].text.toLowerCase();
1721
+ if (t === "-delete")
1722
+ deletes = true;
1723
+ else if (FIND_DELETE_EXECS.has(t) && seg[j + 1] && execName(seg[j + 1].text) === "rm")
1724
+ deletes = true;
1547
1725
  }
1726
+ if (deletes && paths.length > 0)
1727
+ out.push(`rm -rf ${paths.join(" ")}`);
1728
+ }
1729
+ seg = [];
1730
+ };
1731
+ for (const p of pieces) {
1732
+ if (p.op !== null && CHAIN_OPS.has(p.op)) {
1733
+ flush();
1734
+ continue;
1548
1735
  }
1549
- return recursive && force ? `${pre}${cmd} -rf` : whole;
1550
- });
1736
+ seg.push(p);
1737
+ }
1738
+ flush();
1739
+ return out.join(" ; ");
1551
1740
  }
1552
1741
  /**
1553
1742
  * A spelling-normalized view of a command for pattern matching. Decodes ANSI-C
@@ -1666,13 +1855,27 @@ function matchVariants(command) {
1666
1855
  if (raw)
1667
1856
  variants.push(raw);
1668
1857
  const normalized = normalizeForMatching(command);
1669
- if (normalized !== command) {
1670
- const normSan = sanitizeCommandForMatching(normalized).toLowerCase().trim();
1671
- if (normSan && normSan !== raw)
1672
- variants.push(normSan);
1858
+ // rm's flags are canonicalized again AFTER sanitizing, because only the
1859
+ // sanitizer resolves an escaped command word: `r\m --recur --forc /` reads
1860
+ // `rm …` only once the backslash is gone (sable-e7ju).
1861
+ const normSan = canonicalizeRmFlags(normalized !== command ? sanitizeCommandForMatching(normalized) : raw).toLowerCase().trim();
1862
+ if (normSan && normSan !== raw)
1863
+ variants.push(normSan);
1864
+ // The normalized view is truncated past MAX_NORMALIZE_INPUT, which would drop
1865
+ // a flag padded out past the bound (`rm / <20k chars> -rf`); canonicalize the
1866
+ // whole raw view too (sable-e7ju).
1867
+ if (command.length > MAX_NORMALIZE_INPUT) {
1868
+ const rawCanon = canonicalizeRmFlags(raw);
1869
+ if (rawCanon !== raw && !variants.includes(rawCanon))
1870
+ variants.push(rawCanon);
1673
1871
  }
1674
1872
  return variants;
1675
1873
  }
1874
+ /** The views matched against the CRITICAL and HIGH patterns: matchVariants plus find-as-rm. */
1875
+ function criticalVariants(variants) {
1876
+ const found = variants.map(findDeletionAsRm).filter(Boolean);
1877
+ return found.length ? [...variants, ...found] : variants;
1878
+ }
1676
1879
  export function assessCommandRisk(command) {
1677
1880
  // rf-vnxs: checked on the RAW command, before sanitization, because the
1678
1881
  // disarm is an ordinary well-formed invocation — there is nothing malformed
@@ -1684,8 +1887,9 @@ export function assessCommandRisk(command) {
1684
1887
  const variants = matchVariants(command);
1685
1888
  if (variants.length === 0)
1686
1889
  return "low";
1890
+ const critical = criticalVariants(variants);
1687
1891
  for (const pattern of CRITICAL_PATTERNS) {
1688
- if (variants.some((v) => pattern.test(v)))
1892
+ if (critical.some((v) => pattern.test(v)))
1689
1893
  return "critical";
1690
1894
  }
1691
1895
  // rf-zvll B2 + sable-jtxp: a shell fed its program from a channel we cannot
@@ -1695,8 +1899,10 @@ export function assessCommandRisk(command) {
1695
1899
  // softened to approval.
1696
1900
  if (shellProgramFromStream(command) || evasionConstructRisk(command))
1697
1901
  return "high";
1902
+ // The find-as-rm views count here too, so `find ~ -delete` rates like
1903
+ // `rm -rf ~` (sable-e7ju); they hold only critical and home roots.
1698
1904
  for (const pattern of HIGH_PATTERNS) {
1699
- if (variants.some((v) => pattern.test(v)))
1905
+ if (critical.some((v) => pattern.test(v)))
1700
1906
  return "high";
1701
1907
  }
1702
1908
  for (const pattern of MEDIUM_PATTERNS) {
@@ -1716,7 +1922,7 @@ export function matchedCriticalPattern(command) {
1716
1922
  return "rafter security configuration is protected (rf-vnxs)";
1717
1923
  }
1718
1924
  // Mirror assessCommandRisk: a critical match may live in the normalized view.
1719
- const variants = matchVariants(command);
1925
+ const variants = criticalVariants(matchVariants(command));
1720
1926
  for (const pattern of CRITICAL_PATTERNS) {
1721
1927
  if (variants.some((v) => pattern.test(v)))
1722
1928
  return pattern.source;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rafter-security/cli",
3
- "version": "0.11.6",
3
+ "version": "0.11.7",
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.6
4
+ version: 0.11.7
5
5
  homepage: https://rafter.so
6
6
  metadata:
7
7
  openclaw: