@agent-corpus/policy-manager 0.6.0 → 0.8.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 +28 -5
- package/dist/adapters.d.ts +11 -0
- package/dist/adapters.js +77 -6
- package/dist/cli.js +58 -3
- package/dist/evidence.js +1 -1
- package/dist/flow.js +37 -3
- package/dist/grants.d.ts +18 -0
- package/dist/grants.js +131 -0
- package/dist/permissive.js +406 -18
- package/dist/policy.d.ts +2 -0
- package/dist/policy.js +1 -1
- package/dist/service.d.ts +7 -2
- package/dist/service.js +17 -5
- package/dist/session.d.ts +56 -1
- package/dist/session.js +189 -4
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
package/dist/permissive.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { statSync } from "node:fs";
|
|
1
|
+
import { lstatSync, readdirSync, statSync } from "node:fs";
|
|
2
2
|
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
3
3
|
import { branchEvidence, canonical, disposable, git, inside, isProtectedPath, OUTSIDE_REPOSITORY, pathEvidence, policyState, protectedPath, resolveWord, sensitive, tempRoots, tempWrite, } from "./evidence.js";
|
|
4
4
|
import { commandDecision } from "./flow.js";
|
|
@@ -294,6 +294,8 @@ function sedScriptPaths(script) {
|
|
|
294
294
|
function textOperands(program, args) {
|
|
295
295
|
const text = new Set();
|
|
296
296
|
const files = [];
|
|
297
|
+
// Values of file-name pattern options (`--include`, rg `-g`), found by this same parse.
|
|
298
|
+
const patterns = new Set();
|
|
297
299
|
if (program === "gh") {
|
|
298
300
|
for (const [index, word] of args.entries()) {
|
|
299
301
|
if (GH_TEXT.has(word))
|
|
@@ -301,16 +303,19 @@ function textOperands(program, args) {
|
|
|
301
303
|
else if (/^--(?:body|title|jq|template|notes)=/.test(word))
|
|
302
304
|
text.add(index);
|
|
303
305
|
}
|
|
304
|
-
return { text, paths: [], files };
|
|
306
|
+
return { text, paths: [], files, patterns };
|
|
305
307
|
}
|
|
308
|
+
if (program === "find")
|
|
309
|
+
return { text, paths: [], files, patterns: findPatterns(args) };
|
|
306
310
|
const values = SHORT_VALUES[program];
|
|
307
311
|
if (values === undefined)
|
|
308
|
-
return { text, paths: [], files };
|
|
312
|
+
return { text, paths: [], files, patterns };
|
|
309
313
|
// Fallback when an option cannot be understood: every operand is checked.
|
|
310
314
|
const everyWord = {
|
|
311
315
|
text: new Set(),
|
|
312
316
|
paths: [],
|
|
313
317
|
files: args.filter((arg) => arg !== "" && !arg.startsWith("-")),
|
|
318
|
+
patterns: new Set(),
|
|
314
319
|
};
|
|
315
320
|
// grep, rg and GNU sed accept options after operands, so collect everything before deciding
|
|
316
321
|
// which operand, if any, is the pattern or script.
|
|
@@ -344,9 +349,15 @@ function textOperands(program, args) {
|
|
|
344
349
|
// A pattern or script file means there is no pattern operand: every operand is a file.
|
|
345
350
|
if (name === "--file")
|
|
346
351
|
explicit = true;
|
|
352
|
+
const pattern = NAME_PATTERN_OPTIONS[program]?.includes(name);
|
|
353
|
+
if (pattern && eq >= 0)
|
|
354
|
+
patterns.add(index);
|
|
347
355
|
// A value-taking long option consumes the next word, which stays a checked operand.
|
|
348
|
-
if (eq < 0 && LONG_VALUES[program]?.has(name))
|
|
356
|
+
if (eq < 0 && LONG_VALUES[program]?.has(name)) {
|
|
349
357
|
index++;
|
|
358
|
+
if (pattern)
|
|
359
|
+
patterns.add(index);
|
|
360
|
+
}
|
|
350
361
|
}
|
|
351
362
|
continue;
|
|
352
363
|
}
|
|
@@ -372,6 +383,8 @@ function textOperands(program, args) {
|
|
|
372
383
|
explicit = true;
|
|
373
384
|
if (at + 1 === word.length)
|
|
374
385
|
index++;
|
|
386
|
+
if (program === "rg" && flag === "g")
|
|
387
|
+
patterns.add(index);
|
|
375
388
|
break;
|
|
376
389
|
}
|
|
377
390
|
}
|
|
@@ -392,7 +405,7 @@ function textOperands(program, args) {
|
|
|
392
405
|
text.add(index);
|
|
393
406
|
files.push(...operands.map((index) => args[index] ?? ""));
|
|
394
407
|
const paths = program === "sed" ? scripts.flatMap(([, script]) => sedScriptPaths(script)) : [];
|
|
395
|
-
return { text, paths, files };
|
|
408
|
+
return { text, paths, files, patterns };
|
|
396
409
|
}
|
|
397
410
|
/** Pattern and script options per program, besides the value lists. */
|
|
398
411
|
const PATTERN_OPTIONS = {
|
|
@@ -598,6 +611,16 @@ export function permissiveWrite(cwd, input, profile, fill) {
|
|
|
598
611
|
fill.script.approved = true;
|
|
599
612
|
return check;
|
|
600
613
|
}
|
|
614
|
+
/** A file with another hard link may alias a file outside the scratch root; unknown counts as yes. */
|
|
615
|
+
function hardLinked(path) {
|
|
616
|
+
try {
|
|
617
|
+
const stat = lstatSync(path);
|
|
618
|
+
return !stat.isDirectory() && stat.nlink > 1;
|
|
619
|
+
}
|
|
620
|
+
catch (error) {
|
|
621
|
+
return error.code !== "ENOENT";
|
|
622
|
+
}
|
|
623
|
+
}
|
|
601
624
|
function writeEvidence(cwd, input, profile) {
|
|
602
625
|
let lexical;
|
|
603
626
|
let path;
|
|
@@ -614,6 +637,7 @@ function writeEvidence(cwd, input, profile) {
|
|
|
614
637
|
const target = path;
|
|
615
638
|
if (!policyState(target) &&
|
|
616
639
|
!policyState(lexical) &&
|
|
640
|
+
!hardLinked(target) &&
|
|
617
641
|
profile.scratchRoots.some((root) => inside(root, target)))
|
|
618
642
|
return {
|
|
619
643
|
allowed: true,
|
|
@@ -715,7 +739,53 @@ function restorePaths(rest) {
|
|
|
715
739
|
fromHead: staged,
|
|
716
740
|
};
|
|
717
741
|
}
|
|
718
|
-
|
|
742
|
+
/**
|
|
743
|
+
* `git worktree remove --force <path>` where `path` is a linked worktree of this repository (never
|
|
744
|
+
* the main one) strictly inside a temp or scratch root and outside the main checkout.
|
|
745
|
+
*/
|
|
746
|
+
function disposableWorktree(cwd, args, roots, fill) {
|
|
747
|
+
if (fill?.script.tainted)
|
|
748
|
+
return false;
|
|
749
|
+
const paths = [];
|
|
750
|
+
let ended = false;
|
|
751
|
+
let forces = 0;
|
|
752
|
+
for (const arg of args) {
|
|
753
|
+
if (!ended && arg === "--")
|
|
754
|
+
ended = true;
|
|
755
|
+
else if (!ended && arg.startsWith("-")) {
|
|
756
|
+
if (arg !== "-f" && !abbreviates(arg, "--force"))
|
|
757
|
+
return false;
|
|
758
|
+
forces++;
|
|
759
|
+
}
|
|
760
|
+
else
|
|
761
|
+
paths.push(arg);
|
|
762
|
+
}
|
|
763
|
+
// A second force also removes a locked worktree, and a lock is an explicit keep marker.
|
|
764
|
+
if (paths.length !== 1 || forces !== 1)
|
|
765
|
+
return false;
|
|
766
|
+
try {
|
|
767
|
+
const target = canonical(resolveWord(cwd, paths[0]));
|
|
768
|
+
if (policyState(target) || !roots.some((root) => root !== target && inside(root, target)))
|
|
769
|
+
return false;
|
|
770
|
+
const listed = git(cwd, ["worktree", "list", "--porcelain"])
|
|
771
|
+
.split("\n")
|
|
772
|
+
.filter((line) => line.startsWith("worktree "))
|
|
773
|
+
.map((line) => canonical(line.slice("worktree ".length)));
|
|
774
|
+
const [main, ...linked] = listed;
|
|
775
|
+
if (main === undefined || inside(main, target) || !linked.includes(target))
|
|
776
|
+
return false;
|
|
777
|
+
// Removal deletes the whole directory, so no other checkout may live inside it.
|
|
778
|
+
if (listed.some((path) => path !== target && inside(target, path)))
|
|
779
|
+
return false;
|
|
780
|
+
if (fill?.reorder)
|
|
781
|
+
fill.script.approved = true;
|
|
782
|
+
return true;
|
|
783
|
+
}
|
|
784
|
+
catch {
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
function gitDecision(request, argv, cwdForGit, sole, roots, fill) {
|
|
719
789
|
const args = argv.slice(1);
|
|
720
790
|
let index = 0;
|
|
721
791
|
let cwd = cwdForGit;
|
|
@@ -793,8 +863,13 @@ function gitDecision(request, argv, cwdForGit, sole) {
|
|
|
793
863
|
return worktreeAdd(request, cwd, rest.slice(rest.indexOf("add") + 1)) ?? ok();
|
|
794
864
|
if (sub === "worktree" &&
|
|
795
865
|
["remove", "prune"].includes(positional[0] ?? "") &&
|
|
796
|
-
(has("-f", "--force") || positional[0] === "prune"))
|
|
797
|
-
return
|
|
866
|
+
(has("-f", "--force") || cluster("f") || positional[0] === "prune"))
|
|
867
|
+
return positional[0] === "remove" && disposableWorktree(cwd, rest.slice(1), roots, fill)
|
|
868
|
+
? ok("Removes a linked worktree inside a temp or scratch directory")
|
|
869
|
+
: positional[0] === "prune" &&
|
|
870
|
+
rest.slice(1).every((arg) => ["-n", "-v", "--dry-run", "--verbose"].includes(arg))
|
|
871
|
+
? ok("Prunes only records of worktrees whose directories are gone")
|
|
872
|
+
: human(request, "destructive", "Forced worktree removal requires human approval");
|
|
798
873
|
if (sub === "config" &&
|
|
799
874
|
!has("--get", "--get-all", "--get-regexp", "--list", "-l", "--show-origin") &&
|
|
800
875
|
!safeConfigWrite(rest))
|
|
@@ -941,6 +1016,8 @@ const AWS_METADATA_GETTERS = new Set([
|
|
|
941
1016
|
"get-metric-statistics",
|
|
942
1017
|
"get-log-events",
|
|
943
1018
|
]);
|
|
1019
|
+
/** az operations that only read but whose verb is not list or show. */
|
|
1020
|
+
const AZ_READ_OPERATIONS = new Set(["boards query"]);
|
|
944
1021
|
function readVerb(argv) {
|
|
945
1022
|
const [tool] = argv;
|
|
946
1023
|
const firstOption = argv.findIndex((arg) => arg.startsWith("-"));
|
|
@@ -955,7 +1032,8 @@ function readVerb(argv) {
|
|
|
955
1032
|
if (/token|secret|password|credential|key/i.test(verb))
|
|
956
1033
|
return false;
|
|
957
1034
|
if (tool === "az")
|
|
958
|
-
return /^(?:list|show|exists|list-[a-z-]+|show-[a-z-]+)$/.test(verb)
|
|
1035
|
+
return (/^(?:list|show|exists|list-[a-z-]+|show-[a-z-]+)$/.test(verb) ||
|
|
1036
|
+
AZ_READ_OPERATIONS.has(operation.join(" ")));
|
|
959
1037
|
if (tool === "aws") {
|
|
960
1038
|
// `get-*` is not safe as a family: it includes secret, password, token and access-detail
|
|
961
1039
|
// retrieval. Only `describe-`/`list-` and an explicit set of metadata getters are automatic.
|
|
@@ -1085,6 +1163,305 @@ function autoCd(cwd, word) {
|
|
|
1085
1163
|
}
|
|
1086
1164
|
});
|
|
1087
1165
|
}
|
|
1166
|
+
const PRINTERS = new Set(["echo", "printf"]);
|
|
1167
|
+
const GLOB = /[*?[]/;
|
|
1168
|
+
/** At most this many directory entries are matched against a glob operand. */
|
|
1169
|
+
const GLOB_ENTRIES = 5000;
|
|
1170
|
+
/**
|
|
1171
|
+
* Whether a word that may be a file operand names a credential: by name, as resolved against the
|
|
1172
|
+
* working directory (a symlink to a key, or a cwd inside `~/.ssh`), or, for a glob in its last
|
|
1173
|
+
* component, by any entry it would match.
|
|
1174
|
+
*/
|
|
1175
|
+
function sensitiveOperand(cwd, word) {
|
|
1176
|
+
if (word === "")
|
|
1177
|
+
return false;
|
|
1178
|
+
if (sensitive(word))
|
|
1179
|
+
return true;
|
|
1180
|
+
if (GLOB.test(word))
|
|
1181
|
+
return sensitiveGlob(cwd, word);
|
|
1182
|
+
try {
|
|
1183
|
+
const lexical = resolveWord(cwd, word);
|
|
1184
|
+
if (!lstatExists(lexical))
|
|
1185
|
+
return false;
|
|
1186
|
+
return sensitive(lexical) || sensitive(canonical(lexical));
|
|
1187
|
+
}
|
|
1188
|
+
catch {
|
|
1189
|
+
return false;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Expands a glob one component at a time, as the shell would, and reports whether any directory
|
|
1194
|
+
* it passes or entry it matches is a credential path. Fails closed: an unreadable directory, an
|
|
1195
|
+
* unresolvable entry or more than GLOB_ENTRIES entries in total count as sensitive.
|
|
1196
|
+
*/
|
|
1197
|
+
function sensitiveGlob(cwd, word) {
|
|
1198
|
+
try {
|
|
1199
|
+
const parts = word.split(/[\\/]/);
|
|
1200
|
+
// zsh `**/` matches any depth (and `***/` follows symlinks): too wide to expand here.
|
|
1201
|
+
if (parts.some((part) => part.includes("**")))
|
|
1202
|
+
return true;
|
|
1203
|
+
const first = parts.findIndex((part) => GLOB.test(part));
|
|
1204
|
+
const prefix = parts.slice(0, first).join("/");
|
|
1205
|
+
const start = resolveWord(cwd, first === 0 ? "." : prefix || "/");
|
|
1206
|
+
if (sensitive(start) || sensitive(canonical(start)))
|
|
1207
|
+
return true;
|
|
1208
|
+
let current = [start];
|
|
1209
|
+
let budget = GLOB_ENTRIES;
|
|
1210
|
+
for (const part of parts.slice(first)) {
|
|
1211
|
+
if (part === "" || part === ".")
|
|
1212
|
+
continue;
|
|
1213
|
+
const next = [];
|
|
1214
|
+
for (const dir of current) {
|
|
1215
|
+
if (!GLOB.test(part)) {
|
|
1216
|
+
const path = join(dir, part);
|
|
1217
|
+
if (sensitive(path) || (lstatExists(path) && sensitive(canonical(path))))
|
|
1218
|
+
return true;
|
|
1219
|
+
next.push(path);
|
|
1220
|
+
continue;
|
|
1221
|
+
}
|
|
1222
|
+
let entries;
|
|
1223
|
+
try {
|
|
1224
|
+
if (!statSync(dir).isDirectory())
|
|
1225
|
+
continue;
|
|
1226
|
+
entries = readdirSync(dir);
|
|
1227
|
+
}
|
|
1228
|
+
catch (error) {
|
|
1229
|
+
if (error.code === "ENOENT")
|
|
1230
|
+
continue;
|
|
1231
|
+
return true;
|
|
1232
|
+
}
|
|
1233
|
+
budget -= entries.length;
|
|
1234
|
+
if (budget < 0)
|
|
1235
|
+
return true;
|
|
1236
|
+
const pattern = globPattern(part);
|
|
1237
|
+
// `*` skips dot files unless the pattern itself starts with a dot.
|
|
1238
|
+
for (const name of entries) {
|
|
1239
|
+
if ((name.startsWith(".") && !part.startsWith(".")) || !pattern.test(name))
|
|
1240
|
+
continue;
|
|
1241
|
+
const path = join(dir, name);
|
|
1242
|
+
if (sensitive(name) || sensitive(canonical(path)))
|
|
1243
|
+
return true;
|
|
1244
|
+
next.push(path);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
current = next;
|
|
1248
|
+
}
|
|
1249
|
+
return false;
|
|
1250
|
+
}
|
|
1251
|
+
catch {
|
|
1252
|
+
return true;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
function lstatExists(path) {
|
|
1256
|
+
try {
|
|
1257
|
+
lstatSync(path);
|
|
1258
|
+
return true;
|
|
1259
|
+
}
|
|
1260
|
+
catch {
|
|
1261
|
+
return false;
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
/** A shell glob for one path component: `*`, `?` and bracket classes. */
|
|
1265
|
+
function globPattern(glob) {
|
|
1266
|
+
let source = "";
|
|
1267
|
+
for (let index = 0; index < glob.length; index++) {
|
|
1268
|
+
const char = glob[index];
|
|
1269
|
+
if (char === "*")
|
|
1270
|
+
source += ".*";
|
|
1271
|
+
else if (char === "?")
|
|
1272
|
+
source += ".";
|
|
1273
|
+
else if (char === "[") {
|
|
1274
|
+
const close = glob.indexOf("]", index + 2);
|
|
1275
|
+
if (close < 0)
|
|
1276
|
+
source += "\\[";
|
|
1277
|
+
else {
|
|
1278
|
+
const body = glob
|
|
1279
|
+
.slice(index + 1, close)
|
|
1280
|
+
.replace(/^[!^]/, "^")
|
|
1281
|
+
.replace(/\\/g, "\\\\");
|
|
1282
|
+
source += `[${body}]`;
|
|
1283
|
+
index = close;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
else
|
|
1287
|
+
source += char.replace(/[.+^${}()|[\]\\/-]/g, "\\$&");
|
|
1288
|
+
}
|
|
1289
|
+
try {
|
|
1290
|
+
return new RegExp(`^${source}$`, "s");
|
|
1291
|
+
}
|
|
1292
|
+
catch {
|
|
1293
|
+
return /^/; // an unparsable class matches everything, which only sends more to a person
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
const NAME_PATTERN_OPTIONS = {
|
|
1297
|
+
grep: ["--include", "--exclude", "--exclude-dir"],
|
|
1298
|
+
egrep: ["--include", "--exclude", "--exclude-dir"],
|
|
1299
|
+
fgrep: ["--include", "--exclude", "--exclude-dir"],
|
|
1300
|
+
rg: ["--glob", "--iglob", "-g"],
|
|
1301
|
+
find: ["-name", "-iname", "-path", "-ipath", "-wholename", "-iwholename"],
|
|
1302
|
+
};
|
|
1303
|
+
/**
|
|
1304
|
+
* Indices of find's `-name`/`-path` values. Every value-taking primary consumes its value; an
|
|
1305
|
+
* unknown primary (`-printf`, `-newerXY`) makes the parse uncertain, so nothing is a pattern.
|
|
1306
|
+
*/
|
|
1307
|
+
function findPatterns(args) {
|
|
1308
|
+
const found = new Set();
|
|
1309
|
+
const names = NAME_PATTERN_OPTIONS.find ?? [];
|
|
1310
|
+
for (let index = 0; index < args.length; index++) {
|
|
1311
|
+
const word = args[index] ?? "";
|
|
1312
|
+
if (FIND_VALUE_PRIMARIES.has(word)) {
|
|
1313
|
+
if (names.includes(word))
|
|
1314
|
+
found.add(index + 1);
|
|
1315
|
+
index++;
|
|
1316
|
+
}
|
|
1317
|
+
else if (/^[-(!)]/.test(word) && !FIND_FLAG_PRIMARIES.has(word))
|
|
1318
|
+
return new Set();
|
|
1319
|
+
}
|
|
1320
|
+
return found;
|
|
1321
|
+
}
|
|
1322
|
+
/** Index of chmod's mode or chown's owner (`+x`, `u+w`, `user:group`), which names no file. */
|
|
1323
|
+
function modeIndex(program, args) {
|
|
1324
|
+
if (!["chmod", "chown"].includes(program) || args.some((arg) => arg.startsWith("--reference")))
|
|
1325
|
+
return undefined;
|
|
1326
|
+
// BSD chmod's ACL options (-N, -E, -C, -i, -I) take no mode: every operand is a file.
|
|
1327
|
+
if (program === "chmod" && args.some((arg) => /^-[A-Za-z]*[NECiI]/.test(arg)))
|
|
1328
|
+
return undefined;
|
|
1329
|
+
for (let index = 0; index < args.length; index++) {
|
|
1330
|
+
const arg = args[index] ?? "";
|
|
1331
|
+
if (arg === "--")
|
|
1332
|
+
return index + 1 < args.length ? index + 1 : undefined;
|
|
1333
|
+
// `chmod -w file`: a mode may start with `-`.
|
|
1334
|
+
if (arg.startsWith("-") && !(program === "chmod" && /^-[rwxXst]+$/.test(arg)))
|
|
1335
|
+
continue;
|
|
1336
|
+
return index;
|
|
1337
|
+
}
|
|
1338
|
+
return undefined;
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* GNU long options of cp and ln. Of those that take a separate value, only --target-directory
|
|
1342
|
+
* matters: a consumed `--sparse`/`--no-preserve` value only adds a read-checked source.
|
|
1343
|
+
*/
|
|
1344
|
+
const COPY_LONG = {
|
|
1345
|
+
cp: [
|
|
1346
|
+
"--archive",
|
|
1347
|
+
"--attributes-only",
|
|
1348
|
+
"--backup",
|
|
1349
|
+
"--copy-contents",
|
|
1350
|
+
"--debug",
|
|
1351
|
+
"--dereference",
|
|
1352
|
+
"--force",
|
|
1353
|
+
"--interactive",
|
|
1354
|
+
"--keep-directory-symlink",
|
|
1355
|
+
"--link",
|
|
1356
|
+
"--no-clobber",
|
|
1357
|
+
"--no-dereference",
|
|
1358
|
+
"--no-preserve",
|
|
1359
|
+
"--no-target-directory",
|
|
1360
|
+
"--one-file-system",
|
|
1361
|
+
"--parents",
|
|
1362
|
+
"--preserve",
|
|
1363
|
+
"--recursive",
|
|
1364
|
+
"--reflink",
|
|
1365
|
+
"--remove-destination",
|
|
1366
|
+
"--sparse",
|
|
1367
|
+
"--strip-trailing-slashes",
|
|
1368
|
+
"--suffix",
|
|
1369
|
+
"--symbolic-link",
|
|
1370
|
+
"--target-directory",
|
|
1371
|
+
"--update",
|
|
1372
|
+
"--verbose",
|
|
1373
|
+
],
|
|
1374
|
+
ln: [
|
|
1375
|
+
"--backup",
|
|
1376
|
+
"--directory",
|
|
1377
|
+
"--force",
|
|
1378
|
+
"--interactive",
|
|
1379
|
+
"--logical",
|
|
1380
|
+
"--no-dereference",
|
|
1381
|
+
"--no-target-directory",
|
|
1382
|
+
"--physical",
|
|
1383
|
+
"--relative",
|
|
1384
|
+
"--suffix",
|
|
1385
|
+
"--symbolic",
|
|
1386
|
+
"--target-directory",
|
|
1387
|
+
"--verbose",
|
|
1388
|
+
],
|
|
1389
|
+
};
|
|
1390
|
+
/**
|
|
1391
|
+
* Sources and written targets of `cp` and `ln`: sources are only read (a symlink's target is not
|
|
1392
|
+
* touched), so they take the read check. Undefined when the split is uncertain (backup suffixes,
|
|
1393
|
+
* hard links, unknown options), and every operand is then checked as a write.
|
|
1394
|
+
*/
|
|
1395
|
+
function copySources(program, args, cwd) {
|
|
1396
|
+
if (program !== "cp" && program !== "ln")
|
|
1397
|
+
return undefined;
|
|
1398
|
+
const operands = [];
|
|
1399
|
+
let target;
|
|
1400
|
+
let ended = false;
|
|
1401
|
+
let symbolic = false;
|
|
1402
|
+
for (let index = 0; index < args.length; index++) {
|
|
1403
|
+
const arg = args[index] ?? "";
|
|
1404
|
+
if (ended || !arg.startsWith("-") || arg === "-") {
|
|
1405
|
+
operands.push(arg);
|
|
1406
|
+
continue;
|
|
1407
|
+
}
|
|
1408
|
+
if (arg === "--") {
|
|
1409
|
+
ended = true;
|
|
1410
|
+
continue;
|
|
1411
|
+
}
|
|
1412
|
+
if (arg.startsWith("--")) {
|
|
1413
|
+
// Resolved as GNU getopt does (exact or unique prefix); unknown or ambiguous fails closed.
|
|
1414
|
+
const given = arg.split("=")[0] ?? "";
|
|
1415
|
+
const known = COPY_LONG[program] ?? [];
|
|
1416
|
+
const matches = known.includes(given) ? [given] : known.filter((o) => o.startsWith(given));
|
|
1417
|
+
const name = matches.length === 1 ? matches[0] : undefined;
|
|
1418
|
+
if (name === undefined || ["--backup", "--suffix", "--link"].includes(name))
|
|
1419
|
+
return undefined;
|
|
1420
|
+
if (name === "--symbolic")
|
|
1421
|
+
symbolic = true;
|
|
1422
|
+
if (name === "--target-directory") {
|
|
1423
|
+
if (target !== undefined)
|
|
1424
|
+
return undefined;
|
|
1425
|
+
target = arg.includes("=") ? arg.slice(arg.indexOf("=") + 1) : args[++index];
|
|
1426
|
+
if (target === undefined)
|
|
1427
|
+
return undefined;
|
|
1428
|
+
}
|
|
1429
|
+
continue;
|
|
1430
|
+
}
|
|
1431
|
+
const letters = arg.slice(1);
|
|
1432
|
+
const at = letters.indexOf("t");
|
|
1433
|
+
const flags = at < 0 ? letters : letters.slice(0, at);
|
|
1434
|
+
if (/[bS]/.test(flags))
|
|
1435
|
+
return undefined;
|
|
1436
|
+
if (program === "cp" && flags.includes("l"))
|
|
1437
|
+
return undefined;
|
|
1438
|
+
if (program === "ln" && flags.includes("s"))
|
|
1439
|
+
symbolic = true;
|
|
1440
|
+
if (at >= 0) {
|
|
1441
|
+
if (target !== undefined)
|
|
1442
|
+
return undefined;
|
|
1443
|
+
target = at + 1 < letters.length ? letters.slice(at + 1) : args[++index];
|
|
1444
|
+
if (target === undefined)
|
|
1445
|
+
return undefined;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
// A hard link shares its source's inode, so a later write through it changes the source.
|
|
1449
|
+
if (program === "ln" && !symbolic)
|
|
1450
|
+
return undefined;
|
|
1451
|
+
if (target !== undefined)
|
|
1452
|
+
return { sources: operands, targets: [target] };
|
|
1453
|
+
if (operands.length >= 2)
|
|
1454
|
+
return { sources: operands.slice(0, -1), targets: operands.slice(-1) };
|
|
1455
|
+
// `ln -s path` links `basename(path)` in the working directory.
|
|
1456
|
+
if (program === "ln" && operands.length === 1) {
|
|
1457
|
+
const only = operands[0] ?? "";
|
|
1458
|
+
const name = basename(only.replace(/[\\/]+$/, ""));
|
|
1459
|
+
if (!name || name === "." || name === "..")
|
|
1460
|
+
return undefined;
|
|
1461
|
+
return { sources: operands, targets: [resolve(cwd, name)] };
|
|
1462
|
+
}
|
|
1463
|
+
return undefined;
|
|
1464
|
+
}
|
|
1088
1465
|
export function simpleDecision(request, command, profile, depth, stageIndex, cwd, fill) {
|
|
1089
1466
|
for (const [name] of command.env)
|
|
1090
1467
|
if (BANNED_ENV.test(name))
|
|
@@ -1152,14 +1529,20 @@ export function simpleDecision(request, command, profile, depth, stageIndex, cwd
|
|
|
1152
1529
|
// known text positions (sed scripts, grep patterns, gh message text) are not paths, but the
|
|
1153
1530
|
// files a sed script reads or writes are.
|
|
1154
1531
|
const operands = textOperands(program, args);
|
|
1532
|
+
const namePatterns = operands.patterns;
|
|
1155
1533
|
for (const [index, word] of args.entries()) {
|
|
1156
1534
|
if (operands.text.has(index))
|
|
1157
1535
|
continue;
|
|
1158
1536
|
const candidate = word.includes("=") ? word.slice(word.indexOf("=") + 1) : word;
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1537
|
+
// echo and printf read no file, so only a path-like word is judged, by name.
|
|
1538
|
+
const pathLike = candidate.includes("/") || candidate.startsWith(".");
|
|
1539
|
+
// A file-name pattern (`--include=*.md`, `find -name '*.ts'`) is matched below the searched
|
|
1540
|
+
// paths, not against the working directory, so it is judged by name only.
|
|
1541
|
+
const namePattern = namePatterns.has(index);
|
|
1542
|
+
if (PRINTERS.has(program) || namePattern
|
|
1543
|
+
? (pathLike || namePattern) && sensitive(candidate)
|
|
1544
|
+
: sensitiveOperand(cwd, candidate) || (candidate !== word && sensitiveOperand(cwd, word)))
|
|
1545
|
+
return human(request, "sensitive", "Protected path requires human approval");
|
|
1163
1546
|
}
|
|
1164
1547
|
if (operands.paths.some((path) => sensitive(path)))
|
|
1165
1548
|
return human(request, "sensitive", "Protected path requires human approval");
|
|
@@ -1251,10 +1634,15 @@ export function simpleDecision(request, command, profile, depth, stageIndex, cwd
|
|
|
1251
1634
|
: undefined);
|
|
1252
1635
|
return value === undefined ? [] : [value];
|
|
1253
1636
|
});
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
]) {
|
|
1637
|
+
const split = copySources(program, args, cwd);
|
|
1638
|
+
const mode = modeIndex(program, args);
|
|
1639
|
+
const operands = args.filter((arg, index) => index !== mode && !arg.startsWith("-") && !/^[0-9]+$/.test(arg));
|
|
1640
|
+
for (const file of split?.sources ?? []) {
|
|
1641
|
+
const check = permissiveRead(cwd, file);
|
|
1642
|
+
if (!check.allowed)
|
|
1643
|
+
return human(request, check.category, check.reason);
|
|
1644
|
+
}
|
|
1645
|
+
for (const file of [...(split?.targets ?? operands), ...optionValues]) {
|
|
1258
1646
|
const check = permissiveWrite(cwd, file, profile, fill);
|
|
1259
1647
|
if (!check.allowed)
|
|
1260
1648
|
return human(request, check.category, check.reason);
|
|
@@ -1281,7 +1669,7 @@ export function simpleDecision(request, command, profile, depth, stageIndex, cwd
|
|
|
1281
1669
|
/(?:-X|--request)[=\s]?(?:DELETE|PUT)\b/i.test(joined))
|
|
1282
1670
|
return human(request, "destructive", "Destructive HTTP verbs require human approval");
|
|
1283
1671
|
if (program === "git")
|
|
1284
|
-
return gitDecision(request, argv, cwd, () => argv[0] === "git" && command.argv[0] === "git" && depth === 0 && soleEffect(request));
|
|
1672
|
+
return gitDecision(request, argv, cwd, () => argv[0] === "git" && command.argv[0] === "git" && depth === 0 && soleEffect(request), [...profile.scratchRoots, ...tempRoots()], fill);
|
|
1285
1673
|
if (program === "gh") {
|
|
1286
1674
|
const gh = ghDecision(request, argv);
|
|
1287
1675
|
if (gh)
|
package/dist/policy.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type Profile } from "./profile.js";
|
|
2
2
|
import { type Category, type Classification, type Decision, type Request } from "./types.js";
|
|
3
3
|
export declare function hash(value: unknown): string;
|
|
4
|
+
/** Shell tools whose `description` only labels the command. Elsewhere it can be content. */
|
|
5
|
+
export declare const SHELL_TOOLS: Set<string>;
|
|
4
6
|
/** The correlation hash of a request, over its effect-relevant input only. */
|
|
5
7
|
export declare function requestHash(request: Request): string;
|
|
6
8
|
export declare function words(command: string): string[] | undefined;
|
package/dist/policy.js
CHANGED
|
@@ -10,7 +10,7 @@ export function hash(value) {
|
|
|
10
10
|
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
11
11
|
}
|
|
12
12
|
/** Shell tools whose `description` only labels the command. Elsewhere it can be content. */
|
|
13
|
-
const SHELL_TOOLS = new Set(["Bash", "bash", "exec_command", "shell"]);
|
|
13
|
+
export const SHELL_TOOLS = new Set(["Bash", "bash", "exec_command", "shell"]);
|
|
14
14
|
/** The correlation hash of a request, over its effect-relevant input only. */
|
|
15
15
|
export function requestHash(request) {
|
|
16
16
|
if (!SHELL_TOOLS.has(request.tool))
|
package/dist/service.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import type { Classification, OutcomeReport, Request } from "./types.js";
|
|
2
2
|
interface Message {
|
|
3
3
|
token: string;
|
|
4
|
-
action: "ping" | "review" | "classify" | "record" | "stop";
|
|
4
|
+
action: "ping" | "review" | "classify" | "record" | "grant" | "stop";
|
|
5
5
|
request?: Request;
|
|
6
6
|
classification?: Classification;
|
|
7
7
|
report?: OutcomeReport;
|
|
8
8
|
phase?: string;
|
|
9
|
+
grant?: {
|
|
10
|
+
requestHash: string;
|
|
11
|
+
confirm: boolean;
|
|
12
|
+
session?: boolean;
|
|
13
|
+
};
|
|
9
14
|
}
|
|
10
|
-
export declare function serve(harness: string, sessionId: string): Promise<void>;
|
|
15
|
+
export declare function serve(harness: string, sessionId: string, cliPath?: string): Promise<void>;
|
|
11
16
|
export declare function send(harness: string, sessionId: string, message: Omit<Message, "token">): Promise<unknown>;
|
|
12
17
|
/**
|
|
13
18
|
* Stops a session's service. A session with no service (it never started, already ended, or the
|
package/dist/service.js
CHANGED
|
@@ -5,13 +5,13 @@ import { join } from "node:path";
|
|
|
5
5
|
import { audit, sessionDirectory, sessionDirectoryPath, sessionToken } from "./audit.js";
|
|
6
6
|
import { hash, requestHash } from "./policy.js";
|
|
7
7
|
import { loadProfile, strictProfile } from "./profile.js";
|
|
8
|
-
import { PolicySession } from "./session.js";
|
|
8
|
+
import { GrantError, PolicySession } from "./session.js";
|
|
9
9
|
function endpoint(directory) {
|
|
10
10
|
return process.platform === "win32"
|
|
11
11
|
? `\\\\.\\pipe\\acp-${hash(directory).slice(0, 32)}`
|
|
12
12
|
: join(directory, "service.sock");
|
|
13
13
|
}
|
|
14
|
-
export async function serve(harness, sessionId) {
|
|
14
|
+
export async function serve(harness, sessionId, cliPath) {
|
|
15
15
|
const directory = sessionDirectory(harness, sessionId), token = sessionToken(directory);
|
|
16
16
|
let profile = strictProfile;
|
|
17
17
|
let profileFailure = false;
|
|
@@ -21,7 +21,7 @@ export async function serve(harness, sessionId) {
|
|
|
21
21
|
catch {
|
|
22
22
|
profileFailure = true; // an unreadable or untrusted profile must never loosen policy
|
|
23
23
|
}
|
|
24
|
-
const session = new PolicySession(directory, harness, sessionId, profile);
|
|
24
|
+
const session = new PolicySession(directory, harness, sessionId, profile, cliPath);
|
|
25
25
|
const server = createServer((socket) => {
|
|
26
26
|
socket.setTimeout(3000, () => socket.destroy());
|
|
27
27
|
let data = "";
|
|
@@ -49,8 +49,10 @@ export async function serve(harness, sessionId) {
|
|
|
49
49
|
? { requestHash: message.classification.requestHash }
|
|
50
50
|
: message.report
|
|
51
51
|
? { requestHash: message.report.requestHash }
|
|
52
|
-
:
|
|
53
|
-
|
|
52
|
+
: message.grant
|
|
53
|
+
? { requestHash: String(message.grant.requestHash) }
|
|
54
|
+
: {}),
|
|
55
|
+
action: ["ping", "review", "classify", "record", "grant", "stop"].includes(message.action)
|
|
54
56
|
? message.action
|
|
55
57
|
: "invalid",
|
|
56
58
|
});
|
|
@@ -71,6 +73,16 @@ export async function serve(harness, sessionId) {
|
|
|
71
73
|
session.record(message.report);
|
|
72
74
|
result = { recorded: true };
|
|
73
75
|
}
|
|
76
|
+
else if (message.action === "grant" && message.grant) {
|
|
77
|
+
try {
|
|
78
|
+
result = session.grant(String(message.grant.requestHash), message.grant.confirm === true, message.grant.session === true);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
if (!(error instanceof GrantError))
|
|
82
|
+
throw error;
|
|
83
|
+
result = { granted: false, refused: error.message };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
74
86
|
else
|
|
75
87
|
throw new Error("Invalid service request");
|
|
76
88
|
socket.end(`${JSON.stringify({ result })}\n`);
|