@oked/sdk 0.1.8 → 0.2.1
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 +278 -132
- package/package.json +1 -1
package/dist/classify.js
CHANGED
|
@@ -2,10 +2,25 @@ import path from "path";
|
|
|
2
2
|
import os from "os";
|
|
3
3
|
import { findSqlInCommand } from "./describe.js";
|
|
4
4
|
import { TIER_ORDER } from "./degraded.js";
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
5
|
+
// Classification is EFFECT-BASED, not command-name-based. We don't keep an
|
|
6
|
+
// allowlist of "safe commands"; instead we detect the few categories of effect
|
|
7
|
+
// that warrant a human in the loop, and DEFAULT TO NO PROMPT for everything
|
|
8
|
+
// else. Running a command, reading, copying a file, a DB SELECT, or deleting a
|
|
9
|
+
// /tmp scratch file must never prompt — only destructive, irreversible, or
|
|
10
|
+
// outward-facing operations do.
|
|
11
|
+
//
|
|
12
|
+
// Tier 1 - safe: auto-allow, silent. No detected side effect (reads, queries,
|
|
13
|
+
// and any command we don't recognize as mutating — the default).
|
|
14
|
+
// Tier 2 - warning: terminal log only, no push. A reversible LOCAL mutation
|
|
15
|
+
// (file create/copy/move, /tmp delete, local git, install,
|
|
16
|
+
// code-exec, SQL INSERT/CREATE).
|
|
17
|
+
// Tier 3 - review: push notification, tap to approve. Prompt-worthy but not
|
|
18
|
+
// catastrophic: sensitive-path writes (~/.ssh, shell rc, system
|
|
19
|
+
// dirs, OKed's own config), sudo, outward message/email sends.
|
|
20
|
+
// Tier 4 - high_stakes: push + number matching. Destructive / irreversible /
|
|
21
|
+
// external: rm of non-temp data, DROP/TRUNCATE/DELETE FROM,
|
|
22
|
+
// git push / reset --hard / branch -D, mkfs / dd-to-device /
|
|
23
|
+
// shutdown, curl POST/DELETE, ssh/scp, npm publish, kill.
|
|
9
24
|
// Tools that are always safe (read-only, no side effects)
|
|
10
25
|
const SAFE_TOOLS = new Set([
|
|
11
26
|
"Read",
|
|
@@ -40,80 +55,11 @@ const SAFE_TOOL_ALIASES = new Set([
|
|
|
40
55
|
const REVIEW_TOOLS = new Set([]);
|
|
41
56
|
// Tools that are always high stakes
|
|
42
57
|
const HIGH_STAKES_TOOLS = new Set([]);
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
/^cat\b/,
|
|
49
|
-
/^head\b/,
|
|
50
|
-
/^tail\b/,
|
|
51
|
-
/^wc\b/,
|
|
52
|
-
/^date$/,
|
|
53
|
-
/^whoami$/,
|
|
54
|
-
/^which\b/,
|
|
55
|
-
/^type\b/,
|
|
56
|
-
/^file\b/,
|
|
57
|
-
/^stat\b/,
|
|
58
|
-
/^du\b/,
|
|
59
|
-
/^df\b/,
|
|
60
|
-
/^uname\b/,
|
|
61
|
-
/^env$/,
|
|
62
|
-
/^printenv\b/,
|
|
63
|
-
/^node\s+(-v|--version)/,
|
|
64
|
-
/^npm\s+(list|ls|--version|-v|view|info|outdated|audit)\b/,
|
|
65
|
-
/^npx\s+-v/,
|
|
66
|
-
/^git\s+(status|log|diff|branch|remote|show|tag|stash\s+list|ls-files|ls-tree|ls-remote|check-ignore|check-attr|rev-parse|rev-list|describe|cat-file|blame|shortlog|reflog|name-rev|whatchanged|for-each-ref|symbolic-ref|merge-base|var|grep|count-objects|show-ref|cherry|verify-commit|fsck)\b/,
|
|
67
|
-
/^git\s+config\s+(--get|--get-all|--get-regexp|--list|-l)\b/,
|
|
68
|
-
/^docker\s+(ps|images|inspect|logs)\b/,
|
|
69
|
-
/^docker\s+compose\s+(ps|logs)\b/,
|
|
70
|
-
/^tree\b/,
|
|
71
|
-
/^find\b/,
|
|
72
|
-
/^grep\b/,
|
|
73
|
-
/^rg\b/,
|
|
74
|
-
/^fd\b/,
|
|
75
|
-
/^jq\b/,
|
|
76
|
-
/^curl\s+-s.*\|\s*(jq|python|node)/, // curl piped to parser (usually read-only)
|
|
77
|
-
/^curl\b/, // curl without -X defaults to GET (high-stakes patterns checked first)
|
|
78
|
-
// himalaya (email CLI) read-only ops. Listing/reading mail or folder state
|
|
79
|
-
// never mutates anything remotely. Only `message send|reply|forward` and
|
|
80
|
-
// `message delete` / `folder delete|expunge|purge` matter; those land in
|
|
81
|
-
// the review/high-stakes paths.
|
|
82
|
-
/^himalaya\s+(account|folder|envelope|message\s+(?:read|export|search|copy|move)|attachment\s+(?:download|list)|template|search)\b/,
|
|
83
|
-
// cd just changes directory — no side effect of its own. Any dangerous
|
|
84
|
-
// command chained after it (`cd x && rm -rf`) is caught per-stage.
|
|
85
|
-
/^cd\b/,
|
|
86
|
-
// sed without -i / --in-place only prints to stdout (read-only). In-place
|
|
87
|
-
// edits are detected as a write op (below) before this is reached.
|
|
88
|
-
/^sed\b/,
|
|
89
|
-
// gh read-only subcommands — listing/viewing PRs, issues, runs, etc.
|
|
90
|
-
/^gh\s+(pr|issue|repo|run|workflow|release|api)\s+(list|view|status|diff|checks)\b/,
|
|
91
|
-
// Test runners — running the project's own tests is part of the dev loop.
|
|
92
|
-
// (Arbitrary node/npx/python execution is `warning`, see WARNING_COMMANDS.)
|
|
93
|
-
/^npm\s+(test|t)\b/,
|
|
94
|
-
/^npx\s+(tsx|ts-node|jest|vitest|mocha|ava|cypress|playwright|tsc)\b/,
|
|
95
|
-
/^(jest|vitest|mocha|pytest|ava)\b/,
|
|
96
|
-
/^python3?\s+-m\s+pytest\b/,
|
|
97
|
-
// Shell control-flow keywords. A compound like `for f in *; do cmd; done`
|
|
98
|
-
// is split on `;` into stages; these keyword stages carry no risk of their
|
|
99
|
-
// own, and any real command in the body is classified per-stage. Dangerous
|
|
100
|
-
// commands hidden in a $(...) on a keyword line are still caught by the
|
|
101
|
-
// high-stakes scan, which runs on the full command first.
|
|
102
|
-
/^(for|while|until|do|done|then|else|elif|fi|case|esac|if|select)\b/,
|
|
103
|
-
/^(done|fi|esac|\}|\{|:|true|false)\s*$/,
|
|
104
|
-
// Inert shell builtins / flow helpers with no side effects.
|
|
105
|
-
/^sleep\b/,
|
|
106
|
-
/^exit\b/,
|
|
107
|
-
/^return\b/,
|
|
108
|
-
/^seq\b/,
|
|
109
|
-
/^test\b/,
|
|
110
|
-
/^\[\[?\s/, // [ ... ] and [[ ... ]] conditionals
|
|
111
|
-
// `<cmd> --help` / `--version` just prints usage. (Dangerous verbs like
|
|
112
|
-
// `git push` are matched by HIGH_STAKES on the full command first.)
|
|
113
|
-
/\s--(help|version)\b/,
|
|
114
|
-
// Read-only npm subcommands beyond the install/version ones above.
|
|
115
|
-
/^npm\s+(prefix|root|bin|whoami|ping|docs|repo|why|explain|pkg\s+get|config\s+get)\b/,
|
|
116
|
-
];
|
|
58
|
+
// NOTE: there is intentionally NO "safe commands" allowlist. Under the
|
|
59
|
+
// effect-based model, anything we don't detect as a mutation or a prompt-worthy
|
|
60
|
+
// effect IS safe by default (reads, queries, `ls`, `grep`, `jq`, `awk`, `sort`,
|
|
61
|
+
// unknown commands, …). We only enumerate the things that DO warrant a log
|
|
62
|
+
// (WARNING_*) or a prompt (REVIEW_*, HIGH_STAKES_*).
|
|
117
63
|
// File deletion (rm/rmdir/trash). Checked PER-STAGE (not on the full command)
|
|
118
64
|
// so the ephemeral-temp downgrade can run first: `something; rm /tmp/x` must be
|
|
119
65
|
// warning, but `rm /tmp/x` mixed with a non-temp delete in another stage stays
|
|
@@ -141,10 +87,6 @@ const HIGH_STAKES_COMMANDS = [
|
|
|
141
87
|
// -c/-e, interpreter -e/-c bodies, heredocs), then classifySqlSeverity.
|
|
142
88
|
/\bdocker\s+(rm|rmi|system\s+prune)\b/,
|
|
143
89
|
/\bdocker\s+compose\s+down\b/,
|
|
144
|
-
/\bkill\b/,
|
|
145
|
-
/\bpkill\b/,
|
|
146
|
-
/\bkillall\b/,
|
|
147
|
-
/\bchmod\s+777\b/,
|
|
148
90
|
/\bcurl\s+.*-X\s*(DELETE|PUT|POST|PATCH)\b/i,
|
|
149
91
|
/\bcurl\s+.*--request\s*(DELETE|PUT|POST|PATCH)\b/i,
|
|
150
92
|
// curl flags that send a request body (POST/PUT/PATCH) without an explicit -X
|
|
@@ -159,47 +101,83 @@ const HIGH_STAKES_COMMANDS = [
|
|
|
159
101
|
/\bnpm\s+publish\b/,
|
|
160
102
|
/\bnpm\s+unpublish\b/,
|
|
161
103
|
/\bnpx\s+.*\s+deploy\b/,
|
|
162
|
-
// ssh
|
|
163
|
-
//
|
|
164
|
-
//
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
/\bssh\s+(?:\S+\s+)*\S+@\S+/,
|
|
104
|
+
// NOTE: ssh is intentionally NOT a blanket high_stakes here. ssh is a
|
|
105
|
+
// transport — the real effect is whatever runs on the remote host — so it's
|
|
106
|
+
// classified by its remote command in classifySshStage (a read-only remote
|
|
107
|
+
// diagnostic shouldn't prompt; a destructive one still does). scp/rsync to a
|
|
108
|
+
// remote ARE file transfers (outward writes) and stay in HIGH_STAKES_STAGE.
|
|
168
109
|
// himalaya destructive ops. message delete + folder delete/expunge/purge
|
|
169
110
|
// wipe mail from the server irreversibly; account delete wipes local config.
|
|
170
111
|
/\bhimalaya\s+message\s+delete\b/,
|
|
171
112
|
/\bhimalaya\s+folder\s+(delete|expunge|purge)\b/,
|
|
172
113
|
/\bhimalaya\s+account\s+delete\b/,
|
|
114
|
+
// Overwriting a raw block device (`> /dev/sda`, `… > /dev/nvme0n1`) destroys a
|
|
115
|
+
// disk irreversibly. (dd to a device is in HIGH_STAKES_STAGE.)
|
|
116
|
+
/>\s*\/dev\/(sd|nvme|hd|disk|mmcblk|vd)\w*/,
|
|
117
|
+
];
|
|
118
|
+
// Destructive / irreversible commands matched PER-STAGE at the start of a stage
|
|
119
|
+
// (after stripping a leading `sudo `), so the destructive WORD can't be hit
|
|
120
|
+
// inside an argument or a commit message (`git commit -m "kill the flaky test"`
|
|
121
|
+
// must not be high_stakes). These don't span pipes, so per-stage `^`-anchoring
|
|
122
|
+
// is both safe and precise.
|
|
123
|
+
const HIGH_STAKES_STAGE = [
|
|
124
|
+
// Filesystem / disk destruction.
|
|
125
|
+
/^mkfs(\.\w+)?\b/,
|
|
126
|
+
/^mkswap\b/,
|
|
127
|
+
/^(fdisk|parted|gparted)\b/,
|
|
128
|
+
/^dd\b.*\bof=\/dev\//,
|
|
129
|
+
/^shred\b/,
|
|
130
|
+
/^truncate\b/, // `truncate -s 0 file` wipes contents
|
|
131
|
+
/^find\b.*\s-delete\b/, // `find … -delete` removes matches
|
|
132
|
+
// Power / system state.
|
|
133
|
+
/^(shutdown|reboot|halt|poweroff)\b/,
|
|
134
|
+
// Remote/outward file transfer (almost always a remote target).
|
|
135
|
+
/^scp\b/,
|
|
136
|
+
/^rsync\b.*(--delete|\s\S+:)/, // rsync --delete, or to a host:path target
|
|
137
|
+
// Local git history/work destruction (force branch delete, tag delete,
|
|
138
|
+
// dropping/clearing stashed work). Reversible local git (add/commit/stash) is
|
|
139
|
+
// a WARNING marker below.
|
|
140
|
+
/^git\s+branch\s+(?:-D|--delete)\b/,
|
|
141
|
+
/^git\s+tag\s+(?:-d|--delete)\b/,
|
|
142
|
+
/^git\s+stash\s+(?:drop|clear)\b/,
|
|
143
|
+
// Process termination.
|
|
144
|
+
/^kill\b/,
|
|
145
|
+
/^pkill\b/,
|
|
146
|
+
/^killall\b/,
|
|
147
|
+
// Wide-open permissions.
|
|
148
|
+
/^chmod\s+(?:-\S+\s+)*777\b/,
|
|
149
|
+
];
|
|
150
|
+
// Outward-but-recoverable commands → review (prompt, simple approve). Sending
|
|
151
|
+
// mail/messages can't be unsent, but it isn't destructive to local state, so it
|
|
152
|
+
// sits below high_stakes. Matched per-stage. (Destructive himalaya verbs —
|
|
153
|
+
// message delete, folder purge — are high_stakes in the full scan above.)
|
|
154
|
+
const REVIEW_COMMANDS = [
|
|
155
|
+
/^himalaya\s+message\s+(send|reply|forward)\b/,
|
|
173
156
|
];
|
|
174
|
-
//
|
|
175
|
-
//
|
|
157
|
+
// Local-mutation MARKERS for DETECTED side effects. These never gate a prompt —
|
|
158
|
+
// under the effect-based model anything not flagged destructive/outward/sensitive
|
|
159
|
+
// already auto-allows. They exist only to pick the `warning` (logged) vs `safe`
|
|
160
|
+
// (silent) label, and cover the local mutations we can recognize by shape but
|
|
161
|
+
// that aren't a plain file write (those are handled by extractShellWriteOps).
|
|
162
|
+
//
|
|
163
|
+
// Opaque code execution (node/npx/python/claude/test runners) is intentionally
|
|
164
|
+
// NOT here: we can't tell whether it mutated anything, and a destructive effect
|
|
165
|
+
// buried inside `node -e …` would slip through regardless of the label — so
|
|
166
|
+
// logging it adds terminal noise without buying safety. It stays `safe`, just
|
|
167
|
+
// like any other unrecognized command. A real file write it performs is a
|
|
168
|
+
// separate Bash/Write call that gets classified on its own.
|
|
176
169
|
const WARNING_COMMANDS = [
|
|
177
|
-
// Local, reversible git
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
// by HIGH_STAKES_COMMANDS above and wins first. `git stash drop|clear` is
|
|
181
|
-
// excluded — those discard stashed work — so it stays `review`.
|
|
170
|
+
// Local, reversible git writes — stage/commit/switch/stash. They mutate the
|
|
171
|
+
// local repo but can be undone (amend, reset, checkout). Destructive git
|
|
172
|
+
// (push, reset --hard, branch -D, stash drop/clear) is caught above and wins.
|
|
182
173
|
/^git\s+add\b/,
|
|
183
174
|
/^git\s+commit\b/,
|
|
184
175
|
/^git\s+checkout\s+-b\b/,
|
|
185
176
|
/^git\s+switch\b/,
|
|
186
177
|
/^git\s+stash\b(?!\s+(?:drop|clear))/,
|
|
187
|
-
// PR creation is reversible (a PR can be closed); the underlying branch push
|
|
188
|
-
// is separately high_stakes.
|
|
189
178
|
/^gh\s+pr\s+create\b/,
|
|
190
|
-
//
|
|
191
|
-
//
|
|
192
|
-
// we don't prompt but keep a local trail. Known test runners and read-only
|
|
193
|
-
// version flags are handled as `safe` (SAFE_COMMANDS) before reaching here.
|
|
194
|
-
/^node\b/,
|
|
195
|
-
/^npx\b/,
|
|
196
|
-
/^python3?\b/,
|
|
197
|
-
/^npm\s+run\b/,
|
|
198
|
-
/^bun\b/,
|
|
199
|
-
/^deno\b/,
|
|
200
|
-
// Package installs run dependency postinstall scripts (code execution), so
|
|
201
|
-
// they're not "safe" — but they're a constant part of the dev loop, so log
|
|
202
|
-
// (warning) rather than prompt.
|
|
179
|
+
// Package installs always mutate node_modules and run dependency postinstall
|
|
180
|
+
// scripts — a known local mutation worth an audit line.
|
|
203
181
|
/^npm\s+(install|ci|i|update|upgrade|rebuild|prune|dedupe)\b/,
|
|
204
182
|
/^(pnpm|yarn)\s+(install|add|ci|up|upgrade)\b/,
|
|
205
183
|
];
|
|
@@ -227,14 +205,21 @@ function isEphemeralPath(filePath) {
|
|
|
227
205
|
function isSensitiveWritePath(filePath) {
|
|
228
206
|
if (!filePath)
|
|
229
207
|
return true; // unknown target → err toward review
|
|
208
|
+
const home = os.homedir();
|
|
209
|
+
// Expand a leading `~` / `$HOME` / `${HOME}` to the real home dir BEFORE
|
|
210
|
+
// resolving. In a shell `echo x > ~/.zshrc` the tilde IS the home dir, but
|
|
211
|
+
// path.resolve treats `~` as a literal segment under cwd, which would miss
|
|
212
|
+
// sensitive home targets (shell rc, ~/.ssh, …) and silently downgrade them.
|
|
213
|
+
const expanded = filePath
|
|
214
|
+
.replace(/^~(?=\/|$)/, home)
|
|
215
|
+
.replace(/^\$\{?HOME\}?(?=\/|$)/, home);
|
|
230
216
|
let resolved;
|
|
231
217
|
try {
|
|
232
|
-
resolved = path.resolve(
|
|
218
|
+
resolved = path.resolve(expanded);
|
|
233
219
|
}
|
|
234
220
|
catch {
|
|
235
221
|
return true;
|
|
236
222
|
}
|
|
237
|
-
const home = os.homedir();
|
|
238
223
|
const underHome = (rel) => {
|
|
239
224
|
const base = path.join(home, rel);
|
|
240
225
|
return resolved === base || resolved.startsWith(base + path.sep);
|
|
@@ -491,6 +476,146 @@ function classifyAssignmentStage(stage) {
|
|
|
491
476
|
tier = maxTier(tier, classifyBashCommand(rest));
|
|
492
477
|
return tier;
|
|
493
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* `env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]` runs COMMAND with a
|
|
481
|
+
* modified environment (a wrapper like sudo / an assignment prefix). Strips
|
|
482
|
+
* env's own options (`-i`, `-u NAME`, `-C dir`, `-`, `--unset=NAME`, …) and any
|
|
483
|
+
* leading NAME=VALUE pairs, then classifies the inner COMMAND from its original
|
|
484
|
+
* (quote-preserving) offset. `env` with no command just prints the environment
|
|
485
|
+
* → safe. Returns null if the stage doesn't start with `env`.
|
|
486
|
+
*/
|
|
487
|
+
function classifyEnvStage(stage) {
|
|
488
|
+
const lead = /^env\b[ \t]*/.exec(stage);
|
|
489
|
+
if (!lead)
|
|
490
|
+
return null;
|
|
491
|
+
// env options that consume a following argument token.
|
|
492
|
+
const argTaking = new Set(["-u", "--unset", "-C", "--chdir", "-P", "-S", "--split-string"]);
|
|
493
|
+
const readToken = (from) => {
|
|
494
|
+
let i = from;
|
|
495
|
+
let quote = null;
|
|
496
|
+
while (i < stage.length) {
|
|
497
|
+
const ch = stage[i];
|
|
498
|
+
if (quote) {
|
|
499
|
+
if (ch === quote)
|
|
500
|
+
quote = null;
|
|
501
|
+
i++;
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
if (ch === '"' || ch === "'") {
|
|
505
|
+
quote = ch;
|
|
506
|
+
i++;
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
if (/\s/.test(ch))
|
|
510
|
+
break;
|
|
511
|
+
i++;
|
|
512
|
+
}
|
|
513
|
+
return i;
|
|
514
|
+
};
|
|
515
|
+
let i = lead[0].length;
|
|
516
|
+
while (i < stage.length) {
|
|
517
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
518
|
+
i++;
|
|
519
|
+
if (i >= stage.length)
|
|
520
|
+
break;
|
|
521
|
+
const start = i;
|
|
522
|
+
i = readToken(i);
|
|
523
|
+
const tok = stage.slice(start, i);
|
|
524
|
+
if (tok === "-")
|
|
525
|
+
continue; // ignore-environment marker
|
|
526
|
+
if (tok.startsWith("--") && tok.includes("="))
|
|
527
|
+
continue; // --unset=NAME etc.
|
|
528
|
+
if (tok.startsWith("-")) {
|
|
529
|
+
if (argTaking.has(tok)) { // consume its argument token
|
|
530
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
531
|
+
i++;
|
|
532
|
+
i = readToken(i);
|
|
533
|
+
}
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
if (/^\w+=/.test(tok))
|
|
537
|
+
continue; // NAME=VALUE
|
|
538
|
+
return classifyBashCommand(stage.slice(start).trim()); // first real word → the command
|
|
539
|
+
}
|
|
540
|
+
return "safe"; // `env` / `env -u X` with no command just prints the environment
|
|
541
|
+
}
|
|
542
|
+
// ssh options that consume a following argument token (the rest are flags).
|
|
543
|
+
const SSH_ARG_OPTS = new Set([
|
|
544
|
+
"-b", "-c", "-D", "-E", "-e", "-F", "-I", "-i", "-J", "-L", "-l", "-m",
|
|
545
|
+
"-O", "-o", "-p", "-Q", "-R", "-S", "-W", "-w",
|
|
546
|
+
]);
|
|
547
|
+
/**
|
|
548
|
+
* `ssh [opts] [user@]host [remote command]` — ssh is a transport, so the tier
|
|
549
|
+
* comes from the REMOTE command, classified by the same effect rules as a local
|
|
550
|
+
* one (`ssh host ls` → safe; `ssh host 'rm -rf /data'` → high_stakes). An ssh
|
|
551
|
+
* with no command opens an interactive shell, and port/dynamic forwarding
|
|
552
|
+
* (`-L`/`-R`/`-D`/`-W`) grants opaque access — neither is inspectable, so both
|
|
553
|
+
* get a `review` floor (one prompt for remote access). Returns null if the stage
|
|
554
|
+
* isn't an ssh invocation. Excludes `ssh-keygen`/`ssh-add`/`ssh-keyscan` (the
|
|
555
|
+
* `^ssh\s` anchor requires whitespace, not a hyphen).
|
|
556
|
+
*/
|
|
557
|
+
function classifySshStage(stage) {
|
|
558
|
+
const lead = /^ssh\s+/.exec(stage);
|
|
559
|
+
if (!lead)
|
|
560
|
+
return null;
|
|
561
|
+
const readToken = (from) => {
|
|
562
|
+
let i = from;
|
|
563
|
+
let quote = null;
|
|
564
|
+
while (i < stage.length) {
|
|
565
|
+
const ch = stage[i];
|
|
566
|
+
if (quote) {
|
|
567
|
+
if (ch === quote)
|
|
568
|
+
quote = null;
|
|
569
|
+
i++;
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
if (ch === '"' || ch === "'") {
|
|
573
|
+
quote = ch;
|
|
574
|
+
i++;
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (/\s/.test(ch))
|
|
578
|
+
break;
|
|
579
|
+
i++;
|
|
580
|
+
}
|
|
581
|
+
return i;
|
|
582
|
+
};
|
|
583
|
+
let i = lead[0].length;
|
|
584
|
+
let sawForward = false;
|
|
585
|
+
while (i < stage.length) {
|
|
586
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
587
|
+
i++;
|
|
588
|
+
if (i >= stage.length)
|
|
589
|
+
break;
|
|
590
|
+
const start = i;
|
|
591
|
+
i = readToken(i);
|
|
592
|
+
const tok = stage.slice(start, i);
|
|
593
|
+
if (tok.startsWith("-")) {
|
|
594
|
+
if (/^-[LRDW]/.test(tok))
|
|
595
|
+
sawForward = true; // forwarding/tunnel
|
|
596
|
+
if (SSH_ARG_OPTS.has(tok)) { // separated option arg
|
|
597
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
598
|
+
i++;
|
|
599
|
+
i = readToken(i);
|
|
600
|
+
}
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
// First non-option token is the [user@]host. Everything after it is the
|
|
604
|
+
// remote command (usually a single quoted string).
|
|
605
|
+
if (sawForward)
|
|
606
|
+
return "review";
|
|
607
|
+
let rest = stage.slice(i).trim();
|
|
608
|
+
if (!rest)
|
|
609
|
+
return "review"; // interactive shell
|
|
610
|
+
// Strip one wrapping quote layer so the remote command's own operators
|
|
611
|
+
// (`;`, `|`) are classified as command separators, not quoted data.
|
|
612
|
+
if (rest.length >= 2 && (rest[0] === '"' || rest[0] === "'") && rest[rest.length - 1] === rest[0]) {
|
|
613
|
+
rest = rest.slice(1, -1);
|
|
614
|
+
}
|
|
615
|
+
return classifyBashCommand(rest);
|
|
616
|
+
}
|
|
617
|
+
return "review"; // only options / a bare host token → treat as remote access
|
|
618
|
+
}
|
|
494
619
|
function classifyBashCommand(command) {
|
|
495
620
|
if (!command)
|
|
496
621
|
return "safe";
|
|
@@ -526,6 +651,15 @@ function classifyBashCommand(command) {
|
|
|
526
651
|
if (pattern.test(trimmed))
|
|
527
652
|
return "high_stakes";
|
|
528
653
|
}
|
|
654
|
+
// Destructive/irreversible single commands, matched at stage start with any
|
|
655
|
+
// leading `sudo ` stripped (so `sudo mkfs …` is still caught, but the word
|
|
656
|
+
// can't fire inside an argument). These don't span pipes, so `^`-anchoring is
|
|
657
|
+
// precise and avoids prose false-positives.
|
|
658
|
+
const bareStage = trimmed.replace(/^sudo\s+/, "");
|
|
659
|
+
for (const pattern of HIGH_STAKES_STAGE) {
|
|
660
|
+
if (pattern.test(bareStage))
|
|
661
|
+
return "high_stakes";
|
|
662
|
+
}
|
|
529
663
|
// Variable assignments: `NAME=value` (pure) or `NAME=$(cmd) rest` (env prefix
|
|
530
664
|
// / capture). Classify the command-substitution inners and any trailing
|
|
531
665
|
// command; a purely literal assignment is safe. Dangerous substitutions were
|
|
@@ -533,6 +667,18 @@ function classifyBashCommand(command) {
|
|
|
533
667
|
const assignTier = classifyAssignmentStage(trimmed);
|
|
534
668
|
if (assignTier !== null)
|
|
535
669
|
return assignTier;
|
|
670
|
+
// `env [opts] [NAME=VAL]... cmd` runs cmd with a modified environment. Strip
|
|
671
|
+
// the env prefix and classify the inner command (a dangerous inner was already
|
|
672
|
+
// caught by the high-stakes full scan). Bare `env` just prints the env → safe.
|
|
673
|
+
const envTier = classifyEnvStage(trimmed);
|
|
674
|
+
if (envTier !== null)
|
|
675
|
+
return envTier;
|
|
676
|
+
// ssh is a transport — classify the remote command it runs (interactive /
|
|
677
|
+
// port-forwarding ssh has no inspectable command → review floor). scp/rsync
|
|
678
|
+
// to a remote are file transfers and stay high_stakes (HIGH_STAKES_STAGE).
|
|
679
|
+
const sshTier = classifySshStage(trimmed);
|
|
680
|
+
if (sshTier !== null)
|
|
681
|
+
return sshTier;
|
|
536
682
|
// sudo: privilege escalation. A high-stakes inner command was already caught
|
|
537
683
|
// by the full-string scan above (\b patterns match through the `sudo` prefix);
|
|
538
684
|
// anything else still warrants review.
|
|
@@ -545,34 +691,31 @@ function classifyBashCommand(command) {
|
|
|
545
691
|
const sql = findSqlInCommand(trimmed);
|
|
546
692
|
if (sql)
|
|
547
693
|
return classifySqlSeverity(sql);
|
|
548
|
-
// File-mutating shell patterns
|
|
549
|
-
//
|
|
550
|
-
//
|
|
551
|
-
//
|
|
552
|
-
//
|
|
553
|
-
// review.
|
|
694
|
+
// File-mutating shell patterns (echo > X, tee, dd of=, touch, sed -i, cp, mv).
|
|
695
|
+
// A file write/copy/move is a reversible LOCAL mutation, classified like the
|
|
696
|
+
// Write/Edit tool: `warning` (logged) — UNLESS a target is a sensitive path
|
|
697
|
+
// (system dir, secret store, shell rc, OKed config), which stays `review`.
|
|
698
|
+
// (Overwriting a raw block device was already caught by the high-stakes scan.)
|
|
554
699
|
const ops = extractShellWriteOps(trimmed);
|
|
555
700
|
if (ops.length > 0) {
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
return creates.some((o) => isSensitiveWritePath(o.target)) ? "review" : "warning";
|
|
559
|
-
}
|
|
560
|
-
return "review";
|
|
701
|
+
const targets = ops.map((o) => o.target);
|
|
702
|
+
return targets.some((t) => isSensitiveWritePath(t)) ? "review" : "warning";
|
|
561
703
|
}
|
|
562
|
-
//
|
|
563
|
-
for (const pattern of
|
|
704
|
+
// Outward sends (email/message) → review.
|
|
705
|
+
for (const pattern of REVIEW_COMMANDS) {
|
|
564
706
|
if (pattern.test(trimmed))
|
|
565
|
-
return "
|
|
707
|
+
return "review";
|
|
566
708
|
}
|
|
567
|
-
//
|
|
568
|
-
//
|
|
569
|
-
//
|
|
709
|
+
// Local-mutation markers (local git, code execution, installs) → warning:
|
|
710
|
+
// logged, no prompt. Everything else is a read or an unrecognized command with
|
|
711
|
+
// no detected effect — and under the effect-based model that means `safe`.
|
|
570
712
|
for (const pattern of WARNING_COMMANDS) {
|
|
571
713
|
if (pattern.test(trimmed))
|
|
572
714
|
return "warning";
|
|
573
715
|
}
|
|
574
|
-
// Default:
|
|
575
|
-
|
|
716
|
+
// Default: safe. No destructive/outward/sensitive effect was detected, so we
|
|
717
|
+
// do NOT put a human in the loop just for "running a command".
|
|
718
|
+
return "safe";
|
|
576
719
|
}
|
|
577
720
|
function classifySqlSeverity(sql) {
|
|
578
721
|
if (/\bDROP\s+(TABLE|DATABASE|INDEX|VIEW)\b/i.test(sql))
|
|
@@ -592,7 +735,10 @@ function classifySqlSeverity(sql) {
|
|
|
592
735
|
// SQLite dot-commands (.tables, .schema, .dump, etc.) — safe unless they mutate
|
|
593
736
|
if (/^\s*\./.test(sql) && !/^\s*\.(import|read|restore)\b/i.test(sql))
|
|
594
737
|
return "safe";
|
|
595
|
-
|
|
738
|
+
// INSERT/ALTER and other non-destructive DML: a reversible row/schema change,
|
|
739
|
+
// not a data wipe → warning (logged, no prompt). Destructive SQL (DROP /
|
|
740
|
+
// TRUNCATE / DELETE FROM / UPDATE-without-WHERE) returned high_stakes above.
|
|
741
|
+
return "warning";
|
|
596
742
|
}
|
|
597
743
|
/**
|
|
598
744
|
* Detects shell idioms that mutate the filesystem. Used by classify (to
|