@oked/sdk 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classify.js +100 -24
- package/dist/describe.js +44 -14
- 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.
|
|
@@ -325,10 +334,12 @@ function maxTier(a, b) {
|
|
|
325
334
|
return TIER_ORDER[a] >= TIER_ORDER[b] ? a : b;
|
|
326
335
|
}
|
|
327
336
|
/** Split a shell command into top-level segments on the operators that
|
|
328
|
-
* sequence separate commands: `|`, `||`, `&&`,
|
|
329
|
-
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
337
|
+
* sequence separate commands: `|`, `||`, `&&`, `;`, and a bare newline (a
|
|
338
|
+
* newline separates commands in a multi-line script exactly like `;`).
|
|
339
|
+
* Operators inside quoted strings, command substitutions, and heredoc bodies —
|
|
340
|
+
* including the `"$(cat <<'EOF' … )"` heredoc form used for commit messages —
|
|
341
|
+
* are kept intact so message text isn't split. A backslash-escaped newline is a
|
|
342
|
+
* line continuation, not a separator. Returns trimmed, non-empty segments. */
|
|
332
343
|
function splitTopLevel(cmd) {
|
|
333
344
|
const out = [];
|
|
334
345
|
let cur = "";
|
|
@@ -407,6 +418,19 @@ function splitTopLevel(cmd) {
|
|
|
407
418
|
i++;
|
|
408
419
|
continue;
|
|
409
420
|
}
|
|
421
|
+
// A bare newline sequences commands like `;`. A backslash immediately before
|
|
422
|
+
// it is a line continuation (the two lines are one command), so keep it.
|
|
423
|
+
if (ch === "\n") {
|
|
424
|
+
if (cmd[i - 1] === "\\") {
|
|
425
|
+
cur += ch;
|
|
426
|
+
i++;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
out.push(cur.trim());
|
|
430
|
+
cur = "";
|
|
431
|
+
i++;
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
410
434
|
cur += ch;
|
|
411
435
|
i++;
|
|
412
436
|
}
|
|
@@ -636,6 +660,47 @@ function classifySshStage(stage) {
|
|
|
636
660
|
}
|
|
637
661
|
return "review"; // only options / a bare host token → treat as remote access
|
|
638
662
|
}
|
|
663
|
+
/**
|
|
664
|
+
* `sh -c '<shell>'` / `bash -c "<shell>"` (also zsh/ksh/dash, and combined flags
|
|
665
|
+
* like `-lc`) runs the quoted argument AS shell. Recurse on the body so its
|
|
666
|
+
* effect is classified by the same rules — mirrors classifySshStage. Only fires
|
|
667
|
+
* when the shell IS the stage's command; a `-c` flag on another tool (e.g.
|
|
668
|
+
* `grep -c`) is unaffected because the stage doesn't start with a shell name.
|
|
669
|
+
* Returns null if the stage isn't a `sh -c`-style invocation.
|
|
670
|
+
*/
|
|
671
|
+
function classifyShellCStage(stage) {
|
|
672
|
+
const m = stage.match(/^(?:bash|sh|zsh|ksh|dash)\b[^'"]*?\s-[A-Za-z]*c\s+(['"])([\s\S]*?)\1/);
|
|
673
|
+
if (!m)
|
|
674
|
+
return null;
|
|
675
|
+
return classifyBashCommand(m[2]);
|
|
676
|
+
}
|
|
677
|
+
// A curl/wget whose target host is loopback (localhost, 127.0.0.0/8, 0.0.0.0,
|
|
678
|
+
// ::1). A request to a server on this machine is a LOCAL action, not an outward
|
|
679
|
+
// one, so a mutating method/body (POST/PUT/DELETE, -d, …) to it doesn't warrant
|
|
680
|
+
// a prompt — it downgrades to `warning`. (Download-and-execute, `curl … | sh`,
|
|
681
|
+
// is matched earlier on the FULL command and stays high_stakes regardless of
|
|
682
|
+
// host, so this only affects the request-method/body patterns.)
|
|
683
|
+
function isLoopbackAuthority(authority) {
|
|
684
|
+
const host = authority.replace(/^\[/, "").replace(/\]$/, "").toLowerCase();
|
|
685
|
+
return host === "localhost" || host === "0.0.0.0" || host === "::1" ||
|
|
686
|
+
/^127(?:\.\d{1,3}){3}$/.test(host);
|
|
687
|
+
}
|
|
688
|
+
function isLoopbackHttpStage(stage) {
|
|
689
|
+
if (!/\b(?:curl|wget)\b/.test(stage))
|
|
690
|
+
return false;
|
|
691
|
+
const schemed = [
|
|
692
|
+
...stage.matchAll(/\bhttps?:\/\/(?:[^@/\s'"]*@)?(\[[0-9A-Fa-f:]+\]|[^/:\s'"]+)/g),
|
|
693
|
+
].map((m) => m[1]);
|
|
694
|
+
// Every explicit http(s) target must be loopback; a single external URL means
|
|
695
|
+
// the request reaches the network and stays high_stakes.
|
|
696
|
+
if (schemed.length > 0)
|
|
697
|
+
return schemed.every(isLoopbackAuthority);
|
|
698
|
+
// No scheme'd URL: curl also accepts a scheme-less target (`curl localhost:3999/x`).
|
|
699
|
+
// Accept a loopback host token only when no other http(s) URL is present.
|
|
700
|
+
if (/\bhttps?:\/\//i.test(stage))
|
|
701
|
+
return false;
|
|
702
|
+
return /(?:^|[\s@])(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[::1\])(?::\d+)?(?:[/\s'"]|$)/i.test(stage);
|
|
703
|
+
}
|
|
639
704
|
function classifyBashCommand(command) {
|
|
640
705
|
if (!command)
|
|
641
706
|
return "safe";
|
|
@@ -687,15 +752,15 @@ function classifyBashCommand(command) {
|
|
|
687
752
|
* is folded in by the caller. Returns the stage's tier (default `safe`).
|
|
688
753
|
*/
|
|
689
754
|
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.
|
|
755
|
+
// File deletion in COMMAND POSITION, per-stage so the ephemeral-temp carve-out
|
|
756
|
+
// can win: deleting only throwaway temp files (/tmp, $TMPDIR, /var/folders, …)
|
|
757
|
+
// → warning; any non-temp target (or the temp root itself) → high_stakes.
|
|
758
|
+
// A delete word sitting in an argument (filename, branch name, echo text) is
|
|
759
|
+
// NOT a deletion — that's the false positive DELETE_COMMAND_RE avoids.
|
|
693
760
|
if (isEphemeralOnlyDeletion(stage))
|
|
694
761
|
return "warning";
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
return "high_stakes";
|
|
698
|
-
}
|
|
762
|
+
if (DELETE_COMMAND_RE.test(stage.replace(DELETE_LEAD, "")))
|
|
763
|
+
return "high_stakes";
|
|
699
764
|
// Destructive/irreversible single commands, matched at stage start with any
|
|
700
765
|
// leading `sudo ` stripped (so `sudo mkfs …` is still caught, but the word
|
|
701
766
|
// can't fire inside an argument). These don't span pipes, so `^`-anchoring is
|
|
@@ -708,9 +773,12 @@ function classifyStageShellEffects(stage) {
|
|
|
708
773
|
// curl with a mutating method or request body → high_stakes. Scanned per-stage
|
|
709
774
|
// (not on the full compound command) so the greedy `.*` can't reach across a
|
|
710
775
|
// `&&`/`|` and match a `-d`/`-T`/etc. flag belonging to a different command.
|
|
776
|
+
// A request to a LOOPBACK host (localhost/127.x/::1) is a local action, not an
|
|
777
|
+
// outward one, so it downgrades to `warning` (logged, no prompt).
|
|
711
778
|
for (const pattern of HIGH_STAKES_CURL_STAGE) {
|
|
712
|
-
if (pattern.test(stage))
|
|
713
|
-
return "high_stakes";
|
|
779
|
+
if (pattern.test(stage)) {
|
|
780
|
+
return isLoopbackHttpStage(stage) ? "warning" : "high_stakes";
|
|
781
|
+
}
|
|
714
782
|
}
|
|
715
783
|
// Variable assignments: `NAME=value` (pure) or `NAME=$(cmd) rest` (env prefix
|
|
716
784
|
// / capture). Classify the command-substitution inners and any trailing
|
|
@@ -731,6 +799,14 @@ function classifyStageShellEffects(stage) {
|
|
|
731
799
|
const sshTier = classifySshStage(stage);
|
|
732
800
|
if (sshTier !== null)
|
|
733
801
|
return sshTier;
|
|
802
|
+
// `sh -c '<shell>'` / `bash -c "<shell>"` runs the quoted argument AS shell —
|
|
803
|
+
// recurse so its effect is classified by the same rules (mirrors ssh). This
|
|
804
|
+
// is why the command-position delete scan above can ignore quoted text: a real
|
|
805
|
+
// shell-out like `bash -c 'rm -rf /important'` is caught HERE, by re-running
|
|
806
|
+
// the body as a command, while `echo 'rm …'` (data) is not.
|
|
807
|
+
const shellCTier = classifyShellCStage(stage);
|
|
808
|
+
if (shellCTier !== null)
|
|
809
|
+
return shellCTier;
|
|
734
810
|
// sudo: privilege escalation. A high-stakes inner command was already caught
|
|
735
811
|
// by the full-string scan above (\b patterns match through the `sudo` prefix);
|
|
736
812
|
// anything else still warrants review.
|
package/dist/describe.js
CHANGED
|
@@ -83,6 +83,31 @@ function summarize(toolName, toolInput) {
|
|
|
83
83
|
// ───────────────────────────────────────────────────────────────────────
|
|
84
84
|
// Bash / shell — semantic rerendering
|
|
85
85
|
// ───────────────────────────────────────────────────────────────────────
|
|
86
|
+
/**
|
|
87
|
+
* A curl/wget that uses a mutating method (POST/PUT/PATCH/DELETE) or sends a
|
|
88
|
+
* request body. Returns a "<METHOD> request to <host>" rendering, or null if the
|
|
89
|
+
* command isn't a mutating HTTP request. Headlines the outward request ahead of
|
|
90
|
+
* an incidental file redirect in a compound command.
|
|
91
|
+
*/
|
|
92
|
+
function summarizeMutatingCurl(cmd) {
|
|
93
|
+
if (!/\b(?:curl|wget)\b/.test(cmd))
|
|
94
|
+
return null;
|
|
95
|
+
// `(?:^|\s)` not `\b`: `-X` is preceded by whitespace, and `\b` requires a
|
|
96
|
+
// word char before the `-`, so `\b-X` never matches the usual ` -X DELETE`.
|
|
97
|
+
const methodM = cmd.match(/(?:^|\s)-X\s*(DELETE|POST|PUT|PATCH)\b|(?:^|\s)--request[=\s]+(DELETE|POST|PUT|PATCH)\b/i);
|
|
98
|
+
const hasBody = /\s-d[\s=]|\s--data(?:-raw|-binary|-urlencode|-ascii)?[\s=]|\s-F[\s=]|\s--form[\s=]|\s(?:-T|--upload-file)[\s=]|\s--json[\s=]/.test(cmd);
|
|
99
|
+
if (!methodM && !hasBody)
|
|
100
|
+
return null;
|
|
101
|
+
const method = (methodM?.[1] || methodM?.[2] || "POST").toUpperCase();
|
|
102
|
+
const url = cmd.match(/https?:\/\/[^\s'"]+/)?.[0] || "";
|
|
103
|
+
const host = url ? extractHost(url) : "URL";
|
|
104
|
+
const kind = method === "DELETE" ? "http_delete" : method === "PUT" ? "http_put" : "http_post";
|
|
105
|
+
return {
|
|
106
|
+
title: `${method} request to ${host}`,
|
|
107
|
+
body: cmd.length > COMMAND_INLINE_MAX ? truncateBody(cmd) : undefined,
|
|
108
|
+
kind,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
86
111
|
function summarizeBash(command, sizeBytes) {
|
|
87
112
|
const cmd = (command || "").trim();
|
|
88
113
|
if (!cmd)
|
|
@@ -94,6 +119,14 @@ function summarizeBash(command, sizeBytes) {
|
|
|
94
119
|
const sql = findSqlInCommand(cmd);
|
|
95
120
|
if (sql)
|
|
96
121
|
return summarizeSql(sql, cmd);
|
|
122
|
+
// A mutating curl/wget (POST/PUT/DELETE, or a request body) is described
|
|
123
|
+
// BEFORE shell-write detection. In a compound command the outward request is
|
|
124
|
+
// the headline operation, while a `> log` redirect is incidental — otherwise
|
|
125
|
+
// `node … > /tmp/x.log … && curl -X POST …` mislabels as "Create file" even
|
|
126
|
+
// though the prompt is driven by the POST.
|
|
127
|
+
const mutatingCurl = summarizeMutatingCurl(cmd);
|
|
128
|
+
if (mutatingCurl)
|
|
129
|
+
return mutatingCurl;
|
|
97
130
|
const shellWrite = summarizeShellWrite(cmd);
|
|
98
131
|
if (shellWrite)
|
|
99
132
|
return shellWrite;
|
|
@@ -172,19 +205,6 @@ function summarizeBash(command, sizeBytes) {
|
|
|
172
205
|
};
|
|
173
206
|
}
|
|
174
207
|
}
|
|
175
|
-
// curl with method
|
|
176
|
-
const curlMethod = cmd.match(/curl\s+[^|]*-X\s*(DELETE|POST|PUT|PATCH)/i);
|
|
177
|
-
if (curlMethod) {
|
|
178
|
-
const url = cmd.match(/https?:\/\/[^\s'"]+/)?.[0] || "";
|
|
179
|
-
const host = url ? extractHost(url) : "URL";
|
|
180
|
-
const method = curlMethod[1].toUpperCase();
|
|
181
|
-
const kind = method === "DELETE" ? "http_delete" : method === "POST" ? "http_post" : method === "PUT" ? "http_put" : "http_post";
|
|
182
|
-
return {
|
|
183
|
-
title: `${method} request to ${host}`,
|
|
184
|
-
body: cmd.length > COMMAND_INLINE_MAX ? truncateBody(cmd) : undefined,
|
|
185
|
-
kind,
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
208
|
if (/\bwget\b.*\|\s*(bash|sh|zsh)\b/.test(cmd) || /\bcurl\b.*\|\s*(bash|sh|zsh)\b/.test(cmd)) {
|
|
189
209
|
const url = cmd.match(/https?:\/\/[^\s|'"]+/)?.[0];
|
|
190
210
|
return {
|
|
@@ -859,6 +879,14 @@ function parseRmTargets(argsString) {
|
|
|
859
879
|
i++;
|
|
860
880
|
if (i >= str.length)
|
|
861
881
|
break;
|
|
882
|
+
// A shell command separator (`;`, `|`, `||`, `&&`, `&`), a redirect (`>`,
|
|
883
|
+
// `>>`, `2>`, `&>`), a closing `)`/`}`, a comment, or a newline ends the rm
|
|
884
|
+
// argument list — everything after belongs to a different command. Without
|
|
885
|
+
// this, a compound `if …; then rm -rf "$P"; else echo "…"; fi` would sweep
|
|
886
|
+
// the trailing `;`/`else`/`echo`/`fi` (and the echo's text) in as "files",
|
|
887
|
+
// rendering a bogus "Delete N files recursively".
|
|
888
|
+
if (/^(?:&?\d*[<>]|[;|&`)}#\n])/.test(str.slice(i)))
|
|
889
|
+
break;
|
|
862
890
|
let token;
|
|
863
891
|
if (str[i] === '"') {
|
|
864
892
|
const close = str.indexOf('"', i + 1);
|
|
@@ -884,7 +912,9 @@ function parseRmTargets(argsString) {
|
|
|
884
912
|
}
|
|
885
913
|
else {
|
|
886
914
|
const start = i;
|
|
887
|
-
|
|
915
|
+
// Stop the bare token at whitespace OR a separator/redirect char, so
|
|
916
|
+
// `"$P";` and `$P;` both yield just the path.
|
|
917
|
+
while (i < str.length && !/[\s;|&<>`)}#]/.test(str[i]))
|
|
888
918
|
i++;
|
|
889
919
|
token = str.slice(start, i);
|
|
890
920
|
}
|