@oked/sdk 0.1.7 → 0.2.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.
- package/dist/classify.js +314 -124
- 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,77 +55,26 @@ 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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
];
|
|
105
|
-
// Bash commands classified as high stakes (destructive, irreversible, external)
|
|
106
|
-
const HIGH_STAKES_COMMANDS = [
|
|
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_*).
|
|
63
|
+
// File deletion (rm/rmdir/trash). Checked PER-STAGE (not on the full command)
|
|
64
|
+
// so the ephemeral-temp downgrade can run first: `something; rm /tmp/x` must be
|
|
65
|
+
// warning, but `rm /tmp/x` mixed with a non-temp delete in another stage stays
|
|
66
|
+
// high_stakes. Matching the bare word also catches it inside a loop body or a
|
|
67
|
+
// $(...) substitution stage.
|
|
68
|
+
const DELETE_PATTERNS = [
|
|
107
69
|
/\brm\b/,
|
|
108
|
-
/\brm\b\s+(?:-[^\s]*[rf][^\s]*\s+)*-[^\s]*[rf][^\s]*\b/,
|
|
109
|
-
/\brm\s+--recursive\b/,
|
|
110
|
-
/\brm\b.*\s+\//, // rm with absolute path
|
|
111
70
|
/\brmdir\b/,
|
|
112
71
|
/\btrash\b/,
|
|
113
72
|
/\btrash-put\b/,
|
|
73
|
+
];
|
|
74
|
+
// Bash commands classified as high stakes (destructive, irreversible, external).
|
|
75
|
+
// Scanned on the FULL command (some patterns, e.g. download|shell, span a pipe).
|
|
76
|
+
// File deletion lives in DELETE_PATTERNS (per-stage) instead, see above.
|
|
77
|
+
const HIGH_STAKES_COMMANDS = [
|
|
114
78
|
/\bgit\s+push\b/,
|
|
115
79
|
/\bgit\s+reset\s+--hard\b/,
|
|
116
80
|
/\bgit\s+clean\s+-f/,
|
|
@@ -123,10 +87,6 @@ const HIGH_STAKES_COMMANDS = [
|
|
|
123
87
|
// -c/-e, interpreter -e/-c bodies, heredocs), then classifySqlSeverity.
|
|
124
88
|
/\bdocker\s+(rm|rmi|system\s+prune)\b/,
|
|
125
89
|
/\bdocker\s+compose\s+down\b/,
|
|
126
|
-
/\bkill\b/,
|
|
127
|
-
/\bpkill\b/,
|
|
128
|
-
/\bkillall\b/,
|
|
129
|
-
/\bchmod\s+777\b/,
|
|
130
90
|
/\bcurl\s+.*-X\s*(DELETE|PUT|POST|PATCH)\b/i,
|
|
131
91
|
/\bcurl\s+.*--request\s*(DELETE|PUT|POST|PATCH)\b/i,
|
|
132
92
|
// curl flags that send a request body (POST/PUT/PATCH) without an explicit -X
|
|
@@ -152,36 +112,73 @@ const HIGH_STAKES_COMMANDS = [
|
|
|
152
112
|
/\bhimalaya\s+message\s+delete\b/,
|
|
153
113
|
/\bhimalaya\s+folder\s+(delete|expunge|purge)\b/,
|
|
154
114
|
/\bhimalaya\s+account\s+delete\b/,
|
|
115
|
+
// Overwriting a raw block device (`> /dev/sda`, `… > /dev/nvme0n1`) destroys a
|
|
116
|
+
// disk irreversibly. (dd to a device is in HIGH_STAKES_STAGE.)
|
|
117
|
+
/>\s*\/dev\/(sd|nvme|hd|disk|mmcblk|vd)\w*/,
|
|
155
118
|
];
|
|
156
|
-
//
|
|
157
|
-
//
|
|
119
|
+
// Destructive / irreversible commands matched PER-STAGE at the start of a stage
|
|
120
|
+
// (after stripping a leading `sudo `), so the destructive WORD can't be hit
|
|
121
|
+
// inside an argument or a commit message (`git commit -m "kill the flaky test"`
|
|
122
|
+
// must not be high_stakes). These don't span pipes, so per-stage `^`-anchoring
|
|
123
|
+
// is both safe and precise.
|
|
124
|
+
const HIGH_STAKES_STAGE = [
|
|
125
|
+
// Filesystem / disk destruction.
|
|
126
|
+
/^mkfs(\.\w+)?\b/,
|
|
127
|
+
/^mkswap\b/,
|
|
128
|
+
/^(fdisk|parted|gparted)\b/,
|
|
129
|
+
/^dd\b.*\bof=\/dev\//,
|
|
130
|
+
/^shred\b/,
|
|
131
|
+
/^truncate\b/, // `truncate -s 0 file` wipes contents
|
|
132
|
+
/^find\b.*\s-delete\b/, // `find … -delete` removes matches
|
|
133
|
+
// Power / system state.
|
|
134
|
+
/^(shutdown|reboot|halt|poweroff)\b/,
|
|
135
|
+
// Remote/outward file transfer (almost always a remote target).
|
|
136
|
+
/^scp\b/,
|
|
137
|
+
/^rsync\b.*(--delete|\s\S+:)/, // rsync --delete, or to a host:path target
|
|
138
|
+
// Local git history/work destruction (force branch delete, tag delete,
|
|
139
|
+
// dropping/clearing stashed work). Reversible local git (add/commit/stash) is
|
|
140
|
+
// a WARNING marker below.
|
|
141
|
+
/^git\s+branch\s+(?:-D|--delete)\b/,
|
|
142
|
+
/^git\s+tag\s+(?:-d|--delete)\b/,
|
|
143
|
+
/^git\s+stash\s+(?:drop|clear)\b/,
|
|
144
|
+
// Process termination.
|
|
145
|
+
/^kill\b/,
|
|
146
|
+
/^pkill\b/,
|
|
147
|
+
/^killall\b/,
|
|
148
|
+
// Wide-open permissions.
|
|
149
|
+
/^chmod\s+(?:-\S+\s+)*777\b/,
|
|
150
|
+
];
|
|
151
|
+
// Outward-but-recoverable commands → review (prompt, simple approve). Sending
|
|
152
|
+
// mail/messages can't be unsent, but it isn't destructive to local state, so it
|
|
153
|
+
// sits below high_stakes. Matched per-stage. (Destructive himalaya verbs —
|
|
154
|
+
// message delete, folder purge — are high_stakes in the full scan above.)
|
|
155
|
+
const REVIEW_COMMANDS = [
|
|
156
|
+
/^himalaya\s+message\s+(send|reply|forward)\b/,
|
|
157
|
+
];
|
|
158
|
+
// Local-mutation MARKERS for DETECTED side effects. These never gate a prompt —
|
|
159
|
+
// under the effect-based model anything not flagged destructive/outward/sensitive
|
|
160
|
+
// already auto-allows. They exist only to pick the `warning` (logged) vs `safe`
|
|
161
|
+
// (silent) label, and cover the local mutations we can recognize by shape but
|
|
162
|
+
// that aren't a plain file write (those are handled by extractShellWriteOps).
|
|
163
|
+
//
|
|
164
|
+
// Opaque code execution (node/npx/python/claude/test runners) is intentionally
|
|
165
|
+
// NOT here: we can't tell whether it mutated anything, and a destructive effect
|
|
166
|
+
// buried inside `node -e …` would slip through regardless of the label — so
|
|
167
|
+
// logging it adds terminal noise without buying safety. It stays `safe`, just
|
|
168
|
+
// like any other unrecognized command. A real file write it performs is a
|
|
169
|
+
// separate Bash/Write call that gets classified on its own.
|
|
158
170
|
const WARNING_COMMANDS = [
|
|
159
|
-
// Local, reversible git
|
|
160
|
-
//
|
|
161
|
-
//
|
|
162
|
-
// by HIGH_STAKES_COMMANDS above and wins first. `git stash drop|clear` is
|
|
163
|
-
// excluded — those discard stashed work — so it stays `review`.
|
|
171
|
+
// Local, reversible git writes — stage/commit/switch/stash. They mutate the
|
|
172
|
+
// 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.
|
|
164
174
|
/^git\s+add\b/,
|
|
165
175
|
/^git\s+commit\b/,
|
|
166
176
|
/^git\s+checkout\s+-b\b/,
|
|
167
177
|
/^git\s+switch\b/,
|
|
168
178
|
/^git\s+stash\b(?!\s+(?:drop|clear))/,
|
|
169
|
-
// PR creation is reversible (a PR can be closed); the underlying branch push
|
|
170
|
-
// is separately high_stakes.
|
|
171
179
|
/^gh\s+pr\s+create\b/,
|
|
172
|
-
//
|
|
173
|
-
//
|
|
174
|
-
// we don't prompt but keep a local trail. Known test runners and read-only
|
|
175
|
-
// version flags are handled as `safe` (SAFE_COMMANDS) before reaching here.
|
|
176
|
-
/^node\b/,
|
|
177
|
-
/^npx\b/,
|
|
178
|
-
/^python3?\b/,
|
|
179
|
-
/^npm\s+run\b/,
|
|
180
|
-
/^bun\b/,
|
|
181
|
-
/^deno\b/,
|
|
182
|
-
// Package installs run dependency postinstall scripts (code execution), so
|
|
183
|
-
// they're not "safe" — but they're a constant part of the dev loop, so log
|
|
184
|
-
// (warning) rather than prompt.
|
|
180
|
+
// Package installs always mutate node_modules and run dependency postinstall
|
|
181
|
+
// scripts — a known local mutation worth an audit line.
|
|
185
182
|
/^npm\s+(install|ci|i|update|upgrade|rebuild|prune|dedupe)\b/,
|
|
186
183
|
/^(pnpm|yarn)\s+(install|add|ci|up|upgrade)\b/,
|
|
187
184
|
];
|
|
@@ -209,14 +206,21 @@ function isEphemeralPath(filePath) {
|
|
|
209
206
|
function isSensitiveWritePath(filePath) {
|
|
210
207
|
if (!filePath)
|
|
211
208
|
return true; // unknown target → err toward review
|
|
209
|
+
const home = os.homedir();
|
|
210
|
+
// Expand a leading `~` / `$HOME` / `${HOME}` to the real home dir BEFORE
|
|
211
|
+
// resolving. In a shell `echo x > ~/.zshrc` the tilde IS the home dir, but
|
|
212
|
+
// path.resolve treats `~` as a literal segment under cwd, which would miss
|
|
213
|
+
// sensitive home targets (shell rc, ~/.ssh, …) and silently downgrade them.
|
|
214
|
+
const expanded = filePath
|
|
215
|
+
.replace(/^~(?=\/|$)/, home)
|
|
216
|
+
.replace(/^\$\{?HOME\}?(?=\/|$)/, home);
|
|
212
217
|
let resolved;
|
|
213
218
|
try {
|
|
214
|
-
resolved = path.resolve(
|
|
219
|
+
resolved = path.resolve(expanded);
|
|
215
220
|
}
|
|
216
221
|
catch {
|
|
217
222
|
return true;
|
|
218
223
|
}
|
|
219
|
-
const home = os.homedir();
|
|
220
224
|
const underHome = (rel) => {
|
|
221
225
|
const base = path.join(home, rel);
|
|
222
226
|
return resolved === base || resolved.startsWith(base + path.sep);
|
|
@@ -310,6 +314,7 @@ function splitTopLevel(cmd) {
|
|
|
310
314
|
const out = [];
|
|
311
315
|
let cur = "";
|
|
312
316
|
let quote = null;
|
|
317
|
+
let subDepth = 0; // depth inside $( ... ) / `...` command substitutions
|
|
313
318
|
let i = 0;
|
|
314
319
|
while (i < cmd.length) {
|
|
315
320
|
const ch = cmd[i];
|
|
@@ -326,6 +331,31 @@ function splitTopLevel(cmd) {
|
|
|
326
331
|
i++;
|
|
327
332
|
continue;
|
|
328
333
|
}
|
|
334
|
+
// Command substitution: keep `$( ... )` / backticks intact so a pipe or `;`
|
|
335
|
+
// inside (e.g. `V=$(curl … | sed …)`) isn't treated as a top-level operator.
|
|
336
|
+
if (ch === "$" && cmd[i + 1] === "(") {
|
|
337
|
+
subDepth++;
|
|
338
|
+
cur += "$(";
|
|
339
|
+
i += 2;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (subDepth > 0 && ch === "(") {
|
|
343
|
+
subDepth++;
|
|
344
|
+
cur += ch;
|
|
345
|
+
i++;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (subDepth > 0 && ch === ")") {
|
|
349
|
+
subDepth--;
|
|
350
|
+
cur += ch;
|
|
351
|
+
i++;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
if (subDepth > 0) {
|
|
355
|
+
cur += ch;
|
|
356
|
+
i++;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
329
359
|
// Heredoc: consume the opener and the entire body (up to the closing
|
|
330
360
|
// delimiter line) as part of the current segment, so operators inside a
|
|
331
361
|
// heredoc fed to an interpreter (psql/node/…) aren't treated as separators.
|
|
@@ -379,21 +409,149 @@ function isEphemeralOnlyDeletion(command) {
|
|
|
379
409
|
return false;
|
|
380
410
|
return targets.every((a) => isEphemeralPath(unquote(a)));
|
|
381
411
|
}
|
|
412
|
+
/**
|
|
413
|
+
* Classifies a single stage that begins with one or more `NAME=value`
|
|
414
|
+
* assignments (a pure assignment like `TARGET=abc`, an env prefix like
|
|
415
|
+
* `FOO=bar cmd`, or a capture like `V=$(cmd)`). Strips the leading assignments,
|
|
416
|
+
* then takes the worst tier of: each command-substitution inner found in the
|
|
417
|
+
* values, plus any trailing command. A purely literal assignment is `safe`.
|
|
418
|
+
* Returns null if the stage is not assignment-prefixed.
|
|
419
|
+
*/
|
|
420
|
+
function classifyAssignmentStage(stage) {
|
|
421
|
+
if (!/^\w+=/.test(stage))
|
|
422
|
+
return null;
|
|
423
|
+
const inners = [];
|
|
424
|
+
let i = 0;
|
|
425
|
+
while (i < stage.length) {
|
|
426
|
+
const m = stage.slice(i).match(/^(\w+)=/);
|
|
427
|
+
if (!m)
|
|
428
|
+
break; // next token isn't an assignment → it's the command
|
|
429
|
+
i += m[0].length;
|
|
430
|
+
let quote = null;
|
|
431
|
+
while (i < stage.length) {
|
|
432
|
+
const ch = stage[i];
|
|
433
|
+
if (quote) {
|
|
434
|
+
if (ch === quote)
|
|
435
|
+
quote = null;
|
|
436
|
+
i++;
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (ch === '"' || ch === "'") {
|
|
440
|
+
quote = ch;
|
|
441
|
+
i++;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (ch === "$" && stage[i + 1] === "(") {
|
|
445
|
+
let d = 1, j = i + 2;
|
|
446
|
+
while (j < stage.length && d > 0) {
|
|
447
|
+
if (stage[j] === "(")
|
|
448
|
+
d++;
|
|
449
|
+
else if (stage[j] === ")")
|
|
450
|
+
d--;
|
|
451
|
+
j++;
|
|
452
|
+
}
|
|
453
|
+
inners.push(stage.slice(i + 2, j - 1));
|
|
454
|
+
i = j;
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (ch === "`") {
|
|
458
|
+
let j = i + 1;
|
|
459
|
+
while (j < stage.length && stage[j] !== "`")
|
|
460
|
+
j++;
|
|
461
|
+
inners.push(stage.slice(i + 1, j));
|
|
462
|
+
i = j + 1;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (/\s/.test(ch))
|
|
466
|
+
break; // end of this value
|
|
467
|
+
i++;
|
|
468
|
+
}
|
|
469
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
470
|
+
i++;
|
|
471
|
+
}
|
|
472
|
+
const rest = stage.slice(i).trim();
|
|
473
|
+
let tier = "safe";
|
|
474
|
+
for (const inner of inners)
|
|
475
|
+
tier = maxTier(tier, classifyBashCommand(inner));
|
|
476
|
+
if (rest)
|
|
477
|
+
tier = maxTier(tier, classifyBashCommand(rest));
|
|
478
|
+
return tier;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* `env [OPTION]... [NAME=VALUE]... [COMMAND [ARG]...]` runs COMMAND with a
|
|
482
|
+
* modified environment (a wrapper like sudo / an assignment prefix). Strips
|
|
483
|
+
* env's own options (`-i`, `-u NAME`, `-C dir`, `-`, `--unset=NAME`, …) and any
|
|
484
|
+
* leading NAME=VALUE pairs, then classifies the inner COMMAND from its original
|
|
485
|
+
* (quote-preserving) offset. `env` with no command just prints the environment
|
|
486
|
+
* → safe. Returns null if the stage doesn't start with `env`.
|
|
487
|
+
*/
|
|
488
|
+
function classifyEnvStage(stage) {
|
|
489
|
+
const lead = /^env\b[ \t]*/.exec(stage);
|
|
490
|
+
if (!lead)
|
|
491
|
+
return null;
|
|
492
|
+
// env options that consume a following argument token.
|
|
493
|
+
const argTaking = new Set(["-u", "--unset", "-C", "--chdir", "-P", "-S", "--split-string"]);
|
|
494
|
+
const readToken = (from) => {
|
|
495
|
+
let i = from;
|
|
496
|
+
let quote = null;
|
|
497
|
+
while (i < stage.length) {
|
|
498
|
+
const ch = stage[i];
|
|
499
|
+
if (quote) {
|
|
500
|
+
if (ch === quote)
|
|
501
|
+
quote = null;
|
|
502
|
+
i++;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (ch === '"' || ch === "'") {
|
|
506
|
+
quote = ch;
|
|
507
|
+
i++;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
if (/\s/.test(ch))
|
|
511
|
+
break;
|
|
512
|
+
i++;
|
|
513
|
+
}
|
|
514
|
+
return i;
|
|
515
|
+
};
|
|
516
|
+
let i = lead[0].length;
|
|
517
|
+
while (i < stage.length) {
|
|
518
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
519
|
+
i++;
|
|
520
|
+
if (i >= stage.length)
|
|
521
|
+
break;
|
|
522
|
+
const start = i;
|
|
523
|
+
i = readToken(i);
|
|
524
|
+
const tok = stage.slice(start, i);
|
|
525
|
+
if (tok === "-")
|
|
526
|
+
continue; // ignore-environment marker
|
|
527
|
+
if (tok.startsWith("--") && tok.includes("="))
|
|
528
|
+
continue; // --unset=NAME etc.
|
|
529
|
+
if (tok.startsWith("-")) {
|
|
530
|
+
if (argTaking.has(tok)) { // consume its argument token
|
|
531
|
+
while (i < stage.length && /\s/.test(stage[i]))
|
|
532
|
+
i++;
|
|
533
|
+
i = readToken(i);
|
|
534
|
+
}
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
if (/^\w+=/.test(tok))
|
|
538
|
+
continue; // NAME=VALUE
|
|
539
|
+
return classifyBashCommand(stage.slice(start).trim()); // first real word → the command
|
|
540
|
+
}
|
|
541
|
+
return "safe"; // `env` / `env -u X` with no command just prints the environment
|
|
542
|
+
}
|
|
382
543
|
function classifyBashCommand(command) {
|
|
383
544
|
if (!command)
|
|
384
545
|
return "safe";
|
|
385
546
|
// Strip heredoc bodies up front: their contents are literal data, not shell,
|
|
386
547
|
// and must not be scanned for high-stakes tokens, operators, or redirects.
|
|
387
548
|
const trimmed = stripHeredocBodies(command).trim();
|
|
388
|
-
// rm/trash of only ephemeral temp files → warning (before the high-stakes
|
|
389
|
-
// scan, which would otherwise match the bare `rm`).
|
|
390
|
-
if (isEphemeralOnlyDeletion(trimmed))
|
|
391
|
-
return "warning";
|
|
392
549
|
// High-stakes scan on the FULL command, before any splitting. These patterns
|
|
393
550
|
// use \b and several intentionally span an operator (e.g. `curl … | bash`,
|
|
394
551
|
// `wget … | sh` — download-and-execute), so they have to be matched against
|
|
395
552
|
// the whole string. Most-restrictive-wins: a high-stakes match anywhere in a
|
|
396
|
-
// compound command takes the whole command to high_stakes.
|
|
553
|
+
// compound command takes the whole command to high_stakes. (File deletion is
|
|
554
|
+
// NOT here — it's per-stage below so the ephemeral-temp downgrade can run.)
|
|
397
555
|
for (const pattern of HIGH_STAKES_COMMANDS) {
|
|
398
556
|
if (pattern.test(trimmed))
|
|
399
557
|
return "high_stakes";
|
|
@@ -407,6 +565,38 @@ function classifyBashCommand(command) {
|
|
|
407
565
|
if (stages.length > 1) {
|
|
408
566
|
return stages.reduce((worst, stage) => maxTier(worst, classifyBashCommand(stage)), "safe");
|
|
409
567
|
}
|
|
568
|
+
// ---- single stage from here ----
|
|
569
|
+
// File deletion, per-stage so the ephemeral-temp carve-out can win: deleting
|
|
570
|
+
// only throwaway temp files (/tmp, $TMPDIR, /var/folders, …) → warning; any
|
|
571
|
+
// non-temp target (or the temp root itself) → high_stakes.
|
|
572
|
+
if (isEphemeralOnlyDeletion(trimmed))
|
|
573
|
+
return "warning";
|
|
574
|
+
for (const pattern of DELETE_PATTERNS) {
|
|
575
|
+
if (pattern.test(trimmed))
|
|
576
|
+
return "high_stakes";
|
|
577
|
+
}
|
|
578
|
+
// Destructive/irreversible single commands, matched at stage start with any
|
|
579
|
+
// leading `sudo ` stripped (so `sudo mkfs …` is still caught, but the word
|
|
580
|
+
// can't fire inside an argument). These don't span pipes, so `^`-anchoring is
|
|
581
|
+
// precise and avoids prose false-positives.
|
|
582
|
+
const bareStage = trimmed.replace(/^sudo\s+/, "");
|
|
583
|
+
for (const pattern of HIGH_STAKES_STAGE) {
|
|
584
|
+
if (pattern.test(bareStage))
|
|
585
|
+
return "high_stakes";
|
|
586
|
+
}
|
|
587
|
+
// Variable assignments: `NAME=value` (pure) or `NAME=$(cmd) rest` (env prefix
|
|
588
|
+
// / capture). Classify the command-substitution inners and any trailing
|
|
589
|
+
// command; a purely literal assignment is safe. Dangerous substitutions were
|
|
590
|
+
// already caught by the high-stakes/delete scans above.
|
|
591
|
+
const assignTier = classifyAssignmentStage(trimmed);
|
|
592
|
+
if (assignTier !== null)
|
|
593
|
+
return assignTier;
|
|
594
|
+
// `env [opts] [NAME=VAL]... cmd` runs cmd with a modified environment. Strip
|
|
595
|
+
// the env prefix and classify the inner command (a dangerous inner was already
|
|
596
|
+
// caught by the high-stakes full scan). Bare `env` just prints the env → safe.
|
|
597
|
+
const envTier = classifyEnvStage(trimmed);
|
|
598
|
+
if (envTier !== null)
|
|
599
|
+
return envTier;
|
|
410
600
|
// sudo: privilege escalation. A high-stakes inner command was already caught
|
|
411
601
|
// by the full-string scan above (\b patterns match through the `sudo` prefix);
|
|
412
602
|
// anything else still warrants review.
|
|
@@ -419,34 +609,31 @@ function classifyBashCommand(command) {
|
|
|
419
609
|
const sql = findSqlInCommand(trimmed);
|
|
420
610
|
if (sql)
|
|
421
611
|
return classifySqlSeverity(sql);
|
|
422
|
-
// File-mutating shell patterns
|
|
423
|
-
//
|
|
424
|
-
//
|
|
425
|
-
//
|
|
426
|
-
//
|
|
427
|
-
// review.
|
|
612
|
+
// File-mutating shell patterns (echo > X, tee, dd of=, touch, sed -i, cp, mv).
|
|
613
|
+
// A file write/copy/move is a reversible LOCAL mutation, classified like the
|
|
614
|
+
// Write/Edit tool: `warning` (logged) — UNLESS a target is a sensitive path
|
|
615
|
+
// (system dir, secret store, shell rc, OKed config), which stays `review`.
|
|
616
|
+
// (Overwriting a raw block device was already caught by the high-stakes scan.)
|
|
428
617
|
const ops = extractShellWriteOps(trimmed);
|
|
429
618
|
if (ops.length > 0) {
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
return creates.some((o) => isSensitiveWritePath(o.target)) ? "review" : "warning";
|
|
433
|
-
}
|
|
434
|
-
return "review";
|
|
619
|
+
const targets = ops.map((o) => o.target);
|
|
620
|
+
return targets.some((t) => isSensitiveWritePath(t)) ? "review" : "warning";
|
|
435
621
|
}
|
|
436
|
-
//
|
|
437
|
-
for (const pattern of
|
|
622
|
+
// Outward sends (email/message) → review.
|
|
623
|
+
for (const pattern of REVIEW_COMMANDS) {
|
|
438
624
|
if (pattern.test(trimmed))
|
|
439
|
-
return "
|
|
625
|
+
return "review";
|
|
440
626
|
}
|
|
441
|
-
//
|
|
442
|
-
//
|
|
443
|
-
//
|
|
627
|
+
// Local-mutation markers (local git, code execution, installs) → warning:
|
|
628
|
+
// logged, no prompt. Everything else is a read or an unrecognized command with
|
|
629
|
+
// no detected effect — and under the effect-based model that means `safe`.
|
|
444
630
|
for (const pattern of WARNING_COMMANDS) {
|
|
445
631
|
if (pattern.test(trimmed))
|
|
446
632
|
return "warning";
|
|
447
633
|
}
|
|
448
|
-
// Default:
|
|
449
|
-
|
|
634
|
+
// Default: safe. No destructive/outward/sensitive effect was detected, so we
|
|
635
|
+
// do NOT put a human in the loop just for "running a command".
|
|
636
|
+
return "safe";
|
|
450
637
|
}
|
|
451
638
|
function classifySqlSeverity(sql) {
|
|
452
639
|
if (/\bDROP\s+(TABLE|DATABASE|INDEX|VIEW)\b/i.test(sql))
|
|
@@ -466,7 +653,10 @@ function classifySqlSeverity(sql) {
|
|
|
466
653
|
// SQLite dot-commands (.tables, .schema, .dump, etc.) — safe unless they mutate
|
|
467
654
|
if (/^\s*\./.test(sql) && !/^\s*\.(import|read|restore)\b/i.test(sql))
|
|
468
655
|
return "safe";
|
|
469
|
-
|
|
656
|
+
// INSERT/ALTER and other non-destructive DML: a reversible row/schema change,
|
|
657
|
+
// not a data wipe → warning (logged, no prompt). Destructive SQL (DROP /
|
|
658
|
+
// TRUNCATE / DELETE FROM / UPDATE-without-WHERE) returned high_stakes above.
|
|
659
|
+
return "warning";
|
|
470
660
|
}
|
|
471
661
|
/**
|
|
472
662
|
* Detects shell idioms that mutate the filesystem. Used by classify (to
|