@pi-kode/permissions 0.0.0-init → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -0
- package/index.ts +1 -0
- package/lib/src/backends/process.ts +121 -0
- package/lib/src/extensions/agents.ts +209 -0
- package/lib/src/extensions/hooks.ts +98 -0
- package/lib/src/extensions/planning.ts +136 -0
- package/lib/src/extensions/policy.ts +192 -0
- package/lib/src/features/permissions.ts +265 -0
- package/lib/src/runtime/approvals.ts +14 -0
- package/lib/src/runtime/config.ts +89 -0
- package/lib/src/runtime/events.ts +19 -0
- package/lib/src/runtime/governance.ts +132 -0
- package/lib/src/runtime/schema.ts +2 -0
- package/lib/src/runtime/utils.ts +89 -0
- package/package.json +39 -2
- package/tests/agents.test.ts +130 -0
- package/tests/helper.ts +80 -0
- package/tests/policy.test.ts +149 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { decide, shellWords, withoutRedundantCd } from '../lib/src/extensions/policy.ts';
|
|
6
|
+
import install from '../lib/src/features/permissions.ts';
|
|
7
|
+
import { loadConfig } from '../lib/src/runtime/config.ts';
|
|
8
|
+
import { initialState, restoreState } from '../lib/src/runtime/governance.ts';
|
|
9
|
+
import { harness } from './helper.ts';
|
|
10
|
+
|
|
11
|
+
const config: any = { permissions: { allow: ['Bash(npm test:*)'], ask: [], deny: [] }, hooks: {} };
|
|
12
|
+
|
|
13
|
+
test('shell prefix grammar accepts simple arguments and rejects compound/expansion escapes', async () => {
|
|
14
|
+
assert.deepEqual(shellWords(`npm test 'literal value'`), ['npm', 'test', 'literal value']);
|
|
15
|
+
assert.equal((await decide('Bash', { command: 'npm test -- --run' }, 'safe', config, '/tmp')).action, 'allow');
|
|
16
|
+
for (const command of [
|
|
17
|
+
'npm test; touch x',
|
|
18
|
+
'npm test && sh',
|
|
19
|
+
'npm test $(touch x)',
|
|
20
|
+
'npm test `id`',
|
|
21
|
+
'npm test > x',
|
|
22
|
+
'npm test\nsh',
|
|
23
|
+
'env npm test',
|
|
24
|
+
]) {
|
|
25
|
+
assert.equal((await decide('Bash', { command }, 'safe', config, '/tmp')).action, 'ask', command);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('TaskRun shares Bash deny rules and plan mode rejects shell jobs', async () => {
|
|
30
|
+
const c: any = { permissions: { allow: ['TaskRun'], ask: [], deny: ['Bash'] } };
|
|
31
|
+
assert.equal((await decide('TaskRun', { command: 'echo hi' }, 'bypassPermissions', c, '/tmp')).action, 'deny');
|
|
32
|
+
assert.equal((await decide('TaskRun', { command: 'echo hi' }, 'plan', config, '/tmp')).action, 'deny');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('native approvals bind session grants to exact inputs and current policy', async t => {
|
|
36
|
+
const h = await harness(install);
|
|
37
|
+
t.after(() => h.close());
|
|
38
|
+
h.answers.push('Allow this exact call for session');
|
|
39
|
+
const call = { toolName: 'bash', input: { command: 'echo one' } };
|
|
40
|
+
await h.emit('tool_call', call);
|
|
41
|
+
assert.ok(h.entries.some(e => e.customType === 'pk:permission-grant'));
|
|
42
|
+
h.ctx.hasUI = false;
|
|
43
|
+
assert.equal((await h.emit('tool_call', call))[0], undefined);
|
|
44
|
+
assert.equal((await h.emit('tool_call', { ...call, input: { command: 'echo two' } }))[0].block, true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('selectable modes enforce safe defaults, per-call approval and full access', async () => {
|
|
48
|
+
const c: any = { permissions: { allow: ['Bash'], ask: ['Write'], deny: ['Edit'] } };
|
|
49
|
+
assert.equal((await decide('Read', { file_path: '/tmp/example' }, 'safe', c, '/tmp')).action, 'allow');
|
|
50
|
+
assert.equal((await decide('Write', { file_path: '/tmp/example' }, 'safe', config, '/tmp')).action, 'ask');
|
|
51
|
+
assert.equal((await decide('Bash', { command: 'echo hi' }, 'ask', c, '/tmp')).action, 'ask');
|
|
52
|
+
assert.equal((await decide('Read', {}, 'ask', c, '/tmp')).action, 'ask');
|
|
53
|
+
assert.equal((await decide('Write', {}, 'fullAccess', c, '/tmp')).action, 'allow');
|
|
54
|
+
for (const mode of ['safe', 'ask', 'fullAccess'] as const) {
|
|
55
|
+
assert.equal((await decide('Edit', {}, mode, c, '/tmp')).action, 'deny');
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('mode picker persists selection and Ask Everything never reuses grants', async t => {
|
|
60
|
+
const h = await harness(install);
|
|
61
|
+
t.after(() => h.close());
|
|
62
|
+
h.answers.push('Ask Everything — approve every tool call');
|
|
63
|
+
await h.commands.get('permissions').handler('', h.ctx);
|
|
64
|
+
assert.equal(h.runtime.state.mode, 'ask');
|
|
65
|
+
assert.ok(h.entries.some(e => e.customType === 'code-compat:state' && e.data.mode === 'ask'));
|
|
66
|
+
await h.emit('tool_call', { toolName: 'read', input: { path: 'example' } });
|
|
67
|
+
h.ctx.hasUI = false;
|
|
68
|
+
assert.equal((await h.emit('tool_call', { toolName: 'read', input: { path: 'example' } }))[0].block, true);
|
|
69
|
+
h.ctx.hasUI = true;
|
|
70
|
+
h.runtime.state.mode = 'plan';
|
|
71
|
+
await assert.rejects(h.commands.get('permissions').handler('mode fullAccess', h.ctx), /ExitPlanMode/);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('a redundant cd into the working directory matches rules like the bare command', async () => {
|
|
75
|
+
assert.equal(withoutRedundantCd('cd /tmp && npm test -- --run', '/tmp'), 'npm test -- --run');
|
|
76
|
+
assert.equal(withoutRedundantCd('cd /tmp/../tmp && npm test', '/tmp'), 'npm test');
|
|
77
|
+
assert.equal(withoutRedundantCd('cd /tmp/sub && npm test', '/tmp'), undefined);
|
|
78
|
+
assert.equal(withoutRedundantCd('cd "$PWD" && npm test', '/tmp'), undefined);
|
|
79
|
+
assert.equal((await decide('Bash', { command: 'cd /tmp && npm test -- --run' }, 'safe', config, '/tmp')).action, 'allow');
|
|
80
|
+
const denyConfig: any = { permissions: { allow: [], ask: [], deny: ['Bash(rm -rf:*)'] }, hooks: {} };
|
|
81
|
+
assert.equal((await decide('Bash', { command: 'cd /tmp && rm -rf x' }, 'safe', denyConfig, '/tmp')).action, 'deny');
|
|
82
|
+
assert.equal((await decide('Bash', { command: 'cd /tmp && npm test; touch x' }, 'safe', config, '/tmp')).action, 'ask');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('session wildcards ignore a redundant cd prefix', async t => {
|
|
86
|
+
const h = await harness(install);
|
|
87
|
+
t.after(() => h.close());
|
|
88
|
+
h.answers.push('Allow Bash(echo:*) for session');
|
|
89
|
+
const prefix = `cd '${h.ctx.cwd}' && `;
|
|
90
|
+
await h.emit('tool_call', { toolName: 'bash', input: { command: prefix + 'echo hello world' } });
|
|
91
|
+
assert.ok(h.entries.some(e => e.customType === 'pk:permission-rule' && e.data.rule === 'Bash(echo:*)'));
|
|
92
|
+
h.ctx.hasUI = false;
|
|
93
|
+
assert.equal((await h.emit('tool_call', { toolName: 'bash', input: { command: prefix + 'echo again' } }))[0], undefined);
|
|
94
|
+
assert.equal((await h.emit('tool_call', { toolName: 'bash', input: { command: 'rm -rf x' } }))[0].block, true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('wildcard session rules approve matching prefixes without touching policy files', async t => {
|
|
98
|
+
const h = await harness(install);
|
|
99
|
+
t.after(() => h.close());
|
|
100
|
+
h.answers.push('Allow Bash(echo:*) for session');
|
|
101
|
+
await h.emit('tool_call', { toolName: 'bash', input: { command: 'echo hello world' } });
|
|
102
|
+
assert.ok(h.entries.some(e => e.customType === 'pk:permission-rule' && e.data.rule === 'Bash(echo:*)'));
|
|
103
|
+
h.ctx.hasUI = false;
|
|
104
|
+
assert.equal((await h.emit('tool_call', { toolName: 'bash', input: { command: 'echo again' } }))[0], undefined);
|
|
105
|
+
assert.equal((await h.emit('tool_call', { toolName: 'bash', input: { command: 'rm -rf x' } }))[0].block, true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('initial mode is safe, legacy default mode is normalized on restore and rejected as an explicit mode', async t => {
|
|
109
|
+
assert.equal(initialState().mode, 'safe');
|
|
110
|
+
assert.equal(
|
|
111
|
+
restoreState([
|
|
112
|
+
{
|
|
113
|
+
type: 'custom',
|
|
114
|
+
customType: 'code-compat:state',
|
|
115
|
+
data: { version: 1, mode: 'default', plan: { path: '/tmp/plan.md', previousMode: 'default', status: 'draft' } },
|
|
116
|
+
},
|
|
117
|
+
]).mode,
|
|
118
|
+
'safe',
|
|
119
|
+
);
|
|
120
|
+
assert.equal(
|
|
121
|
+
restoreState([
|
|
122
|
+
{
|
|
123
|
+
type: 'custom',
|
|
124
|
+
customType: 'code-compat:state',
|
|
125
|
+
data: { version: 1, mode: 'plan', plan: { path: '/tmp/plan.md', previousMode: 'default', status: 'draft' } },
|
|
126
|
+
},
|
|
127
|
+
]).plan?.previousMode,
|
|
128
|
+
'safe',
|
|
129
|
+
);
|
|
130
|
+
const h = await harness(install);
|
|
131
|
+
t.after(() => h.close());
|
|
132
|
+
assert.equal(h.runtime.state.mode, 'safe');
|
|
133
|
+
await assert.rejects(h.commands.get('permissions').handler('mode default', h.ctx), /Use safe, ask or fullAccess/);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('permissions load from and save to .pi directory instead of .claude', async t => {
|
|
137
|
+
const h = await harness(install);
|
|
138
|
+
t.after(() => h.close());
|
|
139
|
+
await h.commands.get('permissions').handler('allow Bash(ls:*)', h.ctx);
|
|
140
|
+
const saved = JSON.parse(await readFile(join(h.root, '.pi/settings.local.json'), 'utf8'));
|
|
141
|
+
assert.deepEqual(saved.permissions.allow, ['Bash(ls:*)']);
|
|
142
|
+
|
|
143
|
+
const home = join(h.root, 'home');
|
|
144
|
+
await mkdir(join(home, '.pi'), { recursive: true });
|
|
145
|
+
await writeFile(join(home, '.pi/settings.json'), JSON.stringify({ permissions: { defaultMode: 'ask' } }));
|
|
146
|
+
const loaded = await loadConfig(h.root, true, home);
|
|
147
|
+
assert.equal(loaded.permissions.defaultMode, 'ask');
|
|
148
|
+
assert.deepEqual(loaded.permissions.allow, ['Bash(ls:*)']);
|
|
149
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2023",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"noEmit": true,
|
|
8
|
+
"allowImportingTsExtensions": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"types": [
|
|
11
|
+
"node"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"include": [
|
|
15
|
+
"index.ts",
|
|
16
|
+
"lib/**/*.ts",
|
|
17
|
+
"tests/**/*.ts"
|
|
18
|
+
]
|
|
19
|
+
}
|