@oked/sdk 0.3.2 → 0.3.4
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 +51 -6
- package/dist/describe.js +54 -20
- package/package.json +1 -1
package/dist/classify.js
CHANGED
|
@@ -334,10 +334,12 @@ function maxTier(a, b) {
|
|
|
334
334
|
return TIER_ORDER[a] >= TIER_ORDER[b] ? a : b;
|
|
335
335
|
}
|
|
336
336
|
/** Split a shell command into top-level segments on the operators that
|
|
337
|
-
* sequence separate commands: `|`, `||`, `&&`,
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
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. */
|
|
341
343
|
function splitTopLevel(cmd) {
|
|
342
344
|
const out = [];
|
|
343
345
|
let cur = "";
|
|
@@ -416,6 +418,19 @@ function splitTopLevel(cmd) {
|
|
|
416
418
|
i++;
|
|
417
419
|
continue;
|
|
418
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
|
+
}
|
|
419
434
|
cur += ch;
|
|
420
435
|
i++;
|
|
421
436
|
}
|
|
@@ -659,6 +674,33 @@ function classifyShellCStage(stage) {
|
|
|
659
674
|
return null;
|
|
660
675
|
return classifyBashCommand(m[2]);
|
|
661
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
|
+
}
|
|
662
704
|
function classifyBashCommand(command) {
|
|
663
705
|
if (!command)
|
|
664
706
|
return "safe";
|
|
@@ -731,9 +773,12 @@ function classifyStageShellEffects(stage) {
|
|
|
731
773
|
// curl with a mutating method or request body → high_stakes. Scanned per-stage
|
|
732
774
|
// (not on the full compound command) so the greedy `.*` can't reach across a
|
|
733
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).
|
|
734
778
|
for (const pattern of HIGH_STAKES_CURL_STAGE) {
|
|
735
|
-
if (pattern.test(stage))
|
|
736
|
-
return "high_stakes";
|
|
779
|
+
if (pattern.test(stage)) {
|
|
780
|
+
return isLoopbackHttpStage(stage) ? "warning" : "high_stakes";
|
|
781
|
+
}
|
|
737
782
|
}
|
|
738
783
|
// Variable assignments: `NAME=value` (pure) or `NAME=$(cmd) rest` (env prefix
|
|
739
784
|
// / capture). Classify the command-substitution inners and any trailing
|
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,11 +119,23 @@ 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;
|
|
100
|
-
// rm / trash — file deletion
|
|
101
|
-
|
|
133
|
+
// rm / trash — file deletion. Match the delete's OWN line (`[^\n]+`), not
|
|
134
|
+
// `(.+)$`: without the `m` flag `$` only reaches end-of-string, so a `rm` on
|
|
135
|
+
// an interior line of a multi-line script (e.g. `rm "$WT/node_modules"` before
|
|
136
|
+
// a trailing `cd … && git status`) wouldn't match and the card fell back to
|
|
137
|
+
// the generic "Run command".
|
|
138
|
+
const rmMatch = cmd.match(/\b(?:rm|trash|trash-put|rmdir)\s+(?:(-rf?|-fr|--recursive|-r)\s+)?([^\n]+)/);
|
|
102
139
|
if (rmMatch) {
|
|
103
140
|
const recursive = !!rmMatch[1] || /^rm\s+-/.test(cmd);
|
|
104
141
|
const targets = parseRmTargets(rmMatch[2]);
|
|
@@ -172,19 +209,6 @@ function summarizeBash(command, sizeBytes) {
|
|
|
172
209
|
};
|
|
173
210
|
}
|
|
174
211
|
}
|
|
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
212
|
if (/\bwget\b.*\|\s*(bash|sh|zsh)\b/.test(cmd) || /\bcurl\b.*\|\s*(bash|sh|zsh)\b/.test(cmd)) {
|
|
189
213
|
const url = cmd.match(/https?:\/\/[^\s|'"]+/)?.[0];
|
|
190
214
|
return {
|
|
@@ -202,6 +226,19 @@ function summarizeBash(command, sizeBytes) {
|
|
|
202
226
|
if (h)
|
|
203
227
|
return h;
|
|
204
228
|
}
|
|
229
|
+
// Process kill in COMMAND POSITION — headline it ahead of the compound
|
|
230
|
+
// "Run command" catch-all below, so a cleanup script like
|
|
231
|
+
// `if pgrep …; then pkill -f …; fi` reads "Kill processes", not "Run command".
|
|
232
|
+
// Command-position only (stage start, after ;/&&/||/newline/$( / a keyword),
|
|
233
|
+
// so "kill" inside echo text (e.g. "killing it") doesn't mislabel.
|
|
234
|
+
const killM = cmd.match(/(?:^|[\n;&|`({]|\$\(|\b(?:sudo|then|do|else|command|xargs)\s+)\s*(killall|pkill|kill)\b/);
|
|
235
|
+
if (killM) {
|
|
236
|
+
return {
|
|
237
|
+
title: killM[1].toLowerCase() === "kill" ? "Kill process" : "Kill processes",
|
|
238
|
+
body: truncateBody(cmd),
|
|
239
|
+
kind: "kill_process",
|
|
240
|
+
};
|
|
241
|
+
}
|
|
205
242
|
// Multi-step pipeline
|
|
206
243
|
if (/&&|\|\||;/.test(cmd))
|
|
207
244
|
return { title: "Run command", body: truncateBody(cmd), kind: "shell_pipeline" };
|
|
@@ -232,11 +269,8 @@ function summarizeBash(command, sizeBytes) {
|
|
|
232
269
|
return { title: "Unpublish package from npm", kind: "npm_unpublish" };
|
|
233
270
|
if (/\bnpx\s+.*\s+deploy\b/.test(cmd))
|
|
234
271
|
return { title: "Deploy via npx", body: truncateBody(cmd), kind: "npx_deploy" };
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
return { title: "Kill processes", body: truncateBody(cmd), kind: "kill_process" };
|
|
238
|
-
if (/\bkill\b/.test(cmd))
|
|
239
|
-
return { title: "Kill process", body: truncateBody(cmd), kind: "kill_process" };
|
|
272
|
+
// sudo / chmod (kill/pkill/killall are handled in command position above,
|
|
273
|
+
// ahead of the compound catch-all).
|
|
240
274
|
if (/\bsudo\b/.test(cmd)) {
|
|
241
275
|
const inner = cmd.replace(/^sudo\s+/, "");
|
|
242
276
|
return { title: `Run as root: ${truncate(inner, COMMAND_INLINE_MAX)}`, body: truncateBody(cmd), kind: "sudo" };
|