@oked/sdk 0.3.4 → 0.3.6

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.
Files changed (2) hide show
  1. package/dist/classify.js +63 -12
  2. package/package.json +1 -1
package/dist/classify.js CHANGED
@@ -225,6 +225,34 @@ function isEphemeralPath(filePath) {
225
225
  return false;
226
226
  return TEMP_VAR_RE.test(filePath) || EPHEMERAL_PATH_RE.test(filePath);
227
227
  }
228
+ // A shell variable assigned from `mktemp` / `mktemp -d` (optionally `export`ed)
229
+ // at the start of a stage: `EMPTYHOME=$(mktemp -d)`, `T=`mktemp``. Its value is a
230
+ // throwaway temp path the shell just created, so a same-command `rm -rf "$T"`
231
+ // that deletes it is the ephemeral-temp case, not a real-data wipe. These names
232
+ // are gathered across every stage of a compound command BEFORE any stage is
233
+ // classified (see classifyBashCommand), so a delete in a later stage can see a
234
+ // binding declared in an earlier one. Safe because the whole binding is visible
235
+ // in this single inspected command string — no environment-resolution guess.
236
+ const MKTEMP_ASSIGN_RE = /^(?:export\s+|declare\s+|local\s+|readonly\s+)?(\w+)=["']?(?:\$\(|`)\s*mktemp\b/;
237
+ function collectMktempVars(stage, into) {
238
+ const m = stage.match(MKTEMP_ASSIGN_RE);
239
+ if (m)
240
+ into.add(m[1]);
241
+ }
242
+ // True when `target` is a reference (`$VAR`, `${VAR}`, optionally with a trailing
243
+ // subpath like `$VAR/cache`) to a variable known to hold an mktemp temp dir. A
244
+ // subpath that climbs out with `..` could escape the temp tree, so it does NOT
245
+ // qualify — that stays a real deletion.
246
+ function isEphemeralVar(target, vars) {
247
+ if (vars.size === 0)
248
+ return false;
249
+ const m = target.match(/^\$\{?(\w+)\}?(\/.*)?$/);
250
+ if (!m || !vars.has(m[1]))
251
+ return false;
252
+ if (m[2] && m[2].split("/").includes(".."))
253
+ return false;
254
+ return true;
255
+ }
228
256
  // Paths where a write/edit is genuinely dangerous and must stay `review`:
229
257
  // system directories, credential/secret stores, shell startup files (a
230
258
  // persistence vector), and OKed's own config (so an agent can't disable its
@@ -439,18 +467,22 @@ function splitTopLevel(cmd) {
439
467
  return out.filter(Boolean);
440
468
  }
441
469
  // rm/rmdir/trash whose every target is an ephemeral temp path (/tmp, %TEMP%,
442
- // …). Deleting throwaway temp files is low-risk, so it downgrades to warning.
443
- // Any non-temp target (or deleting a temp ROOT like `/tmp` itself, which isn't
444
- // an ephemeral *path*) means this returns false and the deletion stays
445
- // high_stakes.
446
- function isEphemeralOnlyDeletion(command) {
470
+ // …) or a variable bound to one by `mktemp` earlier in the same command
471
+ // (`ephemeralVars`, e.g. `EMPTYHOME=$(mktemp -d); rm -rf "$EMPTYHOME"`).
472
+ // Deleting throwaway temp files is low-risk, so it downgrades to warning. Any
473
+ // non-temp target (or deleting a temp ROOT like `/tmp` itself, which isn't an
474
+ // ephemeral *path*) means this returns false and the deletion stays high_stakes.
475
+ function isEphemeralOnlyDeletion(command, ephemeralVars = new Set()) {
447
476
  const m = command.match(/^(?:sudo\s+)?(?:rm|rmdir|trash|trash-put)\b\s+(.+)$/s);
448
477
  if (!m)
449
478
  return false;
450
479
  const targets = splitArgs(m[1]).filter((a) => !a.startsWith("-"));
451
480
  if (targets.length === 0)
452
481
  return false;
453
- return targets.every((a) => isEphemeralPath(unquote(a)));
482
+ return targets.every((a) => {
483
+ const t = unquote(a);
484
+ return isEphemeralPath(t) || isEphemeralVar(t, ephemeralVars);
485
+ });
454
486
  }
455
487
  /**
456
488
  * Classifies a single stage that begins with one or more `NAME=value`
@@ -701,7 +733,7 @@ function isLoopbackHttpStage(stage) {
701
733
  return false;
702
734
  return /(?:^|[\s@])(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[::1\])(?::\d+)?(?:[/\s'"]|$)/i.test(stage);
703
735
  }
704
- function classifyBashCommand(command) {
736
+ function classifyBashCommand(command, ephemeralVars = new Set()) {
705
737
  if (!command)
706
738
  return "safe";
707
739
  // Two views of the command:
@@ -735,7 +767,14 @@ function classifyBashCommand(command) {
735
767
  // body aren't split points either way — so stage boundaries are identical.)
736
768
  const stages = splitTopLevel(intact);
737
769
  if (stages.length > 1) {
738
- return stages.reduce((worst, stage) => maxTier(worst, classifyBashCommand(stage)), "safe");
770
+ // Gather temp-dir variables (`VAR=$(mktemp -d)`) declared anywhere in this
771
+ // compound command BEFORE classifying any stage, so a later `rm -rf "$VAR"`
772
+ // stage recognizes its target as throwaway even though the binding lives in
773
+ // an earlier stage. Inherits any vars passed from an enclosing command.
774
+ const vars = new Set(ephemeralVars);
775
+ for (const stage of stages)
776
+ collectMktempVars(stage, vars);
777
+ return stages.reduce((worst, stage) => maxTier(worst, classifyBashCommand(stage, vars)), "safe");
739
778
  }
740
779
  // ---- single stage from here ----
741
780
  // SQL severity from the body-INTACT view: an interpreter wrapper (python -c,
@@ -744,20 +783,20 @@ function classifyBashCommand(command) {
744
783
  // shell pass short-circuits to `safe` (e.g. an assignment-prefixed node -e).
745
784
  const sql = findSqlInCommand(intact);
746
785
  const sqlTier = sql ? classifySqlSeverity(sql) : "safe";
747
- return maxTier(sqlTier, classifyStageShellEffects(stripped));
786
+ return maxTier(sqlTier, classifyStageShellEffects(stripped, ephemeralVars));
748
787
  }
749
788
  /**
750
789
  * Shell-effect classification of a SINGLE, already-body-stripped stage. No SQL
751
790
  * detection here — that runs on the body-intact view in classifyBashCommand and
752
791
  * is folded in by the caller. Returns the stage's tier (default `safe`).
753
792
  */
754
- function classifyStageShellEffects(stage) {
793
+ function classifyStageShellEffects(stage, ephemeralVars = new Set()) {
755
794
  // File deletion in COMMAND POSITION, per-stage so the ephemeral-temp carve-out
756
795
  // can win: deleting only throwaway temp files (/tmp, $TMPDIR, /var/folders, …)
757
796
  // → warning; any non-temp target (or the temp root itself) → high_stakes.
758
797
  // A delete word sitting in an argument (filename, branch name, echo text) is
759
798
  // NOT a deletion — that's the false positive DELETE_COMMAND_RE avoids.
760
- if (isEphemeralOnlyDeletion(stage))
799
+ if (isEphemeralOnlyDeletion(stage, ephemeralVars))
761
800
  return "warning";
762
801
  if (DELETE_COMMAND_RE.test(stage.replace(DELETE_LEAD, "")))
763
802
  return "high_stakes";
@@ -1123,7 +1162,19 @@ function splitArgs(s) {
1123
1162
  function classifyMcpTool(toolName) {
1124
1163
  const parts = toolName.split("__");
1125
1164
  const tool = parts[parts.length - 1] || "";
1126
- if (tool.startsWith("list_") || tool.startsWith("get_") || tool.startsWith("search_")) {
1165
+ // Read-only retrieval verbs.
1166
+ if (tool.startsWith("list_") || tool.startsWith("get_") || tool.startsWith("search_") ||
1167
+ tool.startsWith("read_") || tool.startsWith("fetch_") || tool.startsWith("query_") ||
1168
+ tool.startsWith("find_") || tool.startsWith("view_") || tool.startsWith("show_") ||
1169
+ tool.startsWith("describe_") || tool.startsWith("inspect_") || tool.startsWith("check_") ||
1170
+ tool.startsWith("count_")) {
1171
+ return "safe";
1172
+ }
1173
+ // Annotation / transcript-marker verbs — they label or organize the session
1174
+ // (mark a chapter, flag a follow-up, dismiss a chip) without touching the
1175
+ // filesystem, network, or any external service, so they auto-allow silently.
1176
+ if (tool.startsWith("mark_") || tool.startsWith("annotate_") || tool.startsWith("tag_") ||
1177
+ tool.startsWith("label_") || tool.startsWith("dismiss_")) {
1127
1178
  return "safe";
1128
1179
  }
1129
1180
  if (tool.endsWith("_status") || tool.endsWith("_info") || tool.endsWith("_count") ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "OKed SDK - human approval layer for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",