@agent-corpus/policy-manager 0.2.1 → 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/evidence.js +5 -3
- package/dist/flow.d.ts +9 -0
- package/dist/flow.js +698 -0
- package/dist/merged.js +1 -1
- package/dist/permissive.d.ts +15 -6
- package/dist/permissive.js +247 -63
- package/dist/script.d.ts +101 -0
- package/dist/script.js +912 -0
- package/dist/shell.d.ts +7 -0
- package/dist/shell.js +82 -191
- package/package.json +1 -1
package/dist/merged.js
CHANGED
|
@@ -62,7 +62,7 @@ export function mergedEvidence(cwd, ref) {
|
|
|
62
62
|
},
|
|
63
63
|
}).trim();
|
|
64
64
|
const cherry = git(cwd, ["cherry", into, synthetic]);
|
|
65
|
-
if (cherry
|
|
65
|
+
if (cherry?.split("\n").every((line) => line.startsWith("-")))
|
|
66
66
|
return {
|
|
67
67
|
merged: true,
|
|
68
68
|
how: "patch-equivalent to a squash or rebase merge into origin default",
|
package/dist/permissive.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { commandDecision } from "./flow.js";
|
|
1
2
|
import type { Profile } from "./profile.js";
|
|
3
|
+
import { type Redirect, type SimpleCommand } from "./shell.js";
|
|
2
4
|
import type { Decision, Request } from "./types.js";
|
|
5
|
+
export declare const BANNED_ENV: RegExp;
|
|
6
|
+
export declare function human(request: Request, category: Decision["category"], reason: string): Decision;
|
|
3
7
|
export declare function permissiveRead(cwd: string, input: string): {
|
|
4
8
|
allowed: boolean;
|
|
5
9
|
category: "sensitive";
|
|
@@ -14,10 +18,15 @@ export declare function permissiveWrite(cwd: string, input: string, profile: Pro
|
|
|
14
18
|
category: "code-read" | "worktree-write" | "sensitive";
|
|
15
19
|
reason: string;
|
|
16
20
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
interface Unwrapped {
|
|
22
|
+
argv: string[];
|
|
23
|
+
/** Assignments given to `env`, which the parser does not see as shell assignments. */
|
|
24
|
+
env: string[];
|
|
25
|
+
refusal?: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function unwrap(argv: string[]): Unwrapped;
|
|
28
|
+
/** Judges one redirection's target in `cwd`; undefined means it is acceptable. */
|
|
29
|
+
export declare function redirectDecision(request: Request, redirect: Redirect, profile: Profile, cwd: string): Decision | undefined;
|
|
30
|
+
export declare function simpleDecision(request: Request, command: SimpleCommand, profile: Profile, depth: number, stageIndex: number, cwd: string): Decision;
|
|
31
|
+
export { commandDecision };
|
|
23
32
|
//# sourceMappingURL=permissive.d.ts.map
|
package/dist/permissive.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { basename, resolve } from "node:path";
|
|
2
|
+
import { branchEvidence, canonical, inside, isProtectedPath, pathEvidence, protectedPath, sensitive, } from "./evidence.js";
|
|
3
|
+
import { commandDecision } from "./flow.js";
|
|
4
4
|
import { mergedEvidence, remoteTipMatches } from "./merged.js";
|
|
5
5
|
import { evaluateArgv, result } from "./policy.js";
|
|
6
|
-
import {
|
|
6
|
+
import { SUBSTITUTION } from "./shell.js";
|
|
7
7
|
/**
|
|
8
8
|
* Permissive profile: approve everything except effects that are destructive, irreversible,
|
|
9
9
|
* outward-facing, or touch credentials and protected configuration. The default for anything not
|
|
@@ -49,9 +49,110 @@ const WRITERS = new Set([
|
|
|
49
49
|
"chown",
|
|
50
50
|
]);
|
|
51
51
|
const WRAPPERS = new Set(["env", "nohup", "time", "command", "nice", "timeout", "xargs"]);
|
|
52
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Programs whose behaviour cannot be changed by an unknown argument: they only print or compare
|
|
54
|
+
* strings. The output of a command substitution may be an argument of these and of nothing else,
|
|
55
|
+
* because for any other program it could be a flag, a path or a subcommand the checks never saw.
|
|
56
|
+
*/
|
|
57
|
+
const SUBSTITUTION_OK = new Set([
|
|
58
|
+
"echo",
|
|
59
|
+
"printf",
|
|
60
|
+
"true",
|
|
61
|
+
"false",
|
|
62
|
+
"test",
|
|
63
|
+
"[",
|
|
64
|
+
"basename",
|
|
65
|
+
"dirname",
|
|
66
|
+
]);
|
|
67
|
+
// Variables that change how later commands are found, split or started. Assigning one, as a prefix
|
|
68
|
+
// or on its own, needs a human.
|
|
69
|
+
export const BANNED_ENV = /^(?:LD_|DYLD_|GIT_|PYTHON|NODE_OPTIONS$|PATH$|HOME$|BASH_ENV$|ENV$|SHELL$|PAGER$|EDITOR$|GH_TOKEN$|GITHUB_TOKEN$|IFS$|PS4$|PROMPT_COMMAND$|CDPATH$|GLOBIGNORE$|BASHOPTS$|SHELLOPTS$|BASH_XTRACEFD$|FPATH$|ZDOTDIR$|BASH_FUNC_|path$|cdpath$|fpath$|manpath$|module_path$)/;
|
|
70
|
+
// zsh ties the lowercase arrays path, cdpath, fpath, manpath and module_path to PATH, CDPATH, FPATH,
|
|
71
|
+
// MANPATH and MODULE_PATH, so those names are covered too (they are matched case-sensitively).
|
|
72
|
+
/** Runs arbitrary statements on another machine or database, so its argument is not judged. */
|
|
73
|
+
const REMOTE_EXECUTORS = new Set([
|
|
74
|
+
"ssh",
|
|
75
|
+
"psql",
|
|
76
|
+
"mysql",
|
|
77
|
+
"mariadb",
|
|
78
|
+
"sqlite3",
|
|
79
|
+
"sqlcmd",
|
|
80
|
+
"mongo",
|
|
81
|
+
"mongosh",
|
|
82
|
+
"redis-cli",
|
|
83
|
+
"cqlsh",
|
|
84
|
+
"sqlplus",
|
|
85
|
+
]);
|
|
86
|
+
/** Builtins that run a string later or change how later commands behave. */
|
|
87
|
+
const STATE_BUILTINS = new Set([
|
|
88
|
+
"trap",
|
|
89
|
+
"alias",
|
|
90
|
+
"unalias",
|
|
91
|
+
"builtin",
|
|
92
|
+
"enable",
|
|
93
|
+
"declare",
|
|
94
|
+
"typeset",
|
|
95
|
+
"local",
|
|
96
|
+
"readonly",
|
|
97
|
+
"let",
|
|
98
|
+
"mapfile",
|
|
99
|
+
"readarray",
|
|
100
|
+
"getopts",
|
|
101
|
+
"coproc",
|
|
102
|
+
"fc",
|
|
103
|
+
"history",
|
|
104
|
+
"hash",
|
|
105
|
+
// builtins that store into a variable the evaluator tracks, or run a command through a module
|
|
106
|
+
"vared",
|
|
107
|
+
"zparseopts",
|
|
108
|
+
"r",
|
|
109
|
+
"zformat",
|
|
110
|
+
"zstyle",
|
|
111
|
+
"zregexparse",
|
|
112
|
+
"getln",
|
|
113
|
+
"compgen",
|
|
114
|
+
"sysread",
|
|
115
|
+
"zselect",
|
|
116
|
+
"ztcp",
|
|
117
|
+
"zpty",
|
|
118
|
+
"setopt",
|
|
119
|
+
"unsetopt",
|
|
120
|
+
"shopt",
|
|
121
|
+
// zsh precommand modifiers and builtins that run a command or string
|
|
122
|
+
"noglob",
|
|
123
|
+
"nocorrect",
|
|
124
|
+
"repeat",
|
|
125
|
+
"emulate",
|
|
126
|
+
"sched",
|
|
127
|
+
"zmodload",
|
|
128
|
+
"autoload",
|
|
129
|
+
"integer",
|
|
130
|
+
"float",
|
|
131
|
+
"functions",
|
|
132
|
+
"-",
|
|
133
|
+
]);
|
|
134
|
+
/**
|
|
135
|
+
* Options whose next word is a value, not a flag or path, so an unknown (but single, quoted) word
|
|
136
|
+
* there cannot change what the command does: commit and PR messages built with `$(cat <<EOF ...)`.
|
|
137
|
+
*/
|
|
138
|
+
const VALUE_OPTIONS = {
|
|
139
|
+
git: {
|
|
140
|
+
commit: ["-m", "--message"],
|
|
141
|
+
tag: ["-m", "--message"],
|
|
142
|
+
stash: ["-m", "--message"],
|
|
143
|
+
},
|
|
144
|
+
gh: {
|
|
145
|
+
"pr create": ["--title", "-t", "--body", "-b"],
|
|
146
|
+
"pr edit": ["--title", "-t", "--body", "-b"],
|
|
147
|
+
"pr comment": ["--body", "-b"],
|
|
148
|
+
"pr review": ["--body", "-b"],
|
|
149
|
+
"issue create": ["--title", "-t", "--body", "-b"],
|
|
150
|
+
"issue edit": ["--title", "-t", "--body", "-b"],
|
|
151
|
+
"issue comment": ["--body", "-b"],
|
|
152
|
+
},
|
|
153
|
+
};
|
|
53
154
|
const FORCE_PUSH = ["--force", "--force-with-lease", "--force-if-includes", "--mirror", "--prune"];
|
|
54
|
-
function human(request, category, reason) {
|
|
155
|
+
export function human(request, category, reason) {
|
|
55
156
|
return result(request, category, reason);
|
|
56
157
|
}
|
|
57
158
|
export function permissiveRead(cwd, input) {
|
|
@@ -102,7 +203,7 @@ export function permissiveWrite(cwd, input, profile) {
|
|
|
102
203
|
}
|
|
103
204
|
return pathEvidence(cwd, input, true);
|
|
104
205
|
}
|
|
105
|
-
function unwrap(argv) {
|
|
206
|
+
export function unwrap(argv) {
|
|
106
207
|
let current = argv;
|
|
107
208
|
const env = [];
|
|
108
209
|
for (let depth = 0; depth < 4; depth++) {
|
|
@@ -341,20 +442,79 @@ function cloudDecision(request, argv, profile) {
|
|
|
341
442
|
return result(request, "impactful-write", "Permissive profile: non-destructive cloud change", true);
|
|
342
443
|
return strict;
|
|
343
444
|
}
|
|
344
|
-
|
|
445
|
+
/** Judges one redirection's target in `cwd`; undefined means it is acceptable. */
|
|
446
|
+
export function redirectDecision(request, redirect, profile, cwd) {
|
|
447
|
+
if (redirect.target.includes(SUBSTITUTION))
|
|
448
|
+
return human(request, "unknown", "Command substitution in a redirection requires human review");
|
|
449
|
+
// `>& word` duplicates a descriptor only when word is a number or `-`; otherwise it names a file.
|
|
450
|
+
if (redirect.target === "/dev/null" ||
|
|
451
|
+
(redirect.op === ">&" && /^(?:[0-9]+|-)$/.test(redirect.target)))
|
|
452
|
+
return undefined;
|
|
453
|
+
const check = redirect.op === "<"
|
|
454
|
+
? permissiveRead(cwd, redirect.target)
|
|
455
|
+
: permissiveWrite(cwd, redirect.target, profile);
|
|
456
|
+
return check.allowed ? undefined : human(request, check.category, check.reason);
|
|
457
|
+
}
|
|
458
|
+
/** printf formats with only %s, %b, %q and %%: no argument is evaluated as arithmetic. */
|
|
459
|
+
function printfOnlyPrintsStrings(args) {
|
|
460
|
+
const rest = args[0] === "-v" ? args.slice(2) : args;
|
|
461
|
+
const format = rest[0] === "--" ? rest[1] : rest[0];
|
|
462
|
+
if (format === undefined)
|
|
463
|
+
return true;
|
|
464
|
+
if (format.includes(SUBSTITUTION) || format.includes("*"))
|
|
465
|
+
return false;
|
|
466
|
+
for (const match of format.matchAll(/%[-+ #0-9.]*([A-Za-z%])/g))
|
|
467
|
+
if (!"sbq%".includes(match[1]))
|
|
468
|
+
return false;
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
/** The unknown words of `argv` that sit in a value position (see VALUE_OPTIONS), made literal. */
|
|
472
|
+
function withOpaqueValues(argv, loose) {
|
|
473
|
+
const program = basename(argv[0] ?? "");
|
|
474
|
+
const table = VALUE_OPTIONS[program];
|
|
475
|
+
if (!table)
|
|
476
|
+
return argv;
|
|
477
|
+
const first = program === "git" ? argv.findIndex((w, i) => i > 0 && !w.startsWith("-")) : 1;
|
|
478
|
+
const key = program === "git" ? (argv[first] ?? "") : `${argv[1] ?? ""} ${argv[2] ?? ""}`;
|
|
479
|
+
const options = table[key];
|
|
480
|
+
if (!options)
|
|
481
|
+
return argv;
|
|
482
|
+
return argv.map((word, index) => {
|
|
483
|
+
if (index === 0 || !word.includes(SUBSTITUTION) || loose?.[index])
|
|
484
|
+
return word;
|
|
485
|
+
const previous = argv[index - 1] ?? "";
|
|
486
|
+
const valueOfPrevious = !previous.includes(SUBSTITUTION) &&
|
|
487
|
+
(options.includes(previous) ||
|
|
488
|
+
(program === "git" && key === "commit" && /^-[A-Za-z]*m$/.test(previous)));
|
|
489
|
+
const inline = options.some((option) => option.startsWith("--") && word.startsWith(`${option}=`));
|
|
490
|
+
if (!valueOfPrevious && !(inline && !word.split("=")[0]?.includes(SUBSTITUTION)))
|
|
491
|
+
return word;
|
|
492
|
+
return word.split(SUBSTITUTION).join("OPAQUE");
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* A best-effort scan of code an interpreter is about to run, which this policy cannot otherwise
|
|
497
|
+
* read: deleting files, starting processes, evaluating strings, and touching credential or
|
|
498
|
+
* protected paths. It is a denylist, not a proof, so anything it flags goes to a human.
|
|
499
|
+
*/
|
|
500
|
+
const DANGEROUS_CODE = /\b(?:rmtree|removedirs|unlink|unlinkSync|rmdir|rmdirSync|rmSync|os\.remove|os\.removedirs|shutil\.move|child_process|execSync|spawnSync|spawn|subprocess|os\.system|os\.popen|os\.exec[a-z]*|eval|exec|compile|__import__|importlib|ctypes|socket|urlopen|requests\.(?:post|put|delete|patch)|fetch)\b|\bfs\.rm\b|\bPath\([^)]*\)\.(?:unlink|rmdir)|\brm\s+-/;
|
|
501
|
+
function interpreterCodeRefusal(code) {
|
|
502
|
+
if (DANGEROUS_CODE.test(code))
|
|
503
|
+
return "Interpreter code that deletes files, starts processes or evaluates strings requires human review";
|
|
504
|
+
for (const token of code.split(/[\s"'`()[\]{},;=<>|&]+/))
|
|
505
|
+
if ((token.includes("/") || token.startsWith(".")) &&
|
|
506
|
+
(sensitive(token) || protectedPath(token)))
|
|
507
|
+
return "Interpreter code that names a protected or credential path requires human review";
|
|
508
|
+
return undefined;
|
|
509
|
+
}
|
|
510
|
+
export function simpleDecision(request, command, profile, depth, stageIndex, cwd) {
|
|
345
511
|
for (const [name] of command.env)
|
|
346
512
|
if (BANNED_ENV.test(name))
|
|
347
513
|
return human(request, "unknown", "Environment override requires human review");
|
|
348
514
|
for (const redirect of command.redirects) {
|
|
349
|
-
|
|
350
|
-
if (
|
|
351
|
-
|
|
352
|
-
continue;
|
|
353
|
-
const check = redirect.op === "<"
|
|
354
|
-
? permissiveRead(cwd, redirect.target)
|
|
355
|
-
: permissiveWrite(cwd, redirect.target, profile);
|
|
356
|
-
if (!check.allowed)
|
|
357
|
-
return human(request, check.category, check.reason);
|
|
515
|
+
const refusal = redirectDecision(request, redirect, profile, cwd);
|
|
516
|
+
if (refusal)
|
|
517
|
+
return refusal;
|
|
358
518
|
}
|
|
359
519
|
const unwrapped = unwrap(command.argv);
|
|
360
520
|
if (unwrapped.refusal)
|
|
@@ -362,9 +522,44 @@ function simpleDecision(request, command, profile, depth, stageIndex, cwd) {
|
|
|
362
522
|
for (const name of unwrapped.env)
|
|
363
523
|
if (BANNED_ENV.test(name))
|
|
364
524
|
return human(request, "unknown", "Environment override requires human review");
|
|
365
|
-
const
|
|
525
|
+
const looseAfterWrappers = command.loose?.slice(command.argv.length - unwrapped.argv.length);
|
|
526
|
+
const argv = withOpaqueValues(unwrapped.argv, looseAfterWrappers);
|
|
366
527
|
const program = basename(argv[0] ?? "");
|
|
528
|
+
// Words a wrapper consumed (env assignments, options, durations) are gone from `argv`, but an
|
|
529
|
+
// unquoted substitution in them can still split into extra words and pick a different program.
|
|
530
|
+
if (command.argv.slice(0, command.argv.length - argv.length).some((w) => w.includes(SUBSTITUTION)))
|
|
531
|
+
return human(request, "unknown", "Command substitution inside a wrapper command requires human review");
|
|
532
|
+
if (argv.some((word) => word.includes(SUBSTITUTION)) &&
|
|
533
|
+
(argv[0]?.includes(SUBSTITUTION) || !SUBSTITUTION_OK.has(program)))
|
|
534
|
+
return human(request, "unknown", "Command substitution output is only accepted as an argument to echo, printf and similar");
|
|
367
535
|
const args = argv.slice(1);
|
|
536
|
+
// zsh expands `=name` to the path of a command, so `command =rm` runs rm.
|
|
537
|
+
if (argv[0]?.startsWith("=") && argv[0].length > 1)
|
|
538
|
+
return human(request, "unknown", "A command named with = requires human review");
|
|
539
|
+
// `[[` evaluates the operands of -eq, -lt and the like as arithmetic, including the value of a
|
|
540
|
+
// bare variable name, and `a[$(cmd)]` runs the command. Only plain integers are safe.
|
|
541
|
+
// `-v` and `-R` take a variable name, and bash evaluates a subscript in it (`a[$(cmd)]`).
|
|
542
|
+
if (program === "[[" || program === "test" || program === "[")
|
|
543
|
+
for (const [index, word] of args.entries())
|
|
544
|
+
if ((word === "-v" || word === "-R") &&
|
|
545
|
+
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(args[index + 1] ?? ""))
|
|
546
|
+
return human(request, "unknown", "A variable test with a subscript or computed name requires human review");
|
|
547
|
+
if (program === "[[")
|
|
548
|
+
for (const [index, word] of args.entries())
|
|
549
|
+
if (/^(?:-eq|-ne|-lt|-le|-gt|-ge)$/.test(word)) {
|
|
550
|
+
const operands = [args[index - 1], args[index + 1]];
|
|
551
|
+
if (operands.some((operand) => operand === undefined || !/^-?[0-9]+$/.test(operand)))
|
|
552
|
+
return human(request, "unknown", "A numeric test with a non-integer operand requires human review");
|
|
553
|
+
}
|
|
554
|
+
// zsh and bash evaluate arithmetic in some builtins' operands, and a name such as `a[$(cmd)]`
|
|
555
|
+
// runs the command. An unknown value is therefore only safe where nothing is evaluated.
|
|
556
|
+
if (argv.some((word) => word.includes(SUBSTITUTION))) {
|
|
557
|
+
if ((program === "test" || program === "[") &&
|
|
558
|
+
args.some((word) => /^(?:-eq|-ne|-lt|-le|-gt|-ge|-v|-R)$/.test(word)))
|
|
559
|
+
return human(request, "unknown", "An unknown value in a numeric or variable test requires human review");
|
|
560
|
+
if (program === "printf" && !printfOnlyPrintsStrings(args))
|
|
561
|
+
return human(request, "unknown", "An unknown value in a printf with numeric conversions requires human review");
|
|
562
|
+
}
|
|
368
563
|
const joined = argv.join(" ");
|
|
369
564
|
if (profile.humanPatterns.some((pattern) => pattern.test(joined)))
|
|
370
565
|
return human(request, "destructive", "Matches an organization human-approval pattern");
|
|
@@ -378,21 +573,51 @@ function simpleDecision(request, command, profile, depth, stageIndex, cwd) {
|
|
|
378
573
|
}
|
|
379
574
|
if (DESTRUCTIVE.has(program) || program.startsWith("mkfs"))
|
|
380
575
|
return human(request, "destructive", "Destructive operation requires human approval");
|
|
576
|
+
if (REMOTE_EXECUTORS.has(program))
|
|
577
|
+
return human(request, "unknown", "Remote or database command execution requires human review");
|
|
578
|
+
if (STATE_BUILTINS.has(program))
|
|
579
|
+
return human(request, "unknown", "A shell builtin that runs a string later or changes later commands requires human review");
|
|
580
|
+
// These print the whole environment, which can hold credentials.
|
|
581
|
+
if (program === "printenv" || program === "env" || (program === "set" && args.length === 0))
|
|
582
|
+
return human(request, "sensitive", "Printing the environment requires human review");
|
|
583
|
+
// `set -A name values` assigns an array, and `wait -p VAR` assigns a variable.
|
|
584
|
+
if (program === "wait" && args.some((word) => word.startsWith("-")))
|
|
585
|
+
return human(request, "unknown", "wait with options requires human review");
|
|
586
|
+
if (program === "set" && !args.every((word) => /^[-+][B-Za-z]+$|^pipefail$/.test(word)))
|
|
587
|
+
return human(request, "unknown", "Changing shell settings requires human review");
|
|
381
588
|
if (SHELLS.has(program) || INTERPRETERS.has(program)) {
|
|
382
589
|
if (stageIndex > 0)
|
|
383
590
|
return human(request, "unknown", "Piping into a shell or interpreter requires human review");
|
|
384
591
|
if (SHELLS.has(program)) {
|
|
385
592
|
const inline = args.indexOf("-c");
|
|
386
|
-
|
|
593
|
+
// A shell fed by a heredoc runs exactly that text, so judge it as a script.
|
|
594
|
+
const script = inline >= 0
|
|
595
|
+
? args[inline + 1]
|
|
596
|
+
: (args.length === 0 || args.every((arg) => arg === "-" || arg === "-s")) &&
|
|
597
|
+
command.stdin !== undefined
|
|
598
|
+
? command.stdin
|
|
599
|
+
: undefined;
|
|
387
600
|
if (script === undefined || depth >= 2)
|
|
388
601
|
return human(request, "unknown", "Shell invocation requires human review");
|
|
389
602
|
return commandDecision(request, script, profile, depth + 1, [cwd]);
|
|
390
603
|
}
|
|
391
|
-
|
|
604
|
+
// Inline code and a heredoc body are both literal text, so both can be read. Code piped in
|
|
605
|
+
// from elsewhere cannot.
|
|
606
|
+
const fromStdin = args.length === 0 || args[0] === "-";
|
|
607
|
+
const inlineAt = args.findIndex((arg) => ["-c", "-e", "-p", "--eval", "--print"].includes(arg));
|
|
608
|
+
const code = fromStdin ? command.stdin : inlineAt >= 0 ? args[inlineAt + 1] : undefined;
|
|
609
|
+
if (fromStdin && code === undefined)
|
|
392
610
|
return human(request, "unknown", "Interpreter reading code from standard input requires human review");
|
|
611
|
+
const unsafe = code === undefined ? undefined : interpreterCodeRefusal(code);
|
|
612
|
+
if (unsafe)
|
|
613
|
+
return human(request, "unknown", unsafe);
|
|
393
614
|
}
|
|
394
615
|
if (program === "xargs" &&
|
|
395
|
-
args.some((arg) =>
|
|
616
|
+
args.some((arg) =>
|
|
617
|
+
// zsh expands `=rm` to the path of rm.
|
|
618
|
+
DESTRUCTIVE.has(basename(arg.replace(/^=/, ""))) ||
|
|
619
|
+
SHELLS.has(basename(arg.replace(/^=/, ""))) ||
|
|
620
|
+
basename(arg.replace(/^=/, "")) === "git"))
|
|
396
621
|
return human(request, "destructive", "xargs into a destructive or shell command requires human review");
|
|
397
622
|
if (program === "find" &&
|
|
398
623
|
args.some((arg) => ["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fprint", "-fprintf", "-fls"].includes(arg)))
|
|
@@ -446,46 +671,5 @@ function simpleDecision(request, command, profile, depth, stageIndex, cwd) {
|
|
|
446
671
|
return cloudDecision(request, argv, profile);
|
|
447
672
|
return result(request, "impactful-write", "Permissive profile: no destructive effect detected", true);
|
|
448
673
|
}
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* Evaluates a compound command. `cd <literal>` is tracked: every later command is judged in each
|
|
452
|
-
* directory it could run in (before and after the `cd`, since `cd x || ...` may fail), so a command
|
|
453
|
-
* that only looks safe relative to the starting directory cannot hide behind a `cd`.
|
|
454
|
-
*/
|
|
455
|
-
export function commandDecision(request, command, profile, depth = 0, start = [request.cwd]) {
|
|
456
|
-
const pipelines = parseShell(command);
|
|
457
|
-
if (!pipelines)
|
|
458
|
-
return human(request, "unknown", "Unsupported shell syntax; expansions and subshells need human review");
|
|
459
|
-
let cwds = [...start];
|
|
460
|
-
let last;
|
|
461
|
-
for (const pipeline of pipelines)
|
|
462
|
-
for (const [index, simple] of pipeline.entries()) {
|
|
463
|
-
if (basename(simple.argv[0] ?? "") === "cd") {
|
|
464
|
-
const target = simple.argv.slice(1).filter((word) => word !== "--");
|
|
465
|
-
// A bare `cd` and `~` or `~/x` go to the home directory; `~user` and `-` stay with a human.
|
|
466
|
-
const raw = target.length === 0 ? "~" : target[0];
|
|
467
|
-
const word = raw === "~" || raw?.startsWith("~/") ? join(homedir(), raw.slice(1)) : raw;
|
|
468
|
-
if (target.length > 1 || !word || /^~|[*?[\]]/.test(word) || word.startsWith("-"))
|
|
469
|
-
return human(request, "unknown", "Directory change requires human review");
|
|
470
|
-
// The cd's own redirections, env assignments and organization patterns run first, in the
|
|
471
|
-
// directories it could start from.
|
|
472
|
-
for (const cwd of cwds) {
|
|
473
|
-
const own = simpleDecision(request, simple, profile, depth, index, cwd);
|
|
474
|
-
if (own.decision !== "approve")
|
|
475
|
-
return own;
|
|
476
|
-
}
|
|
477
|
-
cwds = [...new Set([...cwds, ...cwds.map((cwd) => resolve(cwd, word))])];
|
|
478
|
-
if (cwds.length > MAX_DIRECTORIES)
|
|
479
|
-
return human(request, "unknown", "Too many directory changes to verify");
|
|
480
|
-
last = result(request, "code-read", "Directory change", true);
|
|
481
|
-
continue;
|
|
482
|
-
}
|
|
483
|
-
for (const cwd of cwds) {
|
|
484
|
-
last = simpleDecision(request, simple, profile, depth, index, cwd);
|
|
485
|
-
if (last.decision !== "approve")
|
|
486
|
-
return last;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
return last ?? human(request, "unknown", "Empty command");
|
|
490
|
-
}
|
|
674
|
+
export { commandDecision };
|
|
491
675
|
//# sourceMappingURL=permissive.js.map
|
package/dist/script.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** Stands for the unknown output of an expansion inside a word. */
|
|
2
|
+
export declare const SUBSTITUTION = "\uE000";
|
|
3
|
+
export interface VarRef {
|
|
4
|
+
name: string;
|
|
5
|
+
quoted: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface UnknownPart {
|
|
8
|
+
unknown: true;
|
|
9
|
+
quoted: boolean;
|
|
10
|
+
/** Body of the command substitution it came from, when it did. */
|
|
11
|
+
inner?: string;
|
|
12
|
+
}
|
|
13
|
+
export type Part = string | VarRef | UnknownPart;
|
|
14
|
+
export interface Word {
|
|
15
|
+
parts: Part[];
|
|
16
|
+
/** Command substitution bodies (and process substitutions) inside this word, in order. */
|
|
17
|
+
subs: string[];
|
|
18
|
+
/** True when any part of the word was quoted or escaped. */
|
|
19
|
+
quoted: boolean;
|
|
20
|
+
/** Set when the word is `NAME=value`: the name, with `parts` holding only the value. */
|
|
21
|
+
assign?: string;
|
|
22
|
+
/** True when the word has an unquoted `*`, `?` or `[`: the shell may replace it by file names. */
|
|
23
|
+
glob?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface Redir {
|
|
26
|
+
fd: string;
|
|
27
|
+
op: ">" | ">>" | "<" | ">&" | "<<" | "<<<";
|
|
28
|
+
target?: Word;
|
|
29
|
+
/** Literal body of a heredoc. */
|
|
30
|
+
body?: string;
|
|
31
|
+
}
|
|
32
|
+
export interface Simple {
|
|
33
|
+
kind: "simple";
|
|
34
|
+
assigns: Array<{
|
|
35
|
+
name: string;
|
|
36
|
+
value: Word;
|
|
37
|
+
}>;
|
|
38
|
+
argv: Word[];
|
|
39
|
+
redirects: Redir[];
|
|
40
|
+
}
|
|
41
|
+
export interface Subshell {
|
|
42
|
+
kind: "subshell";
|
|
43
|
+
body: ListNode;
|
|
44
|
+
redirects: Redir[];
|
|
45
|
+
}
|
|
46
|
+
export interface Group {
|
|
47
|
+
kind: "group";
|
|
48
|
+
body: ListNode;
|
|
49
|
+
redirects: Redir[];
|
|
50
|
+
}
|
|
51
|
+
export interface ForNode {
|
|
52
|
+
kind: "for";
|
|
53
|
+
name: string;
|
|
54
|
+
words: Word[];
|
|
55
|
+
body: ListNode;
|
|
56
|
+
redirects: Redir[];
|
|
57
|
+
}
|
|
58
|
+
export interface WhileNode {
|
|
59
|
+
kind: "while";
|
|
60
|
+
cond: ListNode;
|
|
61
|
+
body: ListNode;
|
|
62
|
+
redirects: Redir[];
|
|
63
|
+
}
|
|
64
|
+
export interface IfNode {
|
|
65
|
+
kind: "if";
|
|
66
|
+
clauses: Array<{
|
|
67
|
+
cond: ListNode;
|
|
68
|
+
body: ListNode;
|
|
69
|
+
}>;
|
|
70
|
+
otherwise?: ListNode;
|
|
71
|
+
redirects: Redir[];
|
|
72
|
+
}
|
|
73
|
+
export interface CaseNode {
|
|
74
|
+
kind: "case";
|
|
75
|
+
word: Word;
|
|
76
|
+
branches: Array<{
|
|
77
|
+
patterns: Word[];
|
|
78
|
+
body: ListNode;
|
|
79
|
+
}>;
|
|
80
|
+
redirects: Redir[];
|
|
81
|
+
}
|
|
82
|
+
export type Command = Simple | Subshell | Group | ForNode | WhileNode | IfNode | CaseNode;
|
|
83
|
+
export interface PipelineNode {
|
|
84
|
+
stages: Command[];
|
|
85
|
+
negated: boolean;
|
|
86
|
+
}
|
|
87
|
+
export interface AndOr {
|
|
88
|
+
first: PipelineNode;
|
|
89
|
+
rest: Array<{
|
|
90
|
+
op: "&&" | "||";
|
|
91
|
+
node: PipelineNode;
|
|
92
|
+
}>;
|
|
93
|
+
}
|
|
94
|
+
export interface ListNode {
|
|
95
|
+
items: Array<{
|
|
96
|
+
node: AndOr;
|
|
97
|
+
background: boolean;
|
|
98
|
+
}>;
|
|
99
|
+
}
|
|
100
|
+
export declare function parseScript(command: string): ListNode | undefined;
|
|
101
|
+
//# sourceMappingURL=script.d.ts.map
|