@oked/sdk 0.3.1 → 0.3.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/classify.js +49 -18
- package/dist/describe.js +11 -1
- package/package.json +1 -1
package/dist/classify.js
CHANGED
|
@@ -60,17 +60,26 @@ const HIGH_STAKES_TOOLS = new Set([]);
|
|
|
60
60
|
// effect IS safe by default (reads, queries, `ls`, `grep`, `jq`, `awk`, `sort`,
|
|
61
61
|
// unknown commands, …). We only enumerate the things that DO warrant a log
|
|
62
62
|
// (WARNING_*) or a prompt (REVIEW_*, HIGH_STAKES_*).
|
|
63
|
-
// File deletion (rm/rmdir/trash)
|
|
64
|
-
// so the ephemeral-temp downgrade can run
|
|
65
|
-
//
|
|
66
|
-
//
|
|
67
|
-
//
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
// File deletion (rm/rmdir/trash), detected in COMMAND POSITION only. Checked
|
|
64
|
+
// PER-STAGE (not the full command) so the ephemeral-temp downgrade can run
|
|
65
|
+
// first. Earlier this matched the bare word `\brm\b` anywhere, which fired on
|
|
66
|
+
// `rm` sitting in an ARGUMENT — a branch name (`git push origin fix-rm-thing`),
|
|
67
|
+
// a filename (`touch fix-rm.txt`), or echo text — turning benign commands
|
|
68
|
+
// (including file creation) into false high_stakes prompts. Now it only fires
|
|
69
|
+
// when the delete word is the command being run.
|
|
70
|
+
//
|
|
71
|
+
// DELETE_LEAD strips leading command wrappers / loop-body keywords (and their
|
|
72
|
+
// flags) so a real delete that isn't literally first — `sudo rm`, `command rm`,
|
|
73
|
+
// `xargs -0 rm`, `do rm` (from `for …; do rm …; done`) — still anchors at start.
|
|
74
|
+
// Wrappers that take their own structured args (env, ssh, NAME=…, sh -c) are
|
|
75
|
+
// handled by their dedicated stage classifiers, which recurse into the inner
|
|
76
|
+
// command, so they're intentionally not here.
|
|
77
|
+
const DELETE_LEAD = /^(?:(?:sudo|command|exec|builtin|time|nice|xargs|then|do|else)\b\s*(?:-\S+\s+)*)+/;
|
|
78
|
+
// rm/rmdir/trash at the (lead-stripped) start of the stage, at the start of a
|
|
79
|
+
// line, or immediately inside a substitution/subshell/group ($( ), `…`, ( ),
|
|
80
|
+
// { ). Deliberately NOT after `;`/`|`/`&`/a quote, so a delete word sitting in
|
|
81
|
+
// quoted DATA (echo text, a commit message) doesn't match.
|
|
82
|
+
const DELETE_COMMAND_RE = /(?:^|[\n`({]|\$\()\s*(?:rm|rmdir|trash|trash-put)\b/;
|
|
74
83
|
// Bash commands classified as high stakes (destructive, irreversible, external).
|
|
75
84
|
// Scanned on the FULL command (some patterns, e.g. download|shell, span a pipe).
|
|
76
85
|
// File deletion lives in DELETE_PATTERNS (per-stage) instead, see above.
|
|
@@ -636,6 +645,20 @@ function classifySshStage(stage) {
|
|
|
636
645
|
}
|
|
637
646
|
return "review"; // only options / a bare host token → treat as remote access
|
|
638
647
|
}
|
|
648
|
+
/**
|
|
649
|
+
* `sh -c '<shell>'` / `bash -c "<shell>"` (also zsh/ksh/dash, and combined flags
|
|
650
|
+
* like `-lc`) runs the quoted argument AS shell. Recurse on the body so its
|
|
651
|
+
* effect is classified by the same rules — mirrors classifySshStage. Only fires
|
|
652
|
+
* when the shell IS the stage's command; a `-c` flag on another tool (e.g.
|
|
653
|
+
* `grep -c`) is unaffected because the stage doesn't start with a shell name.
|
|
654
|
+
* Returns null if the stage isn't a `sh -c`-style invocation.
|
|
655
|
+
*/
|
|
656
|
+
function classifyShellCStage(stage) {
|
|
657
|
+
const m = stage.match(/^(?:bash|sh|zsh|ksh|dash)\b[^'"]*?\s-[A-Za-z]*c\s+(['"])([\s\S]*?)\1/);
|
|
658
|
+
if (!m)
|
|
659
|
+
return null;
|
|
660
|
+
return classifyBashCommand(m[2]);
|
|
661
|
+
}
|
|
639
662
|
function classifyBashCommand(command) {
|
|
640
663
|
if (!command)
|
|
641
664
|
return "safe";
|
|
@@ -687,15 +710,15 @@ function classifyBashCommand(command) {
|
|
|
687
710
|
* is folded in by the caller. Returns the stage's tier (default `safe`).
|
|
688
711
|
*/
|
|
689
712
|
function classifyStageShellEffects(stage) {
|
|
690
|
-
// File deletion, per-stage so the ephemeral-temp carve-out
|
|
691
|
-
// only throwaway temp files (/tmp, $TMPDIR, /var/folders, …)
|
|
692
|
-
// non-temp target (or the temp root itself) → high_stakes.
|
|
713
|
+
// File deletion in COMMAND POSITION, per-stage so the ephemeral-temp carve-out
|
|
714
|
+
// can win: deleting only throwaway temp files (/tmp, $TMPDIR, /var/folders, …)
|
|
715
|
+
// → warning; any non-temp target (or the temp root itself) → high_stakes.
|
|
716
|
+
// A delete word sitting in an argument (filename, branch name, echo text) is
|
|
717
|
+
// NOT a deletion — that's the false positive DELETE_COMMAND_RE avoids.
|
|
693
718
|
if (isEphemeralOnlyDeletion(stage))
|
|
694
719
|
return "warning";
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
return "high_stakes";
|
|
698
|
-
}
|
|
720
|
+
if (DELETE_COMMAND_RE.test(stage.replace(DELETE_LEAD, "")))
|
|
721
|
+
return "high_stakes";
|
|
699
722
|
// Destructive/irreversible single commands, matched at stage start with any
|
|
700
723
|
// leading `sudo ` stripped (so `sudo mkfs …` is still caught, but the word
|
|
701
724
|
// can't fire inside an argument). These don't span pipes, so `^`-anchoring is
|
|
@@ -731,6 +754,14 @@ function classifyStageShellEffects(stage) {
|
|
|
731
754
|
const sshTier = classifySshStage(stage);
|
|
732
755
|
if (sshTier !== null)
|
|
733
756
|
return sshTier;
|
|
757
|
+
// `sh -c '<shell>'` / `bash -c "<shell>"` runs the quoted argument AS shell —
|
|
758
|
+
// recurse so its effect is classified by the same rules (mirrors ssh). This
|
|
759
|
+
// is why the command-position delete scan above can ignore quoted text: a real
|
|
760
|
+
// shell-out like `bash -c 'rm -rf /important'` is caught HERE, by re-running
|
|
761
|
+
// the body as a command, while `echo 'rm …'` (data) is not.
|
|
762
|
+
const shellCTier = classifyShellCStage(stage);
|
|
763
|
+
if (shellCTier !== null)
|
|
764
|
+
return shellCTier;
|
|
734
765
|
// sudo: privilege escalation. A high-stakes inner command was already caught
|
|
735
766
|
// by the full-string scan above (\b patterns match through the `sudo` prefix);
|
|
736
767
|
// anything else still warrants review.
|
package/dist/describe.js
CHANGED
|
@@ -859,6 +859,14 @@ function parseRmTargets(argsString) {
|
|
|
859
859
|
i++;
|
|
860
860
|
if (i >= str.length)
|
|
861
861
|
break;
|
|
862
|
+
// A shell command separator (`;`, `|`, `||`, `&&`, `&`), a redirect (`>`,
|
|
863
|
+
// `>>`, `2>`, `&>`), a closing `)`/`}`, a comment, or a newline ends the rm
|
|
864
|
+
// argument list — everything after belongs to a different command. Without
|
|
865
|
+
// this, a compound `if …; then rm -rf "$P"; else echo "…"; fi` would sweep
|
|
866
|
+
// the trailing `;`/`else`/`echo`/`fi` (and the echo's text) in as "files",
|
|
867
|
+
// rendering a bogus "Delete N files recursively".
|
|
868
|
+
if (/^(?:&?\d*[<>]|[;|&`)}#\n])/.test(str.slice(i)))
|
|
869
|
+
break;
|
|
862
870
|
let token;
|
|
863
871
|
if (str[i] === '"') {
|
|
864
872
|
const close = str.indexOf('"', i + 1);
|
|
@@ -884,7 +892,9 @@ function parseRmTargets(argsString) {
|
|
|
884
892
|
}
|
|
885
893
|
else {
|
|
886
894
|
const start = i;
|
|
887
|
-
|
|
895
|
+
// Stop the bare token at whitespace OR a separator/redirect char, so
|
|
896
|
+
// `"$P";` and `$P;` both yield just the path.
|
|
897
|
+
while (i < str.length && !/[\s;|&<>`)}#]/.test(str[i]))
|
|
888
898
|
i++;
|
|
889
899
|
token = str.slice(start, i);
|
|
890
900
|
}
|