@agentic-kit/permissions 0.0.0-stage → 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/LICENSE +23 -0
- package/README.md +279 -2
- package/bash/analyze.d.ts +133 -0
- package/bash/analyze.js +442 -0
- package/bash/args.d.ts +45 -0
- package/bash/args.js +145 -0
- package/bash/braces.d.ts +6 -0
- package/bash/braces.js +72 -0
- package/bash/commands.d.ts +14 -0
- package/bash/commands.js +361 -0
- package/bash/file-commands.d.ts +22 -0
- package/bash/file-commands.js +278 -0
- package/bash/flows.d.ts +67 -0
- package/bash/flows.js +208 -0
- package/bash/git.d.ts +7 -0
- package/bash/git.js +163 -0
- package/bash/network.d.ts +24 -0
- package/bash/network.js +295 -0
- package/bash/quoting.d.ts +16 -0
- package/bash/quoting.js +164 -0
- package/bash/system.d.ts +16 -0
- package/bash/system.js +122 -0
- package/bash/tokenize.d.ts +67 -0
- package/bash/tokenize.js +440 -0
- package/bash/words.d.ts +9 -0
- package/bash/words.js +30 -0
- package/bash/wrappers.d.ts +32 -0
- package/bash/wrappers.js +237 -0
- package/classify.d.ts +22 -0
- package/classify.js +259 -0
- package/engine.d.ts +109 -0
- package/engine.js +144 -0
- package/env.d.ts +13 -0
- package/env.js +37 -0
- package/esm/bash/analyze.d.ts +133 -0
- package/esm/bash/analyze.js +405 -0
- package/esm/bash/args.d.ts +45 -0
- package/esm/bash/args.js +135 -0
- package/esm/bash/braces.d.ts +6 -0
- package/esm/bash/braces.js +69 -0
- package/esm/bash/commands.d.ts +14 -0
- package/esm/bash/commands.js +324 -0
- package/esm/bash/file-commands.d.ts +22 -0
- package/esm/bash/file-commands.js +233 -0
- package/esm/bash/flows.d.ts +67 -0
- package/esm/bash/flows.js +203 -0
- package/esm/bash/git.d.ts +7 -0
- package/esm/bash/git.js +127 -0
- package/esm/bash/network.d.ts +24 -0
- package/esm/bash/network.js +282 -0
- package/esm/bash/quoting.d.ts +16 -0
- package/esm/bash/quoting.js +153 -0
- package/esm/bash/system.d.ts +16 -0
- package/esm/bash/system.js +82 -0
- package/esm/bash/tokenize.d.ts +67 -0
- package/esm/bash/tokenize.js +437 -0
- package/esm/bash/words.d.ts +9 -0
- package/esm/bash/words.js +24 -0
- package/esm/bash/wrappers.d.ts +32 -0
- package/esm/bash/wrappers.js +200 -0
- package/esm/classify.d.ts +22 -0
- package/esm/classify.js +223 -0
- package/esm/engine.d.ts +109 -0
- package/esm/engine.js +108 -0
- package/esm/env.d.ts +13 -0
- package/esm/env.js +34 -0
- package/esm/findings.d.ts +7 -0
- package/esm/findings.js +9 -0
- package/esm/glob.d.ts +15 -0
- package/esm/glob.js +69 -0
- package/esm/grants.d.ts +36 -0
- package/esm/grants.js +99 -0
- package/esm/index.d.ts +19 -0
- package/esm/index.js +18 -0
- package/esm/node-fs.d.ts +6 -0
- package/esm/node-fs.js +34 -0
- package/esm/paths.d.ts +43 -0
- package/esm/paths.js +121 -0
- package/esm/policy.d.ts +21 -0
- package/esm/policy.js +85 -0
- package/esm/prompt.d.ts +14 -0
- package/esm/prompt.js +79 -0
- package/esm/roots.d.ts +75 -0
- package/esm/roots.js +148 -0
- package/esm/secret-files.d.ts +16 -0
- package/esm/secret-files.js +84 -0
- package/esm/secrets/collect.d.ts +19 -0
- package/esm/secrets/collect.js +60 -0
- package/esm/secrets/index.d.ts +5 -0
- package/esm/secrets/index.js +5 -0
- package/esm/secrets/parse.d.ts +19 -0
- package/esm/secrets/parse.js +140 -0
- package/esm/secrets/redact-result.d.ts +8 -0
- package/esm/secrets/redact-result.js +67 -0
- package/esm/secrets/rules.d.ts +34 -0
- package/esm/secrets/rules.js +101 -0
- package/esm/secrets/vault.d.ts +48 -0
- package/esm/secrets/vault.js +158 -0
- package/esm/signals.d.ts +14 -0
- package/esm/signals.js +26 -0
- package/esm/tool-input.d.ts +13 -0
- package/esm/tool-input.js +40 -0
- package/esm/types.d.ts +146 -0
- package/esm/types.js +20 -0
- package/findings.d.ts +7 -0
- package/findings.js +13 -0
- package/glob.d.ts +15 -0
- package/glob.js +73 -0
- package/grants.d.ts +36 -0
- package/grants.js +110 -0
- package/index.d.ts +19 -0
- package/index.js +52 -0
- package/node-fs.d.ts +6 -0
- package/node-fs.js +37 -0
- package/package.json +35 -3
- package/paths.d.ts +43 -0
- package/paths.js +164 -0
- package/policy.d.ts +21 -0
- package/policy.js +88 -0
- package/prompt.d.ts +14 -0
- package/prompt.js +83 -0
- package/roots.d.ts +75 -0
- package/roots.js +192 -0
- package/secret-files.d.ts +16 -0
- package/secret-files.js +124 -0
- package/secrets/collect.d.ts +19 -0
- package/secrets/collect.js +96 -0
- package/secrets/index.d.ts +5 -0
- package/secrets/index.js +21 -0
- package/secrets/parse.d.ts +19 -0
- package/secrets/parse.js +145 -0
- package/secrets/redact-result.d.ts +8 -0
- package/secrets/redact-result.js +70 -0
- package/secrets/rules.d.ts +34 -0
- package/secrets/rules.js +107 -0
- package/secrets/vault.d.ts +48 -0
- package/secrets/vault.js +163 -0
- package/signals.d.ts +14 -0
- package/signals.js +29 -0
- package/tool-input.d.ts +13 -0
- package/tool-input.js +46 -0
- package/types.d.ts +146 -0
- package/types.js +24 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrappers that run another command (`env`, `sudo`, `timeout`, `xargs`, …).
|
|
3
|
+
* They come off the front of a command before its rules apply, so `/bin/rm`,
|
|
4
|
+
* `command rm` and `env rm` are all `rm`.
|
|
5
|
+
*/
|
|
6
|
+
import type { Analyzer, Arg } from './analyze';
|
|
7
|
+
/** How `xargs` feeds the command it runs. */
|
|
8
|
+
export interface XargsInput {
|
|
9
|
+
/** `xargs -I {}`: the string each input line replaces. */
|
|
10
|
+
replace?: string;
|
|
11
|
+
/** `xargs -a file`: the input comes from a file, not stdin. */
|
|
12
|
+
fromFile: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** What the wrappers taken off so far change about the command they run. */
|
|
15
|
+
export interface WrapperScope {
|
|
16
|
+
sudo: boolean;
|
|
17
|
+
/** Set once `xargs` came off: more operands arrive from its input. */
|
|
18
|
+
xargs: XargsInput | null;
|
|
19
|
+
/** `env -C dir` moved the working dir for this one command. */
|
|
20
|
+
chdir: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The operands of the command `xargs` runs. The words on stdin join them when
|
|
24
|
+
* they are known, and the roots of a file listing stand in for the names it
|
|
25
|
+
* prints. Otherwise the names cannot be seen, which is a signal.
|
|
26
|
+
*/
|
|
27
|
+
export declare function xargsOperands(analyzer: Analyzer, name: string, rest: Arg[], input: XargsInput): Arg[];
|
|
28
|
+
/**
|
|
29
|
+
* Take one wrapper off the front of the command. Returns the index of the
|
|
30
|
+
* next word, or `i` when `args[i]` is not a wrapper.
|
|
31
|
+
*/
|
|
32
|
+
export declare function stripWrapper(analyzer: Analyzer, args: Arg[], i: number, scope: WrapperScope): number;
|
package/bash/wrappers.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Wrappers that run another command (`env`, `sudo`, `timeout`, `xargs`, …).
|
|
4
|
+
* They come off the front of a command before its rules apply, so `/bin/rm`,
|
|
5
|
+
* `command rm` and `env rm` are all `rm`.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.xargsOperands = xargsOperands;
|
|
42
|
+
exports.stripWrapper = stripWrapper;
|
|
43
|
+
const path = __importStar(require("node:path"));
|
|
44
|
+
const args_1 = require("./args");
|
|
45
|
+
const commands_1 = require("./commands");
|
|
46
|
+
/** `xargs` options whose value is the next word (`-i`, `-e`, `-l` and `--replace` take only a glued one). */
|
|
47
|
+
const XARGS_WITH_VALUE = /^-[InPLdEsa]$|^--(max-args|max-procs|max-lines|delimiter|eof|max-chars|arg-file)$/;
|
|
48
|
+
function xargs(analyzer, args, i, scope) {
|
|
49
|
+
const input = (scope.xargs ??= { fromFile: false });
|
|
50
|
+
let k = i + 1;
|
|
51
|
+
while (k < args.length) {
|
|
52
|
+
const t = args[k].text;
|
|
53
|
+
if (t === '--')
|
|
54
|
+
return k + 1;
|
|
55
|
+
if (t === '-I')
|
|
56
|
+
input.replace = args[k + 1]?.text ?? '{}';
|
|
57
|
+
else if (t === '-i' || t === '--replace')
|
|
58
|
+
input.replace = '{}';
|
|
59
|
+
else if (/^-[Ii]./.test(t))
|
|
60
|
+
input.replace = t.slice(2);
|
|
61
|
+
else if (t.startsWith('--replace='))
|
|
62
|
+
input.replace = t.slice('--replace='.length);
|
|
63
|
+
if (t === '-a' || t === '--arg-file' || t.startsWith('--arg-file=')) {
|
|
64
|
+
input.fromFile = true;
|
|
65
|
+
const file = t.startsWith('--arg-file=') ? { ...args[k], text: t.slice('--arg-file='.length) } : args[k + 1];
|
|
66
|
+
if (file)
|
|
67
|
+
analyzer.addPath(file, 'read', true);
|
|
68
|
+
}
|
|
69
|
+
if (XARGS_WITH_VALUE.test(t))
|
|
70
|
+
k += 2;
|
|
71
|
+
else if (t.startsWith('-') && t !== '-')
|
|
72
|
+
k++;
|
|
73
|
+
else
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
return k;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The operands of the command `xargs` runs. The words on stdin join them when
|
|
80
|
+
* they are known, and the roots of a file listing stand in for the names it
|
|
81
|
+
* prints. Otherwise the names cannot be seen, which is a signal.
|
|
82
|
+
*/
|
|
83
|
+
function xargsOperands(analyzer, name, rest, input) {
|
|
84
|
+
const { replace } = input;
|
|
85
|
+
const { words, listedUnder } = input.fromFile ? { words: null, listedUnder: null } : analyzer.stdin;
|
|
86
|
+
if (words !== null) {
|
|
87
|
+
if (!replace)
|
|
88
|
+
return [...rest, ...words];
|
|
89
|
+
return rest.flatMap((a) => (a.text.includes(replace) ? words.map((w) => ({ ...a, text: a.text.split(replace).join(w.text) })) : [a]));
|
|
90
|
+
}
|
|
91
|
+
if (listedUnder !== null) {
|
|
92
|
+
if (!replace)
|
|
93
|
+
return [...rest, ...listedUnder];
|
|
94
|
+
return rest.flatMap((a) => (a.text.includes(replace) ? listedUnder : [a]));
|
|
95
|
+
}
|
|
96
|
+
if (!commands_1.NO_OPERANDS.has(name)) {
|
|
97
|
+
analyzer.flag('stdin-operands', `xargs gives ${name} file names from its input, which cannot be checked before it runs`);
|
|
98
|
+
}
|
|
99
|
+
return rest;
|
|
100
|
+
}
|
|
101
|
+
const ENV_VALUE_OPTIONS = new Set(['-u', '--unset', '-C', '--chdir', '-S', '--split-string']);
|
|
102
|
+
const RUNNER_VALUE_OPTIONS = new Set(['-c', '--call', '-p', '--package']);
|
|
103
|
+
/**
|
|
104
|
+
* `npx -c 'cmd'`, `pnpm exec --call=cmd`: the runner's options from `from` up
|
|
105
|
+
* to the package name, its `-c` shell string, and where the package name is.
|
|
106
|
+
*/
|
|
107
|
+
function runnerOptions(args, from) {
|
|
108
|
+
const { options, next } = (0, args_1.leadingOptions)(args.slice(from), RUNNER_VALUE_OPTIONS);
|
|
109
|
+
const call = options.find((o) => o.key === '-c' || o.key === '--call');
|
|
110
|
+
return { call: call ? (call.value ?? null) : undefined, next: from + next };
|
|
111
|
+
}
|
|
112
|
+
/** A runner's `-c` string: analyzed as a nested command, or dynamic when it is not known. */
|
|
113
|
+
function runCall(analyzer, call, what) {
|
|
114
|
+
if (!call || call.unknown)
|
|
115
|
+
analyzer.dynamic(`it runs ${what} with a string from a variable`);
|
|
116
|
+
else
|
|
117
|
+
analyzer.nested(call.text, `it runs a string with ${what}`);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Take one wrapper off the front of the command. Returns the index of the
|
|
121
|
+
* next word, or `i` when `args[i]` is not a wrapper.
|
|
122
|
+
*/
|
|
123
|
+
function stripWrapper(analyzer, args, i, scope) {
|
|
124
|
+
const name = path.basename(args[i].text);
|
|
125
|
+
const opts = (withValue, flags = /^-/) => {
|
|
126
|
+
let k = i + 1;
|
|
127
|
+
while (k < args.length) {
|
|
128
|
+
const t = args[k].text;
|
|
129
|
+
if (t === '--')
|
|
130
|
+
return k + 1;
|
|
131
|
+
if (withValue.test(t))
|
|
132
|
+
k += 2;
|
|
133
|
+
else if (flags.test(t) && t !== '-')
|
|
134
|
+
k++;
|
|
135
|
+
else
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
return k;
|
|
139
|
+
};
|
|
140
|
+
switch (name) {
|
|
141
|
+
case 'command':
|
|
142
|
+
case 'builtin':
|
|
143
|
+
case 'nohup':
|
|
144
|
+
case 'chronic':
|
|
145
|
+
case 'unbuffer':
|
|
146
|
+
return opts(/^$/);
|
|
147
|
+
case 'time':
|
|
148
|
+
return opts(/^$/, /^-[pv]+$/);
|
|
149
|
+
case 'exec':
|
|
150
|
+
return opts(/^-a$/, /^-[cl]+$/);
|
|
151
|
+
case 'nice':
|
|
152
|
+
return opts(/^(-n|--adjustment)$/, /^-/);
|
|
153
|
+
case 'ionice':
|
|
154
|
+
return opts(/^-[cnp]$/, /^-/);
|
|
155
|
+
case 'stdbuf':
|
|
156
|
+
return opts(/^-[ioe]$/, /^-/);
|
|
157
|
+
case 'caffeinate':
|
|
158
|
+
return opts(/^-[tw]$/, /^-/);
|
|
159
|
+
case 'timeout': {
|
|
160
|
+
const k = opts(/^-[sk]$/, /^-/);
|
|
161
|
+
return k + 1;
|
|
162
|
+
}
|
|
163
|
+
case 'env': {
|
|
164
|
+
// `env -` is `env -i`.
|
|
165
|
+
const from = args[i + 1]?.text === '-' ? i + 2 : i + 1;
|
|
166
|
+
const rest = args.slice(from);
|
|
167
|
+
const { options, next } = (0, args_1.leadingOptions)(rest, ENV_VALUE_OPTIONS);
|
|
168
|
+
for (const { key, value } of options) {
|
|
169
|
+
if (key === '-S' || key === '--split-string') {
|
|
170
|
+
const words = [value, ...rest.slice(next)].filter((a) => a !== undefined);
|
|
171
|
+
if (words.some((a) => a.unknown))
|
|
172
|
+
analyzer.dynamic('it runs env -S with a string from a variable');
|
|
173
|
+
else
|
|
174
|
+
analyzer.nested(words.map((a) => a.text).join(' '), 'it runs a string with env -S');
|
|
175
|
+
return args.length;
|
|
176
|
+
}
|
|
177
|
+
if (key === '-C' || key === '--chdir') {
|
|
178
|
+
analyzer.base = value && !value.unknown ? analyzer.toAbs(value.text) : null;
|
|
179
|
+
scope.chdir = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return from + next;
|
|
183
|
+
}
|
|
184
|
+
case 'sudo':
|
|
185
|
+
case 'doas':
|
|
186
|
+
case 'pkexec': {
|
|
187
|
+
scope.sudo = true;
|
|
188
|
+
analyzer.flag('sudo', `it runs ${name} to get root`);
|
|
189
|
+
const k = opts(/^(-[ugCDhprtTU]|--user|--group)$/, /^-/);
|
|
190
|
+
const shellFlag = args.slice(i + 1, k).some((a) => /^-[a-zA-Z]*[si]/.test(a.text));
|
|
191
|
+
if (shellFlag && k < args.length) {
|
|
192
|
+
analyzer.nested(args.slice(k).map((a) => a.text).join(' '), `it runs a shell with ${name} -s`);
|
|
193
|
+
return args.length;
|
|
194
|
+
}
|
|
195
|
+
return k;
|
|
196
|
+
}
|
|
197
|
+
case 'su': {
|
|
198
|
+
scope.sudo = true;
|
|
199
|
+
analyzer.flag('sudo', 'it runs su to switch user');
|
|
200
|
+
const c = args.findIndex((a, k) => k > i && (a.text === '-c' || a.text === '--command'));
|
|
201
|
+
if (c > 0 && args[c + 1])
|
|
202
|
+
analyzer.nested(args[c + 1].text, 'it runs a string with su -c');
|
|
203
|
+
return args.length;
|
|
204
|
+
}
|
|
205
|
+
case 'xargs':
|
|
206
|
+
return xargs(analyzer, args, i, scope);
|
|
207
|
+
case 'watch': {
|
|
208
|
+
const k = opts(/^-[nqd]$|^--(interval|differences)$/, /^-/);
|
|
209
|
+
analyzer.nested(args.slice(k).map((a) => a.text).join(' '), 'it runs a string with watch');
|
|
210
|
+
return args.length;
|
|
211
|
+
}
|
|
212
|
+
case 'npx':
|
|
213
|
+
case 'pnpx':
|
|
214
|
+
case 'bunx': {
|
|
215
|
+
const { call, next } = runnerOptions(args, i + 1);
|
|
216
|
+
if (call === undefined)
|
|
217
|
+
return next;
|
|
218
|
+
runCall(analyzer, call, `${name} -c`);
|
|
219
|
+
return args.length;
|
|
220
|
+
}
|
|
221
|
+
case 'pnpm':
|
|
222
|
+
case 'npm':
|
|
223
|
+
case 'yarn':
|
|
224
|
+
case 'bun': {
|
|
225
|
+
const sub = args[i + 1]?.text;
|
|
226
|
+
if (sub !== 'exec' && sub !== 'dlx' && sub !== 'x')
|
|
227
|
+
return i;
|
|
228
|
+
const { call, next } = runnerOptions(args, i + 2);
|
|
229
|
+
if (call === undefined)
|
|
230
|
+
return next;
|
|
231
|
+
runCall(analyzer, call, `${name} ${sub} -c`);
|
|
232
|
+
return args.length;
|
|
233
|
+
}
|
|
234
|
+
default:
|
|
235
|
+
return i;
|
|
236
|
+
}
|
|
237
|
+
}
|
package/classify.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn one built-in tool call into a `PermissionRequest`: its canonical
|
|
3
|
+
* targets, their zones, and the risk signals, each with the sentence that
|
|
4
|
+
* explains it. No decision is made here.
|
|
5
|
+
*/
|
|
6
|
+
import { type Roots } from './roots';
|
|
7
|
+
import { type SecretVault } from './secrets';
|
|
8
|
+
import { type BuiltinTool, type FsProbe, type PermissionRequest } from './types';
|
|
9
|
+
export interface ClassifyContext {
|
|
10
|
+
cwd: string;
|
|
11
|
+
homeDir: string;
|
|
12
|
+
roots: Roots;
|
|
13
|
+
fs: FsProbe;
|
|
14
|
+
vault: SecretVault;
|
|
15
|
+
/** Env names the host marked secret (`refreshSecrets({ env })`). */
|
|
16
|
+
secretEnvNames: ReadonlySet<string>;
|
|
17
|
+
}
|
|
18
|
+
export declare function classify(call: {
|
|
19
|
+
toolCallId: string;
|
|
20
|
+
toolName: BuiltinTool;
|
|
21
|
+
input: Record<string, unknown>;
|
|
22
|
+
}, ctx: ClassifyContext): Promise<PermissionRequest>;
|
package/classify.js
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Turn one built-in tool call into a `PermissionRequest`: its canonical
|
|
4
|
+
* targets, their zones, and the risk signals, each with the sentence that
|
|
5
|
+
* explains it. No decision is made here.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.classify = classify;
|
|
42
|
+
const path = __importStar(require("node:path"));
|
|
43
|
+
const analyze_1 = require("./bash/analyze");
|
|
44
|
+
const findings_1 = require("./findings");
|
|
45
|
+
const glob_1 = require("./glob");
|
|
46
|
+
const paths_1 = require("./paths");
|
|
47
|
+
const roots_1 = require("./roots");
|
|
48
|
+
const secret_files_1 = require("./secret-files");
|
|
49
|
+
const secrets_1 = require("./secrets");
|
|
50
|
+
const tool_input_1 = require("./tool-input");
|
|
51
|
+
const types_1 = require("./types");
|
|
52
|
+
/** Zones stricter than the plain ones, strictest first. */
|
|
53
|
+
const RESTRICTIVE = ['hard-deny', 'protected', 'ask-write'];
|
|
54
|
+
const ZONE_SIGNAL = {
|
|
55
|
+
'hard-deny': 'hard-deny',
|
|
56
|
+
protected: 'protected-write',
|
|
57
|
+
'ask-write': 'ask-write',
|
|
58
|
+
outside: 'outside-workspace',
|
|
59
|
+
unresolvable: 'unresolvable-path'
|
|
60
|
+
};
|
|
61
|
+
function strictness(zone) {
|
|
62
|
+
const at = RESTRICTIVE.indexOf(zone);
|
|
63
|
+
return at < 0 ? RESTRICTIVE.length : at;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Where a path sits. The canonical path decides the zone, but the lexical
|
|
67
|
+
* spelling can only make it stricter (`~/.ssh` reached through a link). With
|
|
68
|
+
* no canonical path, only a restrictive lexical zone stands.
|
|
69
|
+
*/
|
|
70
|
+
function placeOf(lexical, canonical, access, roots) {
|
|
71
|
+
const lexicalHit = (0, roots_1.zoneOf)(lexical, access, roots);
|
|
72
|
+
if (canonical === null) {
|
|
73
|
+
return RESTRICTIVE.includes(lexicalHit.zone)
|
|
74
|
+
? { path: lexical, zone: lexicalHit.zone, label: lexicalHit.root.label }
|
|
75
|
+
: { path: lexical, zone: 'unresolvable', label: 'unresolvable' };
|
|
76
|
+
}
|
|
77
|
+
let hit = (0, roots_1.zoneOf)(canonical, access, roots);
|
|
78
|
+
if (canonical !== lexical && strictness(lexicalHit.zone) < strictness(hit.zone))
|
|
79
|
+
hit = lexicalHit;
|
|
80
|
+
return { path: canonical, zone: hit.zone, label: hit.root?.label ?? hit.zone };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The target for one path. A glob is matched as a pattern first: if it can
|
|
84
|
+
* expand into a restrictive root, it takes that root's zone.
|
|
85
|
+
*/
|
|
86
|
+
async function makeTarget(raw, abs, access, ctx, glob) {
|
|
87
|
+
const lexical = path.resolve(abs);
|
|
88
|
+
const canonical = await (0, paths_1.canonicalize)(abs, ctx.fs);
|
|
89
|
+
const secretFile = (0, secret_files_1.isSecretFile)(lexical) ||
|
|
90
|
+
(canonical !== null && (0, secret_files_1.isSecretFile)(canonical)) ||
|
|
91
|
+
(glob !== undefined && (0, secret_files_1.globNamesSecretFiles)(path.basename(lexical)));
|
|
92
|
+
const globHit = glob
|
|
93
|
+
? ((0, roots_1.globZoneOf)(lexical, access, ctx.roots, glob.dot) ?? (canonical ? (0, roots_1.globZoneOf)(canonical, access, ctx.roots, glob.dot) : undefined))
|
|
94
|
+
: undefined;
|
|
95
|
+
const place = globHit
|
|
96
|
+
? { path: canonical ?? lexical, zone: globHit.zone, label: globHit.root.label }
|
|
97
|
+
: placeOf(lexical, canonical, access, ctx.roots);
|
|
98
|
+
return { raw, path: place.path, access, zone: place.zone, secretFile, label: place.label, ...(glob ? { glob: true } : {}) };
|
|
99
|
+
}
|
|
100
|
+
function zoneSentence(t, shown) {
|
|
101
|
+
switch (t.zone) {
|
|
102
|
+
case 'hard-deny':
|
|
103
|
+
return `${shown} is in a credential store (${t.label}) that the agent may never touch`;
|
|
104
|
+
case 'protected':
|
|
105
|
+
return `${shown} is in the ${t.label}, which the agent may not change`;
|
|
106
|
+
case 'ask-write':
|
|
107
|
+
return `${shown} is a ${t.label}: a change there runs code later`;
|
|
108
|
+
case 'outside':
|
|
109
|
+
return `${shown} is outside the project`;
|
|
110
|
+
case 'unresolvable':
|
|
111
|
+
return `${shown} goes through a symlink that cannot be resolved`;
|
|
112
|
+
default:
|
|
113
|
+
return `${shown} is in the ${t.label}`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** `rm`, `dd of=`, `chmod -R`, …: destructive outside the project, or on a whole project or temp root. */
|
|
117
|
+
async function flagDestructive(checks, ctx, findings) {
|
|
118
|
+
const resetRoots = [...ctx.roots.workspace, ...ctx.roots.temp];
|
|
119
|
+
const wipesRoot = (p) => (0, roots_1.isRootItself)(resetRoots, p) || (/^[*?.[\]]+$/.test(path.basename(p)) && (0, roots_1.isRootItself)(resetRoots, path.dirname(p)));
|
|
120
|
+
for (const check of checks) {
|
|
121
|
+
const canonical = await (0, paths_1.canonicalize)(check.abs, ctx.fs);
|
|
122
|
+
if (canonical === null)
|
|
123
|
+
continue;
|
|
124
|
+
if (check.rootCounts && wipesRoot(canonical)) {
|
|
125
|
+
findings.flag('destructive', `it runs ${check.what} on ${check.raw}, which wipes a whole project or temp dir`);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const zone = (0, roots_1.zoneOf)(canonical, 'write', ctx.roots).zone;
|
|
129
|
+
if (zone !== 'workspace' && zone !== 'temp') {
|
|
130
|
+
findings.flag('destructive', `it runs ${check.what} on ${show(canonical, ctx)}, outside the project`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function show(p, ctx) {
|
|
135
|
+
return (0, paths_1.showPath)(p, ctx.roots.workspace[0]?.path, ctx.homeDir);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Copies, clipboard writes and transforms of a secret file. Redaction matches
|
|
139
|
+
* exact values, so a secret that is encoded, cut or copied into a plain file
|
|
140
|
+
* gets past it.
|
|
141
|
+
*/
|
|
142
|
+
async function flagFlows(flows, ctx, findings) {
|
|
143
|
+
const known = new Map();
|
|
144
|
+
const isSecret = async (abs) => {
|
|
145
|
+
if (!known.has(abs)) {
|
|
146
|
+
const canonical = await (0, paths_1.canonicalize)(abs, ctx.fs);
|
|
147
|
+
known.set(abs, (0, secret_files_1.isSecretFile)(abs) || (canonical !== null && (0, secret_files_1.isSecretFile)(canonical)));
|
|
148
|
+
}
|
|
149
|
+
return known.get(abs);
|
|
150
|
+
};
|
|
151
|
+
for (const flow of flows) {
|
|
152
|
+
for (const source of flow.sources) {
|
|
153
|
+
if (!(await isSecret(source.abs)))
|
|
154
|
+
continue;
|
|
155
|
+
const shown = show(path.resolve(source.abs), ctx);
|
|
156
|
+
if (flow.to.kind === 'transform') {
|
|
157
|
+
// An upload already asks, and says more.
|
|
158
|
+
if (!findings.signals.has('upload'))
|
|
159
|
+
findings.flag('secret-transform', `it runs ${flow.what} on the secret file ${shown}; redaction cannot match values it changes`);
|
|
160
|
+
}
|
|
161
|
+
else if (flow.to.kind === 'clipboard') {
|
|
162
|
+
findings.flag('upload', `it uploads data: ${flow.what} puts the secret file ${shown} on the clipboard`);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
const dir = flow.to.dir || (await isDir(flow.to.abs, ctx.fs));
|
|
166
|
+
const dest = dir ? path.join(flow.to.abs, path.basename(source.abs)) : flow.to.abs;
|
|
167
|
+
if (await isSecret(dest))
|
|
168
|
+
continue;
|
|
169
|
+
findings.flag('secret-into-plain-file', `it copies the secret file ${shown} into ${show(path.resolve(dest), ctx)} with ${flow.what}, which is not a secret file`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async function isDir(abs, fs) {
|
|
175
|
+
try {
|
|
176
|
+
await fs.readDir(abs);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async function bashTargets(command, ctx, findings) {
|
|
184
|
+
const analysis = (0, analyze_1.analyzeBash)(command, { cwd: ctx.cwd, homeDir: ctx.homeDir }, findings);
|
|
185
|
+
const targets = [];
|
|
186
|
+
for (const p of analysis.paths) {
|
|
187
|
+
const target = await makeTarget(p.raw, p.abs, p.access, ctx, p.glob ? { dot: analysis.dotglob } : undefined);
|
|
188
|
+
// A bare word is only a target when it leads somewhere that matters.
|
|
189
|
+
const plain = target.zone === 'workspace' || target.zone === 'temp' || target.zone === 'trusted';
|
|
190
|
+
if (p.hard || target.secretFile || !plain)
|
|
191
|
+
targets.push(target);
|
|
192
|
+
}
|
|
193
|
+
await flagDestructive(analysis.destructiveChecks, ctx, findings);
|
|
194
|
+
await flagFlows(analysis.flows, ctx, findings);
|
|
195
|
+
const secretVars = analysis.envRefs.filter((name) => secrets_1.SECRET_KEY_PATTERN.test(name) || ctx.secretEnvNames.has(name));
|
|
196
|
+
if (secretVars.length > 0) {
|
|
197
|
+
findings.flag('secret-env-expansion', `it expands ${secretVars.map((n) => `$${n}`).join(', ')}, which ${secretVars.length === 1 ? 'holds a secret' : 'hold secrets'} the command can send or print`);
|
|
198
|
+
}
|
|
199
|
+
return targets;
|
|
200
|
+
}
|
|
201
|
+
async function fileTarget(toolName, input, ctx) {
|
|
202
|
+
const kind = types_1.TOOL_KIND[toolName];
|
|
203
|
+
const rawPath = (0, tool_input_1.stringField)(input, 'path') ?? (kind === 'read' && toolName !== 'read' ? '.' : undefined);
|
|
204
|
+
const access = kind === 'write' ? 'write' : 'read';
|
|
205
|
+
const abs = rawPath === undefined ? null : (0, paths_1.resolveLikePi)(rawPath, ctx.cwd, ctx.homeDir);
|
|
206
|
+
if (abs === null) {
|
|
207
|
+
const shown = rawPath ?? '(no path)';
|
|
208
|
+
return { raw: shown, path: shown, access, zone: 'unresolvable', secretFile: false, label: 'unresolvable' };
|
|
209
|
+
}
|
|
210
|
+
return makeTarget(rawPath, abs, access, ctx, glob_1.GLOB_CHARS.test(rawPath) ? { dot: true } : undefined);
|
|
211
|
+
}
|
|
212
|
+
function uniqueTargets(targets) {
|
|
213
|
+
const seen = new Set();
|
|
214
|
+
return targets.filter((t) => {
|
|
215
|
+
const key = `${t.access}\0${t.path}`;
|
|
216
|
+
if (seen.has(key))
|
|
217
|
+
return false;
|
|
218
|
+
seen.add(key);
|
|
219
|
+
return true;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
async function classify(call, ctx) {
|
|
223
|
+
const { toolName, input } = call;
|
|
224
|
+
const findings = new findings_1.Findings();
|
|
225
|
+
const command = toolName === 'bash' ? (0, tool_input_1.stringField)(input, 'command') ?? '' : undefined;
|
|
226
|
+
const targets = uniqueTargets(command === undefined ? [await fileTarget(toolName, input, ctx)] : await bashTargets(command, ctx, findings));
|
|
227
|
+
const glob = toolName === 'grep' ? (0, tool_input_1.stringField)(input, 'glob') : undefined;
|
|
228
|
+
if (glob && (0, secret_files_1.globNamesSecretFiles)(glob))
|
|
229
|
+
findings.flag('secret-file', `the glob ${glob} names secret files; matches are redacted`);
|
|
230
|
+
for (const target of targets) {
|
|
231
|
+
const shown = show(target.path, ctx);
|
|
232
|
+
const signal = ZONE_SIGNAL[target.zone];
|
|
233
|
+
if (signal)
|
|
234
|
+
findings.flag(signal, zoneSentence(target, shown));
|
|
235
|
+
if (target.secretFile)
|
|
236
|
+
findings.flag('secret-file', `${shown} is a secret file: its values reach the model only as placeholders`);
|
|
237
|
+
}
|
|
238
|
+
const written = toolName === 'write' || toolName === 'edit' ? targets[0] : undefined;
|
|
239
|
+
if (written && !written.secretFile) {
|
|
240
|
+
const found = (0, tool_input_1.newTexts)(toolName, input)
|
|
241
|
+
.map((text) => ctx.vault.findSecret(text))
|
|
242
|
+
.find(Boolean);
|
|
243
|
+
if (found) {
|
|
244
|
+
const shown = show(written.path, ctx);
|
|
245
|
+
findings.flag('secret-into-plain-file', found.kind === 'value'
|
|
246
|
+
? `the new content of ${shown} holds the value of ${found.name}, and ${shown} is not a secret file`
|
|
247
|
+
: `the new content of ${shown} holds the placeholder for ${found.name}; it would be written as literal text, because ${shown} is not a secret file`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return {
|
|
251
|
+
toolCallId: call.toolCallId,
|
|
252
|
+
toolName,
|
|
253
|
+
kind: types_1.TOOL_KIND[toolName],
|
|
254
|
+
targets,
|
|
255
|
+
...(command === undefined ? {} : { command: (0, analyze_1.normalizeCommand)(command) }),
|
|
256
|
+
signals: [...findings.signals],
|
|
257
|
+
signalReasons: findings.reasons
|
|
258
|
+
};
|
|
259
|
+
}
|
package/engine.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The permission engine: classify a tool call, decide it against the mode,
|
|
3
|
+
* the grants and the fixed lists, and keep secrets out of what the model sees.
|
|
4
|
+
*
|
|
5
|
+
* Harness-neutral. A harness adapter (the pi lane in `@agentic-kit/pi`) owns the
|
|
6
|
+
* glue: call `evaluate` on a tool call and show `decision.prompt` on an ask,
|
|
7
|
+
* call `restoreInput` with the decision before the tool runs, and
|
|
8
|
+
* `redactResult` on every result. The engine holds no state about one call
|
|
9
|
+
* between those steps: the decision carries all of it.
|
|
10
|
+
*/
|
|
11
|
+
import { type PolicyRules } from './policy';
|
|
12
|
+
import { type FsProbe, type Grant, type PermissionDecision, type PermissionMode } from './types';
|
|
13
|
+
/**
|
|
14
|
+
* The fixed paths a host knows about. Every entry is an absolute path (a file
|
|
15
|
+
* or a directory, matched as "equal or inside") unless it says otherwise.
|
|
16
|
+
*/
|
|
17
|
+
export interface RootsInput {
|
|
18
|
+
/** The project folders. Defaults to `[cwd]`. */
|
|
19
|
+
workspace?: string[];
|
|
20
|
+
/** Read-only roots the agent needs (skills, prompts, harness docs). Writes there are DENY. */
|
|
21
|
+
trustedRead?: string[];
|
|
22
|
+
/** Roots the agent may read and run from (harness scripts). Writes there are DENY. */
|
|
23
|
+
trustedExec?: string[];
|
|
24
|
+
/** Scratch dirs: read and write without a prompt. Defaults to the OS temp dirs. */
|
|
25
|
+
temp?: string[];
|
|
26
|
+
/** Added to the built-in credential list (`defaultHardDeny(homeDir)`). Read, write and run are DENY. */
|
|
27
|
+
hardDeny?: string[];
|
|
28
|
+
/** Absolute paths whose writes are DENY in every mode (the permission store, the agent dir). */
|
|
29
|
+
protectedWrite?: string[];
|
|
30
|
+
/** Added to the per-workspace ask-write list (`.git/hooks`, `.husky`, …). Writes ask outside Full mode. */
|
|
31
|
+
askWrite?: string[];
|
|
32
|
+
}
|
|
33
|
+
export interface SecretSourceInput {
|
|
34
|
+
/** Extra secret values the host knows of (a key it just minted). Name → value. */
|
|
35
|
+
values?: Record<string, string>;
|
|
36
|
+
/**
|
|
37
|
+
* The environment the tools run with. Values whose names look secret (or
|
|
38
|
+
* that are token-shaped) are redacted too, so `env | grep TOKEN` shows
|
|
39
|
+
* placeholders. Kept across refreshes.
|
|
40
|
+
*/
|
|
41
|
+
env?: Record<string, string | undefined>;
|
|
42
|
+
}
|
|
43
|
+
export interface PermissionEngineOptions {
|
|
44
|
+
cwd: string;
|
|
45
|
+
homeDir: string;
|
|
46
|
+
roots?: RootsInput;
|
|
47
|
+
/**
|
|
48
|
+
* More hard-deny paths, listed again at every decision: entries that can
|
|
49
|
+
* appear while a session runs (another tool's data dir, a new store file).
|
|
50
|
+
* A throw fails the decision, and the call is blocked.
|
|
51
|
+
*/
|
|
52
|
+
liveHardDeny?: () => Promise<readonly string[]>;
|
|
53
|
+
/** Defaults to the Node fs (`nodeFsProbe`). */
|
|
54
|
+
fs?: FsProbe;
|
|
55
|
+
/** Per-install HMAC key for placeholder suffixes. Without one, a random key per engine. */
|
|
56
|
+
placeholderKey?: string;
|
|
57
|
+
/** Non-fatal problems (an unreadable `.env`): the engine goes on with what it has. */
|
|
58
|
+
onError?: (error: unknown, where: string) => void;
|
|
59
|
+
}
|
|
60
|
+
export interface ToolCallInput {
|
|
61
|
+
toolCallId: string;
|
|
62
|
+
toolName: string;
|
|
63
|
+
input: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
export interface EvaluateContext {
|
|
66
|
+
mode: PermissionMode;
|
|
67
|
+
/** Chat and project grants together; the engine checks chat grants first. */
|
|
68
|
+
grants: readonly Grant[];
|
|
69
|
+
/** User allow and deny rules. Reserved: v1 has no rule editor. */
|
|
70
|
+
rules?: PolicyRules;
|
|
71
|
+
}
|
|
72
|
+
export interface ToolResultInput {
|
|
73
|
+
toolName: string;
|
|
74
|
+
content: readonly unknown[];
|
|
75
|
+
details?: unknown;
|
|
76
|
+
}
|
|
77
|
+
export interface RedactedResult {
|
|
78
|
+
content: unknown[];
|
|
79
|
+
details?: unknown;
|
|
80
|
+
/** `true` when at least one value was replaced. */
|
|
81
|
+
changed: boolean;
|
|
82
|
+
}
|
|
83
|
+
export interface PermissionEngine {
|
|
84
|
+
/**
|
|
85
|
+
* Classify and decide one tool call. Returns `null` for a tool this layer
|
|
86
|
+
* does not own (anything that is not a pi built-in), so the next gate decides.
|
|
87
|
+
*/
|
|
88
|
+
evaluate(call: ToolCallInput, context: EvaluateContext): Promise<PermissionDecision | null>;
|
|
89
|
+
/**
|
|
90
|
+
* Put real secret values back into the call's input, in place, where that is
|
|
91
|
+
* safe: a write or edit to a secret file always, a bash command only when
|
|
92
|
+
* `approved` (a person saw the placeholder in the command and allowed it).
|
|
93
|
+
*/
|
|
94
|
+
restoreInput(decision: PermissionDecision, input: Record<string, unknown>, options?: {
|
|
95
|
+
approved?: boolean;
|
|
96
|
+
}): void;
|
|
97
|
+
/**
|
|
98
|
+
* Replace known secret values in a tool result with placeholders. After a
|
|
99
|
+
* tool that can write, the vault first re-reads the workspace secret files,
|
|
100
|
+
* so a value that tool just wrote is already known. Throws on failure; the
|
|
101
|
+
* caller fails closed.
|
|
102
|
+
*/
|
|
103
|
+
redactResult(result: ToolResultInput): Promise<RedactedResult>;
|
|
104
|
+
/** Re-read the workspace secret files, and take in host values and env. */
|
|
105
|
+
refreshSecrets(extra?: SecretSourceInput): Promise<void>;
|
|
106
|
+
/** Add a value the host knows is secret (a minted API key). */
|
|
107
|
+
addSecret(name: string, value: string): void;
|
|
108
|
+
}
|
|
109
|
+
export declare function createPermissionEngine(options: PermissionEngineOptions): PermissionEngine;
|