@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
package/esm/env.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The environment a shell command gets. A host process inherits the user's
|
|
3
|
+
* shell environment, with provider keys and tokens in it; a command that
|
|
4
|
+
* expands one can send it out without the model ever seeing the value. The
|
|
5
|
+
* host passes the environment of each command through `scrubEnv`.
|
|
6
|
+
*/
|
|
7
|
+
import { SECRET_KEY_PATTERN, TOKEN_SHAPE_RES } from './secrets';
|
|
8
|
+
/** A path is an address, not a secret (`SSH_AUTH_SOCK`, `*_KEY_FILE`). */
|
|
9
|
+
const isPath = (value) => value.startsWith('/') && !/\s/.test(value);
|
|
10
|
+
/**
|
|
11
|
+
* Variables that change how the shell itself behaves before the command runs:
|
|
12
|
+
* a startup script it loads, or a search path that sends a relative `cd`
|
|
13
|
+
* somewhere the analyzer cannot see.
|
|
14
|
+
*/
|
|
15
|
+
const SHELL_STARTUP = new Set(['BASH_ENV', 'ENV', 'CDPATH']);
|
|
16
|
+
/**
|
|
17
|
+
* A copy of `env` without the variables that hold a secret: a name that looks
|
|
18
|
+
* secret (the vault's key rule), whatever the value's length, unless the value
|
|
19
|
+
* is a path; or a value with a token shape, a URL password included. The
|
|
20
|
+
* shell startup variables go too.
|
|
21
|
+
*/
|
|
22
|
+
export function scrubEnv(env) {
|
|
23
|
+
const kept = {};
|
|
24
|
+
for (const [name, value] of Object.entries(env)) {
|
|
25
|
+
if (value === undefined || SHELL_STARTUP.has(name))
|
|
26
|
+
continue;
|
|
27
|
+
if (SECRET_KEY_PATTERN.test(name) && !isPath(value))
|
|
28
|
+
continue;
|
|
29
|
+
if (TOKEN_SHAPE_RES.some(({ re }) => re.test(value)))
|
|
30
|
+
continue;
|
|
31
|
+
kept[name] = value;
|
|
32
|
+
}
|
|
33
|
+
return kept;
|
|
34
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Signal } from './types';
|
|
2
|
+
/** The signals raised for one call, keeping the first and most specific sentence for each. */
|
|
3
|
+
export declare class Findings {
|
|
4
|
+
readonly signals: Set<Signal>;
|
|
5
|
+
readonly reasons: Partial<Record<Signal, string>>;
|
|
6
|
+
flag(signal: Signal, reason: string): void;
|
|
7
|
+
}
|
package/esm/findings.js
ADDED
package/esm/glob.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a path with an unexpanded glob can expand into a root. Bash expands
|
|
3
|
+
* `*`, `?` and `[…]` one path component at a time, so the pattern is matched
|
|
4
|
+
* component by component against the root's own path. A pattern that reaches
|
|
5
|
+
* the root can name the root or anything inside it.
|
|
6
|
+
*/
|
|
7
|
+
export declare const GLOB_CHARS: RegExp;
|
|
8
|
+
export interface GlobOptions {
|
|
9
|
+
/** `*` and `?` match a leading `.` (bash `dotglob`). */
|
|
10
|
+
dot: boolean;
|
|
11
|
+
/** Compare case-insensitively (macOS and Windows file systems). */
|
|
12
|
+
caseInsensitive: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** `pattern` (absolute, `..` already resolved) can expand to `root` or a path inside it. */
|
|
15
|
+
export declare function globReaches(pattern: string, root: string, opts: GlobOptions): boolean;
|
package/esm/glob.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a path with an unexpanded glob can expand into a root. Bash expands
|
|
3
|
+
* `*`, `?` and `[…]` one path component at a time, so the pattern is matched
|
|
4
|
+
* component by component against the root's own path. A pattern that reaches
|
|
5
|
+
* the root can name the root or anything inside it.
|
|
6
|
+
*/
|
|
7
|
+
export const GLOB_CHARS = /[*?[]/;
|
|
8
|
+
function escape(ch) {
|
|
9
|
+
return ch.replace(/[.+^${}()|\\\]]/g, '\\$&');
|
|
10
|
+
}
|
|
11
|
+
/** One component as a regex; `null` when it cannot be compiled (match anything then). */
|
|
12
|
+
function componentRegex(c, ci) {
|
|
13
|
+
let re = '';
|
|
14
|
+
for (let i = 0; i < c.length; i++) {
|
|
15
|
+
const ch = c[i];
|
|
16
|
+
if (ch === '*')
|
|
17
|
+
re += '.*';
|
|
18
|
+
else if (ch === '?')
|
|
19
|
+
re += '.';
|
|
20
|
+
else if (ch === '[') {
|
|
21
|
+
const close = c.indexOf(']', i + 2);
|
|
22
|
+
if (close < 0) {
|
|
23
|
+
re += '\\[';
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const body = c
|
|
27
|
+
.slice(i + 1, close)
|
|
28
|
+
.replace(/\\/g, '\\\\')
|
|
29
|
+
.replace(/^(!?)\]/, '$1\\]');
|
|
30
|
+
re += `[${body.startsWith('!') ? `^${body.slice(1)}` : body}]`;
|
|
31
|
+
i = close;
|
|
32
|
+
}
|
|
33
|
+
else
|
|
34
|
+
re += escape(ch);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
return new RegExp(`^${re}$`, ci ? 'i' : '');
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function componentMatches(pattern, name, opts) {
|
|
44
|
+
if (!GLOB_CHARS.test(pattern)) {
|
|
45
|
+
return opts.caseInsensitive ? pattern.toLowerCase() === name.toLowerCase() : pattern === name;
|
|
46
|
+
}
|
|
47
|
+
if (name.startsWith('.') && !pattern.startsWith('.') && !opts.dot)
|
|
48
|
+
return false;
|
|
49
|
+
const re = componentRegex(pattern, opts.caseInsensitive);
|
|
50
|
+
return re === null || re.test(name);
|
|
51
|
+
}
|
|
52
|
+
function reach(pattern, i, root, j, opts) {
|
|
53
|
+
if (j === root.length)
|
|
54
|
+
return true;
|
|
55
|
+
if (i === pattern.length)
|
|
56
|
+
return false;
|
|
57
|
+
if (pattern[i] === '**') {
|
|
58
|
+
// globstar: zero or more whole components.
|
|
59
|
+
if (reach(pattern, i + 1, root, j, opts))
|
|
60
|
+
return true;
|
|
61
|
+
return (opts.dot || !root[j].startsWith('.')) && reach(pattern, i, root, j + 1, opts);
|
|
62
|
+
}
|
|
63
|
+
return componentMatches(pattern[i], root[j], opts) && reach(pattern, i + 1, root, j + 1, opts);
|
|
64
|
+
}
|
|
65
|
+
/** `pattern` (absolute, `..` already resolved) can expand to `root` or a path inside it. */
|
|
66
|
+
export function globReaches(pattern, root, opts) {
|
|
67
|
+
const parts = (p) => p.split('/').filter(Boolean);
|
|
68
|
+
return reach(parts(pattern), 0, parts(root), 0, opts);
|
|
69
|
+
}
|
package/esm/grants.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grants: "allow this again" answers, keyed by what the call touches.
|
|
3
|
+
*
|
|
4
|
+
* A path grant covers a file tool call whose every target sits inside its
|
|
5
|
+
* folder with the same access. A command grant covers a bash call with the
|
|
6
|
+
* exact same normalized command. Nothing ever matches on a tool name alone.
|
|
7
|
+
*/
|
|
8
|
+
import type { Access, Grant, GrantMatch, GrantOption, PermissionRequest, Signal } from './types';
|
|
9
|
+
/** Signals a grant must never cover: each such call needs its own yes. */
|
|
10
|
+
export declare const NEVER_GRANTED: readonly Signal[];
|
|
11
|
+
export declare function isGrantable(request: PermissionRequest): boolean;
|
|
12
|
+
/** `match` covers the request. */
|
|
13
|
+
export declare function matchCovers(match: GrantMatch, request: PermissionRequest): boolean;
|
|
14
|
+
/** The first grant that covers the request, or `undefined`. Never for an ungrantable request. */
|
|
15
|
+
export declare function findGrant(grants: readonly Grant[], request: PermissionRequest): Grant | undefined;
|
|
16
|
+
export declare const ACCESS_VERB: Readonly<Record<Access, string>>;
|
|
17
|
+
/** What a grant covers, in words: "reads in ~/work/lib/", "bash: pnpm dlx shadcn add button". */
|
|
18
|
+
export declare function describeGrant(match: GrantMatch, homeDir: string): string;
|
|
19
|
+
/** The scopes the user may pick on an ASK: this chat, or always for this project. */
|
|
20
|
+
export declare function grantOptionsFor(request: PermissionRequest, homeDir: string): GrantOption[];
|
|
21
|
+
/** The grant one of an ASK's options saves. */
|
|
22
|
+
export declare function grantFrom(option: GrantOption, now?: Date): Grant;
|
|
23
|
+
/** Two matches are the same grant (for de-duplicating a store). */
|
|
24
|
+
export declare function sameMatch(a: GrantMatch, b: GrantMatch): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Where a host keeps project grants. Desktop keeps one JSON file per project;
|
|
27
|
+
* a worktree project reads and writes its parent's.
|
|
28
|
+
*/
|
|
29
|
+
export interface GrantStore {
|
|
30
|
+
list(): readonly Grant[] | Promise<readonly Grant[]>;
|
|
31
|
+
add(grant: Grant): void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/** An in-memory store (tests, and hosts with no persistence). */
|
|
34
|
+
export declare function memoryGrantStore(initial?: readonly Grant[]): GrantStore & {
|
|
35
|
+
grants: Grant[];
|
|
36
|
+
};
|
package/esm/grants.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Grants: "allow this again" answers, keyed by what the call touches.
|
|
3
|
+
*
|
|
4
|
+
* A path grant covers a file tool call whose every target sits inside its
|
|
5
|
+
* folder with the same access. A command grant covers a bash call with the
|
|
6
|
+
* exact same normalized command. Nothing ever matches on a tool name alone.
|
|
7
|
+
*/
|
|
8
|
+
import { commonParent, contains, displayDir } from './paths';
|
|
9
|
+
import { SIGNAL_RULES } from './signals';
|
|
10
|
+
/** Signals a grant must never cover: each such call needs its own yes. */
|
|
11
|
+
export const NEVER_GRANTED = Object.keys(SIGNAL_RULES).filter((s) => !SIGNAL_RULES[s].grantable);
|
|
12
|
+
export function isGrantable(request) {
|
|
13
|
+
return !request.signals.some((s) => NEVER_GRANTED.includes(s));
|
|
14
|
+
}
|
|
15
|
+
/** `match` covers the request. */
|
|
16
|
+
export function matchCovers(match, request) {
|
|
17
|
+
if (match.type === 'command') {
|
|
18
|
+
return request.toolName === 'bash' && request.command !== undefined && request.command === match.command;
|
|
19
|
+
}
|
|
20
|
+
if (request.toolName === 'bash' || !pathGrantable(request))
|
|
21
|
+
return false;
|
|
22
|
+
return request.targets.every((t) => t.access === match.access && t.zone !== 'unresolvable' && contains(match.pathPrefix, t.path));
|
|
23
|
+
}
|
|
24
|
+
/** A glob outside the project: a folder grant cannot say where it expands to. */
|
|
25
|
+
function isOutsideGlob(t) {
|
|
26
|
+
return Boolean(t.glob) && t.zone !== 'workspace' && t.zone !== 'temp';
|
|
27
|
+
}
|
|
28
|
+
/** A folder grant can hold the request: it has targets, and none is a glob outside the project. */
|
|
29
|
+
function pathGrantable(request) {
|
|
30
|
+
return request.targets.length > 0 && !request.targets.some(isOutsideGlob);
|
|
31
|
+
}
|
|
32
|
+
/** The first grant that covers the request, or `undefined`. Never for an ungrantable request. */
|
|
33
|
+
export function findGrant(grants, request) {
|
|
34
|
+
if (!isGrantable(request))
|
|
35
|
+
return undefined;
|
|
36
|
+
const ordered = [...grants.filter((g) => g.scope === 'chat'), ...grants.filter((g) => g.scope !== 'chat')];
|
|
37
|
+
return ordered.find((g) => matchCovers(g.match, request));
|
|
38
|
+
}
|
|
39
|
+
export const ACCESS_VERB = { read: 'reads', write: 'writes', exec: 'runs' };
|
|
40
|
+
/** What a grant covers, in words: "reads in ~/work/lib/", "bash: pnpm dlx shadcn add button". */
|
|
41
|
+
export function describeGrant(match, homeDir) {
|
|
42
|
+
if (match.type === 'command')
|
|
43
|
+
return `bash: ${match.command}`;
|
|
44
|
+
return `${ACCESS_VERB[match.access]} in ${displayDir(match.pathPrefix, homeDir)}`;
|
|
45
|
+
}
|
|
46
|
+
/** The scopes the user may pick on an ASK: this chat, or always for this project. */
|
|
47
|
+
export function grantOptionsFor(request, homeDir) {
|
|
48
|
+
if (!isGrantable(request))
|
|
49
|
+
return [];
|
|
50
|
+
let match;
|
|
51
|
+
let pattern;
|
|
52
|
+
let always;
|
|
53
|
+
if (request.toolName === 'bash') {
|
|
54
|
+
if (!request.command)
|
|
55
|
+
return [];
|
|
56
|
+
match = { type: 'command', command: request.command };
|
|
57
|
+
pattern = request.command;
|
|
58
|
+
always = `Always allow ${request.command}`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
if (!pathGrantable(request))
|
|
62
|
+
return [];
|
|
63
|
+
const access = request.targets[0].access;
|
|
64
|
+
if (request.targets.some((t) => t.access !== access))
|
|
65
|
+
return [];
|
|
66
|
+
const prefix = commonParent(request.targets.map((t) => t.path));
|
|
67
|
+
match = { type: 'path', access, pathPrefix: prefix };
|
|
68
|
+
pattern = displayDir(prefix, homeDir);
|
|
69
|
+
always = `Always allow ${describeGrant(match, homeDir)}`;
|
|
70
|
+
}
|
|
71
|
+
return [
|
|
72
|
+
{ scope: 'chat', label: 'Allow in this chat', pattern, match },
|
|
73
|
+
{ scope: 'project', label: always, pattern, match }
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
/** The grant one of an ASK's options saves. */
|
|
77
|
+
export function grantFrom(option, now = new Date()) {
|
|
78
|
+
return { scope: option.scope, match: option.match, createdAt: now.toISOString() };
|
|
79
|
+
}
|
|
80
|
+
/** Two matches are the same grant (for de-duplicating a store). */
|
|
81
|
+
export function sameMatch(a, b) {
|
|
82
|
+
if (a.type === 'command' && b.type === 'command')
|
|
83
|
+
return a.command === b.command;
|
|
84
|
+
if (a.type === 'path' && b.type === 'path')
|
|
85
|
+
return a.access === b.access && a.pathPrefix === b.pathPrefix;
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
/** An in-memory store (tests, and hosts with no persistence). */
|
|
89
|
+
export function memoryGrantStore(initial = []) {
|
|
90
|
+
const grants = [...initial];
|
|
91
|
+
return {
|
|
92
|
+
grants,
|
|
93
|
+
list: () => grants,
|
|
94
|
+
add(grant) {
|
|
95
|
+
if (!grants.some((g) => sameMatch(g.match, grant.match)))
|
|
96
|
+
grants.push(grant);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@agentic-kit/permissions` — harness-neutral permissions for a coding agent:
|
|
3
|
+
* tool-call classification, the workspace boundary, bash risk signals, modes,
|
|
4
|
+
* grants, and secret redaction.
|
|
5
|
+
*
|
|
6
|
+
* No harness SDK is imported here. An adapter (the pi lane in `@agentic-kit/pi`)
|
|
7
|
+
* calls `evaluate` on each tool call, shows `decision.prompt` through its
|
|
8
|
+
* approval channel on `ask`, calls `restoreInput` before the tool runs and
|
|
9
|
+
* `redactResult` after. Classification, the policy and the vault stay internal.
|
|
10
|
+
*/
|
|
11
|
+
export { createPermissionEngine, type EvaluateContext, type PermissionEngine, type PermissionEngineOptions, type RedactedResult, type RootsInput, type SecretSourceInput, type ToolCallInput, type ToolResultInput } from './engine';
|
|
12
|
+
export { scrubEnv } from './env';
|
|
13
|
+
export { describeGrant, grantFrom, type GrantStore, memoryGrantStore, NEVER_GRANTED, sameMatch } from './grants';
|
|
14
|
+
export { nodeFsProbe } from './node-fs';
|
|
15
|
+
export type { PolicyRules } from './policy';
|
|
16
|
+
export { defaultHardDeny, defaultTempDirs, HOME_HARD_DENY, type KnownPath, WORKSPACE_ASK_WRITE, WORKSPACE_PROTECTED_WRITE } from './roots';
|
|
17
|
+
export { isSecretFile, isSecretFileName } from './secret-files';
|
|
18
|
+
export { findPlaceholders, PLACEHOLDER_PATTERN } from './secrets';
|
|
19
|
+
export * from './types';
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@agentic-kit/permissions` — harness-neutral permissions for a coding agent:
|
|
3
|
+
* tool-call classification, the workspace boundary, bash risk signals, modes,
|
|
4
|
+
* grants, and secret redaction.
|
|
5
|
+
*
|
|
6
|
+
* No harness SDK is imported here. An adapter (the pi lane in `@agentic-kit/pi`)
|
|
7
|
+
* calls `evaluate` on each tool call, shows `decision.prompt` through its
|
|
8
|
+
* approval channel on `ask`, calls `restoreInput` before the tool runs and
|
|
9
|
+
* `redactResult` after. Classification, the policy and the vault stay internal.
|
|
10
|
+
*/
|
|
11
|
+
export { createPermissionEngine } from './engine';
|
|
12
|
+
export { scrubEnv } from './env';
|
|
13
|
+
export { describeGrant, grantFrom, memoryGrantStore, NEVER_GRANTED, sameMatch } from './grants';
|
|
14
|
+
export { nodeFsProbe } from './node-fs';
|
|
15
|
+
export { defaultHardDeny, defaultTempDirs, HOME_HARD_DENY, WORKSPACE_ASK_WRITE, WORKSPACE_PROTECTED_WRITE } from './roots';
|
|
16
|
+
export { isSecretFile, isSecretFileName } from './secret-files';
|
|
17
|
+
export { findPlaceholders, PLACEHOLDER_PATTERN } from './secrets';
|
|
18
|
+
export * from './types';
|
package/esm/node-fs.d.ts
ADDED
package/esm/node-fs.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Node implementation of `FsProbe`. The only module that imports `node:fs`,
|
|
3
|
+
* so everything else stays pure and testable with a fake.
|
|
4
|
+
*/
|
|
5
|
+
import { promises as fs } from 'node:fs';
|
|
6
|
+
function isMissing(error) {
|
|
7
|
+
const code = error?.code;
|
|
8
|
+
return code === 'ENOENT' || code === 'ENOTDIR';
|
|
9
|
+
}
|
|
10
|
+
export const nodeFsProbe = {
|
|
11
|
+
realpath: (p) => fs.realpath(p),
|
|
12
|
+
async lexists(p) {
|
|
13
|
+
try {
|
|
14
|
+
await fs.lstat(p);
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// Anything but "missing" (EACCES, ELOOP) counts as present, so the
|
|
19
|
+
// path is treated as unresolvable instead of silently skipped.
|
|
20
|
+
return !isMissing(error);
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
readFile: (p) => fs.readFile(p, 'utf8'),
|
|
24
|
+
async readDir(p) {
|
|
25
|
+
const entries = await fs.readdir(p, { withFileTypes: true });
|
|
26
|
+
return entries.map((e) => ({
|
|
27
|
+
name: e.name,
|
|
28
|
+
kind: e.isDirectory() ? 'dir' : e.isFile() ? 'file' : e.isSymbolicLink() ? 'symlink' : 'other'
|
|
29
|
+
}));
|
|
30
|
+
},
|
|
31
|
+
async size(p) {
|
|
32
|
+
return (await fs.stat(p)).size;
|
|
33
|
+
}
|
|
34
|
+
};
|
package/esm/paths.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path handling that matches what the tools will actually open.
|
|
3
|
+
*
|
|
4
|
+
* `resolveLikePi` copies pi's `resolveToCwd` (`utils/paths.ts`), so the engine
|
|
5
|
+
* classifies the same absolute path the tool reads or writes. `canonicalize`
|
|
6
|
+
* then follows symlinks the way the kernel will, so a link inside the project
|
|
7
|
+
* that points at `~/.ssh` is judged as `~/.ssh`.
|
|
8
|
+
*/
|
|
9
|
+
import type { FsProbe } from './types';
|
|
10
|
+
/** macOS and Windows file systems are case-insensitive by default. */
|
|
11
|
+
export declare const CASE_INSENSITIVE_FS: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* pi's resolution for a tool `path` argument: normalize unicode spaces, strip
|
|
14
|
+
* one leading `@`, expand `~` and `~/`, accept `file://` URLs, then resolve
|
|
15
|
+
* against the cwd. Returns `null` when the input cannot name a local path (a
|
|
16
|
+
* malformed `file://` URL), which the caller treats as unresolvable.
|
|
17
|
+
*/
|
|
18
|
+
export declare function resolveLikePi(raw: string, cwd: string, homeDir: string): string | null;
|
|
19
|
+
/**
|
|
20
|
+
* The real location of `absolute`: realpath the deepest ancestor that exists,
|
|
21
|
+
* then add the missing tail again. `absolute` may hold `..` segments; they are
|
|
22
|
+
* resolved physically (after the symlinks before them), the way a shell's
|
|
23
|
+
* `open()` resolves them.
|
|
24
|
+
*
|
|
25
|
+
* Returns `null` (unresolvable) when some entry on the way exists but cannot be
|
|
26
|
+
* resolved: a dangling symlink, a link loop, or an entry we may not inspect. A
|
|
27
|
+
* write there would follow the link somewhere we cannot see.
|
|
28
|
+
*/
|
|
29
|
+
export declare function canonicalize(absolute: string, fs: FsProbe): Promise<string | null>;
|
|
30
|
+
/** `p` is `root` or inside it. Both must be absolute and normalized. */
|
|
31
|
+
export declare function contains(root: string, p: string, caseInsensitive?: boolean): boolean;
|
|
32
|
+
/** The deepest directory that holds every path (the parent of a single path). */
|
|
33
|
+
export declare function commonParent(paths: readonly string[]): string;
|
|
34
|
+
/**
|
|
35
|
+
* `~/…` for paths under the home dir, the absolute path otherwise. On macOS
|
|
36
|
+
* `/private/etc`, `/private/tmp` and `/private/var` show as `/etc`, `/tmp`
|
|
37
|
+
* and `/var`, the names people use for them.
|
|
38
|
+
*/
|
|
39
|
+
export declare function displayPath(p: string, homeDir: string): string;
|
|
40
|
+
/** Display form for a directory, with a trailing separator. */
|
|
41
|
+
export declare function displayDir(p: string, homeDir: string): string;
|
|
42
|
+
/** How a path reads on a card and in a reason: project-relative inside the workspace, `~/…` otherwise. */
|
|
43
|
+
export declare function showPath(p: string, workspace: string | undefined, homeDir: string): string;
|
package/esm/paths.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path handling that matches what the tools will actually open.
|
|
3
|
+
*
|
|
4
|
+
* `resolveLikePi` copies pi's `resolveToCwd` (`utils/paths.ts`), so the engine
|
|
5
|
+
* classifies the same absolute path the tool reads or writes. `canonicalize`
|
|
6
|
+
* then follows symlinks the way the kernel will, so a link inside the project
|
|
7
|
+
* that points at `~/.ssh` is judged as `~/.ssh`.
|
|
8
|
+
*/
|
|
9
|
+
import * as path from 'node:path';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
|
|
12
|
+
/** macOS and Windows file systems are case-insensitive by default. */
|
|
13
|
+
export const CASE_INSENSITIVE_FS = process.platform === 'darwin' || process.platform === 'win32';
|
|
14
|
+
/**
|
|
15
|
+
* pi's resolution for a tool `path` argument: normalize unicode spaces, strip
|
|
16
|
+
* one leading `@`, expand `~` and `~/`, accept `file://` URLs, then resolve
|
|
17
|
+
* against the cwd. Returns `null` when the input cannot name a local path (a
|
|
18
|
+
* malformed `file://` URL), which the caller treats as unresolvable.
|
|
19
|
+
*/
|
|
20
|
+
export function resolveLikePi(raw, cwd, homeDir) {
|
|
21
|
+
let normalized = raw.replace(UNICODE_SPACES, ' ');
|
|
22
|
+
if (normalized.startsWith('@'))
|
|
23
|
+
normalized = normalized.slice(1);
|
|
24
|
+
if (normalized === '~')
|
|
25
|
+
return path.resolve(homeDir);
|
|
26
|
+
if (normalized.startsWith('~/'))
|
|
27
|
+
return path.resolve(homeDir, normalized.slice(2));
|
|
28
|
+
if (/^file:\/\//.test(normalized)) {
|
|
29
|
+
try {
|
|
30
|
+
normalized = fileURLToPath(normalized);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return path.isAbsolute(normalized) ? path.resolve(normalized) : path.resolve(cwd, normalized);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The real location of `absolute`: realpath the deepest ancestor that exists,
|
|
40
|
+
* then add the missing tail again. `absolute` may hold `..` segments; they are
|
|
41
|
+
* resolved physically (after the symlinks before them), the way a shell's
|
|
42
|
+
* `open()` resolves them.
|
|
43
|
+
*
|
|
44
|
+
* Returns `null` (unresolvable) when some entry on the way exists but cannot be
|
|
45
|
+
* resolved: a dangling symlink, a link loop, or an entry we may not inspect. A
|
|
46
|
+
* write there would follow the link somewhere we cannot see.
|
|
47
|
+
*/
|
|
48
|
+
export async function canonicalize(absolute, fs) {
|
|
49
|
+
if (!path.isAbsolute(absolute))
|
|
50
|
+
return null;
|
|
51
|
+
const tail = [];
|
|
52
|
+
let current = absolute;
|
|
53
|
+
for (let depth = 0; depth < 4096; depth++) {
|
|
54
|
+
try {
|
|
55
|
+
const real = await fs.realpath(current);
|
|
56
|
+
return path.resolve(real, ...tail);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// fall through: does the entry itself exist?
|
|
60
|
+
}
|
|
61
|
+
if (await fs.lexists(current))
|
|
62
|
+
return null;
|
|
63
|
+
const parent = path.dirname(current);
|
|
64
|
+
if (parent === current)
|
|
65
|
+
return null;
|
|
66
|
+
tail.unshift(path.basename(current));
|
|
67
|
+
current = parent;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
function fold(p, caseInsensitive) {
|
|
72
|
+
return caseInsensitive ? p.toLowerCase() : p;
|
|
73
|
+
}
|
|
74
|
+
/** `p` is `root` or inside it. Both must be absolute and normalized. */
|
|
75
|
+
export function contains(root, p, caseInsensitive = false) {
|
|
76
|
+
const rel = path.relative(fold(root, caseInsensitive), fold(p, caseInsensitive));
|
|
77
|
+
if (rel === '')
|
|
78
|
+
return true;
|
|
79
|
+
return rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
|
|
80
|
+
}
|
|
81
|
+
/** The deepest directory that holds every path (the parent of a single path). */
|
|
82
|
+
export function commonParent(paths) {
|
|
83
|
+
if (paths.length === 0)
|
|
84
|
+
return path.sep;
|
|
85
|
+
let prefix = path.dirname(paths[0]);
|
|
86
|
+
for (const p of paths.slice(1)) {
|
|
87
|
+
while (!contains(prefix, p) || prefix === p) {
|
|
88
|
+
const parent = path.dirname(prefix);
|
|
89
|
+
if (parent === prefix)
|
|
90
|
+
return prefix;
|
|
91
|
+
prefix = parent;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return prefix;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* `~/…` for paths under the home dir, the absolute path otherwise. On macOS
|
|
98
|
+
* `/private/etc`, `/private/tmp` and `/private/var` show as `/etc`, `/tmp`
|
|
99
|
+
* and `/var`, the names people use for them.
|
|
100
|
+
*/
|
|
101
|
+
export function displayPath(p, homeDir) {
|
|
102
|
+
const home = path.resolve(homeDir);
|
|
103
|
+
if (p === home)
|
|
104
|
+
return '~';
|
|
105
|
+
if (contains(home, p))
|
|
106
|
+
return `~/${path.relative(home, p)}`;
|
|
107
|
+
if (process.platform === 'darwin' && /^\/private\/(etc|tmp|var)(\/|$)/.test(p))
|
|
108
|
+
return p.slice('/private'.length);
|
|
109
|
+
return p;
|
|
110
|
+
}
|
|
111
|
+
/** Display form for a directory, with a trailing separator. */
|
|
112
|
+
export function displayDir(p, homeDir) {
|
|
113
|
+
const shown = displayPath(p, homeDir);
|
|
114
|
+
return shown.endsWith('/') ? shown : `${shown}/`;
|
|
115
|
+
}
|
|
116
|
+
/** How a path reads on a card and in a reason: project-relative inside the workspace, `~/…` otherwise. */
|
|
117
|
+
export function showPath(p, workspace, homeDir) {
|
|
118
|
+
if (workspace && p !== workspace && contains(workspace, p))
|
|
119
|
+
return path.relative(workspace, p);
|
|
120
|
+
return displayPath(p, homeDir);
|
|
121
|
+
}
|
package/esm/policy.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision for a classified request. First match wins:
|
|
3
|
+
*
|
|
4
|
+
* 1. a hard-deny or protected-write target: DENY (no grant changes this)
|
|
5
|
+
* 2. a user deny rule: DENY (reserved; no rule UI in v1)
|
|
6
|
+
* 3. a chat grant, then a project grant, that covers the request: ALLOW
|
|
7
|
+
* 4. a user allow rule: ALLOW (reserved)
|
|
8
|
+
* 5. the mode default (see `modeDefault`)
|
|
9
|
+
*/
|
|
10
|
+
import { type PromptDisplay } from './prompt';
|
|
11
|
+
import type { Grant, GrantMatch, PermissionDecision, PermissionMode, PermissionRequest } from './types';
|
|
12
|
+
export interface PolicyRules {
|
|
13
|
+
deny?: readonly GrantMatch[];
|
|
14
|
+
allow?: readonly GrantMatch[];
|
|
15
|
+
}
|
|
16
|
+
export interface PolicyContext extends PromptDisplay {
|
|
17
|
+
mode: PermissionMode;
|
|
18
|
+
grants: readonly Grant[];
|
|
19
|
+
rules?: PolicyRules;
|
|
20
|
+
}
|
|
21
|
+
export declare function decide(request: PermissionRequest, ctx: PolicyContext): PermissionDecision;
|
package/esm/policy.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision for a classified request. First match wins:
|
|
3
|
+
*
|
|
4
|
+
* 1. a hard-deny or protected-write target: DENY (no grant changes this)
|
|
5
|
+
* 2. a user deny rule: DENY (reserved; no rule UI in v1)
|
|
6
|
+
* 3. a chat grant, then a project grant, that covers the request: ALLOW
|
|
7
|
+
* 4. a user allow rule: ALLOW (reserved)
|
|
8
|
+
* 5. the mode default (see `modeDefault`)
|
|
9
|
+
*/
|
|
10
|
+
import { ACCESS_VERB, findGrant, grantOptionsFor, matchCovers } from './grants';
|
|
11
|
+
import { showPath } from './paths';
|
|
12
|
+
import { buildPrompt } from './prompt';
|
|
13
|
+
import { SIGNAL_RULES } from './signals';
|
|
14
|
+
const ING = { read: 'reading', write: 'writing', exec: 'running' };
|
|
15
|
+
/** The classifier's sentence for a signal. */
|
|
16
|
+
function reasonFor(signal, request) {
|
|
17
|
+
return request.signalReasons[signal] ?? signal;
|
|
18
|
+
}
|
|
19
|
+
function blockedTarget(request) {
|
|
20
|
+
return request.targets.find((t) => t.zone === 'hard-deny' || (t.zone === 'protected' && t.access === 'write'));
|
|
21
|
+
}
|
|
22
|
+
function denyReason(request, target, ctx) {
|
|
23
|
+
const shown = showPath(target.path, ctx.workspace, ctx.homeDir);
|
|
24
|
+
const bash = request.toolName === 'bash';
|
|
25
|
+
if (target.zone === 'hard-deny') {
|
|
26
|
+
const what = bash ? `this command ${ACCESS_VERB[target.access]} ${shown}, which` : `${ING[target.access]} ${shown}`;
|
|
27
|
+
return `permissions: ${what} is not allowed (${target.label}; blocked in every mode)`;
|
|
28
|
+
}
|
|
29
|
+
const what = bash ? `this command writes ${shown}, which` : `writing ${shown}`;
|
|
30
|
+
return `permissions: ${what} is not allowed (${target.label}; protected in every mode)`;
|
|
31
|
+
}
|
|
32
|
+
/** What the request is, for a reason: "reading /etc/hosts", "this command". */
|
|
33
|
+
function describe(request, ctx) {
|
|
34
|
+
if (request.toolName === 'bash')
|
|
35
|
+
return 'this command';
|
|
36
|
+
const shown = request.targets.map((t) => showPath(t.path, ctx.workspace, ctx.homeDir)).join(', ');
|
|
37
|
+
const verb = request.toolName === 'edit' ? 'editing' : ING[request.targets[0]?.access ?? 'read'];
|
|
38
|
+
return `${verb} ${shown}`;
|
|
39
|
+
}
|
|
40
|
+
/** The mode default (step 5): the sentences that make it ask; empty means allow. */
|
|
41
|
+
function modeDefault(request, mode) {
|
|
42
|
+
const asks = (s) => {
|
|
43
|
+
const rule = SIGNAL_RULES[s].asks;
|
|
44
|
+
return rule === 'always' || (rule === 'unless-full' && mode !== 'full');
|
|
45
|
+
};
|
|
46
|
+
const reasons = request.signals.filter(asks).map((s) => reasonFor(s, request));
|
|
47
|
+
if (mode === 'ask') {
|
|
48
|
+
if (request.kind === 'write')
|
|
49
|
+
reasons.push('Ask mode asks before every file change');
|
|
50
|
+
if (request.kind === 'exec')
|
|
51
|
+
reasons.push('Ask mode asks before every command');
|
|
52
|
+
}
|
|
53
|
+
return reasons;
|
|
54
|
+
}
|
|
55
|
+
export function decide(request, ctx) {
|
|
56
|
+
const allow = (reason) => ({ decision: 'allow', reason, request });
|
|
57
|
+
const deny = (reason) => ({ decision: 'deny', reason, request });
|
|
58
|
+
const blocked = blockedTarget(request);
|
|
59
|
+
if (blocked)
|
|
60
|
+
return deny(denyReason(request, blocked, ctx));
|
|
61
|
+
if (ctx.rules?.deny?.some((m) => matchCovers(m, request))) {
|
|
62
|
+
return deny(`permissions: ${describe(request, ctx)} is blocked by a deny rule`);
|
|
63
|
+
}
|
|
64
|
+
const grant = findGrant(ctx.grants, request);
|
|
65
|
+
if (grant)
|
|
66
|
+
return allow(`permissions: allowed by a ${grant.scope} grant`);
|
|
67
|
+
if (ctx.rules?.allow?.some((m) => matchCovers(m, request)) && !request.signals.includes('unresolvable-path')) {
|
|
68
|
+
return allow('permissions: allowed by an allow rule');
|
|
69
|
+
}
|
|
70
|
+
const asks = modeDefault(request, ctx.mode);
|
|
71
|
+
if (asks.length === 0)
|
|
72
|
+
return allow(`permissions: allowed in ${ctx.mode} mode`);
|
|
73
|
+
// The card leads with why it asks, then adds what else the user should know
|
|
74
|
+
// (a secret file's values reach the model only as placeholders).
|
|
75
|
+
const explained = request.signals.map((s) => reasonFor(s, request));
|
|
76
|
+
const reasons = [...new Set([...asks, ...explained])];
|
|
77
|
+
const grantOptions = grantOptionsFor(request, ctx.homeDir);
|
|
78
|
+
return {
|
|
79
|
+
decision: 'ask',
|
|
80
|
+
reason: `permissions: ${describe(request, ctx)} needs approval: ${asks.join('; ')}`,
|
|
81
|
+
request,
|
|
82
|
+
grantOptions,
|
|
83
|
+
prompt: buildPrompt(request, reasons, grantOptions, ctx)
|
|
84
|
+
};
|
|
85
|
+
}
|
package/esm/prompt.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The approval card for an ASK: a title, what it touches, and why it asks. */
|
|
2
|
+
import type { GrantOption, PermissionPrompt, PermissionRequest } from './types';
|
|
3
|
+
/** The most specific title, by signal priority. */
|
|
4
|
+
export declare function promptTitle(request: PermissionRequest): string;
|
|
5
|
+
export interface PromptDisplay {
|
|
6
|
+
workspace: string | undefined;
|
|
7
|
+
homeDir: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The card for an ASK. `reasons` are the sentences the policy asked for, in
|
|
11
|
+
* order. With one target and no command, the card shows the path in its own
|
|
12
|
+
* row, so a reason that opens with that path says "it" instead.
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildPrompt(request: PermissionRequest, reasons: readonly string[], grantOptions: readonly GrantOption[], display: PromptDisplay): PermissionPrompt;
|