@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,395 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { getEventInput } from "#src/handlers/permission-gate-handler";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
makeBashCommandCheck,
|
|
7
|
+
makeCheckResult,
|
|
8
|
+
makeCtx,
|
|
9
|
+
makeHandler,
|
|
10
|
+
makeSurfaceCheck,
|
|
11
|
+
makeToolCallEvent,
|
|
12
|
+
} from "#test/helpers/handler-fixtures";
|
|
13
|
+
|
|
14
|
+
// ── SDK stubs ──────────────────────────────────────────────────────────────
|
|
15
|
+
vi.mock("@earendil-works/pi-coding-agent", async (importOriginal) => {
|
|
16
|
+
const original =
|
|
17
|
+
await importOriginal<typeof import("@earendil-works/pi-coding-agent")>();
|
|
18
|
+
return { ...original };
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// ── getEventInput ──────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
describe("getEventInput", () => {
|
|
24
|
+
it("returns the input field when present", () => {
|
|
25
|
+
expect(getEventInput({ input: { path: "/foo" } })).toEqual({
|
|
26
|
+
path: "/foo",
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("returns the arguments field when input is absent", () => {
|
|
31
|
+
expect(getEventInput({ arguments: { command: "ls" } })).toEqual({
|
|
32
|
+
command: "ls",
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("returns empty object when neither field is present", () => {
|
|
37
|
+
expect(getEventInput({ type: "tool_call" })).toEqual({});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("prefers input over arguments when both are present", () => {
|
|
41
|
+
expect(getEventInput({ input: { a: 1 }, arguments: { b: 2 } })).toEqual({
|
|
42
|
+
a: 1,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// ── handleToolCall ─────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
describe("handleToolCall", () => {
|
|
50
|
+
it("activates session with ctx", async () => {
|
|
51
|
+
const ctx = makeCtx();
|
|
52
|
+
const { handler, forwarding } = makeHandler();
|
|
53
|
+
await handler.handleToolCall(makeToolCallEvent("read"), ctx);
|
|
54
|
+
// session.activate(ctx) calls forwarding.start(ctx) on the real session
|
|
55
|
+
expect(forwarding.start).toHaveBeenCalledWith(ctx);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("blocks when tool name cannot be resolved", async () => {
|
|
59
|
+
const { handler } = makeHandler();
|
|
60
|
+
const result = await handler.handleToolCall(
|
|
61
|
+
{ type: "tool_call" },
|
|
62
|
+
makeCtx(),
|
|
63
|
+
);
|
|
64
|
+
expect(result).toEqual({
|
|
65
|
+
action: "block",
|
|
66
|
+
reason: expect.stringContaining("tool"),
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("blocks when tool is not registered", async () => {
|
|
71
|
+
const { handler } = makeHandler({ tools: ["read"] });
|
|
72
|
+
const result = await handler.handleToolCall(
|
|
73
|
+
makeToolCallEvent("unknown-tool"),
|
|
74
|
+
makeCtx(),
|
|
75
|
+
);
|
|
76
|
+
expect(result).toMatchObject({ action: "block" });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("returns empty object when tool is allowed", async () => {
|
|
80
|
+
const { handler } = makeHandler();
|
|
81
|
+
const result = await handler.handleToolCall(
|
|
82
|
+
makeToolCallEvent("read"),
|
|
83
|
+
makeCtx(),
|
|
84
|
+
);
|
|
85
|
+
expect(result).toEqual({ action: "allow" });
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("blocks when tool is denied by policy", async () => {
|
|
89
|
+
const { handler } = makeHandler({
|
|
90
|
+
session: {
|
|
91
|
+
checkPermission: vi
|
|
92
|
+
.fn()
|
|
93
|
+
.mockReturnValue(makeCheckResult({ state: "deny" })),
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
const result = await handler.handleToolCall(
|
|
97
|
+
makeToolCallEvent("read"),
|
|
98
|
+
makeCtx(),
|
|
99
|
+
);
|
|
100
|
+
expect(result).toMatchObject({ action: "block" });
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// ── skill-read gate ────────────────────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
describe("handleToolCall — skill-read gate", () => {
|
|
107
|
+
it("blocks a read of a denied skill path", async () => {
|
|
108
|
+
const skillEntry = {
|
|
109
|
+
name: "librarian",
|
|
110
|
+
description: "Research skills",
|
|
111
|
+
location: "/skills/librarian/SKILL.md",
|
|
112
|
+
state: "deny" as const,
|
|
113
|
+
normalizedLocation: "/skills/librarian/SKILL.md",
|
|
114
|
+
normalizedBaseDir: "/skills/librarian",
|
|
115
|
+
};
|
|
116
|
+
const { handler } = makeHandler({
|
|
117
|
+
session: {
|
|
118
|
+
getActiveSkillEntries: vi.fn().mockReturnValue([skillEntry]),
|
|
119
|
+
},
|
|
120
|
+
toolRegistry: {
|
|
121
|
+
getAll: vi.fn().mockReturnValue([{ toolName: "read" }]),
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
const event = {
|
|
125
|
+
type: "tool_call",
|
|
126
|
+
toolCallId: "tc-skill",
|
|
127
|
+
toolName: "read",
|
|
128
|
+
input: { path: "/skills/librarian/SKILL.md" },
|
|
129
|
+
};
|
|
130
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
131
|
+
expect(result).toMatchObject({ action: "block" });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("allows a read of a non-skill path even when skill entries are present", async () => {
|
|
135
|
+
const skillEntry = {
|
|
136
|
+
name: "librarian",
|
|
137
|
+
description: "Research skills",
|
|
138
|
+
location: "/skills/librarian/SKILL.md",
|
|
139
|
+
state: "deny" as const,
|
|
140
|
+
normalizedLocation: "/skills/librarian/SKILL.md",
|
|
141
|
+
normalizedBaseDir: "/skills/librarian",
|
|
142
|
+
};
|
|
143
|
+
const { handler } = makeHandler({
|
|
144
|
+
session: {
|
|
145
|
+
getActiveSkillEntries: vi.fn().mockReturnValue([skillEntry]),
|
|
146
|
+
},
|
|
147
|
+
toolRegistry: {
|
|
148
|
+
getAll: vi.fn().mockReturnValue([{ toolName: "read" }]),
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
const event = {
|
|
152
|
+
type: "tool_call",
|
|
153
|
+
toolCallId: "tc-ok",
|
|
154
|
+
toolName: "read",
|
|
155
|
+
input: { path: "/test/project/src/index.ts" },
|
|
156
|
+
};
|
|
157
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
158
|
+
expect(result).toEqual({ action: "allow" });
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// ── external-directory gate ────────────────────────────────────────────────
|
|
163
|
+
|
|
164
|
+
describe("handleToolCall — external-directory gate", () => {
|
|
165
|
+
it("blocks a read of a path outside cwd when policy is deny", async () => {
|
|
166
|
+
const { handler } = makeHandler({
|
|
167
|
+
session: {
|
|
168
|
+
checkPermission: vi
|
|
169
|
+
.fn()
|
|
170
|
+
.mockReturnValue(makeCheckResult({ state: "deny" })),
|
|
171
|
+
},
|
|
172
|
+
tools: ["read"],
|
|
173
|
+
});
|
|
174
|
+
const event = makeToolCallEvent("read", {
|
|
175
|
+
input: { path: "/outside/project/file.ts" },
|
|
176
|
+
});
|
|
177
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
178
|
+
expect(result).toMatchObject({ action: "block" });
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ── bash external-directory gate ──────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
describe("handleToolCall — bash external-directory gate", () => {
|
|
185
|
+
it("blocks a bash command referencing an external path when policy is deny", async () => {
|
|
186
|
+
const { handler } = makeHandler({
|
|
187
|
+
session: {
|
|
188
|
+
checkPermission: vi
|
|
189
|
+
.fn()
|
|
190
|
+
.mockReturnValue(makeCheckResult({ state: "deny" })),
|
|
191
|
+
},
|
|
192
|
+
tools: ["bash"],
|
|
193
|
+
});
|
|
194
|
+
const event = makeToolCallEvent("bash", {
|
|
195
|
+
input: { command: "cat /outside/project/file.ts" },
|
|
196
|
+
});
|
|
197
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
198
|
+
expect(result).toMatchObject({ action: "block" });
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// ── path gate (tools) ─────────────────────────────────────────────────────
|
|
203
|
+
|
|
204
|
+
describe("handleToolCall — path gate (tools)", () => {
|
|
205
|
+
it("blocks a read of .env when path surface denies *.env", async () => {
|
|
206
|
+
const { handler } = makeHandler({
|
|
207
|
+
session: {
|
|
208
|
+
checkPermission: makeSurfaceCheck({
|
|
209
|
+
path: { state: "deny", matchedPattern: "*.env" },
|
|
210
|
+
}),
|
|
211
|
+
},
|
|
212
|
+
tools: ["read"],
|
|
213
|
+
});
|
|
214
|
+
const event = makeToolCallEvent("read", { input: { path: ".env" } });
|
|
215
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
216
|
+
expect(result).toMatchObject({ action: "block" });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("allows a read when path surface allows", async () => {
|
|
220
|
+
const { handler } = makeHandler({ tools: ["read"] });
|
|
221
|
+
const event = makeToolCallEvent("read", {
|
|
222
|
+
input: { path: "src/index.ts" },
|
|
223
|
+
});
|
|
224
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
225
|
+
expect(result).toEqual({ action: "allow" });
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// ── bash path gate ────────────────────────────────────────────────────────
|
|
230
|
+
|
|
231
|
+
describe("handleToolCall — bash path gate", () => {
|
|
232
|
+
it("blocks a bash command accessing .env when path surface denies", async () => {
|
|
233
|
+
const { handler } = makeHandler({
|
|
234
|
+
session: {
|
|
235
|
+
checkPermission: makeSurfaceCheck({
|
|
236
|
+
path: { state: "deny", matchedPattern: "*.env" },
|
|
237
|
+
}),
|
|
238
|
+
},
|
|
239
|
+
tools: ["bash"],
|
|
240
|
+
});
|
|
241
|
+
const event = makeToolCallEvent("bash", { input: { command: "cat .env" } });
|
|
242
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
243
|
+
expect(result).toMatchObject({ action: "block" });
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
// ── bash command chain gate ───────────────────────────────────────────────
|
|
248
|
+
|
|
249
|
+
describe("handleToolCall — bash command chain gate", () => {
|
|
250
|
+
it("blocks a chain when a later sub-command is denied (#301)", async () => {
|
|
251
|
+
const { handler } = makeHandler({
|
|
252
|
+
session: {
|
|
253
|
+
checkPermission: makeBashCommandCheck({
|
|
254
|
+
deny: /^npm\b/,
|
|
255
|
+
denyMatched: "npm *",
|
|
256
|
+
allowMatched: "echo *",
|
|
257
|
+
}),
|
|
258
|
+
},
|
|
259
|
+
tools: ["bash"],
|
|
260
|
+
});
|
|
261
|
+
const event = makeToolCallEvent("bash", {
|
|
262
|
+
input: { command: "echo start && npm install compromised-package" },
|
|
263
|
+
});
|
|
264
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
265
|
+
expect(result).toMatchObject({ action: "block" });
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("blocks a command nested inside command substitution (#306)", async () => {
|
|
269
|
+
const { handler } = makeHandler({
|
|
270
|
+
session: {
|
|
271
|
+
checkPermission: makeBashCommandCheck({
|
|
272
|
+
deny: /^rm\b/,
|
|
273
|
+
denyMatched: "rm *",
|
|
274
|
+
allowMatched: "echo *",
|
|
275
|
+
}),
|
|
276
|
+
},
|
|
277
|
+
tools: ["bash"],
|
|
278
|
+
});
|
|
279
|
+
const event = makeToolCallEvent("bash", {
|
|
280
|
+
input: { command: "echo $(rm -rf foo)" },
|
|
281
|
+
});
|
|
282
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
283
|
+
expect(result).toMatchObject({ action: "block" });
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("allows a single non-chained bash command", async () => {
|
|
287
|
+
const { handler } = makeHandler({ tools: ["bash"] });
|
|
288
|
+
const event = makeToolCallEvent("bash", { input: { command: "echo hi" } });
|
|
289
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
290
|
+
expect(result).toEqual({ action: "allow" });
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// ---------------------------------------------------------------------------
|
|
295
|
+
// Moved from permission-system.test.ts catch-all (#342)
|
|
296
|
+
// ---------------------------------------------------------------------------
|
|
297
|
+
|
|
298
|
+
describe("handleToolCall — bash external-directory policy states", () => {
|
|
299
|
+
it("allows bash command with only internal paths when external_directory is denied", async () => {
|
|
300
|
+
const { handler } = makeHandler({ tools: ["bash"] });
|
|
301
|
+
const event = makeToolCallEvent("bash", {
|
|
302
|
+
input: { command: "cat src/index.ts" },
|
|
303
|
+
});
|
|
304
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
305
|
+
expect(result).toEqual({ action: "allow" });
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it("blocks bash command with external path when external_directory is ask and no UI", async () => {
|
|
309
|
+
const { handler } = makeHandler({
|
|
310
|
+
session: {
|
|
311
|
+
checkPermission: makeSurfaceCheck({
|
|
312
|
+
external_directory: { state: "ask", source: "special" },
|
|
313
|
+
}),
|
|
314
|
+
},
|
|
315
|
+
tools: ["bash"],
|
|
316
|
+
prompter: {
|
|
317
|
+
canConfirm: vi.fn().mockReturnValue(false),
|
|
318
|
+
prompt: vi.fn().mockResolvedValue({ approved: false, state: "denied" }),
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
const event = makeToolCallEvent("bash", {
|
|
322
|
+
input: { command: "cat /etc/hosts" },
|
|
323
|
+
});
|
|
324
|
+
const result = await handler.handleToolCall(
|
|
325
|
+
event,
|
|
326
|
+
makeCtx({ hasUI: false }),
|
|
327
|
+
);
|
|
328
|
+
expect(result).toMatchObject({ action: "block" });
|
|
329
|
+
expect(String((result as { reason?: unknown }).reason)).toMatch(
|
|
330
|
+
/no interactive UI/i,
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("allows bash command with external path when external_directory is allow", async () => {
|
|
335
|
+
const { handler } = makeHandler({
|
|
336
|
+
session: {
|
|
337
|
+
checkPermission: makeSurfaceCheck({
|
|
338
|
+
external_directory: { state: "allow", source: "special" },
|
|
339
|
+
}),
|
|
340
|
+
},
|
|
341
|
+
tools: ["bash"],
|
|
342
|
+
});
|
|
343
|
+
const event = makeToolCallEvent("bash", {
|
|
344
|
+
input: { command: "cat /etc/hosts" },
|
|
345
|
+
});
|
|
346
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
347
|
+
expect(result).toEqual({ action: "allow" });
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("applies bash pattern deny after external_directory allow", async () => {
|
|
351
|
+
const { handler } = makeHandler({
|
|
352
|
+
session: {
|
|
353
|
+
checkPermission: makeSurfaceCheck(
|
|
354
|
+
{
|
|
355
|
+
external_directory: { state: "allow", source: "special" },
|
|
356
|
+
bash: { state: "deny", source: "bash" },
|
|
357
|
+
},
|
|
358
|
+
{ state: "allow" },
|
|
359
|
+
),
|
|
360
|
+
},
|
|
361
|
+
tools: ["bash"],
|
|
362
|
+
});
|
|
363
|
+
const event = makeToolCallEvent("bash", {
|
|
364
|
+
input: { command: "cat /etc/hosts" },
|
|
365
|
+
});
|
|
366
|
+
const result = await handler.handleToolCall(event, makeCtx());
|
|
367
|
+
expect(result).toMatchObject({ action: "block" });
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe("handleToolCall — generic ask prompt content", () => {
|
|
372
|
+
it("ask prompt includes serialized tool input for informed approval", async () => {
|
|
373
|
+
const { handler, prompter } = makeHandler({
|
|
374
|
+
session: {
|
|
375
|
+
checkPermission: makeSurfaceCheck({
|
|
376
|
+
weather_lookup: { state: "ask" },
|
|
377
|
+
}),
|
|
378
|
+
},
|
|
379
|
+
tools: ["weather_lookup"],
|
|
380
|
+
prompter: {
|
|
381
|
+
canConfirm: vi.fn().mockReturnValue(true),
|
|
382
|
+
prompt: vi.fn().mockResolvedValue({ approved: false, state: "denied" }),
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
const event = makeToolCallEvent("weather_lookup", {
|
|
386
|
+
input: { city: "Chicago", units: "metric" },
|
|
387
|
+
});
|
|
388
|
+
await handler.handleToolCall(event, makeCtx());
|
|
389
|
+
expect(vi.mocked(prompter.prompt)).toHaveBeenCalledTimes(1);
|
|
390
|
+
const promptDetails = vi.mocked(prompter.prompt).mock.calls[0][0];
|
|
391
|
+
expect(promptDetails.message).toMatch(
|
|
392
|
+
/\{"city":"Chicago","units":"metric"\}/,
|
|
393
|
+
);
|
|
394
|
+
});
|
|
395
|
+
});
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type RequestedToolValidation,
|
|
5
|
+
validateRequestedTool,
|
|
6
|
+
} from "#src/handlers/permission-gate-handler";
|
|
7
|
+
|
|
8
|
+
// ── helpers ────────────────────────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
function makeTools(names: string[]): { name: string }[] {
|
|
11
|
+
return names.map((name) => ({ name }));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const TOOLS = makeTools(["read", "bash", "edit"]);
|
|
15
|
+
|
|
16
|
+
// ── validateRequestedTool ──────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
describe("validateRequestedTool", () => {
|
|
19
|
+
describe("missing / unresolvable tool name", () => {
|
|
20
|
+
it("blocks when event has no name field", () => {
|
|
21
|
+
const result = validateRequestedTool({ type: "tool_call" }, TOOLS);
|
|
22
|
+
expect(result.status).toBe("block");
|
|
23
|
+
expect(
|
|
24
|
+
(result as Extract<RequestedToolValidation, { status: "block" }>)
|
|
25
|
+
.reason,
|
|
26
|
+
).toBeTruthy();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("blocks when name field is an empty string", () => {
|
|
30
|
+
const result = validateRequestedTool({ name: "" }, TOOLS);
|
|
31
|
+
expect(result.status).toBe("block");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("blocks when name field is null", () => {
|
|
35
|
+
const result = validateRequestedTool({ name: null }, TOOLS);
|
|
36
|
+
expect(result.status).toBe("block");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("blocks when event is a primitive", () => {
|
|
40
|
+
const result = validateRequestedTool("not-an-object", TOOLS);
|
|
41
|
+
expect(result.status).toBe("block");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("unregistered tool", () => {
|
|
46
|
+
it("blocks when the tool name is not in the registered list", () => {
|
|
47
|
+
const result = validateRequestedTool({ name: "unknown-tool" }, TOOLS);
|
|
48
|
+
expect(result.status).toBe("block");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("includes available tool names in the block reason", () => {
|
|
52
|
+
const result = validateRequestedTool({ name: "unknown-tool" }, TOOLS);
|
|
53
|
+
expect(result.status).toBe("block");
|
|
54
|
+
const { reason } = result as Extract<
|
|
55
|
+
RequestedToolValidation,
|
|
56
|
+
{ status: "block" }
|
|
57
|
+
>;
|
|
58
|
+
expect(reason).toContain("read");
|
|
59
|
+
expect(reason).toContain("bash");
|
|
60
|
+
expect(reason).toContain("edit");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("blocks with empty available list when no tools are registered", () => {
|
|
64
|
+
const result = validateRequestedTool({ name: "anything" }, []);
|
|
65
|
+
expect(result.status).toBe("block");
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("registered tool (ok path)", () => {
|
|
70
|
+
it("returns ok with the raw tool name for a known tool", () => {
|
|
71
|
+
const result = validateRequestedTool({ name: "read" }, TOOLS);
|
|
72
|
+
expect(result).toEqual({ status: "ok", toolName: "read" });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("returns the raw name as it appeared in the event (not normalised)", () => {
|
|
76
|
+
// If an alias mechanism were to normalise "Read" → "read",
|
|
77
|
+
// validateRequestedTool still returns the raw value from the event.
|
|
78
|
+
// Without aliases the raw name and registered name are the same; this
|
|
79
|
+
// asserts the contract that toolName comes from the event, not from the
|
|
80
|
+
// registration lookup's normalizedToolName field.
|
|
81
|
+
const result = validateRequestedTool({ name: "bash" }, TOOLS);
|
|
82
|
+
expect(result).toEqual({ status: "ok", toolName: "bash" });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("resolves tool name via the `arguments` field naming convention", () => {
|
|
86
|
+
// getToolNameFromValue reads `.name` then falls back to other fields;
|
|
87
|
+
// a plain `{ name: "edit" }` event is sufficient here.
|
|
88
|
+
const result = validateRequestedTool({ name: "edit" }, TOOLS);
|
|
89
|
+
expect(result).toEqual({ status: "ok", toolName: "edit" });
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|