@agent-corpus/policy-manager 0.3.0 → 0.4.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/README.md +11 -1
- package/dist/flow.d.ts +9 -0
- package/dist/flow.js +698 -0
- package/dist/permissive.d.ts +15 -6
- package/dist/permissive.js +225 -79
- package/dist/script.d.ts +101 -0
- package/dist/script.js +912 -0
- package/dist/shell.d.ts +6 -3
- package/dist/shell.js +75 -353
- package/package.json +1 -1
package/dist/flow.js
ADDED
|
@@ -0,0 +1,698 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { basename, join, resolve } from "node:path";
|
|
3
|
+
import { BANNED_ENV, human, redirectDecision, simpleDecision, unwrap } from "./permissive.js";
|
|
4
|
+
import { result } from "./policy.js";
|
|
5
|
+
import { parseScript, SUBSTITUTION, } from "./script.js";
|
|
6
|
+
/**
|
|
7
|
+
* Walks a parsed script and judges every command that could run. The state carried along is the
|
|
8
|
+
* set of directories the next command could run in and the variables whose value is known. A
|
|
9
|
+
* variable with a known, simple value is replaced by it and the real command is judged; anything
|
|
10
|
+
* else is an unknown value, which `simpleDecision` confines to arguments that cannot change what a
|
|
11
|
+
* command does. Branches are judged on copies and merged conservatively; anything a loop assigns
|
|
12
|
+
* is unknown inside it and afterwards.
|
|
13
|
+
*/
|
|
14
|
+
const MAX_DIRECTORIES = 8;
|
|
15
|
+
const MAX_VARIANTS = 16;
|
|
16
|
+
const MAX_SUBSTITUTION_DEPTH = 3;
|
|
17
|
+
/** Commands one script may be judged as; past this it is too large to verify in time. */
|
|
18
|
+
const MAX_JUDGEMENTS = 400;
|
|
19
|
+
/** No whitespace, glob, quote, expansion or tilde characters: the value is exactly one word. */
|
|
20
|
+
const SIMPLE_VALUE = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
21
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
22
|
+
const UNSUPPORTED = "Unsupported shell syntax; expansions and subshells need human review";
|
|
23
|
+
const copy = (state) => ({ cwds: [...state.cwds], vars: new Map(state.vars) });
|
|
24
|
+
function merge(a, b) {
|
|
25
|
+
const vars = new Map();
|
|
26
|
+
for (const [name, left] of a.vars) {
|
|
27
|
+
if (!b.vars.has(name))
|
|
28
|
+
continue; // assigned on one path only: an environment value is possible
|
|
29
|
+
const right = b.vars.get(name);
|
|
30
|
+
const union = left && right ? [...new Set([...left, ...right])] : null;
|
|
31
|
+
vars.set(name, union && union.length <= MAX_VARIANTS ? union : null);
|
|
32
|
+
}
|
|
33
|
+
return { cwds: [...new Set([...a.cwds, ...b.cwds])], vars };
|
|
34
|
+
}
|
|
35
|
+
function replace(target, source) {
|
|
36
|
+
target.cwds = source.cwds;
|
|
37
|
+
target.vars = source.vars;
|
|
38
|
+
}
|
|
39
|
+
// --- words ------------------------------------------------------------------------------------
|
|
40
|
+
const isKnown = (values) => !!values?.length && values.every((v) => SIMPLE_VALUE.test(v));
|
|
41
|
+
/**
|
|
42
|
+
* Environment variables whose value is harmless to print or pass on. Any other variable the script
|
|
43
|
+
* did not assign could hold a credential (`$GITHUB_TOKEN`, `$AWS_SECRET_ACCESS_KEY`), so using it
|
|
44
|
+
* needs a human; its value is never known, so it is also an unknown marker.
|
|
45
|
+
*/
|
|
46
|
+
const ENVIRONMENT_OK = new Set([
|
|
47
|
+
"PWD",
|
|
48
|
+
"OLDPWD",
|
|
49
|
+
"USER",
|
|
50
|
+
"LOGNAME",
|
|
51
|
+
"HOSTNAME",
|
|
52
|
+
"LANG",
|
|
53
|
+
"TERM",
|
|
54
|
+
"TMPDIR",
|
|
55
|
+
"SHLVL",
|
|
56
|
+
"CI",
|
|
57
|
+
"NODE_ENV",
|
|
58
|
+
"TZ",
|
|
59
|
+
"LC_ALL",
|
|
60
|
+
"COLUMNS",
|
|
61
|
+
"LINES",
|
|
62
|
+
]);
|
|
63
|
+
function environmentRefs(ctx, words, state) {
|
|
64
|
+
for (const word of words)
|
|
65
|
+
for (const part of word.parts)
|
|
66
|
+
if (typeof part === "object" &&
|
|
67
|
+
"name" in part &&
|
|
68
|
+
!state.vars.has(part.name) &&
|
|
69
|
+
!ENVIRONMENT_OK.has(part.name))
|
|
70
|
+
return human(ctx.request, "unknown", "An environment variable of unknown content requires human review");
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
function knownVariables(words, state) {
|
|
74
|
+
const names = new Set();
|
|
75
|
+
for (const word of words)
|
|
76
|
+
for (const part of word.parts)
|
|
77
|
+
if (typeof part === "object" && "name" in part)
|
|
78
|
+
names.add(part.name);
|
|
79
|
+
const known = [...names].filter((name) => isKnown(state.vars.get(name)));
|
|
80
|
+
const size = (list) => list.reduce((n, k) => n * (state.vars.get(k)?.length ?? 1), 1);
|
|
81
|
+
known.sort((a, b) => (state.vars.get(b)?.length ?? 0) - (state.vars.get(a)?.length ?? 0));
|
|
82
|
+
while (known.length && size(known) > MAX_VARIANTS)
|
|
83
|
+
known.shift();
|
|
84
|
+
return new Map(known.map((name) => [name, state.vars.get(name)]));
|
|
85
|
+
}
|
|
86
|
+
function combinations(known) {
|
|
87
|
+
let out = [{}];
|
|
88
|
+
for (const [name, values] of known)
|
|
89
|
+
out = out.flatMap((env) => values.map((value) => ({ ...env, [name]: value })));
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
/** Text of a word for one choice of variable values; unknown parts become SUBSTITUTION. */
|
|
93
|
+
function instantiate(word, known, env) {
|
|
94
|
+
let text = "";
|
|
95
|
+
let loose = false;
|
|
96
|
+
for (const part of word.parts) {
|
|
97
|
+
if (typeof part === "string")
|
|
98
|
+
text += part;
|
|
99
|
+
else if ("unknown" in part) {
|
|
100
|
+
text += SUBSTITUTION;
|
|
101
|
+
loose ||= !part.quoted;
|
|
102
|
+
}
|
|
103
|
+
else if (known.has(part.name))
|
|
104
|
+
text += env[part.name] ?? "";
|
|
105
|
+
else {
|
|
106
|
+
text += SUBSTITUTION;
|
|
107
|
+
loose ||= !part.quoted;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { text, loose };
|
|
111
|
+
}
|
|
112
|
+
function plainText(word) {
|
|
113
|
+
if (!word)
|
|
114
|
+
return undefined;
|
|
115
|
+
let text = "";
|
|
116
|
+
for (const part of word.parts) {
|
|
117
|
+
if (typeof part !== "string")
|
|
118
|
+
return undefined;
|
|
119
|
+
text += part;
|
|
120
|
+
}
|
|
121
|
+
return text;
|
|
122
|
+
}
|
|
123
|
+
// --- substitutions and redirections -----------------------------------------------------------
|
|
124
|
+
/** Judges the commands inside `$(...)`, backticks and `<(...)`; they run before their command. */
|
|
125
|
+
function runSubs(ctx, subs, state) {
|
|
126
|
+
for (const inner of subs) {
|
|
127
|
+
if (ctx.depth >= MAX_SUBSTITUTION_DEPTH)
|
|
128
|
+
return human(ctx.request, "unknown", "Command substitutions are nested too deeply");
|
|
129
|
+
const script = parseScript(inner);
|
|
130
|
+
if (!script)
|
|
131
|
+
return human(ctx.request, "unknown", UNSUPPORTED);
|
|
132
|
+
const refusal = runList({ ...ctx, depth: ctx.depth + 1, loop: 0 }, script, copy(state));
|
|
133
|
+
if (refusal)
|
|
134
|
+
return refusal;
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
function toRedirect(redir, text) {
|
|
139
|
+
if (redir.op === "<<" || redir.op === "<<<")
|
|
140
|
+
return undefined;
|
|
141
|
+
return { fd: redir.fd, op: redir.op, target: text };
|
|
142
|
+
}
|
|
143
|
+
/** Redirections attached to a compound command. */
|
|
144
|
+
function runRedirects(ctx, redirs, state) {
|
|
145
|
+
const words = redirs.flatMap((r) => (r.target ? [r.target] : []));
|
|
146
|
+
const refusal = runSubs(ctx, words.flatMap((w) => w.subs), state);
|
|
147
|
+
if (refusal)
|
|
148
|
+
return refusal;
|
|
149
|
+
const unsafe = environmentRefs(ctx, words, state);
|
|
150
|
+
if (unsafe)
|
|
151
|
+
return unsafe;
|
|
152
|
+
const known = knownVariables(words, state);
|
|
153
|
+
for (const env of combinations(known))
|
|
154
|
+
for (const redir of redirs) {
|
|
155
|
+
if (!redir.target)
|
|
156
|
+
continue;
|
|
157
|
+
const redirect = toRedirect(redir, instantiate(redir.target, known, env).text);
|
|
158
|
+
if (!redirect)
|
|
159
|
+
continue;
|
|
160
|
+
for (const cwd of state.cwds) {
|
|
161
|
+
const check = redirectDecision(ctx.request, redirect, ctx.profile, cwd);
|
|
162
|
+
if (check)
|
|
163
|
+
return check;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
// --- assigned names ---------------------------------------------------------------------------
|
|
169
|
+
/**
|
|
170
|
+
* Special builtins keep a prefix assignment in the shell afterwards when it runs as sh, dash or
|
|
171
|
+
* bash in POSIX mode (and zsh with posixbuiltins), so `X=rm :` assigns X.
|
|
172
|
+
*/
|
|
173
|
+
const SPECIAL_BUILTINS = new Set([
|
|
174
|
+
":",
|
|
175
|
+
".",
|
|
176
|
+
"eval",
|
|
177
|
+
"exec",
|
|
178
|
+
"exit",
|
|
179
|
+
"export",
|
|
180
|
+
"readonly",
|
|
181
|
+
"return",
|
|
182
|
+
"set",
|
|
183
|
+
"shift",
|
|
184
|
+
"times",
|
|
185
|
+
"trap",
|
|
186
|
+
"unset",
|
|
187
|
+
"break",
|
|
188
|
+
"continue",
|
|
189
|
+
]);
|
|
190
|
+
/**
|
|
191
|
+
* The variable `printf -v` or zsh `print -v` stores into: `-v NAME` or `-vNAME`. A cluster that
|
|
192
|
+
* contains `v` in any other form (`-rv`, `-vr`), or a computed option or name, is unreadable.
|
|
193
|
+
*/
|
|
194
|
+
function printTargets(args) {
|
|
195
|
+
const names = [];
|
|
196
|
+
for (let i = 0; i < args.length; i++) {
|
|
197
|
+
const arg = args[i];
|
|
198
|
+
if (arg === undefined)
|
|
199
|
+
return { names, unreadable: true };
|
|
200
|
+
if (arg === "--" || !arg.startsWith("-") || arg === "-")
|
|
201
|
+
break;
|
|
202
|
+
if (!arg.includes("v"))
|
|
203
|
+
continue;
|
|
204
|
+
const next = args[i + 1];
|
|
205
|
+
if (arg === "-v") {
|
|
206
|
+
if (next && NAME.test(next)) {
|
|
207
|
+
names.push(next);
|
|
208
|
+
i++;
|
|
209
|
+
}
|
|
210
|
+
else
|
|
211
|
+
return { names, unreadable: true };
|
|
212
|
+
}
|
|
213
|
+
else if (/^-v[A-Za-z_][A-Za-z0-9_]*$/.test(arg)) {
|
|
214
|
+
// `-vNAME`, or a cluster whose last option takes the next word: both are possible targets.
|
|
215
|
+
names.push(arg.slice(2));
|
|
216
|
+
if (next && NAME.test(next))
|
|
217
|
+
names.push(next);
|
|
218
|
+
}
|
|
219
|
+
else
|
|
220
|
+
return { names, unreadable: true };
|
|
221
|
+
}
|
|
222
|
+
return { names, unreadable: false };
|
|
223
|
+
}
|
|
224
|
+
function assignedInSimple(node, out) {
|
|
225
|
+
if (!node.argv.length) {
|
|
226
|
+
for (const a of node.assigns)
|
|
227
|
+
out.add(a.name);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const name = basename(plainText(node.argv[0]) ?? "");
|
|
231
|
+
const args = node.argv.slice(1).map(plainText);
|
|
232
|
+
if (SPECIAL_BUILTINS.has(name))
|
|
233
|
+
for (const a of node.assigns)
|
|
234
|
+
out.add(a.name);
|
|
235
|
+
if (name === "export")
|
|
236
|
+
for (const arg of args) {
|
|
237
|
+
const head = arg?.split("=")[0];
|
|
238
|
+
if (head && NAME.test(head))
|
|
239
|
+
out.add(head);
|
|
240
|
+
}
|
|
241
|
+
else if (name === "unset" || name === "read")
|
|
242
|
+
for (const arg of args) {
|
|
243
|
+
if (arg && NAME.test(arg))
|
|
244
|
+
out.add(arg);
|
|
245
|
+
else if (arg === undefined)
|
|
246
|
+
out.add("*");
|
|
247
|
+
}
|
|
248
|
+
else if (name === "printf" || name === "print") {
|
|
249
|
+
const targets = printTargets(args);
|
|
250
|
+
for (const target of targets.names)
|
|
251
|
+
out.add(target);
|
|
252
|
+
if (targets.unreadable)
|
|
253
|
+
out.add("*");
|
|
254
|
+
}
|
|
255
|
+
if (name === "read")
|
|
256
|
+
out.add("REPLY");
|
|
257
|
+
}
|
|
258
|
+
function assignedInList(list, out) {
|
|
259
|
+
for (const item of list.items) {
|
|
260
|
+
const nodes = [item.node.first, ...item.node.rest.map((r) => r.node)];
|
|
261
|
+
for (const node of nodes)
|
|
262
|
+
for (const stage of node.stages)
|
|
263
|
+
assignedInCommand(stage, out);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function assignedInCommand(command, out) {
|
|
267
|
+
switch (command.kind) {
|
|
268
|
+
case "simple":
|
|
269
|
+
assignedInSimple(command, out);
|
|
270
|
+
return;
|
|
271
|
+
case "subshell":
|
|
272
|
+
case "group":
|
|
273
|
+
assignedInList(command.body, out);
|
|
274
|
+
return;
|
|
275
|
+
case "for":
|
|
276
|
+
out.add(command.name);
|
|
277
|
+
assignedInList(command.body, out);
|
|
278
|
+
return;
|
|
279
|
+
case "while":
|
|
280
|
+
assignedInList(command.cond, out);
|
|
281
|
+
assignedInList(command.body, out);
|
|
282
|
+
return;
|
|
283
|
+
case "if":
|
|
284
|
+
for (const clause of command.clauses) {
|
|
285
|
+
assignedInList(clause.cond, out);
|
|
286
|
+
assignedInList(clause.body, out);
|
|
287
|
+
}
|
|
288
|
+
if (command.otherwise)
|
|
289
|
+
assignedInList(command.otherwise, out);
|
|
290
|
+
return;
|
|
291
|
+
case "case":
|
|
292
|
+
for (const branch of command.branches)
|
|
293
|
+
assignedInList(branch.body, out);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/** An assignment to a name the analysis could not read (`*`) makes every variable unknown. */
|
|
298
|
+
function forget(state, names) {
|
|
299
|
+
if (names.has("*"))
|
|
300
|
+
state.vars.clear();
|
|
301
|
+
else
|
|
302
|
+
for (const name of names)
|
|
303
|
+
state.vars.set(name, null);
|
|
304
|
+
}
|
|
305
|
+
// --- simple commands --------------------------------------------------------------------------
|
|
306
|
+
const STATEFUL = new Set([
|
|
307
|
+
"cd",
|
|
308
|
+
"pushd",
|
|
309
|
+
"popd",
|
|
310
|
+
"export",
|
|
311
|
+
"unset",
|
|
312
|
+
"read",
|
|
313
|
+
"set",
|
|
314
|
+
"shift",
|
|
315
|
+
"printf",
|
|
316
|
+
"print",
|
|
317
|
+
]);
|
|
318
|
+
function splitAssignment(word) {
|
|
319
|
+
const first = word.parts[0];
|
|
320
|
+
if (typeof first !== "string")
|
|
321
|
+
return undefined;
|
|
322
|
+
const eq = first.indexOf("=");
|
|
323
|
+
const name = eq < 0 ? first : first.slice(0, eq);
|
|
324
|
+
if (!NAME.test(name))
|
|
325
|
+
return undefined;
|
|
326
|
+
if (eq < 0)
|
|
327
|
+
return undefined;
|
|
328
|
+
return {
|
|
329
|
+
name,
|
|
330
|
+
value: { ...word, parts: [first.slice(eq + 1), ...word.parts.slice(1)], subs: [] },
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function runSimple(ctx, node, state, stage, piped) {
|
|
334
|
+
const { request, profile } = ctx;
|
|
335
|
+
const refusal = runSubs(ctx, [
|
|
336
|
+
...node.assigns.flatMap((a) => a.value.subs),
|
|
337
|
+
...node.argv.flatMap((w) => w.subs),
|
|
338
|
+
...node.redirects.flatMap((r) => r.target?.subs ?? []),
|
|
339
|
+
], state);
|
|
340
|
+
if (refusal)
|
|
341
|
+
return refusal;
|
|
342
|
+
// A glob in the command name or in a flag-like word expands to whatever file names exist, so
|
|
343
|
+
// `r[m] -rf x` runs `rm` once a file called `rm` is there, and `--forc[e]` can become `--force`.
|
|
344
|
+
const hasGlob = (word) => word.glob === true && plainText(word) !== "[" && plainText(word) !== "[[";
|
|
345
|
+
const globbed = node.argv.find((word, index) => hasGlob(word) &&
|
|
346
|
+
(index === 0 || word.parts.some((p) => typeof p === "string" && p.startsWith("-"))));
|
|
347
|
+
if (globbed)
|
|
348
|
+
return human(request, "unknown", "A pattern in a command name or option requires human review");
|
|
349
|
+
const values = node.assigns.map((a) => a.value);
|
|
350
|
+
const words = [
|
|
351
|
+
...values,
|
|
352
|
+
...node.argv,
|
|
353
|
+
...node.redirects.flatMap((r) => (r.target ? [r.target] : [])),
|
|
354
|
+
];
|
|
355
|
+
const unsafe = environmentRefs(ctx, words, state);
|
|
356
|
+
if (unsafe)
|
|
357
|
+
return unsafe;
|
|
358
|
+
const known = knownVariables(words, state);
|
|
359
|
+
const envs = combinations(known);
|
|
360
|
+
const program = node.argv[0];
|
|
361
|
+
const literal = plainText(program);
|
|
362
|
+
const name = literal === undefined ? undefined : basename(literal);
|
|
363
|
+
const build = (env) => {
|
|
364
|
+
const loose = [];
|
|
365
|
+
const argv = node.argv.map((word) => {
|
|
366
|
+
const expanded = instantiate(word, known, env);
|
|
367
|
+
loose.push(expanded.loose);
|
|
368
|
+
return expanded.text;
|
|
369
|
+
});
|
|
370
|
+
const redirects = [];
|
|
371
|
+
for (const redir of node.redirects) {
|
|
372
|
+
if (!redir.target)
|
|
373
|
+
continue;
|
|
374
|
+
const redirect = toRedirect(redir, instantiate(redir.target, known, env).text);
|
|
375
|
+
if (redirect)
|
|
376
|
+
redirects.push(redirect);
|
|
377
|
+
}
|
|
378
|
+
// Standard input is the heredoc or here-string only when nothing else also feeds it: a later
|
|
379
|
+
// `<file` replaces it, and zsh's multios runs both.
|
|
380
|
+
let stdin;
|
|
381
|
+
const inputs = node.redirects.filter((redir) => redir.fd === "0" && (redir.op === "<<" || redir.op === "<<<" || redir.op === "<"));
|
|
382
|
+
const only = inputs.length === 1 ? inputs[0] : undefined;
|
|
383
|
+
if (only?.op === "<<")
|
|
384
|
+
stdin = only.body ?? "";
|
|
385
|
+
else if (only?.op === "<<<" && only.target)
|
|
386
|
+
stdin = `${instantiate(only.target, known, env).text}\n`;
|
|
387
|
+
return {
|
|
388
|
+
env: node.assigns.map((a) => [a.name, instantiate(a.value, known, env).text]),
|
|
389
|
+
argv,
|
|
390
|
+
redirects,
|
|
391
|
+
subs: [],
|
|
392
|
+
loose,
|
|
393
|
+
...(stdin === undefined ? {} : { stdin }),
|
|
394
|
+
};
|
|
395
|
+
};
|
|
396
|
+
// A builtin the analysis must track can hide behind a variable (`X=cd; $X /etc`) or a wrapper
|
|
397
|
+
// that runs it in the current shell (`command cd /etc`, `time cd /etc`).
|
|
398
|
+
for (const env of envs) {
|
|
399
|
+
const effective = basename(unwrap(build(env).argv).argv[0] ?? "");
|
|
400
|
+
if (STATEFUL.has(effective) && name !== effective)
|
|
401
|
+
return human(request, "unknown", "A builtin behind a variable or wrapper requires human review");
|
|
402
|
+
}
|
|
403
|
+
if (!node.argv.length) {
|
|
404
|
+
// Assignments on their own, or redirections with no command.
|
|
405
|
+
for (const a of node.assigns)
|
|
406
|
+
if (BANNED_ENV.test(a.name))
|
|
407
|
+
return human(request, "unknown", "Environment override requires human review");
|
|
408
|
+
if (piped && node.assigns.length)
|
|
409
|
+
return human(request, "unknown", "Assignment inside a pipeline requires human review");
|
|
410
|
+
for (const env of envs)
|
|
411
|
+
for (const redir of node.redirects) {
|
|
412
|
+
if (!redir.target)
|
|
413
|
+
continue;
|
|
414
|
+
const redirect = toRedirect(redir, instantiate(redir.target, known, env).text);
|
|
415
|
+
if (!redirect)
|
|
416
|
+
continue;
|
|
417
|
+
for (const cwd of state.cwds) {
|
|
418
|
+
const check = redirectDecision(request, redirect, profile, cwd);
|
|
419
|
+
if (check)
|
|
420
|
+
return check;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
for (const a of node.assigns) {
|
|
424
|
+
const texts = envs.map((env) => instantiate(a.value, known, env).text);
|
|
425
|
+
if (texts.length && texts.every((t) => SIMPLE_VALUE.test(t))) {
|
|
426
|
+
const set = [...new Set(texts)];
|
|
427
|
+
if (set.length <= MAX_VARIANTS) {
|
|
428
|
+
state.vars.set(a.name, set);
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
state.vars.set(a.name, null);
|
|
433
|
+
}
|
|
434
|
+
ctx.box.last = result(request, "code-read", "Variable assignment", true);
|
|
435
|
+
return undefined;
|
|
436
|
+
}
|
|
437
|
+
for (const env of envs) {
|
|
438
|
+
const command = build(env);
|
|
439
|
+
for (const cwd of state.cwds) {
|
|
440
|
+
if (++ctx.box.calls > MAX_JUDGEMENTS)
|
|
441
|
+
return human(request, "unknown", "Too many commands to verify");
|
|
442
|
+
const decision = simpleDecision(request, command, profile, ctx.depth, stage, cwd);
|
|
443
|
+
if (decision.decision !== "approve")
|
|
444
|
+
return decision;
|
|
445
|
+
ctx.box.last = decision;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
// A prefix assignment before a special builtin outlives the command in some shells.
|
|
449
|
+
if (name && SPECIAL_BUILTINS.has(name))
|
|
450
|
+
for (const a of node.assigns)
|
|
451
|
+
state.vars.set(a.name, null);
|
|
452
|
+
if (name === "cd")
|
|
453
|
+
return changeDirectory(ctx, state, piped, envs, build);
|
|
454
|
+
if (name === "export")
|
|
455
|
+
return exportVariables(ctx, node, state, piped, known, envs);
|
|
456
|
+
if (name === "unset" || name === "read" || name === "printf" || name === "print") {
|
|
457
|
+
const forgotten = new Set();
|
|
458
|
+
assignedInSimple(node, forgotten);
|
|
459
|
+
if (forgotten.has("*"))
|
|
460
|
+
return human(request, "unknown", `${name} naming a variable it cannot be read from requires human review`);
|
|
461
|
+
if (name === "read" || name === "unset") {
|
|
462
|
+
const flags = node.argv
|
|
463
|
+
.slice(1)
|
|
464
|
+
.map(plainText)
|
|
465
|
+
.filter((w) => w?.startsWith("-"));
|
|
466
|
+
const allowed = name === "read" ? ["-r", "-s", "-e"] : ["-v"];
|
|
467
|
+
if (flags.some((flag) => !flag || !allowed.includes(flag)))
|
|
468
|
+
return human(request, "unknown", `${name} with these options requires human review`);
|
|
469
|
+
}
|
|
470
|
+
for (const forgottenName of forgotten)
|
|
471
|
+
if (BANNED_ENV.test(forgottenName))
|
|
472
|
+
return human(request, "unknown", "Environment override requires human review");
|
|
473
|
+
forget(state, forgotten);
|
|
474
|
+
}
|
|
475
|
+
return undefined;
|
|
476
|
+
}
|
|
477
|
+
function changeDirectory(ctx, state, piped, envs, build) {
|
|
478
|
+
const { request } = ctx;
|
|
479
|
+
if (piped || ctx.loop > 0)
|
|
480
|
+
return human(request, "unknown", "Directory change in a loop or pipeline requires human review");
|
|
481
|
+
let cwds = [...state.cwds];
|
|
482
|
+
for (const env of envs) {
|
|
483
|
+
const target = build(env)
|
|
484
|
+
.argv.slice(1)
|
|
485
|
+
.filter((word) => word !== "--");
|
|
486
|
+
// A bare `cd` and `~` or `~/x` go to the home directory; `~user` and `-` stay with a human.
|
|
487
|
+
const raw = target.length === 0 ? "~" : target[0];
|
|
488
|
+
const word = raw === "~" || raw?.startsWith("~/") ? join(homedir(), raw.slice(1)) : raw;
|
|
489
|
+
if (target.length > 1 ||
|
|
490
|
+
!word ||
|
|
491
|
+
word.includes(SUBSTITUTION) ||
|
|
492
|
+
/^~|[*?[\]]/.test(word) ||
|
|
493
|
+
word.startsWith("-"))
|
|
494
|
+
return human(request, "unknown", "Directory change requires human review");
|
|
495
|
+
// `cd` can fail, so the directory it started from stays possible.
|
|
496
|
+
cwds = [...new Set([...cwds, ...state.cwds.map((cwd) => resolve(cwd, word))])];
|
|
497
|
+
}
|
|
498
|
+
if (cwds.length > MAX_DIRECTORIES)
|
|
499
|
+
return human(request, "unknown", "Too many directory changes to verify");
|
|
500
|
+
state.cwds = cwds;
|
|
501
|
+
ctx.box.last = result(request, "code-read", "Directory change", true);
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
function exportVariables(ctx, node, state, piped, known, envs) {
|
|
505
|
+
const { request } = ctx;
|
|
506
|
+
if (piped)
|
|
507
|
+
return human(request, "unknown", "Export inside a pipeline requires human review");
|
|
508
|
+
for (const arg of node.argv.slice(1)) {
|
|
509
|
+
const text = plainText(arg);
|
|
510
|
+
if (text?.startsWith("-"))
|
|
511
|
+
return human(request, "unknown", "export with options requires human review");
|
|
512
|
+
const assignment = splitAssignment(arg);
|
|
513
|
+
if (!assignment) {
|
|
514
|
+
if (text !== undefined && NAME.test(text))
|
|
515
|
+
continue; // exports an existing variable
|
|
516
|
+
return human(request, "unknown", "export of a computed name requires human review");
|
|
517
|
+
}
|
|
518
|
+
if (BANNED_ENV.test(assignment.name))
|
|
519
|
+
return human(request, "unknown", "Environment override requires human review");
|
|
520
|
+
const texts = envs.map((env) => instantiate(assignment.value, known, env).text);
|
|
521
|
+
if (texts.length && texts.every((t) => SIMPLE_VALUE.test(t)))
|
|
522
|
+
state.vars.set(assignment.name, [...new Set(texts)]);
|
|
523
|
+
else
|
|
524
|
+
state.vars.set(assignment.name, null);
|
|
525
|
+
}
|
|
526
|
+
return undefined;
|
|
527
|
+
}
|
|
528
|
+
// --- compound commands ------------------------------------------------------------------------
|
|
529
|
+
function runCommand(ctx, command, state, stage, piped) {
|
|
530
|
+
switch (command.kind) {
|
|
531
|
+
case "simple":
|
|
532
|
+
return runSimple(ctx, command, state, stage, piped);
|
|
533
|
+
case "subshell":
|
|
534
|
+
return runList(ctx, command.body, copy(state)) ?? runRedirects(ctx, command.redirects, state);
|
|
535
|
+
case "group":
|
|
536
|
+
return runList(ctx, command.body, state) ?? runRedirects(ctx, command.redirects, state);
|
|
537
|
+
case "for":
|
|
538
|
+
return runFor(ctx, command, state);
|
|
539
|
+
case "while":
|
|
540
|
+
return runWhile(ctx, command, state);
|
|
541
|
+
case "if":
|
|
542
|
+
return runIf(ctx, command, state);
|
|
543
|
+
case "case":
|
|
544
|
+
return runCase(ctx, command, state);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function runFor(ctx, node, state) {
|
|
548
|
+
if (BANNED_ENV.test(node.name))
|
|
549
|
+
return human(ctx.request, "unknown", "Environment override requires human review");
|
|
550
|
+
const refusal = runSubs(ctx, node.words.flatMap((w) => w.subs), state);
|
|
551
|
+
if (refusal)
|
|
552
|
+
return refusal;
|
|
553
|
+
const unsafe = environmentRefs(ctx, node.words, state);
|
|
554
|
+
if (unsafe)
|
|
555
|
+
return unsafe;
|
|
556
|
+
const known = knownVariables(node.words, state);
|
|
557
|
+
const values = new Set();
|
|
558
|
+
let unknown = false;
|
|
559
|
+
for (const env of combinations(known))
|
|
560
|
+
for (const word of node.words) {
|
|
561
|
+
const text = instantiate(word, known, env).text;
|
|
562
|
+
if (SIMPLE_VALUE.test(text))
|
|
563
|
+
values.add(text);
|
|
564
|
+
else
|
|
565
|
+
unknown = true;
|
|
566
|
+
}
|
|
567
|
+
if (!values.size || values.size > MAX_VARIANTS)
|
|
568
|
+
unknown = true;
|
|
569
|
+
const changed = new Set([node.name]);
|
|
570
|
+
assignedInList(node.body, changed);
|
|
571
|
+
const inner = copy(state);
|
|
572
|
+
forget(inner, changed);
|
|
573
|
+
inner.vars.set(node.name, unknown ? null : [...values]);
|
|
574
|
+
const body = runList({ ...ctx, loop: ctx.loop + 1 }, node.body, inner);
|
|
575
|
+
if (body)
|
|
576
|
+
return body;
|
|
577
|
+
forget(state, changed);
|
|
578
|
+
return runRedirects(ctx, node.redirects, state);
|
|
579
|
+
}
|
|
580
|
+
function runWhile(ctx, node, state) {
|
|
581
|
+
const changed = new Set();
|
|
582
|
+
assignedInList(node.cond, changed);
|
|
583
|
+
assignedInList(node.body, changed);
|
|
584
|
+
const inner = copy(state);
|
|
585
|
+
forget(inner, changed);
|
|
586
|
+
const loop = { ...ctx, loop: ctx.loop + 1 };
|
|
587
|
+
const refusal = runList(loop, node.cond, inner) ?? runList(loop, node.body, inner);
|
|
588
|
+
if (refusal)
|
|
589
|
+
return refusal;
|
|
590
|
+
forget(state, changed);
|
|
591
|
+
return runRedirects(ctx, node.redirects, state);
|
|
592
|
+
}
|
|
593
|
+
function runIf(ctx, node, state) {
|
|
594
|
+
const branches = [];
|
|
595
|
+
let current = state;
|
|
596
|
+
for (const [index, clause] of node.clauses.entries()) {
|
|
597
|
+
// The first condition always runs; later ones only when every earlier one failed.
|
|
598
|
+
const conditionState = index === 0 ? current : copy(current);
|
|
599
|
+
const refusal = runList(ctx, clause.cond, conditionState);
|
|
600
|
+
if (refusal)
|
|
601
|
+
return refusal;
|
|
602
|
+
if (index > 0)
|
|
603
|
+
current = merge(current, conditionState);
|
|
604
|
+
const taken = copy(current);
|
|
605
|
+
const body = runList(ctx, clause.body, taken);
|
|
606
|
+
if (body)
|
|
607
|
+
return body;
|
|
608
|
+
branches.push(taken);
|
|
609
|
+
}
|
|
610
|
+
if (node.otherwise) {
|
|
611
|
+
const taken = copy(current);
|
|
612
|
+
const body = runList(ctx, node.otherwise, taken);
|
|
613
|
+
if (body)
|
|
614
|
+
return body;
|
|
615
|
+
branches.push(taken);
|
|
616
|
+
}
|
|
617
|
+
else
|
|
618
|
+
branches.push(current);
|
|
619
|
+
const merged = branches.reduce((a, b) => merge(a, b));
|
|
620
|
+
if (merged.cwds.length > MAX_DIRECTORIES)
|
|
621
|
+
return human(ctx.request, "unknown", "Too many directory changes to verify");
|
|
622
|
+
replace(state, merged);
|
|
623
|
+
return runRedirects(ctx, node.redirects, state);
|
|
624
|
+
}
|
|
625
|
+
function runCase(ctx, node, state) {
|
|
626
|
+
const words = [node.word, ...node.branches.flatMap((b) => b.patterns)];
|
|
627
|
+
const refusal = runSubs(ctx, words.flatMap((w) => w.subs), state) ?? environmentRefs(ctx, words, state);
|
|
628
|
+
if (refusal)
|
|
629
|
+
return refusal;
|
|
630
|
+
let merged = copy(state);
|
|
631
|
+
for (const branch of node.branches) {
|
|
632
|
+
const taken = copy(state);
|
|
633
|
+
const body = runList(ctx, branch.body, taken);
|
|
634
|
+
if (body)
|
|
635
|
+
return body;
|
|
636
|
+
merged = merge(merged, taken);
|
|
637
|
+
}
|
|
638
|
+
if (merged.cwds.length > MAX_DIRECTORIES)
|
|
639
|
+
return human(ctx.request, "unknown", "Too many directory changes to verify");
|
|
640
|
+
replace(state, merged);
|
|
641
|
+
return runRedirects(ctx, node.redirects, state);
|
|
642
|
+
}
|
|
643
|
+
// --- lists, pipelines and the entry point -----------------------------------------------------
|
|
644
|
+
function runList(ctx, list, state) {
|
|
645
|
+
for (const item of list.items) {
|
|
646
|
+
// A background job runs in a subshell of its own.
|
|
647
|
+
const target = item.background ? copy(state) : state;
|
|
648
|
+
const { first, rest } = item.node;
|
|
649
|
+
const refusal = runPipeline(ctx, first, target);
|
|
650
|
+
if (refusal)
|
|
651
|
+
return refusal;
|
|
652
|
+
if (!rest.length)
|
|
653
|
+
continue;
|
|
654
|
+
// In `a && b || c` each later command runs only if the ones before it did, so it sees their
|
|
655
|
+
// effects, but the chain may stop after any of them: the state afterwards is every prefix's.
|
|
656
|
+
let running = copy(target);
|
|
657
|
+
let possible = copy(target);
|
|
658
|
+
for (const next of rest) {
|
|
659
|
+
running = copy(running);
|
|
660
|
+
const skipped = runPipeline(ctx, next.node, running);
|
|
661
|
+
if (skipped)
|
|
662
|
+
return skipped;
|
|
663
|
+
possible = merge(possible, running);
|
|
664
|
+
if (possible.cwds.length > MAX_DIRECTORIES)
|
|
665
|
+
return human(ctx.request, "unknown", "Too many directory changes to verify");
|
|
666
|
+
}
|
|
667
|
+
replace(target, possible);
|
|
668
|
+
}
|
|
669
|
+
return undefined;
|
|
670
|
+
}
|
|
671
|
+
function runPipeline(ctx, node, state) {
|
|
672
|
+
if (node.stages.length === 1)
|
|
673
|
+
return runCommand(ctx, node.stages[0], state, 0, false);
|
|
674
|
+
const changed = new Set();
|
|
675
|
+
for (const [index, stage] of node.stages.entries()) {
|
|
676
|
+
assignedInCommand(stage, changed);
|
|
677
|
+
const refusal = runCommand(ctx, stage, copy(state), index, true);
|
|
678
|
+
if (refusal)
|
|
679
|
+
return refusal;
|
|
680
|
+
}
|
|
681
|
+
// The last stage may run in the current shell, so what the stages assign is unknown afterwards.
|
|
682
|
+
forget(state, changed);
|
|
683
|
+
return undefined;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Evaluates a compound command. `cd <literal>` is tracked: every later command is judged in each
|
|
687
|
+
* directory it could run in (before and after the `cd`, since `cd x || ...` may fail), so a command
|
|
688
|
+
* that only looks safe relative to the starting directory cannot hide behind a `cd`.
|
|
689
|
+
*/
|
|
690
|
+
export function commandDecision(request, command, profile, depth = 0, start = [request.cwd]) {
|
|
691
|
+
const script = parseScript(command);
|
|
692
|
+
if (!script)
|
|
693
|
+
return human(request, "unknown", UNSUPPORTED);
|
|
694
|
+
const ctx = { request, profile, depth, loop: 0, box: { calls: 0 } };
|
|
695
|
+
const refusal = runList(ctx, script, { cwds: [...start], vars: new Map() });
|
|
696
|
+
return refusal ?? ctx.box.last ?? human(request, "unknown", "Empty command");
|
|
697
|
+
}
|
|
698
|
+
//# sourceMappingURL=flow.js.map
|