@agentic-kit/permissions 0.0.0-stage → 0.2.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/LICENSE +23 -0
- package/README.md +278 -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 +38 -4
- 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,67 @@
|
|
|
1
|
+
/** Redaction of a whole tool result: its content blocks and its details. */
|
|
2
|
+
function isTextBlock(block) {
|
|
3
|
+
const b = block;
|
|
4
|
+
return Boolean(b) && typeof b === 'object' && b.type === 'text' && typeof b.text === 'string';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Join a run of adjacent text blocks into one when a secret value runs across
|
|
8
|
+
* the boundary between two of them, so it is redacted whole.
|
|
9
|
+
*/
|
|
10
|
+
function joinSplitSecrets(content, vault) {
|
|
11
|
+
const out = [];
|
|
12
|
+
let run = [];
|
|
13
|
+
const flush = () => {
|
|
14
|
+
const texts = run.map((b) => b.text);
|
|
15
|
+
if (run.length > 1 && vault.crossesBoundary(texts))
|
|
16
|
+
out.push({ ...run[0], text: texts.join('') });
|
|
17
|
+
else
|
|
18
|
+
out.push(...run);
|
|
19
|
+
run = [];
|
|
20
|
+
};
|
|
21
|
+
for (const block of content) {
|
|
22
|
+
if (isTextBlock(block)) {
|
|
23
|
+
run.push(block);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
flush();
|
|
27
|
+
out.push(block);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
flush();
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
/** Redact every string inside a value, copying containers. Cycles keep their shape. */
|
|
34
|
+
function redactDeep(value, text, seen = new WeakMap()) {
|
|
35
|
+
if (typeof value === 'string')
|
|
36
|
+
return text(value);
|
|
37
|
+
if (!value || typeof value !== 'object')
|
|
38
|
+
return value;
|
|
39
|
+
if (seen.has(value))
|
|
40
|
+
return seen.get(value);
|
|
41
|
+
if (Array.isArray(value)) {
|
|
42
|
+
const out = [];
|
|
43
|
+
seen.set(value, out);
|
|
44
|
+
for (const item of value)
|
|
45
|
+
out.push(redactDeep(item, text, seen));
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
if (value instanceof Uint8Array || value instanceof Date)
|
|
49
|
+
return value;
|
|
50
|
+
const out = {};
|
|
51
|
+
seen.set(value, out);
|
|
52
|
+
for (const [k, v] of Object.entries(value))
|
|
53
|
+
out[k] = redactDeep(v, text, seen);
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** Replace every known secret value in the text blocks and in every string of `details`. */
|
|
57
|
+
export function redactToolResult(vault, content, details) {
|
|
58
|
+
let changed = false;
|
|
59
|
+
const text = (s) => {
|
|
60
|
+
const out = vault.redact(s);
|
|
61
|
+
if (out.changed)
|
|
62
|
+
changed = true;
|
|
63
|
+
return out.text;
|
|
64
|
+
};
|
|
65
|
+
const redacted = joinSplitSecrets(content, vault).map((block) => (isTextBlock(block) ? { ...block, text: text(block.text) } : block));
|
|
66
|
+
return { content: redacted, details: details === undefined ? undefined : redactDeep(details, text), changed };
|
|
67
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which values are secret: the key-name rule, the vendor token shapes, and the
|
|
3
|
+
* local-dev defaults that are not worth hiding.
|
|
4
|
+
*/
|
|
5
|
+
export declare const MIN_SECRET_LENGTH = 8;
|
|
6
|
+
/** Env keys whose values are secret. `DATABASE_ID`, `OWNER_ID` do not match. */
|
|
7
|
+
export declare const SECRET_KEY_PATTERN: RegExp;
|
|
8
|
+
/**
|
|
9
|
+
* Vendor token shapes (oh-my-pi `secrets/patterns.ts`). Group 1, when present,
|
|
10
|
+
* is the secret part. `hints`: lowercase text one of which every match holds,
|
|
11
|
+
* so a scan skips a shape whose hints are not in the text.
|
|
12
|
+
*/
|
|
13
|
+
export declare const TOKEN_SHAPES: readonly {
|
|
14
|
+
name: string;
|
|
15
|
+
source: string;
|
|
16
|
+
flags?: string;
|
|
17
|
+
hints: readonly string[];
|
|
18
|
+
}[];
|
|
19
|
+
/** The shapes compiled once, to test a whole value. */
|
|
20
|
+
export declare const TOKEN_SHAPE_RES: readonly {
|
|
21
|
+
name: string;
|
|
22
|
+
re: RegExp;
|
|
23
|
+
}[];
|
|
24
|
+
/** The shapes compiled once to scan a text. `matchAll` copies a global regex, so no `lastIndex` state leaks. */
|
|
25
|
+
export declare const TOKEN_SHAPE_SCANNERS: readonly {
|
|
26
|
+
name: string;
|
|
27
|
+
re: RegExp;
|
|
28
|
+
hints: readonly string[];
|
|
29
|
+
}[];
|
|
30
|
+
/** The password of a URL value, if it has one. */
|
|
31
|
+
export declare function urlPasswordOf(value: string): string | undefined;
|
|
32
|
+
export declare function isTrivial(value: string): boolean;
|
|
33
|
+
/** The vault rule for one `KEY=value` pair. */
|
|
34
|
+
export declare function isSecretEntry(key: string, value: string): boolean;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which values are secret: the key-name rule, the vendor token shapes, and the
|
|
3
|
+
* local-dev defaults that are not worth hiding.
|
|
4
|
+
*/
|
|
5
|
+
export const MIN_SECRET_LENGTH = 8;
|
|
6
|
+
/** Env keys whose values are secret. `DATABASE_ID`, `OWNER_ID` do not match. */
|
|
7
|
+
export const SECRET_KEY_PATTERN = /(KEY|SECRET|TOKEN|PASSWORD|PASS|AUTH|CREDENTIAL|PRIVATE|DSN|DATABASE_URL)(_|$)/i;
|
|
8
|
+
const B = '(?<![A-Za-z0-9_-])';
|
|
9
|
+
const E = '(?![A-Za-z0-9_-])';
|
|
10
|
+
/**
|
|
11
|
+
* Vendor token shapes (oh-my-pi `secrets/patterns.ts`). Group 1, when present,
|
|
12
|
+
* is the secret part. `hints`: lowercase text one of which every match holds,
|
|
13
|
+
* so a scan skips a shape whose hints are not in the text.
|
|
14
|
+
*/
|
|
15
|
+
export const TOKEN_SHAPES = [
|
|
16
|
+
{
|
|
17
|
+
name: 'Credential',
|
|
18
|
+
source: '(?<![a-zA-Z0-9_*-])(gh[opusr]_[a-zA-Z0-9_]{36,}|github_pat_[a-zA-Z0-9_]{36,}|glpat-[a-zA-Z0-9_-]{20,}|sk-proj-[a-zA-Z0-9_-]{36,}|sk-ant-[a-zA-Z0-9_-]{36,}|sk-[a-zA-Z0-9_-]{48,})(?![a-zA-Z0-9_*-])',
|
|
19
|
+
hints: ['gh', 'glpat-', 'sk-']
|
|
20
|
+
},
|
|
21
|
+
{ name: 'AWSAccessKey', source: `${B}(?:AKIA|ASIA)[A-Z0-9]{16}${E}`, hints: ['akia', 'asia'] },
|
|
22
|
+
{ name: 'GoogleAPIKey', source: `${B}AIza[A-Za-z0-9_-]{30,}${E}`, hints: ['aiza'] },
|
|
23
|
+
{ name: 'SlackToken', source: `${B}xox[abprs]-[A-Za-z0-9-]{10,}${E}`, hints: ['xox'] },
|
|
24
|
+
{ name: 'NpmToken', source: `${B}npm_[A-Za-z0-9]{30,}${E}`, hints: ['npm_'] },
|
|
25
|
+
{ name: 'StripeKey', source: `${B}(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{20,}${E}`, hints: ['_live_', '_test_'] },
|
|
26
|
+
{ name: 'StripeWebhookSecret', source: `${B}whsec_[A-Za-z0-9]{20,}${E}`, hints: ['whsec_'] },
|
|
27
|
+
{ name: 'HuggingFaceToken', source: `${B}hf_[A-Za-z0-9]{30,}${E}`, hints: ['hf_'] },
|
|
28
|
+
{ name: 'SendGridKey', source: `${B}SG\\.[A-Za-z0-9_-]{22}\\.[A-Za-z0-9_-]{43}${E}`, hints: ['sg.'] },
|
|
29
|
+
{ name: 'JWT', source: `${B}eyJ[A-Za-z0-9_-]{10,}\\.eyJ[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]{10,}${E}`, hints: ['eyj'] },
|
|
30
|
+
{ name: 'BearerToken', source: '\\bBearer ([A-Za-z0-9._~+/=-]{20,})(?![A-Za-z0-9_-])', flags: 'i', hints: ['bearer '] },
|
|
31
|
+
{
|
|
32
|
+
name: 'PrivateKey',
|
|
33
|
+
source: '-----BEGIN [A-Z ]*PRIVATE KEY-----[\\s\\S]*?-----END [A-Z ]*PRIVATE KEY-----',
|
|
34
|
+
hints: ['-----begin']
|
|
35
|
+
},
|
|
36
|
+
{ name: 'UrlPassword', source: '\\b[a-z][a-z0-9+.-]*://[^\\s/:@?#]*:([^\\s/@?#]+)@', flags: 'i', hints: ['://'] }
|
|
37
|
+
];
|
|
38
|
+
/** The shapes compiled once, to test a whole value. */
|
|
39
|
+
export const TOKEN_SHAPE_RES = TOKEN_SHAPES.map((shape) => ({
|
|
40
|
+
name: shape.name,
|
|
41
|
+
re: new RegExp(shape.source, shape.flags)
|
|
42
|
+
}));
|
|
43
|
+
/** The shapes compiled once to scan a text. `matchAll` copies a global regex, so no `lastIndex` state leaks. */
|
|
44
|
+
export const TOKEN_SHAPE_SCANNERS = TOKEN_SHAPES.map((shape) => ({ name: shape.name, re: new RegExp(shape.source, `${shape.flags ?? ''}g`), hints: shape.hints }));
|
|
45
|
+
/** A value that is a URL with a password (`postgres://app:pw@host`); group 1 is the password. */
|
|
46
|
+
const URL_WITH_PASSWORD = /^[a-z][a-z0-9+.-]*:\/\/[^\s/:@?#]*:([^\s/@?#]+)@/i;
|
|
47
|
+
/** The password of a URL value, if it has one. */
|
|
48
|
+
export function urlPasswordOf(value) {
|
|
49
|
+
return URL_WITH_PASSWORD.exec(value)?.[1];
|
|
50
|
+
}
|
|
51
|
+
/** Common local-dev defaults: redacting them would garble ordinary source text. */
|
|
52
|
+
const TRIVIAL_VALUES = new Set([
|
|
53
|
+
'password',
|
|
54
|
+
'postgres',
|
|
55
|
+
'changeme',
|
|
56
|
+
'change_me',
|
|
57
|
+
'change-me',
|
|
58
|
+
'development',
|
|
59
|
+
'production',
|
|
60
|
+
'localhost',
|
|
61
|
+
'undefined',
|
|
62
|
+
'placeholder',
|
|
63
|
+
'username',
|
|
64
|
+
'example',
|
|
65
|
+
'testtest',
|
|
66
|
+
'password1',
|
|
67
|
+
'secret123',
|
|
68
|
+
'mysecret',
|
|
69
|
+
'supersecret'
|
|
70
|
+
]);
|
|
71
|
+
export function isTrivial(value) {
|
|
72
|
+
const v = value.trim();
|
|
73
|
+
if (v.length < MIN_SECRET_LENGTH)
|
|
74
|
+
return true;
|
|
75
|
+
if (TRIVIAL_VALUES.has(v.toLowerCase()))
|
|
76
|
+
return true;
|
|
77
|
+
if (/^(.)\1*$/.test(v))
|
|
78
|
+
return true;
|
|
79
|
+
if (/^\$\{[^}]*\}$|^<[^>]*>$|^your[-_]/i.test(v))
|
|
80
|
+
return true;
|
|
81
|
+
if (/^(true|false|\d+)$/i.test(v))
|
|
82
|
+
return true;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
function matchesTokenShape(value) {
|
|
86
|
+
return TOKEN_SHAPE_RES.some(({ name, re }) => name !== 'UrlPassword' && re.test(value));
|
|
87
|
+
}
|
|
88
|
+
/** A URL with no password in it is an address, not a secret. */
|
|
89
|
+
function isPlainUrl(value) {
|
|
90
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(value) && urlPasswordOf(value) === undefined;
|
|
91
|
+
}
|
|
92
|
+
/** The vault rule for one `KEY=value` pair. */
|
|
93
|
+
export function isSecretEntry(key, value) {
|
|
94
|
+
if (value.trim().length < MIN_SECRET_LENGTH)
|
|
95
|
+
return false;
|
|
96
|
+
if (matchesTokenShape(value))
|
|
97
|
+
return true;
|
|
98
|
+
if (!SECRET_KEY_PATTERN.test(key))
|
|
99
|
+
return false;
|
|
100
|
+
return !isTrivial(value) && !isPlainUrl(value);
|
|
101
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The secret vault: the values the model must never see, and the placeholders
|
|
3
|
+
* it sees instead.
|
|
4
|
+
*
|
|
5
|
+
* A value is replaced by `«secret:NAME#h4»`, where `h4` is the first 4 hex
|
|
6
|
+
* chars of HMAC-SHA256(key, value). The key is per install (or per engine), so
|
|
7
|
+
* a placeholder says which secret it stands for without revealing anything
|
|
8
|
+
* about the value. Restoring maps a placeholder back to its value; the engine
|
|
9
|
+
* only does that where it is safe (writes to secret files, approved commands).
|
|
10
|
+
*/
|
|
11
|
+
export declare const PLACEHOLDER_PATTERN: RegExp;
|
|
12
|
+
/** The name used for a token-shaped value found in text with no name of its own. */
|
|
13
|
+
export declare const UNNAMED_TOKEN = "TOKEN";
|
|
14
|
+
export type SecretOrigin = 'file' | 'host' | 'token';
|
|
15
|
+
export declare class SecretVault {
|
|
16
|
+
private readonly key;
|
|
17
|
+
private readonly byValue;
|
|
18
|
+
private readonly byPlaceholder;
|
|
19
|
+
private readonly origin;
|
|
20
|
+
private sorted;
|
|
21
|
+
constructor(key: string);
|
|
22
|
+
get size(): number;
|
|
23
|
+
/** Register a value. The first name a value gets is the one it keeps. */
|
|
24
|
+
add(name: string, value: string, origin?: SecretOrigin): string;
|
|
25
|
+
/** Drop the values from one origin (before re-reading the files). */
|
|
26
|
+
clear(origin: SecretOrigin): void;
|
|
27
|
+
private entries;
|
|
28
|
+
/** Learn token-shaped values in `text` that no source named. */
|
|
29
|
+
private discover;
|
|
30
|
+
/** Replace every known value, longest first, with its placeholder. */
|
|
31
|
+
redact(text: string): {
|
|
32
|
+
text: string;
|
|
33
|
+
changed: boolean;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A known value runs across the boundary between two of `parts` (adjacent
|
|
37
|
+
* text blocks), so redacting each part alone would leave both halves.
|
|
38
|
+
*/
|
|
39
|
+
crossesBoundary(parts: readonly string[]): boolean;
|
|
40
|
+
/** Put real values back for known placeholders. Unknown placeholders stay. */
|
|
41
|
+
restore(text: string): string;
|
|
42
|
+
/** The secret a text carries: a known value or a placeholder. */
|
|
43
|
+
findSecret(text: string): {
|
|
44
|
+
name: string;
|
|
45
|
+
kind: 'value' | 'placeholder';
|
|
46
|
+
} | null;
|
|
47
|
+
}
|
|
48
|
+
export declare function findPlaceholders(text: string): string[];
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The secret vault: the values the model must never see, and the placeholders
|
|
3
|
+
* it sees instead.
|
|
4
|
+
*
|
|
5
|
+
* A value is replaced by `«secret:NAME#h4»`, where `h4` is the first 4 hex
|
|
6
|
+
* chars of HMAC-SHA256(key, value). The key is per install (or per engine), so
|
|
7
|
+
* a placeholder says which secret it stands for without revealing anything
|
|
8
|
+
* about the value. Restoring maps a placeholder back to its value; the engine
|
|
9
|
+
* only does that where it is safe (writes to secret files, approved commands).
|
|
10
|
+
*/
|
|
11
|
+
import { createHmac } from 'node:crypto';
|
|
12
|
+
import { isTrivial, MIN_SECRET_LENGTH, TOKEN_SHAPE_SCANNERS } from './rules';
|
|
13
|
+
export const PLACEHOLDER_PATTERN = /«secret:([A-Za-z0-9_]+)#([0-9a-f]{4,64})»/g;
|
|
14
|
+
/** The name used for a token-shaped value found in text with no name of its own. */
|
|
15
|
+
export const UNNAMED_TOKEN = 'TOKEN';
|
|
16
|
+
function sanitizeName(name) {
|
|
17
|
+
return name.replace(/[^A-Za-z0-9_]+/g, '_').replace(/^_+|_+$/g, '') || 'SECRET';
|
|
18
|
+
}
|
|
19
|
+
export class SecretVault {
|
|
20
|
+
key;
|
|
21
|
+
byValue = new Map();
|
|
22
|
+
byPlaceholder = new Map();
|
|
23
|
+
origin = new Map();
|
|
24
|
+
sorted = null;
|
|
25
|
+
constructor(key) {
|
|
26
|
+
this.key = key;
|
|
27
|
+
}
|
|
28
|
+
get size() {
|
|
29
|
+
return this.byValue.size;
|
|
30
|
+
}
|
|
31
|
+
/** Register a value. The first name a value gets is the one it keeps. */
|
|
32
|
+
add(name, value, origin = 'host') {
|
|
33
|
+
const existing = this.byValue.get(value);
|
|
34
|
+
if (existing) {
|
|
35
|
+
if (origin === 'host')
|
|
36
|
+
this.origin.set(value, 'host');
|
|
37
|
+
return existing.placeholder;
|
|
38
|
+
}
|
|
39
|
+
const safe = sanitizeName(name);
|
|
40
|
+
const digest = createHmac('sha256', this.key).update(value).digest('hex');
|
|
41
|
+
let placeholder = '';
|
|
42
|
+
for (let len = 4; len <= digest.length; len += 4) {
|
|
43
|
+
placeholder = `«secret:${safe}#${digest.slice(0, len)}»`;
|
|
44
|
+
if (!this.byPlaceholder.has(placeholder))
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const entry = { name: safe, value, placeholder };
|
|
48
|
+
this.byValue.set(value, entry);
|
|
49
|
+
this.byPlaceholder.set(placeholder, entry);
|
|
50
|
+
this.origin.set(value, origin);
|
|
51
|
+
this.sorted = null;
|
|
52
|
+
return placeholder;
|
|
53
|
+
}
|
|
54
|
+
/** Drop the values from one origin (before re-reading the files). */
|
|
55
|
+
clear(origin) {
|
|
56
|
+
for (const [value, o] of this.origin) {
|
|
57
|
+
if (o !== origin)
|
|
58
|
+
continue;
|
|
59
|
+
const entry = this.byValue.get(value);
|
|
60
|
+
this.byValue.delete(value);
|
|
61
|
+
this.byPlaceholder.delete(entry.placeholder);
|
|
62
|
+
this.origin.delete(value);
|
|
63
|
+
}
|
|
64
|
+
this.sorted = null;
|
|
65
|
+
}
|
|
66
|
+
entries() {
|
|
67
|
+
this.sorted ??= [...this.byValue.values()].sort((a, b) => b.value.length - a.value.length);
|
|
68
|
+
return this.sorted;
|
|
69
|
+
}
|
|
70
|
+
/** Learn token-shaped values in `text` that no source named. */
|
|
71
|
+
discover(text) {
|
|
72
|
+
const lower = text.toLowerCase();
|
|
73
|
+
for (const shape of TOKEN_SHAPE_SCANNERS) {
|
|
74
|
+
if (!shape.hints.some((hint) => lower.includes(hint)))
|
|
75
|
+
continue;
|
|
76
|
+
for (const m of text.matchAll(shape.re)) {
|
|
77
|
+
const value = m[1] ?? m[0];
|
|
78
|
+
if (shape.name === 'UrlPassword' && (isTrivial(value) || /[${}]/.test(value)))
|
|
79
|
+
continue;
|
|
80
|
+
if (value.length >= MIN_SECRET_LENGTH && !this.byValue.has(value))
|
|
81
|
+
this.add(UNNAMED_TOKEN, value, 'token');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Replace every known value, longest first, with its placeholder. */
|
|
86
|
+
redact(text) {
|
|
87
|
+
if (!text)
|
|
88
|
+
return { text, changed: false };
|
|
89
|
+
this.discover(text);
|
|
90
|
+
const claimed = new Uint8Array(text.length);
|
|
91
|
+
const hits = [];
|
|
92
|
+
for (const entry of this.entries()) {
|
|
93
|
+
const len = entry.value.length;
|
|
94
|
+
for (let at = text.indexOf(entry.value); at >= 0; at = text.indexOf(entry.value, at + 1)) {
|
|
95
|
+
let free = true;
|
|
96
|
+
for (let k = at; k < at + len; k++) {
|
|
97
|
+
if (claimed[k]) {
|
|
98
|
+
free = false;
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!free)
|
|
103
|
+
continue;
|
|
104
|
+
claimed.fill(1, at, at + len);
|
|
105
|
+
hits.push({ start: at, end: at + len, placeholder: entry.placeholder });
|
|
106
|
+
at += len - 1;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (hits.length === 0)
|
|
110
|
+
return { text, changed: false };
|
|
111
|
+
hits.sort((a, b) => a.start - b.start);
|
|
112
|
+
let out = '';
|
|
113
|
+
let last = 0;
|
|
114
|
+
for (const hit of hits) {
|
|
115
|
+
out += text.slice(last, hit.start) + hit.placeholder;
|
|
116
|
+
last = hit.end;
|
|
117
|
+
}
|
|
118
|
+
return { text: out + text.slice(last), changed: true };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A known value runs across the boundary between two of `parts` (adjacent
|
|
122
|
+
* text blocks), so redacting each part alone would leave both halves.
|
|
123
|
+
*/
|
|
124
|
+
crossesBoundary(parts) {
|
|
125
|
+
const joined = parts.join('');
|
|
126
|
+
this.discover(joined);
|
|
127
|
+
const bounds = [];
|
|
128
|
+
let offset = 0;
|
|
129
|
+
for (const part of parts.slice(0, -1))
|
|
130
|
+
bounds.push((offset += part.length));
|
|
131
|
+
for (const entry of this.entries()) {
|
|
132
|
+
for (let at = joined.indexOf(entry.value); at >= 0; at = joined.indexOf(entry.value, at + 1)) {
|
|
133
|
+
const end = at + entry.value.length;
|
|
134
|
+
if (bounds.some((b) => b > at && b < end))
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
/** Put real values back for known placeholders. Unknown placeholders stay. */
|
|
141
|
+
restore(text) {
|
|
142
|
+
return text.replace(PLACEHOLDER_PATTERN, (m) => this.byPlaceholder.get(m)?.value ?? m);
|
|
143
|
+
}
|
|
144
|
+
/** The secret a text carries: a known value or a placeholder. */
|
|
145
|
+
findSecret(text) {
|
|
146
|
+
const placeholder = findPlaceholders(text)[0];
|
|
147
|
+
if (placeholder)
|
|
148
|
+
return { name: /«secret:([A-Za-z0-9_]+)#/.exec(placeholder)[1], kind: 'placeholder' };
|
|
149
|
+
for (const entry of this.entries()) {
|
|
150
|
+
if (text.includes(entry.value))
|
|
151
|
+
return { name: entry.name, kind: 'value' };
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export function findPlaceholders(text) {
|
|
157
|
+
return [...text.matchAll(PLACEHOLDER_PATTERN)].map((m) => m[0]);
|
|
158
|
+
}
|
package/esm/signals.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How each signal moves a decision, in one table so a new signal cannot be
|
|
3
|
+
* left out: when it makes a call ask, and whether a grant may cover it. A
|
|
4
|
+
* hard-deny or protected-write target is denied by its zone before this applies.
|
|
5
|
+
*/
|
|
6
|
+
import type { Signal } from './types';
|
|
7
|
+
interface SignalRule {
|
|
8
|
+
/** `always`: in every mode, Full included. `unless-full`: in Ask and Workspace mode. `never`: it only informs the card. */
|
|
9
|
+
asks: 'always' | 'unless-full' | 'never';
|
|
10
|
+
/** A grant may cover a call that raised it; otherwise each such call needs its own yes. */
|
|
11
|
+
grantable: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const SIGNAL_RULES: Readonly<Record<Signal, SignalRule>>;
|
|
14
|
+
export {};
|
package/esm/signals.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How each signal moves a decision, in one table so a new signal cannot be
|
|
3
|
+
* left out: when it makes a call ask, and whether a grant may cover it. A
|
|
4
|
+
* hard-deny or protected-write target is denied by its zone before this applies.
|
|
5
|
+
*/
|
|
6
|
+
const ASKS = { asks: 'unless-full', grantable: true };
|
|
7
|
+
export const SIGNAL_RULES = {
|
|
8
|
+
'secret-file': { asks: 'never', grantable: true },
|
|
9
|
+
'outside-workspace': ASKS,
|
|
10
|
+
'hard-deny': { asks: 'never', grantable: false },
|
|
11
|
+
'protected-write': { asks: 'never', grantable: false },
|
|
12
|
+
'ask-write': ASKS,
|
|
13
|
+
'shell-dynamic': ASKS,
|
|
14
|
+
sudo: ASKS,
|
|
15
|
+
upload: ASKS,
|
|
16
|
+
destructive: ASKS,
|
|
17
|
+
'placeholder-in-command': { asks: 'always', grantable: false },
|
|
18
|
+
'secret-into-plain-file': { asks: 'unless-full', grantable: false },
|
|
19
|
+
'unresolvable-path': { asks: 'always', grantable: false },
|
|
20
|
+
'stdin-operands': ASKS,
|
|
21
|
+
persistence: ASKS,
|
|
22
|
+
'network-exposure': ASKS,
|
|
23
|
+
clipboard: ASKS,
|
|
24
|
+
'secret-transform': ASKS,
|
|
25
|
+
'secret-env-expansion': ASKS
|
|
26
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of pi's built-in tool inputs, in one place: `path`, `command` and
|
|
3
|
+
* `glob` strings, a write's `content`, and an edit's `edits[]` of
|
|
4
|
+
* `{ oldText, newText }` (older callers send `oldText`/`newText` at the top).
|
|
5
|
+
*/
|
|
6
|
+
import type { BuiltinTool } from './types';
|
|
7
|
+
export declare function stringField(input: Record<string, unknown>, key: string): string | undefined;
|
|
8
|
+
/** The new text a write or edit puts in the file. */
|
|
9
|
+
export declare function newTexts(toolName: BuiltinTool, input: Record<string, unknown>): string[];
|
|
10
|
+
/** Rewrite, in place, every text field of a write or edit input. */
|
|
11
|
+
export declare function mapWriteTexts(input: Record<string, unknown>, fn: (text: string) => string): void;
|
|
12
|
+
/** Rewrite, in place, a bash input's command. */
|
|
13
|
+
export declare function mapCommand(input: Record<string, unknown>, fn: (text: string) => string): void;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape of pi's built-in tool inputs, in one place: `path`, `command` and
|
|
3
|
+
* `glob` strings, a write's `content`, and an edit's `edits[]` of
|
|
4
|
+
* `{ oldText, newText }` (older callers send `oldText`/`newText` at the top).
|
|
5
|
+
*/
|
|
6
|
+
export function stringField(input, key) {
|
|
7
|
+
const v = input[key];
|
|
8
|
+
return typeof v === 'string' ? v : undefined;
|
|
9
|
+
}
|
|
10
|
+
function editsOf(input) {
|
|
11
|
+
const edits = Array.isArray(input.edits) ? input.edits : [];
|
|
12
|
+
return edits.filter((e) => Boolean(e) && typeof e === 'object');
|
|
13
|
+
}
|
|
14
|
+
/** The new text a write or edit puts in the file. */
|
|
15
|
+
export function newTexts(toolName, input) {
|
|
16
|
+
if (toolName === 'write')
|
|
17
|
+
return [stringField(input, 'content') ?? ''];
|
|
18
|
+
const texts = editsOf(input)
|
|
19
|
+
.map((e) => e.newText)
|
|
20
|
+
.filter((t) => typeof t === 'string');
|
|
21
|
+
const legacy = stringField(input, 'newText');
|
|
22
|
+
return legacy === undefined ? texts : [...texts, legacy];
|
|
23
|
+
}
|
|
24
|
+
function mapFields(target, keys, fn) {
|
|
25
|
+
for (const key of keys) {
|
|
26
|
+
const value = target[key];
|
|
27
|
+
if (typeof value === 'string')
|
|
28
|
+
target[key] = fn(value);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Rewrite, in place, every text field of a write or edit input. */
|
|
32
|
+
export function mapWriteTexts(input, fn) {
|
|
33
|
+
mapFields(input, ['content', 'oldText', 'newText'], fn);
|
|
34
|
+
for (const edit of editsOf(input))
|
|
35
|
+
mapFields(edit, ['oldText', 'newText'], fn);
|
|
36
|
+
}
|
|
37
|
+
/** Rewrite, in place, a bash input's command. */
|
|
38
|
+
export function mapCommand(input, fn) {
|
|
39
|
+
mapFields(input, ['command'], fn);
|
|
40
|
+
}
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared vocabulary of the permissions layer. Everything here is plain data
|
|
3
|
+
* so a host can store it, show it, and assert on it in a test.
|
|
4
|
+
*/
|
|
5
|
+
/** How much the agent may do without asking. The hard-deny list, redaction and
|
|
6
|
+
* self-protection apply in every mode; the mode only moves the defaults. */
|
|
7
|
+
export type PermissionMode = 'ask' | 'workspace' | 'full';
|
|
8
|
+
export declare const PERMISSION_MODES: readonly PermissionMode[];
|
|
9
|
+
export declare const DEFAULT_PERMISSION_MODE: PermissionMode;
|
|
10
|
+
/** The pi built-in tools this layer owns. Every other tool belongs to another
|
|
11
|
+
* gate (the DB gate) or to trusted host code, and the layer abstains on it. */
|
|
12
|
+
export type BuiltinTool = 'read' | 'write' | 'edit' | 'bash' | 'grep' | 'find' | 'ls';
|
|
13
|
+
export type Access = 'read' | 'write' | 'exec';
|
|
14
|
+
export type RequestKind = 'read' | 'write' | 'exec';
|
|
15
|
+
/** What each built-in does: the request kind it makes. */
|
|
16
|
+
export declare const TOOL_KIND: Readonly<Record<BuiltinTool, RequestKind>>;
|
|
17
|
+
export declare const BUILTIN_TOOLS: ReadonlySet<string>;
|
|
18
|
+
export declare function isBuiltinTool(name: string): name is BuiltinTool;
|
|
19
|
+
/**
|
|
20
|
+
* Where a canonical path sits. Computed from the roots, most specific first:
|
|
21
|
+
* `hard-deny` and `protected` win over `workspace`, so `.pi/` inside the
|
|
22
|
+
* workspace is `protected`, and a credential file under a temp dir is `hard-deny`.
|
|
23
|
+
*/
|
|
24
|
+
export type Zone = 'workspace' | 'trusted' | 'temp' | 'outside' | 'hard-deny' | 'protected' | 'ask-write' | 'unresolvable';
|
|
25
|
+
export interface Target {
|
|
26
|
+
/** The path as the agent wrote it. */
|
|
27
|
+
raw: string;
|
|
28
|
+
/** Absolute and canonical (realpath of the deepest existing ancestor). */
|
|
29
|
+
path: string;
|
|
30
|
+
access: Access;
|
|
31
|
+
zone: Zone;
|
|
32
|
+
/** The file name matches a secret-file pattern (`.env`, `*.pem`, …). */
|
|
33
|
+
secretFile: boolean;
|
|
34
|
+
/** What the zone's root is, in words: "SSH keys", "git hook", "project". */
|
|
35
|
+
label: string;
|
|
36
|
+
/** The path holds an unexpanded glob (`~/.ss?/*`): no path grant covers it outside the project. */
|
|
37
|
+
glob?: boolean;
|
|
38
|
+
}
|
|
39
|
+
export type Signal = 'secret-file' | 'outside-workspace' | 'hard-deny' | 'protected-write' | 'ask-write' | 'shell-dynamic' | 'sudo' | 'upload' | 'destructive' | 'placeholder-in-command' | 'secret-into-plain-file' | 'unresolvable-path'
|
|
40
|
+
/** `xargs` or an archiver takes file names from stdin that the analyzer cannot see. */
|
|
41
|
+
| 'stdin-operands'
|
|
42
|
+
/** `crontab`, `at`, `launchctl load`, `defaults write`: a change that outlasts the session. */
|
|
43
|
+
| 'persistence'
|
|
44
|
+
/** A dev server on a public interface, or a tunnel (`ngrok`, `ssh -R`). */
|
|
45
|
+
| 'network-exposure'
|
|
46
|
+
/** `pbcopy`, `pbpaste`, `xclip`, … */
|
|
47
|
+
| 'clipboard'
|
|
48
|
+
/** A secret file read by a program that may change its text, so redaction cannot match it. */
|
|
49
|
+
| 'secret-transform'
|
|
50
|
+
/** The command expands a variable whose name looks secret, or that the host marked secret. */
|
|
51
|
+
| 'secret-env-expansion';
|
|
52
|
+
/** One tool call, classified. */
|
|
53
|
+
export interface PermissionRequest {
|
|
54
|
+
toolCallId: string;
|
|
55
|
+
toolName: BuiltinTool;
|
|
56
|
+
kind: RequestKind;
|
|
57
|
+
targets: Target[];
|
|
58
|
+
/** bash only: the command with whitespace collapsed (the grant key). */
|
|
59
|
+
command?: string;
|
|
60
|
+
signals: Signal[];
|
|
61
|
+
/** One specific sentence for each signal ("it uploads data: curl -d @.env sends a file's contents"). */
|
|
62
|
+
signalReasons: Partial<Record<Signal, string>>;
|
|
63
|
+
}
|
|
64
|
+
export type PermissionVerdict = 'allow' | 'ask' | 'deny';
|
|
65
|
+
/** What a grant matches. Never a tool name alone. */
|
|
66
|
+
export type GrantMatch = {
|
|
67
|
+
type: 'path';
|
|
68
|
+
access: Access;
|
|
69
|
+
pathPrefix: string;
|
|
70
|
+
} | {
|
|
71
|
+
type: 'command';
|
|
72
|
+
command: string;
|
|
73
|
+
};
|
|
74
|
+
export type GrantScope = 'chat' | 'project';
|
|
75
|
+
export interface Grant {
|
|
76
|
+
scope: GrantScope;
|
|
77
|
+
match: GrantMatch;
|
|
78
|
+
createdAt: string;
|
|
79
|
+
}
|
|
80
|
+
/** A scope the user can pick on an ASK, with the exact pattern it would save. */
|
|
81
|
+
export interface GrantOption {
|
|
82
|
+
scope: GrantScope;
|
|
83
|
+
/** "Allow in this chat", "Always: reads in ~/work/lib/". */
|
|
84
|
+
label: string;
|
|
85
|
+
/** The human-readable pattern ("~/work/lib/", "pnpm dlx shadcn add button"). */
|
|
86
|
+
pattern: string;
|
|
87
|
+
match: GrantMatch;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The decision for one call. It is self-contained: an `ask` carries the card
|
|
91
|
+
* to show and the scopes the user may grant, so a host needs nothing else
|
|
92
|
+
* from the engine to put the question in front of a person.
|
|
93
|
+
*/
|
|
94
|
+
export type PermissionDecision = {
|
|
95
|
+
decision: 'allow';
|
|
96
|
+
reason: string;
|
|
97
|
+
request: PermissionRequest;
|
|
98
|
+
} | {
|
|
99
|
+
decision: 'deny';
|
|
100
|
+
reason: string;
|
|
101
|
+
request: PermissionRequest;
|
|
102
|
+
} | {
|
|
103
|
+
decision: 'ask';
|
|
104
|
+
reason: string;
|
|
105
|
+
request: PermissionRequest;
|
|
106
|
+
prompt: PermissionPrompt;
|
|
107
|
+
/** The scopes the user may grant. Empty when a grant is not allowed. */
|
|
108
|
+
grantOptions: GrantOption[];
|
|
109
|
+
};
|
|
110
|
+
/** What a host puts in front of the user for one ASK (`ApprovalRequest.permission` in `@agentic-kit/harness`). */
|
|
111
|
+
export interface PermissionPrompt {
|
|
112
|
+
title: string;
|
|
113
|
+
summary: string;
|
|
114
|
+
toolName: string;
|
|
115
|
+
kind: RequestKind;
|
|
116
|
+
command?: string;
|
|
117
|
+
targets: {
|
|
118
|
+
path: string;
|
|
119
|
+
access: Access;
|
|
120
|
+
zone: Zone;
|
|
121
|
+
}[];
|
|
122
|
+
signals: Signal[];
|
|
123
|
+
/** One plain sentence per signal, in order. */
|
|
124
|
+
reasons: string[];
|
|
125
|
+
grantOptions: {
|
|
126
|
+
scope: GrantScope;
|
|
127
|
+
label: string;
|
|
128
|
+
pattern: string;
|
|
129
|
+
}[];
|
|
130
|
+
}
|
|
131
|
+
/** The filesystem the engine needs. Injected so the pure modules stay testable. */
|
|
132
|
+
export interface FsProbe {
|
|
133
|
+
/** Resolve symlinks; reject when the path does not exist. */
|
|
134
|
+
realpath(path: string): Promise<string>;
|
|
135
|
+
/** `true` when an entry exists at the path itself (a dangling link included). */
|
|
136
|
+
lexists(path: string): Promise<boolean>;
|
|
137
|
+
readFile(path: string): Promise<string>;
|
|
138
|
+
/** The entries of a directory. */
|
|
139
|
+
readDir(path: string): Promise<DirEntry[]>;
|
|
140
|
+
/** File size in bytes, so large files are skipped before they are read. */
|
|
141
|
+
size(path: string): Promise<number>;
|
|
142
|
+
}
|
|
143
|
+
export interface DirEntry {
|
|
144
|
+
name: string;
|
|
145
|
+
kind: 'file' | 'dir' | 'symlink' | 'other';
|
|
146
|
+
}
|
package/esm/types.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared vocabulary of the permissions layer. Everything here is plain data
|
|
3
|
+
* so a host can store it, show it, and assert on it in a test.
|
|
4
|
+
*/
|
|
5
|
+
export const PERMISSION_MODES = ['ask', 'workspace', 'full'];
|
|
6
|
+
export const DEFAULT_PERMISSION_MODE = 'workspace';
|
|
7
|
+
/** What each built-in does: the request kind it makes. */
|
|
8
|
+
export const TOOL_KIND = {
|
|
9
|
+
read: 'read',
|
|
10
|
+
write: 'write',
|
|
11
|
+
edit: 'write',
|
|
12
|
+
bash: 'exec',
|
|
13
|
+
grep: 'read',
|
|
14
|
+
find: 'read',
|
|
15
|
+
ls: 'read'
|
|
16
|
+
};
|
|
17
|
+
export const BUILTIN_TOOLS = new Set(Object.keys(TOOL_KIND));
|
|
18
|
+
export function isBuiltinTool(name) {
|
|
19
|
+
return BUILTIN_TOOLS.has(name);
|
|
20
|
+
}
|