@komarspn/pi-permission-system 16.0.2
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/CHANGELOG.md +2234 -0
- package/LICENSE +21 -0
- package/README.md +158 -0
- package/config/config.example.json +39 -0
- package/package.json +82 -0
- package/schemas/permissions.schema.json +158 -0
- package/src/active-agent.ts +72 -0
- package/src/async-cache.ts +21 -0
- package/src/bash-arity.ts +210 -0
- package/src/builtin-tool-input-formatters.ts +82 -0
- package/src/canonicalize-path.ts +30 -0
- package/src/common.ts +121 -0
- package/src/config-loader.ts +432 -0
- package/src/config-modal.ts +259 -0
- package/src/config-paths.ts +47 -0
- package/src/config-reporter.ts +34 -0
- package/src/config-store.ts +222 -0
- package/src/decision-audit.ts +75 -0
- package/src/decision-reporter.ts +41 -0
- package/src/denial-messages.ts +232 -0
- package/src/expand-home.ts +28 -0
- package/src/extension-config.ts +79 -0
- package/src/extension-paths.ts +66 -0
- package/src/forwarded-permissions/io.ts +404 -0
- package/src/forwarded-permissions/permission-forwarder.ts +580 -0
- package/src/forwarding-manager.ts +74 -0
- package/src/gate-prompter.ts +12 -0
- package/src/handlers/before-agent-start.ts +94 -0
- package/src/handlers/gates/bash-command.ts +75 -0
- package/src/handlers/gates/bash-external-directory.ts +127 -0
- package/src/handlers/gates/bash-path-extractor.ts +15 -0
- package/src/handlers/gates/bash-path.ts +152 -0
- package/src/handlers/gates/bash-program.ts +1143 -0
- package/src/handlers/gates/bash-token-classification.ts +105 -0
- package/src/handlers/gates/candidate-check.ts +32 -0
- package/src/handlers/gates/descriptor.ts +81 -0
- package/src/handlers/gates/external-directory-messages.ts +20 -0
- package/src/handlers/gates/external-directory.ts +133 -0
- package/src/handlers/gates/helpers.ts +76 -0
- package/src/handlers/gates/path.ts +91 -0
- package/src/handlers/gates/runner.ts +186 -0
- package/src/handlers/gates/skill-input-gate-pipeline.ts +104 -0
- package/src/handlers/gates/skill-input.ts +46 -0
- package/src/handlers/gates/skill-read.ts +87 -0
- package/src/handlers/gates/tool-call-gate-pipeline.ts +129 -0
- package/src/handlers/gates/tool.ts +102 -0
- package/src/handlers/gates/types.ts +13 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/lifecycle.ts +95 -0
- package/src/handlers/permission-gate-handler.ts +190 -0
- package/src/handlers/tool-call-boundary.ts +91 -0
- package/src/index.ts +225 -0
- package/src/input-normalizer.ts +157 -0
- package/src/logging.ts +113 -0
- package/src/mcp-targets.ts +170 -0
- package/src/node-modules-discovery.ts +76 -0
- package/src/normalize.ts +43 -0
- package/src/path-utils.ts +355 -0
- package/src/pattern-suggest.ts +132 -0
- package/src/permission-dialog.ts +138 -0
- package/src/permission-event-rpc.ts +223 -0
- package/src/permission-events.ts +266 -0
- package/src/permission-forwarding.ts +188 -0
- package/src/permission-gate.ts +94 -0
- package/src/permission-manager.ts +392 -0
- package/src/permission-merge.ts +32 -0
- package/src/permission-prompter.ts +142 -0
- package/src/permission-prompts.ts +93 -0
- package/src/permission-resolver.ts +109 -0
- package/src/permission-session.ts +189 -0
- package/src/permission-ui-prompt.ts +127 -0
- package/src/permissions-service.ts +63 -0
- package/src/persistent-approval-recorder.ts +139 -0
- package/src/policy-loader.ts +350 -0
- package/src/prompting-gateway.ts +104 -0
- package/src/rule.ts +188 -0
- package/src/scope-merge.ts +72 -0
- package/src/service-lifecycle.ts +49 -0
- package/src/service.ts +163 -0
- package/src/session-approval-recorder.ts +6 -0
- package/src/session-approval.ts +43 -0
- package/src/session-logger.ts +91 -0
- package/src/session-rules.ts +79 -0
- package/src/skill-prompt-sanitizer.ts +292 -0
- package/src/status.ts +35 -0
- package/src/subagent-context.ts +104 -0
- package/src/subagent-lifecycle-events.ts +72 -0
- package/src/subagent-registry.ts +105 -0
- package/src/synthesize.ts +92 -0
- package/src/system-prompt-sanitizer.ts +274 -0
- package/src/tool-access-extractor-registry.ts +68 -0
- package/src/tool-input-formatter-registry.ts +67 -0
- package/src/tool-input-preview.ts +34 -0
- package/src/tool-input-prompt-formatters.ts +63 -0
- package/src/tool-preview-formatter.ts +207 -0
- package/src/tool-registry.ts +148 -0
- package/src/types.ts +64 -0
- package/src/wildcard-matcher.ts +120 -0
- package/src/yolo-mode.ts +30 -0
- package/test/active-agent.test.ts +155 -0
- package/test/async-cache.test.ts +48 -0
- package/test/bash-arity.test.ts +144 -0
- package/test/bash-external-directory.test.ts +956 -0
- package/test/builtin-tool-input-formatters.test.ts +109 -0
- package/test/canonicalize-path.test.ts +93 -0
- package/test/common.test.ts +287 -0
- package/test/composition-root.test.ts +603 -0
- package/test/config-loader.test.ts +740 -0
- package/test/config-modal.test.ts +320 -0
- package/test/config-paths.test.ts +83 -0
- package/test/config-pipeline.test.ts +90 -0
- package/test/config-reporter.test.ts +147 -0
- package/test/config-store.test.ts +466 -0
- package/test/decision-audit.test.ts +72 -0
- package/test/decision-reporter.test.ts +112 -0
- package/test/denial-messages.test.ts +656 -0
- package/test/detect-permissive-bash-fallback.test.ts +56 -0
- package/test/expand-home.test.ts +93 -0
- package/test/extension-config.test.ts +129 -0
- package/test/extension-paths.test.ts +108 -0
- package/test/forwarded-permissions/io.test.ts +251 -0
- package/test/forwarding-manager.test.ts +194 -0
- package/test/handlers/before-agent-start.test.ts +317 -0
- package/test/handlers/external-directory-integration.test.ts +623 -0
- package/test/handlers/external-directory-session-dedup.test.ts +430 -0
- package/test/handlers/external-directory-symlink-acceptance.test.ts +149 -0
- package/test/handlers/gates/bash-command-metamorphic.test.ts +83 -0
- package/test/handlers/gates/bash-command.test.ts +191 -0
- package/test/handlers/gates/bash-external-directory.test.ts +269 -0
- package/test/handlers/gates/bash-path.test.ts +337 -0
- package/test/handlers/gates/bash-program.test.ts +410 -0
- package/test/handlers/gates/bash-token-classification.test.ts +241 -0
- package/test/handlers/gates/candidate-check.test.ts +52 -0
- package/test/handlers/gates/external-directory-messages.test.ts +61 -0
- package/test/handlers/gates/external-directory.test.ts +259 -0
- package/test/handlers/gates/helpers.test.ts +177 -0
- package/test/handlers/gates/path.test.ts +294 -0
- package/test/handlers/gates/runner.test.ts +447 -0
- package/test/handlers/gates/skill-input-gate-pipeline.test.ts +176 -0
- package/test/handlers/gates/skill-input.test.ts +131 -0
- package/test/handlers/gates/skill-read.test.ts +158 -0
- package/test/handlers/gates/tool-call-gate-pipeline.test.ts +252 -0
- package/test/handlers/gates/tool.test.ts +223 -0
- package/test/handlers/input-events.test.ts +168 -0
- package/test/handlers/input.test.ts +199 -0
- package/test/handlers/lifecycle.test.ts +221 -0
- package/test/handlers/tool-call-boundary.test.ts +145 -0
- package/test/handlers/tool-call-events.test.ts +277 -0
- package/test/handlers/tool-call.test.ts +395 -0
- package/test/handlers/validate-requested-tool.test.ts +92 -0
- package/test/helpers/gate-fixtures.ts +323 -0
- package/test/helpers/handler-fixtures.ts +335 -0
- package/test/helpers/make-fake-pi.ts +100 -0
- package/test/helpers/manager-harness.ts +112 -0
- package/test/helpers/session-fixtures.ts +204 -0
- package/test/input-normalizer.test.ts +367 -0
- package/test/logging.test.ts +51 -0
- package/test/mcp-targets.test.ts +233 -0
- package/test/node-modules-discovery.test.ts +97 -0
- package/test/normalize.test.ts +247 -0
- package/test/path-utils.test.ts +650 -0
- package/test/pattern-suggest.test.ts +248 -0
- package/test/permission-dialog.test.ts +241 -0
- package/test/permission-event-rpc.test.ts +541 -0
- package/test/permission-events.test.ts +402 -0
- package/test/permission-forwarder.test.ts +369 -0
- package/test/permission-forwarding.test.ts +315 -0
- package/test/permission-gate.test.ts +305 -0
- package/test/permission-manager-unified.test.ts +3368 -0
- package/test/permission-merge.test.ts +61 -0
- package/test/permission-prompter.test.ts +518 -0
- package/test/permission-prompts.test.ts +363 -0
- package/test/permission-resolver.test.ts +265 -0
- package/test/permission-session.test.ts +363 -0
- package/test/permission-ui-prompt.test.ts +146 -0
- package/test/permissions-service.test.ts +177 -0
- package/test/persistent-approval-recorder.test.ts +133 -0
- package/test/pi-infrastructure-read.test.ts +369 -0
- package/test/policy-loader.test.ts +561 -0
- package/test/prompting-gateway.test.ts +230 -0
- package/test/rule.test.ts +604 -0
- package/test/scope-merge.test.ts +116 -0
- package/test/service-lifecycle.test.ts +163 -0
- package/test/service.test.ts +308 -0
- package/test/session-approval.test.ts +75 -0
- package/test/session-logger.test.ts +200 -0
- package/test/session-rules.test.ts +304 -0
- package/test/session-start.test.ts +112 -0
- package/test/skill-prompt-sanitizer.test.ts +374 -0
- package/test/status.test.ts +10 -0
- package/test/subagent-context.test.ts +326 -0
- package/test/subagent-lifecycle-events.test.ts +132 -0
- package/test/subagent-registry.test.ts +145 -0
- package/test/synthesize.test.ts +300 -0
- package/test/system-prompt-sanitizer.test.ts +382 -0
- package/test/tool-access-extractor-registry.test.ts +77 -0
- package/test/tool-input-formatter-registry.test.ts +75 -0
- package/test/tool-input-preview.test.ts +129 -0
- package/test/tool-input-prompt-formatters.test.ts +115 -0
- package/test/tool-preview-formatter.test.ts +458 -0
- package/test/tool-registry.test.ts +197 -0
- package/test/wildcard-matcher.test.ts +424 -0
- package/test/yolo-mode.test.ts +188 -0
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type DenialContext,
|
|
5
|
+
EXTENSION_TAG,
|
|
6
|
+
formatDenyReason,
|
|
7
|
+
formatUnavailableReason,
|
|
8
|
+
formatUserDeniedReason,
|
|
9
|
+
} from "#src/denial-messages";
|
|
10
|
+
import type { PermissionCheckResult } from "#src/types";
|
|
11
|
+
|
|
12
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
function toolCheck(
|
|
15
|
+
toolName: string,
|
|
16
|
+
overrides: Partial<PermissionCheckResult> = {},
|
|
17
|
+
): PermissionCheckResult {
|
|
18
|
+
return {
|
|
19
|
+
toolName,
|
|
20
|
+
state: "deny",
|
|
21
|
+
source: "tool",
|
|
22
|
+
origin: "builtin",
|
|
23
|
+
...overrides,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mcpCheck(
|
|
28
|
+
target: string,
|
|
29
|
+
overrides: Partial<PermissionCheckResult> = {},
|
|
30
|
+
): PermissionCheckResult {
|
|
31
|
+
return {
|
|
32
|
+
toolName: "mcp",
|
|
33
|
+
target,
|
|
34
|
+
state: "deny",
|
|
35
|
+
source: "mcp",
|
|
36
|
+
origin: "builtin",
|
|
37
|
+
...overrides,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toolCtx(
|
|
42
|
+
check: PermissionCheckResult,
|
|
43
|
+
agentName?: string,
|
|
44
|
+
): Extract<DenialContext, { kind: "tool" }> {
|
|
45
|
+
return { kind: "tool", check, agentName };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── EXTENSION_TAG ──────────────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
describe("EXTENSION_TAG", () => {
|
|
51
|
+
test("is [pi-permission-system]", () => {
|
|
52
|
+
expect(EXTENSION_TAG).toBe("[pi-permission-system]");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// ── formatDenyReason ───────────────────────────────────────────────────────
|
|
57
|
+
|
|
58
|
+
describe("formatDenyReason", () => {
|
|
59
|
+
describe("tool context", () => {
|
|
60
|
+
test("generic tool without agent", () => {
|
|
61
|
+
expect(formatDenyReason(toolCtx(toolCheck("write")))).toBe(
|
|
62
|
+
"[pi-permission-system] is not permitted to run 'write'.",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("generic tool with agent", () => {
|
|
67
|
+
expect(formatDenyReason(toolCtx(toolCheck("write"), "my-agent"))).toBe(
|
|
68
|
+
"[pi-permission-system] Agent 'my-agent' is not permitted to run 'write'.",
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("MCP target", () => {
|
|
73
|
+
expect(formatDenyReason(toolCtx(mcpCheck("server:do-thing")))).toBe(
|
|
74
|
+
"[pi-permission-system] is not permitted to run MCP target 'server:do-thing'.",
|
|
75
|
+
);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("bash with command", () => {
|
|
79
|
+
expect(
|
|
80
|
+
formatDenyReason(toolCtx(toolCheck("bash", { command: "rm -rf /" }))),
|
|
81
|
+
).toBe(
|
|
82
|
+
"[pi-permission-system] is not permitted to run 'bash' command 'rm -rf /'.",
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("bash with command and matched pattern", () => {
|
|
87
|
+
expect(
|
|
88
|
+
formatDenyReason(
|
|
89
|
+
toolCtx(
|
|
90
|
+
toolCheck("bash", {
|
|
91
|
+
command: "rm -rf /",
|
|
92
|
+
matchedPattern: "rm *",
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
),
|
|
96
|
+
).toBe(
|
|
97
|
+
"[pi-permission-system] is not permitted to run 'bash' command 'rm -rf /' (matched 'rm *').",
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("bash with nested execution context", () => {
|
|
102
|
+
expect(
|
|
103
|
+
formatDenyReason(
|
|
104
|
+
toolCtx(
|
|
105
|
+
toolCheck("bash", {
|
|
106
|
+
command: "rm -rf foo",
|
|
107
|
+
matchedPattern: "rm *",
|
|
108
|
+
commandContext: "command_substitution",
|
|
109
|
+
}),
|
|
110
|
+
),
|
|
111
|
+
),
|
|
112
|
+
).toBe(
|
|
113
|
+
"[pi-permission-system] is not permitted to run 'bash' command 'rm -rf foo' (matched 'rm *', inside command substitution).",
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("bash with a custom reason appended after the period", () => {
|
|
118
|
+
expect(
|
|
119
|
+
formatDenyReason(
|
|
120
|
+
toolCtx(
|
|
121
|
+
toolCheck("bash", {
|
|
122
|
+
command: "npm install",
|
|
123
|
+
matchedPattern: "npm *",
|
|
124
|
+
reason: "Use pnpm instead",
|
|
125
|
+
}),
|
|
126
|
+
),
|
|
127
|
+
),
|
|
128
|
+
).toBe(
|
|
129
|
+
"[pi-permission-system] is not permitted to run 'bash' command 'npm install' (matched 'npm *'). Reason: Use pnpm instead.",
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("custom reason with no matched pattern", () => {
|
|
134
|
+
expect(
|
|
135
|
+
formatDenyReason(
|
|
136
|
+
toolCtx(
|
|
137
|
+
toolCheck("write", {
|
|
138
|
+
reason: "Write access is disabled for security",
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
),
|
|
142
|
+
).toBe(
|
|
143
|
+
"[pi-permission-system] is not permitted to run 'write'. Reason: Write access is disabled for security.",
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("custom reason is included alongside the agent name", () => {
|
|
148
|
+
expect(
|
|
149
|
+
formatDenyReason(
|
|
150
|
+
toolCtx(
|
|
151
|
+
toolCheck("bash", {
|
|
152
|
+
command: "yarn build",
|
|
153
|
+
matchedPattern: "yarn *",
|
|
154
|
+
reason: "Use pnpm instead",
|
|
155
|
+
}),
|
|
156
|
+
"dev-agent",
|
|
157
|
+
),
|
|
158
|
+
),
|
|
159
|
+
).toBe(
|
|
160
|
+
"[pi-permission-system] Agent 'dev-agent' is not permitted to run 'bash' command 'yarn build' (matched 'yarn *'). Reason: Use pnpm instead.",
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("custom reason on an MCP target", () => {
|
|
165
|
+
expect(
|
|
166
|
+
formatDenyReason(
|
|
167
|
+
toolCtx(
|
|
168
|
+
mcpCheck("server:deploy", {
|
|
169
|
+
reason: "Deploy requires approval from a senior engineer",
|
|
170
|
+
}),
|
|
171
|
+
),
|
|
172
|
+
),
|
|
173
|
+
).toBe(
|
|
174
|
+
"[pi-permission-system] is not permitted to run MCP target 'server:deploy'. Reason: Deploy requires approval from a senior engineer.",
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("MCP source with target on non-mcp toolName", () => {
|
|
179
|
+
expect(
|
|
180
|
+
formatDenyReason(
|
|
181
|
+
toolCtx(
|
|
182
|
+
toolCheck("anything", { source: "mcp", target: "server:tool" }),
|
|
183
|
+
),
|
|
184
|
+
),
|
|
185
|
+
).toBe(
|
|
186
|
+
"[pi-permission-system] is not permitted to run MCP target 'server:tool'.",
|
|
187
|
+
);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("path context", () => {
|
|
192
|
+
test("without agent", () => {
|
|
193
|
+
expect(
|
|
194
|
+
formatDenyReason({
|
|
195
|
+
kind: "path",
|
|
196
|
+
toolName: "read",
|
|
197
|
+
pathValue: "/etc/passwd",
|
|
198
|
+
}),
|
|
199
|
+
).toBe(
|
|
200
|
+
"[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'read'.",
|
|
201
|
+
);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("with agent", () => {
|
|
205
|
+
expect(
|
|
206
|
+
formatDenyReason({
|
|
207
|
+
kind: "path",
|
|
208
|
+
toolName: "read",
|
|
209
|
+
pathValue: "/etc/passwd",
|
|
210
|
+
agentName: "sec-agent",
|
|
211
|
+
}),
|
|
212
|
+
).toBe(
|
|
213
|
+
"[pi-permission-system] Agent 'sec-agent' is not permitted to access path '/etc/passwd' via tool 'read'.",
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe("external_directory context", () => {
|
|
219
|
+
test("without agent", () => {
|
|
220
|
+
expect(
|
|
221
|
+
formatDenyReason({
|
|
222
|
+
kind: "external_directory",
|
|
223
|
+
toolName: "read",
|
|
224
|
+
pathValue: "/etc/passwd",
|
|
225
|
+
cwd: "/project",
|
|
226
|
+
}),
|
|
227
|
+
).toBe(
|
|
228
|
+
"[pi-permission-system] Current agent is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
|
|
229
|
+
);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("with agent", () => {
|
|
233
|
+
expect(
|
|
234
|
+
formatDenyReason({
|
|
235
|
+
kind: "external_directory",
|
|
236
|
+
toolName: "read",
|
|
237
|
+
pathValue: "/etc/passwd",
|
|
238
|
+
cwd: "/project",
|
|
239
|
+
agentName: "sec-agent",
|
|
240
|
+
}),
|
|
241
|
+
).toBe(
|
|
242
|
+
"[pi-permission-system] Agent 'sec-agent' is not permitted to run tool 'read' for path '/etc/passwd' outside working directory '/project'.",
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("bash_external_directory context", () => {
|
|
248
|
+
test("single path without agent", () => {
|
|
249
|
+
expect(
|
|
250
|
+
formatDenyReason({
|
|
251
|
+
kind: "bash_external_directory",
|
|
252
|
+
command: "cat /etc/hosts",
|
|
253
|
+
externalPaths: ["/etc/hosts"],
|
|
254
|
+
cwd: "/project",
|
|
255
|
+
}),
|
|
256
|
+
).toBe(
|
|
257
|
+
"[pi-permission-system] Current agent is not permitted to run bash command 'cat /etc/hosts' which references path(s) outside working directory '/project': /etc/hosts.",
|
|
258
|
+
);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("multiple paths with agent", () => {
|
|
262
|
+
expect(
|
|
263
|
+
formatDenyReason({
|
|
264
|
+
kind: "bash_external_directory",
|
|
265
|
+
command: "cp /etc/hosts /tmp/out",
|
|
266
|
+
externalPaths: ["/etc/hosts", "/tmp/out"],
|
|
267
|
+
cwd: "/project",
|
|
268
|
+
agentName: "my-agent",
|
|
269
|
+
}),
|
|
270
|
+
).toBe(
|
|
271
|
+
"[pi-permission-system] Agent 'my-agent' is not permitted to run bash command 'cp /etc/hosts /tmp/out' which references path(s) outside working directory '/project': /etc/hosts, /tmp/out.",
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
describe("bash_path context", () => {
|
|
277
|
+
test("without agent", () => {
|
|
278
|
+
expect(
|
|
279
|
+
formatDenyReason({
|
|
280
|
+
kind: "bash_path",
|
|
281
|
+
command: "cat /etc/passwd",
|
|
282
|
+
pathValue: "/etc/passwd",
|
|
283
|
+
}),
|
|
284
|
+
).toBe(
|
|
285
|
+
"[pi-permission-system] Current agent is not permitted to access path '/etc/passwd' via tool 'bash'.",
|
|
286
|
+
);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
test("with agent", () => {
|
|
290
|
+
expect(
|
|
291
|
+
formatDenyReason({
|
|
292
|
+
kind: "bash_path",
|
|
293
|
+
command: "cat /etc/passwd",
|
|
294
|
+
pathValue: "/etc/passwd",
|
|
295
|
+
agentName: "my-agent",
|
|
296
|
+
}),
|
|
297
|
+
).toBe(
|
|
298
|
+
"[pi-permission-system] Agent 'my-agent' is not permitted to access path '/etc/passwd' via tool 'bash'.",
|
|
299
|
+
);
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
describe("skill_read context", () => {
|
|
304
|
+
test("without agent", () => {
|
|
305
|
+
expect(
|
|
306
|
+
formatDenyReason({
|
|
307
|
+
kind: "skill_read",
|
|
308
|
+
skillName: "librarian",
|
|
309
|
+
readPath: "/skills/librarian/SKILL.md",
|
|
310
|
+
}),
|
|
311
|
+
).toBe(
|
|
312
|
+
"[pi-permission-system] Current agent is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
|
|
313
|
+
);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test("with agent", () => {
|
|
317
|
+
expect(
|
|
318
|
+
formatDenyReason({
|
|
319
|
+
kind: "skill_read",
|
|
320
|
+
skillName: "librarian",
|
|
321
|
+
readPath: "/skills/librarian/SKILL.md",
|
|
322
|
+
agentName: "my-agent",
|
|
323
|
+
}),
|
|
324
|
+
).toBe(
|
|
325
|
+
"[pi-permission-system] Agent 'my-agent' is not permitted to access skill 'librarian' via '/skills/librarian/SKILL.md'.",
|
|
326
|
+
);
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
describe("skill_input context", () => {
|
|
331
|
+
test("without agent", () => {
|
|
332
|
+
expect(
|
|
333
|
+
formatDenyReason({
|
|
334
|
+
kind: "skill_input",
|
|
335
|
+
skillName: "librarian",
|
|
336
|
+
}),
|
|
337
|
+
).toBe(
|
|
338
|
+
"[pi-permission-system] Current agent is not permitted to access skill 'librarian'.",
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("with agent", () => {
|
|
343
|
+
expect(
|
|
344
|
+
formatDenyReason({
|
|
345
|
+
kind: "skill_input",
|
|
346
|
+
skillName: "librarian",
|
|
347
|
+
agentName: "my-agent",
|
|
348
|
+
}),
|
|
349
|
+
).toBe(
|
|
350
|
+
"[pi-permission-system] Agent 'my-agent' is not permitted to access skill 'librarian'.",
|
|
351
|
+
);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// ── formatUnavailableReason ────────────────────────────────────────────────
|
|
357
|
+
|
|
358
|
+
describe("formatUnavailableReason", () => {
|
|
359
|
+
test("generic tool", () => {
|
|
360
|
+
expect(formatUnavailableReason(toolCtx(toolCheck("write")))).toBe(
|
|
361
|
+
"[pi-permission-system] Using tool 'write' requires approval, but no interactive UI is available.",
|
|
362
|
+
);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
test("bash with command", () => {
|
|
366
|
+
expect(
|
|
367
|
+
formatUnavailableReason(
|
|
368
|
+
toolCtx(toolCheck("bash", { command: "git push" })),
|
|
369
|
+
),
|
|
370
|
+
).toBe(
|
|
371
|
+
"[pi-permission-system] Running bash command 'git push' requires approval, but no interactive UI is available.",
|
|
372
|
+
);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
test("mcp", () => {
|
|
376
|
+
expect(formatUnavailableReason(toolCtx(mcpCheck("server:tool")))).toBe(
|
|
377
|
+
"[pi-permission-system] Using tool 'mcp' requires approval, but no interactive UI is available.",
|
|
378
|
+
);
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
test("path", () => {
|
|
382
|
+
expect(
|
|
383
|
+
formatUnavailableReason({
|
|
384
|
+
kind: "path",
|
|
385
|
+
toolName: "read",
|
|
386
|
+
pathValue: "/etc/passwd",
|
|
387
|
+
}),
|
|
388
|
+
).toBe(
|
|
389
|
+
"[pi-permission-system] Accessing '/etc/passwd' requires approval, but no interactive UI is available.",
|
|
390
|
+
);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
test("external_directory", () => {
|
|
394
|
+
expect(
|
|
395
|
+
formatUnavailableReason({
|
|
396
|
+
kind: "external_directory",
|
|
397
|
+
toolName: "read",
|
|
398
|
+
pathValue: "/etc/passwd",
|
|
399
|
+
cwd: "/project",
|
|
400
|
+
}),
|
|
401
|
+
).toBe(
|
|
402
|
+
"[pi-permission-system] Accessing '/etc/passwd' outside the working directory requires approval, but no interactive UI is available.",
|
|
403
|
+
);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
test("bash_external_directory", () => {
|
|
407
|
+
expect(
|
|
408
|
+
formatUnavailableReason({
|
|
409
|
+
kind: "bash_external_directory",
|
|
410
|
+
command: "cat /etc/hosts",
|
|
411
|
+
externalPaths: ["/etc/hosts"],
|
|
412
|
+
cwd: "/project",
|
|
413
|
+
}),
|
|
414
|
+
).toBe(
|
|
415
|
+
"[pi-permission-system] Bash command 'cat /etc/hosts' references path(s) outside the working directory and requires approval, but no interactive UI is available.",
|
|
416
|
+
);
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
test("bash_path", () => {
|
|
420
|
+
expect(
|
|
421
|
+
formatUnavailableReason({
|
|
422
|
+
kind: "bash_path",
|
|
423
|
+
command: "cat /etc/passwd",
|
|
424
|
+
pathValue: "/etc/passwd",
|
|
425
|
+
}),
|
|
426
|
+
).toBe(
|
|
427
|
+
"[pi-permission-system] Bash command 'cat /etc/passwd' accesses path '/etc/passwd' which requires approval, but no interactive UI is available.",
|
|
428
|
+
);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test("skill_read", () => {
|
|
432
|
+
expect(
|
|
433
|
+
formatUnavailableReason({
|
|
434
|
+
kind: "skill_read",
|
|
435
|
+
skillName: "librarian",
|
|
436
|
+
readPath: "/skills/librarian/SKILL.md",
|
|
437
|
+
}),
|
|
438
|
+
).toBe(
|
|
439
|
+
"[pi-permission-system] Accessing skill 'librarian' requires approval, but no interactive UI is available.",
|
|
440
|
+
);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("skill_input", () => {
|
|
444
|
+
expect(
|
|
445
|
+
formatUnavailableReason({
|
|
446
|
+
kind: "skill_input",
|
|
447
|
+
skillName: "librarian",
|
|
448
|
+
}),
|
|
449
|
+
).toBe(
|
|
450
|
+
"[pi-permission-system] Accessing skill 'librarian' requires approval, but no interactive UI is available.",
|
|
451
|
+
);
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// ── formatUserDeniedReason ─────────────────────────────────────────────────
|
|
456
|
+
|
|
457
|
+
describe("formatUserDeniedReason", () => {
|
|
458
|
+
describe("tool context", () => {
|
|
459
|
+
test("generic tool without reason", () => {
|
|
460
|
+
expect(formatUserDeniedReason(toolCtx(toolCheck("write")))).toBe(
|
|
461
|
+
"[pi-permission-system] User denied tool 'write'.",
|
|
462
|
+
);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
test("generic tool with reason", () => {
|
|
466
|
+
expect(
|
|
467
|
+
formatUserDeniedReason(toolCtx(toolCheck("write")), "too risky"),
|
|
468
|
+
).toBe(
|
|
469
|
+
"[pi-permission-system] User denied tool 'write'. Reason: too risky.",
|
|
470
|
+
);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
test("bash with command", () => {
|
|
474
|
+
expect(
|
|
475
|
+
formatUserDeniedReason(
|
|
476
|
+
toolCtx(toolCheck("bash", { command: "ls -la" })),
|
|
477
|
+
),
|
|
478
|
+
).toBe("[pi-permission-system] User denied bash command 'ls -la'.");
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
test("MCP target", () => {
|
|
482
|
+
expect(formatUserDeniedReason(toolCtx(mcpCheck("server:query")))).toBe(
|
|
483
|
+
"[pi-permission-system] User denied MCP target 'server:query'.",
|
|
484
|
+
);
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
describe("path context", () => {
|
|
489
|
+
test("without reason", () => {
|
|
490
|
+
expect(
|
|
491
|
+
formatUserDeniedReason({
|
|
492
|
+
kind: "path",
|
|
493
|
+
toolName: "read",
|
|
494
|
+
pathValue: "/etc/passwd",
|
|
495
|
+
}),
|
|
496
|
+
).toBe(
|
|
497
|
+
"[pi-permission-system] User denied access to path '/etc/passwd'.",
|
|
498
|
+
);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
test("with reason", () => {
|
|
502
|
+
expect(
|
|
503
|
+
formatUserDeniedReason(
|
|
504
|
+
{ kind: "path", toolName: "read", pathValue: "/etc/passwd" },
|
|
505
|
+
"sensitive",
|
|
506
|
+
),
|
|
507
|
+
).toBe(
|
|
508
|
+
"[pi-permission-system] User denied access to path '/etc/passwd'. Reason: sensitive.",
|
|
509
|
+
);
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
describe("external_directory context", () => {
|
|
514
|
+
test("without reason", () => {
|
|
515
|
+
expect(
|
|
516
|
+
formatUserDeniedReason({
|
|
517
|
+
kind: "external_directory",
|
|
518
|
+
toolName: "edit",
|
|
519
|
+
pathValue: "/etc/hosts",
|
|
520
|
+
cwd: "/project",
|
|
521
|
+
}),
|
|
522
|
+
).toBe(
|
|
523
|
+
"[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'.",
|
|
524
|
+
);
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
test("with reason", () => {
|
|
528
|
+
expect(
|
|
529
|
+
formatUserDeniedReason(
|
|
530
|
+
{
|
|
531
|
+
kind: "external_directory",
|
|
532
|
+
toolName: "edit",
|
|
533
|
+
pathValue: "/etc/hosts",
|
|
534
|
+
cwd: "/project",
|
|
535
|
+
},
|
|
536
|
+
"too risky",
|
|
537
|
+
),
|
|
538
|
+
).toBe(
|
|
539
|
+
"[pi-permission-system] User denied external directory access for tool 'edit' path '/etc/hosts'. Reason: too risky.",
|
|
540
|
+
);
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
describe("bash_external_directory context", () => {
|
|
545
|
+
test("without reason", () => {
|
|
546
|
+
expect(
|
|
547
|
+
formatUserDeniedReason({
|
|
548
|
+
kind: "bash_external_directory",
|
|
549
|
+
command: "rm /etc/hosts",
|
|
550
|
+
externalPaths: ["/etc/hosts"],
|
|
551
|
+
cwd: "/project",
|
|
552
|
+
}),
|
|
553
|
+
).toBe(
|
|
554
|
+
"[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'.",
|
|
555
|
+
);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
test("with reason", () => {
|
|
559
|
+
expect(
|
|
560
|
+
formatUserDeniedReason(
|
|
561
|
+
{
|
|
562
|
+
kind: "bash_external_directory",
|
|
563
|
+
command: "rm /etc/hosts",
|
|
564
|
+
externalPaths: ["/etc/hosts"],
|
|
565
|
+
cwd: "/project",
|
|
566
|
+
},
|
|
567
|
+
"dangerous",
|
|
568
|
+
),
|
|
569
|
+
).toBe(
|
|
570
|
+
"[pi-permission-system] User denied external directory access for bash command 'rm /etc/hosts'. Reason: dangerous.",
|
|
571
|
+
);
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
describe("bash_path context", () => {
|
|
576
|
+
test("without reason", () => {
|
|
577
|
+
expect(
|
|
578
|
+
formatUserDeniedReason({
|
|
579
|
+
kind: "bash_path",
|
|
580
|
+
command: "cat /etc/passwd",
|
|
581
|
+
pathValue: "/etc/passwd",
|
|
582
|
+
}),
|
|
583
|
+
).toBe(
|
|
584
|
+
"[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd').",
|
|
585
|
+
);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
test("with reason", () => {
|
|
589
|
+
expect(
|
|
590
|
+
formatUserDeniedReason(
|
|
591
|
+
{
|
|
592
|
+
kind: "bash_path",
|
|
593
|
+
command: "cat /etc/passwd",
|
|
594
|
+
pathValue: "/etc/passwd",
|
|
595
|
+
},
|
|
596
|
+
"sensitive",
|
|
597
|
+
),
|
|
598
|
+
).toBe(
|
|
599
|
+
"[pi-permission-system] User denied path access for bash command 'cat /etc/passwd' (path '/etc/passwd'). Reason: sensitive.",
|
|
600
|
+
);
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
describe("skill_read context", () => {
|
|
605
|
+
test("without reason", () => {
|
|
606
|
+
expect(
|
|
607
|
+
formatUserDeniedReason({
|
|
608
|
+
kind: "skill_read",
|
|
609
|
+
skillName: "librarian",
|
|
610
|
+
readPath: "/skills/librarian/SKILL.md",
|
|
611
|
+
}),
|
|
612
|
+
).toBe("[pi-permission-system] User denied access to skill 'librarian'.");
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
test("with reason", () => {
|
|
616
|
+
expect(
|
|
617
|
+
formatUserDeniedReason(
|
|
618
|
+
{
|
|
619
|
+
kind: "skill_read",
|
|
620
|
+
skillName: "librarian",
|
|
621
|
+
readPath: "/skills/librarian/SKILL.md",
|
|
622
|
+
},
|
|
623
|
+
"not needed",
|
|
624
|
+
),
|
|
625
|
+
).toBe(
|
|
626
|
+
"[pi-permission-system] User denied access to skill 'librarian'. Reason: not needed.",
|
|
627
|
+
);
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
describe("skill_input context", () => {
|
|
632
|
+
test("without agent and without reason", () => {
|
|
633
|
+
expect(
|
|
634
|
+
formatUserDeniedReason({
|
|
635
|
+
kind: "skill_input",
|
|
636
|
+
skillName: "librarian",
|
|
637
|
+
}),
|
|
638
|
+
).toBe("[pi-permission-system] User denied access to skill 'librarian'.");
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
test("with agent and with reason", () => {
|
|
642
|
+
expect(
|
|
643
|
+
formatUserDeniedReason(
|
|
644
|
+
{
|
|
645
|
+
kind: "skill_input",
|
|
646
|
+
skillName: "librarian",
|
|
647
|
+
agentName: "code-agent",
|
|
648
|
+
},
|
|
649
|
+
"not permitted",
|
|
650
|
+
),
|
|
651
|
+
).toBe(
|
|
652
|
+
"[pi-permission-system] User denied access to skill 'librarian'. Reason: not permitted.",
|
|
653
|
+
);
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { detectPermissiveBashFallback } from "#src/config-loader";
|
|
4
|
+
import type { FlatPermissionConfig } from "#src/types";
|
|
5
|
+
|
|
6
|
+
describe("detectPermissiveBashFallback", () => {
|
|
7
|
+
it("warns when top-level '*' is allow and bash is absent", () => {
|
|
8
|
+
const permission: FlatPermissionConfig = { "*": "allow" };
|
|
9
|
+
|
|
10
|
+
const issue = detectPermissiveBashFallback(permission);
|
|
11
|
+
|
|
12
|
+
expect(issue).toBeDefined();
|
|
13
|
+
expect(issue).toContain("bash");
|
|
14
|
+
expect(issue).toContain("allow");
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("warns when top-level '*' is allow and bash map has no '*' key", () => {
|
|
18
|
+
const permission: FlatPermissionConfig = {
|
|
19
|
+
"*": "allow",
|
|
20
|
+
bash: { "git *": "ask" },
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
expect(detectPermissiveBashFallback(permission)).toBeDefined();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("does not warn when bash is a bare string surface", () => {
|
|
27
|
+
const permission: FlatPermissionConfig = { "*": "allow", bash: "ask" };
|
|
28
|
+
|
|
29
|
+
expect(detectPermissiveBashFallback(permission)).toBeUndefined();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("does not warn when bash map has an explicit '*' key", () => {
|
|
33
|
+
const permission: FlatPermissionConfig = {
|
|
34
|
+
"*": "allow",
|
|
35
|
+
bash: { "*": "ask", "git *": "allow" },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
expect(detectPermissiveBashFallback(permission)).toBeUndefined();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("does not warn when top-level '*' is not allow", () => {
|
|
42
|
+
const permission: FlatPermissionConfig = { "*": "ask" };
|
|
43
|
+
|
|
44
|
+
expect(detectPermissiveBashFallback(permission)).toBeUndefined();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("does not warn when top-level '*' is absent", () => {
|
|
48
|
+
const permission: FlatPermissionConfig = { bash: { "git *": "ask" } };
|
|
49
|
+
|
|
50
|
+
expect(detectPermissiveBashFallback(permission)).toBeUndefined();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("does not warn when permission is undefined", () => {
|
|
54
|
+
expect(detectPermissiveBashFallback(undefined)).toBeUndefined();
|
|
55
|
+
});
|
|
56
|
+
});
|