@oked/sdk 0.2.0 → 0.3.0

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 +97 -9
  2. package/package.json +1 -1
package/dist/classify.js CHANGED
@@ -19,7 +19,7 @@ import { TIER_ORDER } from "./degraded.js";
19
19
  // dirs, OKed's own config), sudo, outward message/email sends.
20
20
  // Tier 4 - high_stakes: push + number matching. Destructive / irreversible /
21
21
  // external: rm of non-temp data, DROP/TRUNCATE/DELETE FROM,
22
- // git push / reset --hard / branch -D, mkfs / dd-to-device /
22
+ // git push --force / reset --hard / branch -D, mkfs / dd-to-device /
23
23
  // shutdown, curl POST/DELETE, ssh/scp, npm publish, kill.
24
24
  // Tools that are always safe (read-only, no side effects)
25
25
  const SAFE_TOOLS = new Set([
@@ -75,7 +75,9 @@ const DELETE_PATTERNS = [
75
75
  // Scanned on the FULL command (some patterns, e.g. download|shell, span a pipe).
76
76
  // File deletion lives in DELETE_PATTERNS (per-stage) instead, see above.
77
77
  const HIGH_STAKES_COMMANDS = [
78
- /\bgit\s+push\b/,
78
+ // Force push rewrites remote history irreversibly. A plain push is downgraded
79
+ // to `warning` in WARNING_COMMANDS below. Mirrors describe.ts force detection.
80
+ /\bgit\s+push\b[^\n]*\s(?:--force(?:-with-lease)?|-f)\b/,
79
81
  /\bgit\s+reset\s+--hard\b/,
80
82
  /\bgit\s+clean\s+-f/,
81
83
  /\bgit\s+checkout\s+--\s+\./,
@@ -101,12 +103,11 @@ const HIGH_STAKES_COMMANDS = [
101
103
  /\bnpm\s+publish\b/,
102
104
  /\bnpm\s+unpublish\b/,
103
105
  /\bnpx\s+.*\s+deploy\b/,
104
- // ssh to a remote host. Effects on the remote side cannot be undone from
105
- // here, so treat every interactive/remote-exec ssh as high_stakes. Matches
106
- // `ssh user@host ...` and `ssh -i key.pem ubuntu@1.2.3.4 ...`. The
107
- // \bssh\s+ prefix (whitespace required) excludes `ssh-keygen`/`ssh-add`/
108
- // `ssh-keyscan` which are local and reversible.
109
- /\bssh\s+(?:\S+\s+)*\S+@\S+/,
106
+ // NOTE: ssh is intentionally NOT a blanket high_stakes here. ssh is a
107
+ // transport — the real effect is whatever runs on the remote host — so it's
108
+ // classified by its remote command in classifySshStage (a read-only remote
109
+ // diagnostic shouldn't prompt; a destructive one still does). scp/rsync to a
110
+ // remote ARE file transfers (outward writes) and stay in HIGH_STAKES_STAGE.
110
111
  // himalaya destructive ops. message delete + folder delete/expunge/purge
111
112
  // wipe mail from the server irreversibly; account delete wipes local config.
112
113
  /\bhimalaya\s+message\s+delete\b/,
@@ -170,12 +171,16 @@ const REVIEW_COMMANDS = [
170
171
  const WARNING_COMMANDS = [
171
172
  // Local, reversible git writes — stage/commit/switch/stash. They mutate the
172
173
  // local repo but can be undone (amend, reset, checkout). Destructive git
173
- // (push, reset --hard, branch -D, stash drop/clear) is caught above and wins.
174
+ // (force push, reset --hard, branch -D, stash drop/clear) is caught above and wins.
174
175
  /^git\s+add\b/,
175
176
  /^git\s+commit\b/,
176
177
  /^git\s+checkout\s+-b\b/,
177
178
  /^git\s+switch\b/,
178
179
  /^git\s+stash\b(?!\s+(?:drop|clear))/,
180
+ // Plain push (no force). Outward, but the remote keeps full history and the
181
+ // push is recoverable, so by policy it logs rather than prompts. Force push is
182
+ // high_stakes in the full scan above and wins before reaching here.
183
+ /^git\s+push\b/,
179
184
  /^gh\s+pr\s+create\b/,
180
185
  // Package installs always mutate node_modules and run dependency postinstall
181
186
  // scripts — a known local mutation worth an audit line.
@@ -540,6 +545,83 @@ function classifyEnvStage(stage) {
540
545
  }
541
546
  return "safe"; // `env` / `env -u X` with no command just prints the environment
542
547
  }
548
+ // ssh options that consume a following argument token (the rest are flags).
549
+ const SSH_ARG_OPTS = new Set([
550
+ "-b", "-c", "-D", "-E", "-e", "-F", "-I", "-i", "-J", "-L", "-l", "-m",
551
+ "-O", "-o", "-p", "-Q", "-R", "-S", "-W", "-w",
552
+ ]);
553
+ /**
554
+ * `ssh [opts] [user@]host [remote command]` — ssh is a transport, so the tier
555
+ * comes from the REMOTE command, classified by the same effect rules as a local
556
+ * one (`ssh host ls` → safe; `ssh host 'rm -rf /data'` → high_stakes). An ssh
557
+ * with no command opens an interactive shell, and port/dynamic forwarding
558
+ * (`-L`/`-R`/`-D`/`-W`) grants opaque access — neither is inspectable, so both
559
+ * get a `review` floor (one prompt for remote access). Returns null if the stage
560
+ * isn't an ssh invocation. Excludes `ssh-keygen`/`ssh-add`/`ssh-keyscan` (the
561
+ * `^ssh\s` anchor requires whitespace, not a hyphen).
562
+ */
563
+ function classifySshStage(stage) {
564
+ const lead = /^ssh\s+/.exec(stage);
565
+ if (!lead)
566
+ return null;
567
+ const readToken = (from) => {
568
+ let i = from;
569
+ let quote = null;
570
+ while (i < stage.length) {
571
+ const ch = stage[i];
572
+ if (quote) {
573
+ if (ch === quote)
574
+ quote = null;
575
+ i++;
576
+ continue;
577
+ }
578
+ if (ch === '"' || ch === "'") {
579
+ quote = ch;
580
+ i++;
581
+ continue;
582
+ }
583
+ if (/\s/.test(ch))
584
+ break;
585
+ i++;
586
+ }
587
+ return i;
588
+ };
589
+ let i = lead[0].length;
590
+ let sawForward = false;
591
+ while (i < stage.length) {
592
+ while (i < stage.length && /\s/.test(stage[i]))
593
+ i++;
594
+ if (i >= stage.length)
595
+ break;
596
+ const start = i;
597
+ i = readToken(i);
598
+ const tok = stage.slice(start, i);
599
+ if (tok.startsWith("-")) {
600
+ if (/^-[LRDW]/.test(tok))
601
+ sawForward = true; // forwarding/tunnel
602
+ if (SSH_ARG_OPTS.has(tok)) { // separated option arg
603
+ while (i < stage.length && /\s/.test(stage[i]))
604
+ i++;
605
+ i = readToken(i);
606
+ }
607
+ continue;
608
+ }
609
+ // First non-option token is the [user@]host. Everything after it is the
610
+ // remote command (usually a single quoted string).
611
+ if (sawForward)
612
+ return "review";
613
+ let rest = stage.slice(i).trim();
614
+ if (!rest)
615
+ return "review"; // interactive shell
616
+ // Strip one wrapping quote layer so the remote command's own operators
617
+ // (`;`, `|`) are classified as command separators, not quoted data.
618
+ if (rest.length >= 2 && (rest[0] === '"' || rest[0] === "'") && rest[rest.length - 1] === rest[0]) {
619
+ rest = rest.slice(1, -1);
620
+ }
621
+ return classifyBashCommand(rest);
622
+ }
623
+ return "review"; // only options / a bare host token → treat as remote access
624
+ }
543
625
  function classifyBashCommand(command) {
544
626
  if (!command)
545
627
  return "safe";
@@ -597,6 +679,12 @@ function classifyBashCommand(command) {
597
679
  const envTier = classifyEnvStage(trimmed);
598
680
  if (envTier !== null)
599
681
  return envTier;
682
+ // ssh is a transport — classify the remote command it runs (interactive /
683
+ // port-forwarding ssh has no inspectable command → review floor). scp/rsync
684
+ // to a remote are file transfers and stay high_stakes (HIGH_STAKES_STAGE).
685
+ const sshTier = classifySshStage(trimmed);
686
+ if (sshTier !== null)
687
+ return sshTier;
600
688
  // sudo: privilege escalation. A high-stakes inner command was already caught
601
689
  // by the full-string scan above (\b patterns match through the `sudo` prefix);
602
690
  // anything else still warrants review.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oked/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "OKed SDK - human approval layer for AI agents",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",