@agent-corpus/policy-manager 0.1.0 → 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/README.md +29 -0
- package/dist/audit.d.ts +1 -0
- package/dist/audit.js +5 -2
- package/dist/cli.js +47 -3
- package/dist/evidence.d.ts +7 -0
- package/dist/evidence.js +33 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/merged.d.ts +14 -0
- package/dist/merged.js +96 -0
- package/dist/permissive.d.ts +23 -0
- package/dist/permissive.js +491 -0
- package/dist/policy.d.ts +5 -2
- package/dist/policy.js +22 -9
- package/dist/profile.d.ts +24 -0
- package/dist/profile.js +113 -0
- package/dist/replay.d.ts +18 -0
- package/dist/replay.js +81 -0
- package/dist/service.d.ts +7 -0
- package/dist/service.js +42 -3
- package/dist/session.d.ts +3 -1
- package/dist/session.js +6 -3
- package/dist/shell.d.ts +13 -0
- package/dist/shell.js +201 -0
- package/package.json +1 -1
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { basename, join, resolve } from "node:path";
|
|
3
|
+
import { branchEvidence, canonical, inside, isProtectedPath, pathEvidence, sensitive, } from "./evidence.js";
|
|
4
|
+
import { mergedEvidence, remoteTipMatches } from "./merged.js";
|
|
5
|
+
import { evaluateArgv, result } from "./policy.js";
|
|
6
|
+
import { parseShell } from "./shell.js";
|
|
7
|
+
/**
|
|
8
|
+
* Permissive profile: approve everything except effects that are destructive, irreversible,
|
|
9
|
+
* outward-facing, or touch credentials and protected configuration. The default for anything not
|
|
10
|
+
* recognised as dangerous is approval, so the deny side below is the policy. Each simple command
|
|
11
|
+
* of a compound command is judged separately and the first refusal decides.
|
|
12
|
+
*/
|
|
13
|
+
const SHELLS = new Set(["sh", "bash", "zsh", "dash", "ksh", "fish", "csh", "tcsh"]);
|
|
14
|
+
const INTERPRETERS = new Set(["python", "python3", "node", "perl", "ruby", "php", "deno", "bun"]);
|
|
15
|
+
const DESTRUCTIVE = new Set([
|
|
16
|
+
"rm",
|
|
17
|
+
"rmdir",
|
|
18
|
+
"unlink",
|
|
19
|
+
"shred",
|
|
20
|
+
"srm",
|
|
21
|
+
"sudo",
|
|
22
|
+
"su",
|
|
23
|
+
"doas",
|
|
24
|
+
"dd",
|
|
25
|
+
"mkfs",
|
|
26
|
+
"diskutil",
|
|
27
|
+
"fdisk",
|
|
28
|
+
"truncate",
|
|
29
|
+
"eval",
|
|
30
|
+
"source",
|
|
31
|
+
".",
|
|
32
|
+
"exec",
|
|
33
|
+
"crontab",
|
|
34
|
+
"launchctl",
|
|
35
|
+
"systemctl",
|
|
36
|
+
"pushd",
|
|
37
|
+
"popd",
|
|
38
|
+
]);
|
|
39
|
+
const WRITERS = new Set([
|
|
40
|
+
"mv",
|
|
41
|
+
"cp",
|
|
42
|
+
"mkdir",
|
|
43
|
+
"touch",
|
|
44
|
+
"tee",
|
|
45
|
+
"ln",
|
|
46
|
+
"install",
|
|
47
|
+
"rsync",
|
|
48
|
+
"chmod",
|
|
49
|
+
"chown",
|
|
50
|
+
]);
|
|
51
|
+
const WRAPPERS = new Set(["env", "nohup", "time", "command", "nice", "timeout", "xargs"]);
|
|
52
|
+
const BANNED_ENV = /^(?:LD_|DYLD_|GIT_|PYTHON|NODE_OPTIONS$|PATH$|HOME$|BASH_ENV$|ENV$|SHELL$|PAGER$|EDITOR$|GH_TOKEN$|GITHUB_TOKEN$)/;
|
|
53
|
+
const FORCE_PUSH = ["--force", "--force-with-lease", "--force-if-includes", "--mirror", "--prune"];
|
|
54
|
+
function human(request, category, reason) {
|
|
55
|
+
return result(request, category, reason);
|
|
56
|
+
}
|
|
57
|
+
export function permissiveRead(cwd, input) {
|
|
58
|
+
try {
|
|
59
|
+
const lexical = resolve(cwd, input);
|
|
60
|
+
const path = canonical(lexical);
|
|
61
|
+
if (sensitive(path) || sensitive(lexical))
|
|
62
|
+
return {
|
|
63
|
+
allowed: false,
|
|
64
|
+
category: "sensitive",
|
|
65
|
+
reason: "Protected path requires human approval",
|
|
66
|
+
};
|
|
67
|
+
return {
|
|
68
|
+
allowed: true,
|
|
69
|
+
category: "code-read",
|
|
70
|
+
reason: "Read of a non-sensitive path",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return {
|
|
75
|
+
allowed: false,
|
|
76
|
+
category: "code-read",
|
|
77
|
+
reason: "Cannot verify path evidence",
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export function permissiveWrite(cwd, input, profile) {
|
|
82
|
+
try {
|
|
83
|
+
const lexical = resolve(cwd, input);
|
|
84
|
+
const path = canonical(lexical);
|
|
85
|
+
if (sensitive(path) || sensitive(lexical) || isProtectedPath(path) || isProtectedPath(lexical))
|
|
86
|
+
return {
|
|
87
|
+
allowed: false,
|
|
88
|
+
category: "sensitive",
|
|
89
|
+
reason: "Protected path requires human approval",
|
|
90
|
+
};
|
|
91
|
+
// Never let a scratch root reach the policy service's own session directories.
|
|
92
|
+
if (!path.split(/[\\/]/).some((part) => part.startsWith("acp-")) &&
|
|
93
|
+
profile.scratchRoots.some((root) => inside(root, path)))
|
|
94
|
+
return {
|
|
95
|
+
allowed: true,
|
|
96
|
+
category: "worktree-write",
|
|
97
|
+
reason: "Write within a scratch directory",
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
/* fall through to the repository check, which fails closed */
|
|
102
|
+
}
|
|
103
|
+
return pathEvidence(cwd, input, true);
|
|
104
|
+
}
|
|
105
|
+
function unwrap(argv) {
|
|
106
|
+
let current = argv;
|
|
107
|
+
const env = [];
|
|
108
|
+
for (let depth = 0; depth < 4; depth++) {
|
|
109
|
+
const program = basename(current[0] ?? "");
|
|
110
|
+
if (!WRAPPERS.has(program) || program === "xargs")
|
|
111
|
+
return { argv: current, env };
|
|
112
|
+
let rest = current.slice(1);
|
|
113
|
+
while (rest[0]?.startsWith("-") ||
|
|
114
|
+
// `env` treats any word with a non-empty name before `=` as an assignment (`A+=1`, `A[0]=1`).
|
|
115
|
+
(program === "env" && /^[^=-][^=]*=/.test(rest[0] ?? "")) ||
|
|
116
|
+
(program === "timeout" && /^[0-9.]+[smhd]?$/.test(rest[0] ?? "")) ||
|
|
117
|
+
(program === "nice" && /^[0-9-]+$/.test(rest[0] ?? ""))) {
|
|
118
|
+
const word = rest[0] ?? "";
|
|
119
|
+
// env options can change the program searched for, the directory or the environment.
|
|
120
|
+
if (program === "env" && word.startsWith("-") && word !== "-i" && word !== "--")
|
|
121
|
+
return { argv: current, env, refusal: "env options require human review" };
|
|
122
|
+
if (program === "env" && !word.startsWith("-"))
|
|
123
|
+
env.push(word.slice(0, word.indexOf("=")));
|
|
124
|
+
rest = rest.slice(1);
|
|
125
|
+
}
|
|
126
|
+
if (!rest.length)
|
|
127
|
+
return { argv: current, env };
|
|
128
|
+
current = rest;
|
|
129
|
+
}
|
|
130
|
+
return { argv: current, env };
|
|
131
|
+
}
|
|
132
|
+
/** True when `flag` is `name` or a unique-looking abbreviation Git's option parser would accept. */
|
|
133
|
+
function abbreviates(flag, name) {
|
|
134
|
+
if (!flag.startsWith("--") || !name.startsWith("--"))
|
|
135
|
+
return flag === name;
|
|
136
|
+
const given = flag.split("=")[0];
|
|
137
|
+
return given.length > 2 && name.startsWith(given);
|
|
138
|
+
}
|
|
139
|
+
const SAFE_CONFIG_KEYS = /^(?:core\.(?:bare|filemode|autocrlf|longpaths|ignorecase|precomposeunicode|untrackedcache)|user\.(?:name|email)|push\.(?:default|autosetupremote|followtags)|pull\.(?:rebase|ff)|fetch\.prune|merge\.ff|rebase\.autostash|rerere\.enabled|init\.defaultbranch|branch\.[A-Za-z0-9/_.-]+\.(?:remote|merge|rebase)|advice\.[a-z]+|color\.[a-z.]+)$/i;
|
|
140
|
+
/** Only local, non-executable settings; hooks, aliases, credential helpers and URLs need a human. */
|
|
141
|
+
function safeConfigWrite(rest) {
|
|
142
|
+
const flags = rest.filter((word) => word.startsWith("-"));
|
|
143
|
+
if (flags.some((flag) => !["--local", "--worktree", "--unset", "--bool", "--int"].includes(flag)))
|
|
144
|
+
return false;
|
|
145
|
+
const words = rest.filter((word) => !word.startsWith("-"));
|
|
146
|
+
return words.length >= 1 && words.length <= 2 && SAFE_CONFIG_KEYS.test(words[0] ?? "");
|
|
147
|
+
}
|
|
148
|
+
function gitDecision(request, argv, cwdForGit) {
|
|
149
|
+
const args = argv.slice(1);
|
|
150
|
+
let index = 0;
|
|
151
|
+
let cwd = cwdForGit;
|
|
152
|
+
while (index < args.length && args[index]?.startsWith("-")) {
|
|
153
|
+
if (args[index] === "-C" && args[index + 1]) {
|
|
154
|
+
cwd = resolve(cwd, args[index + 1]);
|
|
155
|
+
index += 2;
|
|
156
|
+
}
|
|
157
|
+
else if (args[index] === "--no-pager" || args[index] === "-P")
|
|
158
|
+
index++;
|
|
159
|
+
else
|
|
160
|
+
return human(request, "unknown", "Git global option requires human review");
|
|
161
|
+
}
|
|
162
|
+
const sub = args[index];
|
|
163
|
+
const rest = args.slice(index + 1);
|
|
164
|
+
const flags = rest.filter((word) => word.startsWith("-"));
|
|
165
|
+
const positional = rest.filter((word) => !word.startsWith("-"));
|
|
166
|
+
const has = (...names) => flags.some((flag) => names.some((name) => abbreviates(flag, name)));
|
|
167
|
+
const cluster = (letter) => flags.some((flag) => /^-[a-zA-Z]+$/.test(flag) && flag.includes(letter));
|
|
168
|
+
const ok = (reason = "Permissive profile: Git operation has no destructive effect") => result(request, "impactful-write", reason, true);
|
|
169
|
+
if (!sub)
|
|
170
|
+
return ok();
|
|
171
|
+
if (sub === "push")
|
|
172
|
+
return gitPush(request, cwd, rest);
|
|
173
|
+
if (sub === "branch" && (has("-d", "-D", "--delete") || cluster("d") || cluster("D"))) {
|
|
174
|
+
if (has("-r", "--remotes"))
|
|
175
|
+
return ok();
|
|
176
|
+
const unmerged = positional.filter((name) => !mergedEvidence(cwd, `refs/heads/${name}`).merged);
|
|
177
|
+
return unmerged.length
|
|
178
|
+
? human(request, "destructive", branchEvidence(cwd, unmerged[0] ?? ""))
|
|
179
|
+
: ok("Branch is already merged into origin's default branch");
|
|
180
|
+
}
|
|
181
|
+
if (sub === "branch" && (has("-f", "--force") || cluster("f") || has("-M")))
|
|
182
|
+
return human(request, "destructive", "Forced branch move requires human approval");
|
|
183
|
+
if (sub === "reset" && has("--hard"))
|
|
184
|
+
return human(request, "destructive", "git reset --hard discards work");
|
|
185
|
+
if (sub === "clean" && !has("-n", "--dry-run") && !cluster("n"))
|
|
186
|
+
return human(request, "destructive", "git clean deletes untracked files");
|
|
187
|
+
if (sub === "checkout" &&
|
|
188
|
+
(has("-f", "--force", "-B") ||
|
|
189
|
+
rest.includes("--") ||
|
|
190
|
+
positional.includes(".") ||
|
|
191
|
+
positional.includes(":/")))
|
|
192
|
+
return human(request, "destructive", "git checkout would discard working-tree changes");
|
|
193
|
+
if (sub === "restore" && (!has("--staged", "-S") || has("--worktree", "-W") || cluster("W")))
|
|
194
|
+
return human(request, "destructive", "git restore would discard working-tree changes");
|
|
195
|
+
if (sub === "switch" && has("-f", "--force", "--discard-changes"))
|
|
196
|
+
return human(request, "destructive", "git switch would discard working-tree changes");
|
|
197
|
+
if (sub === "stash" && ["drop", "clear"].includes(positional[0] ?? ""))
|
|
198
|
+
return human(request, "destructive", "Dropping stashes discards work");
|
|
199
|
+
if (sub === "reflog" && ["expire", "delete"].includes(positional[0] ?? ""))
|
|
200
|
+
return human(request, "destructive", "Reflog expiry removes recovery points");
|
|
201
|
+
if (["gc", "prune", "filter-branch", "filter-repo", "replace"].includes(sub) ||
|
|
202
|
+
(sub === "update-ref" && has("-d")))
|
|
203
|
+
return human(request, "destructive", "Repository history or object pruning requires human approval");
|
|
204
|
+
if (sub === "worktree" &&
|
|
205
|
+
["remove", "prune"].includes(positional[0] ?? "") &&
|
|
206
|
+
(has("-f", "--force") || positional[0] === "prune"))
|
|
207
|
+
return human(request, "destructive", "Forced worktree removal requires human approval");
|
|
208
|
+
if (sub === "config" &&
|
|
209
|
+
!has("--get", "--get-all", "--get-regexp", "--list", "-l", "--show-origin") &&
|
|
210
|
+
!safeConfigWrite(rest))
|
|
211
|
+
return human(request, "sensitive", "Git configuration can install hooks or credential helpers");
|
|
212
|
+
if (sub === "remote" &&
|
|
213
|
+
["set-url", "remove", "rm", "rename", "set-head"].includes(positional[0] ?? ""))
|
|
214
|
+
return human(request, "impactful-write", "Changing remotes requires human approval");
|
|
215
|
+
return ok();
|
|
216
|
+
}
|
|
217
|
+
function gitPush(request, cwd, rest) {
|
|
218
|
+
const flags = rest.filter((word) => word.startsWith("-"));
|
|
219
|
+
const positional = rest.filter((word) => !word.startsWith("-"));
|
|
220
|
+
const optionValues = new Set();
|
|
221
|
+
rest.forEach((word, i) => {
|
|
222
|
+
if ((word === "-o" ||
|
|
223
|
+
word === "--push-option" ||
|
|
224
|
+
word === "--receive-pack" ||
|
|
225
|
+
word === "--exec" ||
|
|
226
|
+
word === "--repo") &&
|
|
227
|
+
rest[i + 1])
|
|
228
|
+
optionValues.add(rest[i + 1]);
|
|
229
|
+
});
|
|
230
|
+
const refs = positional.filter((word) => !optionValues.has(word));
|
|
231
|
+
const remote = refs[0];
|
|
232
|
+
const refspecs = refs.slice(1);
|
|
233
|
+
if (flags.some((flag) => FORCE_PUSH.some((name) => abbreviates(flag, name)) ||
|
|
234
|
+
(/^-[a-zA-Z]+$/.test(flag) && flag.includes("f"))))
|
|
235
|
+
return human(request, "destructive", "Force or mirror pushes overwrite remote history");
|
|
236
|
+
if (flags.some((flag) => abbreviates(flag, "--receive-pack") || abbreviates(flag, "--exec")))
|
|
237
|
+
return human(request, "unknown", "Custom push executables require human review");
|
|
238
|
+
const deleting = flags.some((flag) => abbreviates(flag, "--delete")) ||
|
|
239
|
+
flags.some((flag) => /^-[a-zA-Z]*d[a-zA-Z]*$/.test(flag));
|
|
240
|
+
const targets = [];
|
|
241
|
+
for (const spec of refspecs) {
|
|
242
|
+
if (spec.startsWith("+"))
|
|
243
|
+
return human(request, "destructive", "Forced refspecs overwrite remote history");
|
|
244
|
+
if (spec.startsWith(":"))
|
|
245
|
+
targets.push(spec.slice(1));
|
|
246
|
+
else if (deleting)
|
|
247
|
+
targets.push(spec);
|
|
248
|
+
}
|
|
249
|
+
if (!targets.length)
|
|
250
|
+
return result(request, "impactful-write", "Permissive profile: push without deletion", true);
|
|
251
|
+
if (remote !== "origin")
|
|
252
|
+
return human(request, "destructive", "Remote branch deletion is only verified for origin");
|
|
253
|
+
for (const target of targets) {
|
|
254
|
+
const name = target.replace(/^refs\/heads\//, "");
|
|
255
|
+
if (target.startsWith("refs/tags/") || !/^[a-zA-Z0-9][a-zA-Z0-9/_.-]*$/.test(name))
|
|
256
|
+
return human(request, "destructive", "Only branch deletions with verified merge state are automatic");
|
|
257
|
+
const evidence = mergedEvidence(cwd, `refs/remotes/origin/${name}`);
|
|
258
|
+
if (!evidence.merged)
|
|
259
|
+
return human(request, "destructive", `Remote branch deletion may lose work: ${evidence.how}`);
|
|
260
|
+
if (!remoteTipMatches(cwd, name))
|
|
261
|
+
return human(request, "destructive", "Remote branch has commits that are not fetched; fetch and retry");
|
|
262
|
+
}
|
|
263
|
+
return result(request, "impactful-write", "Deleting remote branch already merged into origin's default branch", true);
|
|
264
|
+
}
|
|
265
|
+
function ghDecision(request, argv) {
|
|
266
|
+
const text = argv.join(" ");
|
|
267
|
+
if (argv[1] === "api") {
|
|
268
|
+
const method = /(?:^|\s)(?:-X|--method)[=\s]?DELETE\b/i.test(text) || /\s-XDELETE\b/i.test(text);
|
|
269
|
+
if (method || /\/(?:protection|rulesets|keys|secrets|hooks)\b/.test(text))
|
|
270
|
+
return human(request, "destructive", "Destructive or security-sensitive GitHub API call");
|
|
271
|
+
}
|
|
272
|
+
if (argv.includes("delete") ||
|
|
273
|
+
(argv[1] === "pr" && argv[2] === "merge" && argv.includes("--admin")))
|
|
274
|
+
return human(request, "destructive", "Destructive GitHub operation requires human approval");
|
|
275
|
+
if (argv[1] === "secret" ||
|
|
276
|
+
(argv[1] === "auth" && ["token", "refresh", "logout"].includes(argv[2] ?? "")))
|
|
277
|
+
return human(request, "sensitive", "Credential or secret handling requires human approval");
|
|
278
|
+
return undefined;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Read-only operations by verb, for services outside the exact catalog. The selectors already
|
|
282
|
+
* passed the credential screen in `evaluateArgv`; endpoint overrides still need a human.
|
|
283
|
+
*/
|
|
284
|
+
const AWS_METADATA_GETTERS = new Set([
|
|
285
|
+
"get-caller-identity",
|
|
286
|
+
"get-account-summary",
|
|
287
|
+
"get-role",
|
|
288
|
+
"get-user",
|
|
289
|
+
"get-group",
|
|
290
|
+
"get-policy",
|
|
291
|
+
"get-policy-version",
|
|
292
|
+
"get-function-configuration",
|
|
293
|
+
"get-bucket-location",
|
|
294
|
+
"get-bucket-versioning",
|
|
295
|
+
"get-bucket-tagging",
|
|
296
|
+
"get-bucket-policy-status",
|
|
297
|
+
"get-rest-apis",
|
|
298
|
+
"get-resources",
|
|
299
|
+
"get-hosted-zone",
|
|
300
|
+
"get-metric-statistics",
|
|
301
|
+
"get-log-events",
|
|
302
|
+
]);
|
|
303
|
+
function readVerb(argv) {
|
|
304
|
+
const [tool] = argv;
|
|
305
|
+
const firstOption = argv.findIndex((arg) => arg.startsWith("-"));
|
|
306
|
+
const operation = (firstOption < 0 ? argv : argv.slice(0, firstOption)).slice(1);
|
|
307
|
+
const verb = operation.at(-1) ?? "";
|
|
308
|
+
if (tool === "gcloud" && /token|secret|password|credential|key/i.test(operation.join(" ")))
|
|
309
|
+
return false;
|
|
310
|
+
if (operation.length < 2 || !operation.every((word) => /^[a-z][a-z0-9-]*$/.test(word)))
|
|
311
|
+
return false;
|
|
312
|
+
if (argv.some((arg) => /^--(?:endpoint|endpoint-url|api-endpoint|debug|profile-file)/.test(arg)))
|
|
313
|
+
return false;
|
|
314
|
+
if (/token|secret|password|credential|key/i.test(verb))
|
|
315
|
+
return false;
|
|
316
|
+
if (tool === "az")
|
|
317
|
+
return /^(?:list|show|exists|list-[a-z-]+|show-[a-z-]+)$/.test(verb);
|
|
318
|
+
if (tool === "aws") {
|
|
319
|
+
// `get-*` is not safe as a family: it includes secret, password, token and access-detail
|
|
320
|
+
// retrieval. Only `describe-`/`list-` and an explicit set of metadata getters are automatic.
|
|
321
|
+
if (argv.some((arg) => /^--(?:no-)?with-decryption/.test(arg)))
|
|
322
|
+
return false;
|
|
323
|
+
return /^(?:describe|list)-[a-z-]+$/.test(verb) || AWS_METADATA_GETTERS.has(verb);
|
|
324
|
+
}
|
|
325
|
+
if (tool === "gcloud") {
|
|
326
|
+
// gcloud passes the resource as a positional after the verb: `run services describe svc`.
|
|
327
|
+
const at = operation.findIndex((word) => /^(?:list|describe)$/.test(word));
|
|
328
|
+
return at >= 0 && operation.length - at <= 2;
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
function cloudDecision(request, argv, profile) {
|
|
333
|
+
const strict = evaluateArgv(request, argv);
|
|
334
|
+
if (strict.decision === "approve" || strict.category === "sensitive")
|
|
335
|
+
return strict;
|
|
336
|
+
if (argv.some((arg) => /^(delete|destroy|purge|remove|terminate|deregister)/.test(arg)))
|
|
337
|
+
return human(request, "destructive", "Cloud deletion requires human approval");
|
|
338
|
+
if (readVerb(argv))
|
|
339
|
+
return result(request, "cloud-read", "Permissive profile: cloud read-only verb", true);
|
|
340
|
+
if (profile.cloudWrites === "allow" && strict.category === "impactful-write")
|
|
341
|
+
return result(request, "impactful-write", "Permissive profile: non-destructive cloud change", true);
|
|
342
|
+
return strict;
|
|
343
|
+
}
|
|
344
|
+
function simpleDecision(request, command, profile, depth, stageIndex, cwd) {
|
|
345
|
+
for (const [name] of command.env)
|
|
346
|
+
if (BANNED_ENV.test(name))
|
|
347
|
+
return human(request, "unknown", "Environment override requires human review");
|
|
348
|
+
for (const redirect of command.redirects) {
|
|
349
|
+
// `>& word` duplicates a descriptor only when word is a number or `-`; otherwise it names a file.
|
|
350
|
+
if (redirect.target === "/dev/null" ||
|
|
351
|
+
(redirect.op === ">&" && /^(?:[0-9]+|-)$/.test(redirect.target)))
|
|
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);
|
|
358
|
+
}
|
|
359
|
+
const unwrapped = unwrap(command.argv);
|
|
360
|
+
if (unwrapped.refusal)
|
|
361
|
+
return human(request, "unknown", unwrapped.refusal);
|
|
362
|
+
for (const name of unwrapped.env)
|
|
363
|
+
if (BANNED_ENV.test(name))
|
|
364
|
+
return human(request, "unknown", "Environment override requires human review");
|
|
365
|
+
const argv = unwrapped.argv;
|
|
366
|
+
const program = basename(argv[0] ?? "");
|
|
367
|
+
const args = argv.slice(1);
|
|
368
|
+
const joined = argv.join(" ");
|
|
369
|
+
if (profile.humanPatterns.some((pattern) => pattern.test(joined)))
|
|
370
|
+
return human(request, "destructive", "Matches an organization human-approval pattern");
|
|
371
|
+
// Anything that looks like a credential or protected-config path stays with a human.
|
|
372
|
+
for (const word of argv.slice(1)) {
|
|
373
|
+
const candidate = word.includes("=") ? word.slice(word.indexOf("=") + 1) : word;
|
|
374
|
+
if (candidate.includes("/") || candidate.startsWith(".")) {
|
|
375
|
+
if (sensitive(candidate))
|
|
376
|
+
return human(request, "sensitive", "Protected path requires human approval");
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (DESTRUCTIVE.has(program) || program.startsWith("mkfs"))
|
|
380
|
+
return human(request, "destructive", "Destructive operation requires human approval");
|
|
381
|
+
if (SHELLS.has(program) || INTERPRETERS.has(program)) {
|
|
382
|
+
if (stageIndex > 0)
|
|
383
|
+
return human(request, "unknown", "Piping into a shell or interpreter requires human review");
|
|
384
|
+
if (SHELLS.has(program)) {
|
|
385
|
+
const inline = args.indexOf("-c");
|
|
386
|
+
const script = inline >= 0 ? args[inline + 1] : undefined;
|
|
387
|
+
if (script === undefined || depth >= 2)
|
|
388
|
+
return human(request, "unknown", "Shell invocation requires human review");
|
|
389
|
+
return commandDecision(request, script, profile, depth + 1, [cwd]);
|
|
390
|
+
}
|
|
391
|
+
if (args.length === 0 || args[0] === "-")
|
|
392
|
+
return human(request, "unknown", "Interpreter reading code from standard input requires human review");
|
|
393
|
+
}
|
|
394
|
+
if (program === "xargs" &&
|
|
395
|
+
args.some((arg) => DESTRUCTIVE.has(basename(arg)) || SHELLS.has(basename(arg)) || basename(arg) === "git"))
|
|
396
|
+
return human(request, "destructive", "xargs into a destructive or shell command requires human review");
|
|
397
|
+
if (program === "find" &&
|
|
398
|
+
args.some((arg) => ["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fprint", "-fprintf", "-fls"].includes(arg)))
|
|
399
|
+
return human(request, "destructive", "find with actions requires human review");
|
|
400
|
+
if (["chmod", "chown"].includes(program) && args.some((arg) => /^-[a-zA-Z]*R/.test(arg)))
|
|
401
|
+
return human(request, "destructive", "Recursive permission changes require human approval");
|
|
402
|
+
if ((program === "sed" &&
|
|
403
|
+
args.some((arg) => /^-[a-zA-Z]*i/.test(arg) || arg.startsWith("--in-place"))) ||
|
|
404
|
+
(program === "perl" && args.some((arg) => /^-[a-zA-Z]*i/.test(arg)))) {
|
|
405
|
+
for (const file of args.filter((arg) => !arg.startsWith("-")).slice(1)) {
|
|
406
|
+
const check = permissiveWrite(cwd, file, profile);
|
|
407
|
+
if (!check.allowed)
|
|
408
|
+
return human(request, check.category, check.reason);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else if (WRITERS.has(program)) {
|
|
412
|
+
for (const file of args.filter((arg) => !arg.startsWith("-") && !/^[0-9]+$/.test(arg))) {
|
|
413
|
+
const check = permissiveWrite(cwd, file, profile);
|
|
414
|
+
if (!check.allowed)
|
|
415
|
+
return human(request, check.category, check.reason);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (program === "terraform" || program === "pulumi" || program === "tofu")
|
|
419
|
+
if (args.includes("destroy") ||
|
|
420
|
+
args.includes("-destroy") ||
|
|
421
|
+
(args[0] === "state" && ["rm", "push"].includes(args[1] ?? "")))
|
|
422
|
+
return human(request, "destructive", "Infrastructure destruction requires human approval");
|
|
423
|
+
if (program === "kubectl" && ["delete", "drain", "replace", "cordon"].includes(args[0] ?? ""))
|
|
424
|
+
return human(request, "destructive", "Cluster deletion requires human approval");
|
|
425
|
+
if (program === "docker" &&
|
|
426
|
+
(["rm", "rmi", "kill", "prune"].includes(args[0] ?? "") ||
|
|
427
|
+
args.includes("prune") ||
|
|
428
|
+
(args[0] === "volume" && args[1] === "rm")))
|
|
429
|
+
return human(request, "destructive", "Container deletion requires human approval");
|
|
430
|
+
if (["npm", "pnpm", "yarn"].includes(program) &&
|
|
431
|
+
["publish", "unpublish", "deprecate", "owner", "token"].includes(args[0] ?? ""))
|
|
432
|
+
return human(request, "destructive", "Publishing or registry changes require human approval");
|
|
433
|
+
if (program === "dotnet" && args[0] === "nuget" && ["push", "delete"].includes(args[1] ?? ""))
|
|
434
|
+
return human(request, "destructive", "Package publishing requires human approval");
|
|
435
|
+
if ((program === "curl" || program === "wget") &&
|
|
436
|
+
/(?:-X|--request)[=\s]?(?:DELETE|PUT)\b/i.test(joined))
|
|
437
|
+
return human(request, "destructive", "Destructive HTTP verbs require human approval");
|
|
438
|
+
if (program === "git")
|
|
439
|
+
return gitDecision(request, argv, cwd);
|
|
440
|
+
if (program === "gh") {
|
|
441
|
+
const gh = ghDecision(request, argv);
|
|
442
|
+
if (gh)
|
|
443
|
+
return gh;
|
|
444
|
+
}
|
|
445
|
+
if (["az", "aws", "gcloud"].includes(program))
|
|
446
|
+
return cloudDecision(request, argv, profile);
|
|
447
|
+
return result(request, "impactful-write", "Permissive profile: no destructive effect detected", true);
|
|
448
|
+
}
|
|
449
|
+
const MAX_DIRECTORIES = 8;
|
|
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
|
+
}
|
|
491
|
+
//# sourceMappingURL=permissive.js.map
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type Profile } from "./profile.js";
|
|
2
|
+
import type { Category, Classification, Decision, Request } from "./types.js";
|
|
2
3
|
export declare function hash(value: unknown): string;
|
|
3
4
|
export declare function words(command: string): string[] | undefined;
|
|
4
|
-
export declare function
|
|
5
|
+
export declare function result(request: Request, category: Category, reason: string, approve?: boolean): Decision;
|
|
6
|
+
export declare function evaluate(request: Request, ai?: Classification, profile?: Profile): Decision;
|
|
7
|
+
export declare function evaluateArgv(request: Request, argv: string[]): Decision;
|
|
5
8
|
//# sourceMappingURL=policy.d.ts.map
|
package/dist/policy.js
CHANGED
|
@@ -2,6 +2,8 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { existsSync } from "node:fs";
|
|
3
3
|
import { resolve } from "node:path";
|
|
4
4
|
import { branchEvidence, canonical, git, inside, pathEvidence } from "./evidence.js";
|
|
5
|
+
import { commandDecision, permissiveRead, permissiveWrite } from "./permissive.js";
|
|
6
|
+
import { strictProfile } from "./profile.js";
|
|
5
7
|
export function hash(value) {
|
|
6
8
|
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
7
9
|
}
|
|
@@ -160,7 +162,7 @@ function cloudReadCandidate(argv) {
|
|
|
160
162
|
]);
|
|
161
163
|
return metadataOperations.has(operation.join(" "));
|
|
162
164
|
}
|
|
163
|
-
function result(request, category, reason, approve = false) {
|
|
165
|
+
export function result(request, category, reason, approve = false) {
|
|
164
166
|
return {
|
|
165
167
|
decision: approve ? "approve" : "human",
|
|
166
168
|
category,
|
|
@@ -170,8 +172,8 @@ function result(request, category, reason, approve = false) {
|
|
|
170
172
|
cached: false,
|
|
171
173
|
};
|
|
172
174
|
}
|
|
173
|
-
export function evaluate(request, ai) {
|
|
174
|
-
const decision = evaluateCore(request);
|
|
175
|
+
export function evaluate(request, ai, profile = strictProfile) {
|
|
176
|
+
const decision = evaluateCore(request, profile);
|
|
175
177
|
if (ai) {
|
|
176
178
|
// Self-reported confidence is not a probability or a substitute for verified effects.
|
|
177
179
|
if (![
|
|
@@ -220,7 +222,7 @@ export function evaluate(request, ai) {
|
|
|
220
222
|
}
|
|
221
223
|
return decision;
|
|
222
224
|
}
|
|
223
|
-
function evaluateCore(request) {
|
|
225
|
+
function evaluateCore(request, profile) {
|
|
224
226
|
if (!request.sessionId ||
|
|
225
227
|
!request.cwd ||
|
|
226
228
|
!request.tool ||
|
|
@@ -247,26 +249,37 @@ function evaluateCore(request) {
|
|
|
247
249
|
}
|
|
248
250
|
if (!paths.length)
|
|
249
251
|
return result(request, "unknown", "Patch has no verified target paths");
|
|
250
|
-
const checks = paths.map((path) =>
|
|
252
|
+
const checks = paths.map((path) => profile.mode === "permissive"
|
|
253
|
+
? permissiveWrite(request.cwd, path, profile)
|
|
254
|
+
: pathEvidence(request.cwd, path, true));
|
|
251
255
|
const denied = checks.find((check) => !check.allowed);
|
|
252
256
|
return result(request, denied?.category ?? "worktree-write", denied?.reason ?? "Verified patch targets within feature worktrees", !denied);
|
|
253
257
|
}
|
|
254
258
|
if (["Read", "view", "read_file", "Edit", "Write", "edit", "create", "write_file"].includes(request.tool) &&
|
|
255
259
|
typeof file === "string") {
|
|
256
260
|
const write = !["Read", "view", "read_file"].includes(request.tool);
|
|
257
|
-
const evidence =
|
|
261
|
+
const evidence = profile.mode === "permissive"
|
|
262
|
+
? write
|
|
263
|
+
? permissiveWrite(request.cwd, file, profile)
|
|
264
|
+
: permissiveRead(request.cwd, file)
|
|
265
|
+
: pathEvidence(request.cwd, file, write);
|
|
258
266
|
return result(request, evidence.category, evidence.reason, evidence.allowed);
|
|
259
267
|
}
|
|
260
268
|
if (!["Bash", "bash", "powershell", "exec_command", "shell"].includes(request.tool))
|
|
261
|
-
return result(request, "unknown", "Unknown tool or file operation;
|
|
269
|
+
return result(request, "unknown", "Unknown tool or file operation; no AI classification was performed for this request, so a human decision is required");
|
|
262
270
|
if (request.tool === "powershell")
|
|
263
271
|
return result(request, "unknown", "PowerShell semantics require human review");
|
|
264
272
|
if (input.env !== undefined || input.shell !== undefined || input.stdin !== undefined)
|
|
265
273
|
return result(request, "unknown", "Custom execution environment requires human review");
|
|
266
274
|
const command = input.command ?? input.cmd;
|
|
275
|
+
if (profile.mode === "permissive" && typeof command === "string")
|
|
276
|
+
return commandDecision(request, command, profile);
|
|
267
277
|
const argv = typeof command === "string" ? words(command) : undefined;
|
|
268
278
|
if (!argv)
|
|
269
|
-
return result(request, "unknown", "Unsupported shell syntax;
|
|
279
|
+
return result(request, "unknown", "Unsupported shell syntax; no AI classification was performed for this request, so a human decision is required");
|
|
280
|
+
return evaluateArgv(request, argv);
|
|
281
|
+
}
|
|
282
|
+
export function evaluateArgv(request, argv) {
|
|
270
283
|
const [program, ...args] = argv;
|
|
271
284
|
let gitCwd = request.cwd;
|
|
272
285
|
if (program === "git" && args[0] === "-C" && args[1]) {
|
|
@@ -339,6 +352,6 @@ function evaluateCore(request) {
|
|
|
339
352
|
const denied = checks.find((check) => !check.allowed);
|
|
340
353
|
return result(request, denied?.category ?? "code-read", denied?.reason ?? "Verified file reads", !denied);
|
|
341
354
|
}
|
|
342
|
-
return result(request, "unknown", "Unknown executable or effects;
|
|
355
|
+
return result(request, "unknown", "Unknown executable or effects; no AI classification was performed for this request, so a human decision is required");
|
|
343
356
|
}
|
|
344
357
|
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A profile is policy *data*. The engine's checks (protected paths, secret retrieval, verified
|
|
3
|
+
* effects) are code and cannot be relaxed by a profile; a profile only chooses how much of the
|
|
4
|
+
* remaining space is approved automatically. Without a profile the engine is strict.
|
|
5
|
+
*/
|
|
6
|
+
export interface Profile {
|
|
7
|
+
mode: "strict" | "permissive";
|
|
8
|
+
/** Cloud operations outside the read catalog: `human` (default) or `allow` non-destructive ones. */
|
|
9
|
+
cloudWrites: "human" | "allow";
|
|
10
|
+
/** Extra simple commands that always need a human, matched against the joined argv. */
|
|
11
|
+
humanPatterns: RegExp[];
|
|
12
|
+
/** Absolute directories where permissive mode may write besides linked feature worktrees. */
|
|
13
|
+
scratchRoots: string[];
|
|
14
|
+
/** SHA-256 of the file the profile came from, for the audit log. */
|
|
15
|
+
digest?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare const strictProfile: Profile;
|
|
18
|
+
export declare function defaultProfilePath(): string;
|
|
19
|
+
export declare class ProfileError extends Error {
|
|
20
|
+
}
|
|
21
|
+
export declare function parseProfile(text: string): Profile;
|
|
22
|
+
/** Loads the installed profile. A missing file means strict; an invalid one is an error. */
|
|
23
|
+
export declare function loadProfile(path?: string): Profile;
|
|
24
|
+
//# sourceMappingURL=profile.d.ts.map
|