@oked/sdk 0.3.2 → 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 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: `|`, `||`, `&&`, `;`. Operators inside quoted
338
- * strings — including the `"$(cat <<'EOF' … )"` heredoc form used for commit
339
- * messages — are kept intact so message text isn't split. Returns trimmed,
340
- * non-empty segments. */
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,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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "OKed SDK - human approval layer for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",