Elffuss BraidHub

kikocis/continue public

7dd3a742e7cd · 20.9 KiBRawPermalink
1import {
2 checkToolPermission,
3 filterExcludedTools,
4 matchesArguments,
5 matchesToolPattern,
6} from "./permissionChecker.js";
7import { ToolPermissions } from "./types.js";
8
9describe("Permission Checker", () => {
10 describe("matchesToolPattern", () => {
11 it("should match exact tool names", () => {
12 expect(matchesToolPattern("readFile", "readFile")).toBe(true);
13 expect(matchesToolPattern("writeFile", "readFile")).toBe(false);
14 });
15
16 it("should match universal wildcard", () => {
17 expect(matchesToolPattern("anyTool", "*")).toBe(true);
18 expect(matchesToolPattern("readFile", "*")).toBe(true);
19 expect(matchesToolPattern("", "*")).toBe(true);
20 });
21
22 it("should match prefix wildcards", () => {
23 expect(matchesToolPattern("mcp__ide__getDiagnostics", "mcp__*")).toBe(
24 true,
25 );
26 expect(matchesToolPattern("mcp__filesystem__read", "mcp__*")).toBe(true);
27 expect(matchesToolPattern("builtin__readFile", "mcp__*")).toBe(false);
28 });
29
30 it("should match suffix wildcards", () => {
31 expect(matchesToolPattern("readFile", "*File")).toBe(true);
32 expect(matchesToolPattern("writeFile", "*File")).toBe(true);
33 expect(matchesToolPattern("readData", "*File")).toBe(false);
34 });
35
36 it("should match complex wildcard patterns", () => {
37 expect(matchesToolPattern("test_function_123", "test_*_123")).toBe(true);
38 expect(matchesToolPattern("test_abc_123", "test_*_123")).toBe(true);
39 expect(matchesToolPattern("test_function_456", "test_*_123")).toBe(false);
40 });
41
42 it("should handle empty strings", () => {
43 expect(matchesToolPattern("", "")).toBe(true);
44 expect(matchesToolPattern("tool", "")).toBe(false);
45 expect(matchesToolPattern("", "tool")).toBe(false);
46 });
47
48 it("should handle special regex characters in patterns", () => {
49 expect(matchesToolPattern("test.file", "test.file")).toBe(true);
50 expect(matchesToolPattern("test_file", "test.file")).toBe(false);
51 expect(matchesToolPattern("test[file]", "test[file]")).toBe(true);
52 expect(matchesToolPattern("testa", "test[abc]")).toBe(false);
53 expect(matchesToolPattern("test[abc]", "test[abc]")).toBe(true);
54 expect(matchesToolPattern("test(abc)", "test(abc)")).toBe(true);
55 expect(matchesToolPattern("test+plus", "test+plus")).toBe(true);
56 expect(matchesToolPattern("test^caret", "test^caret")).toBe(true);
57 expect(matchesToolPattern("test$dollar", "test$dollar")).toBe(true);
58 expect(matchesToolPattern("test{brace}", "test{brace}")).toBe(true);
59 expect(matchesToolPattern("test|pipe", "test|pipe")).toBe(true);
60 expect(matchesToolPattern("test\\backslash", "test\\backslash")).toBe(
61 true,
62 );
63 });
64
65 it("should handle wildcard patterns with special regex characters", () => {
66 expect(matchesToolPattern("test[abc].txt", "test[abc].*")).toBe(true);
67 expect(matchesToolPattern("test[abc]_file", "test[abc].*")).toBe(false);
68 expect(matchesToolPattern("mcp__tool[1]", "mcp__*")).toBe(true);
69 expect(matchesToolPattern("file.test.txt", "*.test.*")).toBe(true);
70 expect(matchesToolPattern("(tool)_name", "(tool)*")).toBe(true);
71 expect(matchesToolPattern("tool+plus_extra", "tool+plus*")).toBe(true);
72 });
73
74 describe("Bash command patterns", () => {
75 it("should match Bash command patterns with Bash tool", () => {
76 expect(matchesToolPattern("Bash", "Bash(ls*)", { command: "ls" })).toBe(
77 true,
78 );
79 expect(
80 matchesToolPattern("Bash", "Bash(ls*)", { command: "ls -la" }),
81 ).toBe(true);
82 expect(
83 matchesToolPattern("Bash", "Bash(ls*)", { command: "pwd" }),
84 ).toBe(false);
85 });
86
87 it("should match Bash command patterns with Bash display name", () => {
88 expect(
89 matchesToolPattern("Bash", "Bash(git*)", { command: "git status" }),
90 ).toBe(true);
91 expect(
92 matchesToolPattern("Bash", "Bash(git*)", { command: "git commit" }),
93 ).toBe(true);
94 expect(
95 matchesToolPattern("Bash", "Bash(git*)", { command: "npm install" }),
96 ).toBe(false);
97 });
98
99 it("should match exact Bash commands", () => {
100 expect(matchesToolPattern("Bash", "Bash(ls)", { command: "ls" })).toBe(
101 true,
102 );
103 expect(
104 matchesToolPattern("Bash", "Bash(ls)", { command: "ls -la" }),
105 ).toBe(false);
106 });
107
108 it("should not match Bash patterns for non-bash tools", () => {
109 expect(matchesToolPattern("Read", "Bash(ls*)", { command: "ls" })).toBe(
110 false,
111 );
112 expect(
113 matchesToolPattern("Write", "Bash(git*)", { command: "git status" }),
114 ).toBe(false);
115 });
116
117 it("should not match Bash patterns without command argument", () => {
118 expect(matchesToolPattern("Bash", "Bash(ls*)", {})).toBe(false);
119 expect(
120 matchesToolPattern("Bash", "Bash(ls*)", { other: "value" }),
121 ).toBe(false);
122 });
123
124 it("should handle complex Bash command patterns", () => {
125 expect(
126 matchesToolPattern("Bash", "Bash(npm*)", { command: "npm install" }),
127 ).toBe(true);
128 expect(
129 matchesToolPattern("Bash", "Bash(npm*)", {
130 command: "npm run build",
131 }),
132 ).toBe(true);
133 expect(
134 matchesToolPattern("Bash", "Bash(git*commit*)", {
135 command: "git commit -m 'test'",
136 }),
137 ).toBe(true);
138 });
139 });
140 });
141
142 describe("matchesArguments", () => {
143 it("should return true when no patterns provided", () => {
144 expect(matchesArguments({ path: "/test.txt" })).toBe(true);
145 expect(matchesArguments({})).toBe(true);
146 expect(matchesArguments({ any: "value" }, undefined)).toBe(true);
147 });
148
149 it("should match exact argument values", () => {
150 const args = { path: "/test.txt", mode: "read" };
151 const patterns = { path: "/test.txt" };
152 expect(matchesArguments(args, patterns)).toBe(true);
153
154 const nonMatchingPatterns = { path: "/other.txt" };
155 expect(matchesArguments(args, nonMatchingPatterns)).toBe(false);
156 });
157
158 it("should match wildcard argument patterns", () => {
159 const args = { path: "/any/file.txt", mode: "write" };
160 const patterns = { path: "*", mode: "write" };
161 expect(matchesArguments(args, patterns)).toBe(true);
162
163 const wildcardOnlyPatterns = { path: "*" };
164 expect(matchesArguments(args, wildcardOnlyPatterns)).toBe(true);
165 });
166
167 it("should require all patterns to match", () => {
168 const args = { path: "/test.txt", mode: "read", format: "json" };
169 const allMatchingPatterns = { path: "/test.txt", mode: "read" };
170 expect(matchesArguments(args, allMatchingPatterns)).toBe(true);
171
172 const partialMatchingPatterns = { path: "/test.txt", mode: "write" };
173 expect(matchesArguments(args, partialMatchingPatterns)).toBe(false);
174 });
175
176 it("should handle missing arguments", () => {
177 const args = { path: "/test.txt" };
178 const patternsRequiringMissing = { path: "/test.txt", mode: "read" };
179 expect(matchesArguments(args, patternsRequiringMissing)).toBe(false);
180 });
181
182 it("should handle different data types", () => {
183 const args = { count: 42, enabled: true, data: null, list: [1, 2, 3] };
184 const patterns = { count: 42, enabled: true };
185 expect(matchesArguments(args, patterns)).toBe(true);
186
187 const mismatchedPatterns = { count: "42" }; // string vs number
188 expect(matchesArguments(args, mismatchedPatterns)).toBe(false);
189 });
190
191 it("should handle complex object arguments with reference equality", () => {
192 const configObj = { host: "localhost", port: 3000 };
193 const optionsArray = ["verbose", "debug"];
194
195 const args = {
196 config: configObj,
197 options: optionsArray,
198 };
199 const patterns = {
200 config: configObj, // Same reference
201 options: optionsArray, // Same reference
202 };
203 expect(matchesArguments(args, patterns)).toBe(true);
204
205 const differentObjectPatterns = {
206 config: { host: "localhost", port: 8080 }, // Different object
207 };
208 expect(matchesArguments(args, differentObjectPatterns)).toBe(false);
209
210 const differentButEqualObjectPatterns = {
211 config: { host: "localhost", port: 3000 }, // Different reference, same content
212 };
213 expect(matchesArguments(args, differentButEqualObjectPatterns)).toBe(
214 false,
215 );
216 });
217
218 describe("glob pattern matching", () => {
219 it("should match glob patterns with asterisk wildcards", () => {
220 const args = {
221 command: "cd ../projects && ls",
222 file_path: "/src/file.ts",
223 };
224
225 // Test command patterns
226 expect(matchesArguments(args, { command: "cd*" })).toBe(true);
227 expect(matchesArguments(args, { command: "*ls" })).toBe(true);
228 expect(matchesArguments(args, { command: "*&&*" })).toBe(true);
229 expect(matchesArguments(args, { command: "pwd*" })).toBe(false);
230
231 // Test file path patterns
232 expect(matchesArguments(args, { file_path: "*.ts" })).toBe(true);
233 expect(matchesArguments(args, { file_path: "/src/*" })).toBe(true);
234 expect(matchesArguments(args, { file_path: "**/file.ts" })).toBe(true);
235 expect(matchesArguments(args, { file_path: "*.js" })).toBe(false);
236 });
237
238 it("should match glob patterns with question mark wildcards", () => {
239 const args = { command: "git add", file: "test1.js" };
240
241 expect(matchesArguments(args, { command: "git a?d" })).toBe(true);
242 expect(matchesArguments(args, { command: "git a??d" })).toBe(false);
243 expect(matchesArguments(args, { file: "test?.js" })).toBe(true);
244 expect(matchesArguments(args, { file: "test??.js" })).toBe(false);
245 });
246
247 it("should match complex glob patterns", () => {
248 const args = {
249 command: "npm run test:unit",
250 path: "/home/user/projects/myapp/src/utils/helper.ts",
251 };
252
253 expect(matchesArguments(args, { command: "npm * test:*" })).toBe(true);
254 expect(matchesArguments(args, { command: "npm run *:unit" })).toBe(
255 true,
256 );
257 expect(matchesArguments(args, { path: "**/src/**/*.ts" })).toBe(true);
258 expect(
259 matchesArguments(args, { path: "/home/*/projects/*/src/**" }),
260 ).toBe(true);
261 expect(matchesArguments(args, { command: "yarn *" })).toBe(false);
262 });
263
264 it("should escape special regex characters in glob patterns", () => {
265 const args = {
266 command: "echo 'test.pattern'",
267 regex: "test[0-9]+",
268 special: "file(1).txt",
269 };
270
271 // Literal dots, brackets, parentheses should match exactly
272 expect(matchesArguments(args, { command: "*'test.pattern'*" })).toBe(
273 true,
274 );
275 expect(matchesArguments(args, { regex: "test[0-9]+" })).toBe(true);
276 expect(matchesArguments(args, { special: "file(1).txt" })).toBe(true);
277 expect(matchesArguments(args, { special: "file(*).txt" })).toBe(true);
278
279 // These should not match due to regex escaping
280 expect(matchesArguments(args, { command: "*test_pattern*" })).toBe(
281 false,
282 );
283 expect(matchesArguments(args, { regex: "test*" })).toBe(true); // * is wildcard, + is literal
284 });
285
286 it("should handle mixed exact and glob patterns", () => {
287 const args = {
288 tool: "bash",
289 command: "cd projects && npm install",
290 flag: "--verbose",
291 };
292
293 // Mix of exact and glob matching
294 expect(
295 matchesArguments(args, {
296 tool: "bash", // exact match
297 command: "cd*install", // glob match
298 flag: "--verbose", // exact match
299 }),
300 ).toBe(true);
301
302 expect(
303 matchesArguments(args, {
304 tool: "bash", // exact match
305 command: "cd*test", // glob no match
306 flag: "--verbose", // exact match
307 }),
308 ).toBe(false);
309 });
310
311 it("should convert non-string values to strings for pattern matching", () => {
312 const args = {
313 port: 3000,
314 enabled: true,
315 count: null,
316 items: [1, 2, 3],
317 };
318
319 expect(matchesArguments(args, { port: "30*" })).toBe(true);
320 expect(matchesArguments(args, { enabled: "tr*" })).toBe(true);
321 expect(matchesArguments(args, { count: "*" })).toBe(true); // null -> ""
322 expect(matchesArguments(args, { items: "*,*,*" })).toBe(true); // array toString
323
324 expect(matchesArguments(args, { port: "40*" })).toBe(false);
325 expect(matchesArguments(args, { enabled: "fa*" })).toBe(false);
326 });
327
328 it("should handle empty and undefined argument values", () => {
329 const args = {
330 empty: "",
331 undef: undefined,
332 zero: 0,
333 false_val: false,
334 };
335
336 expect(matchesArguments(args, { empty: "*" })).toBe(true);
337 expect(matchesArguments(args, { undef: "*" })).toBe(true); // undefined -> ""
338 expect(matchesArguments(args, { zero: "*" })).toBe(true); // 0 -> "0"
339 expect(matchesArguments(args, { false_val: "*" })).toBe(true); // false -> "false"
340
341 expect(matchesArguments(args, { empty: "nonempty*" })).toBe(false);
342 expect(matchesArguments(args, { zero: "1*" })).toBe(false);
343 });
344 });
345 });
346
347 describe("checkToolPermission", () => {
348 it("should allow tools with allow permission", () => {
349 const permissions: ToolPermissions = {
350 policies: [{ tool: "readFile", permission: "allow" }],
351 };
352
353 const result = checkToolPermission(
354 { name: "readFile", arguments: { path: "/test.txt" } },
355 permissions,
356 );
357
358 expect(result.permission).toBe("allow");
359 expect(result.matchedPolicy?.tool).toBe("readFile");
360 });
361
362 it("should ask for tools with ask permission", () => {
363 const permissions: ToolPermissions = {
364 policies: [{ tool: "writeFile", permission: "ask" }],
365 };
366
367 const result = checkToolPermission(
368 {
369 name: "writeFile",
370 arguments: { path: "/test.txt", content: "hello" },
371 },
372 permissions,
373 );
374
375 expect(result.permission).toBe("ask");
376 expect(result.matchedPolicy?.tool).toBe("writeFile");
377 });
378
379 it("should exclude tools with exclude permission", () => {
380 const permissions: ToolPermissions = {
381 policies: [{ tool: "runTerminalCommand", permission: "exclude" }],
382 };
383
384 const result = checkToolPermission(
385 { name: "runTerminalCommand", arguments: { command: "rm -rf /" } },
386 permissions,
387 );
388
389 expect(result.permission).toBe("exclude");
390 expect(result.matchedPolicy?.tool).toBe("runTerminalCommand");
391 });
392
393 it("should match wildcard patterns", () => {
394 const permissions: ToolPermissions = {
395 policies: [
396 { tool: "mcp__*", permission: "ask" },
397 { tool: "*", permission: "allow" },
398 ],
399 };
400
401 const mcpResult = checkToolPermission(
402 { name: "mcp__ide__getDiagnostics", arguments: {} },
403 permissions,
404 );
405 expect(mcpResult.permission).toBe("ask");
406 expect(mcpResult.matchedPolicy?.tool).toBe("mcp__*");
407
408 const builtinResult = checkToolPermission(
409 { name: "readFile", arguments: { path: "/test.txt" } },
410 permissions,
411 );
412 expect(builtinResult.permission).toBe("allow");
413 expect(builtinResult.matchedPolicy?.tool).toBe("*");
414 });
415
416 it("should match based on arguments", () => {
417 const permissions: ToolPermissions = {
418 policies: [
419 {
420 tool: "writeFile",
421 permission: "exclude",
422 argumentMatches: { path: "/sensitive.txt" },
423 },
424 { tool: "writeFile", permission: "allow" },
425 ],
426 };
427
428 const sensitiveResult = checkToolPermission(
429 {
430 name: "writeFile",
431 arguments: { path: "/sensitive.txt", content: "data" },
432 },
433 permissions,
434 );
435 expect(sensitiveResult.permission).toBe("exclude");
436
437 const normalResult = checkToolPermission(
438 {
439 name: "writeFile",
440 arguments: { path: "/normal.txt", content: "data" },
441 },
442 permissions,
443 );
444 expect(normalResult.permission).toBe("allow");
445 });
446
447 it("should use default policies when no custom permissions provided", () => {
448 const result = checkToolPermission({
449 name: "Read",
450 arguments: { path: "/test.txt" },
451 });
452
453 // Should match the default policies (Read should be "allow")
454 expect(result.permission).toBe("allow");
455 });
456
457 it("should fall back to ask when no policy matches", () => {
458 const permissions: ToolPermissions = {
459 policies: [{ tool: "specificTool", permission: "allow" }],
460 };
461
462 const result = checkToolPermission(
463 { name: "unknownTool", arguments: {} },
464 permissions,
465 );
466
467 expect(result.permission).toBe("ask");
468 });
469
470 it("should match Bash command patterns in checkToolPermission", () => {
471 const permissions: ToolPermissions = {
472 policies: [
473 { tool: "Bash(ls*)", permission: "allow" },
474 { tool: "Bash(git*)", permission: "ask" },
475 { tool: "Bash", permission: "ask" }, // Fallback for other commands
476 ],
477 };
478
479 // Should match "Bash(ls*)" pattern and allow
480 const lsResult = checkToolPermission(
481 { name: "Bash", arguments: { command: "ls -la" } },
482 permissions,
483 );
484 expect(lsResult.permission).toBe("allow");
485 expect(lsResult.matchedPolicy?.tool).toBe("Bash(ls*)");
486
487 // Should match "Bash(git*)" pattern and ask
488 const gitResult = checkToolPermission(
489 { name: "Bash", arguments: { command: "git status" } },
490 permissions,
491 );
492 expect(gitResult.permission).toBe("ask");
493 expect(gitResult.matchedPolicy?.tool).toBe("Bash(git*)");
494
495 // Should match general fallback for other commands
496 const npmResult = checkToolPermission(
497 { name: "Bash", arguments: { command: "npm install" } },
498 permissions,
499 );
500 expect(npmResult.permission).toBe("ask");
501 expect(npmResult.matchedPolicy?.tool).toBe("Bash");
502 });
503
504 it("should match argument patterns with glob patterns", () => {
505 const permissions: ToolPermissions = {
506 policies: [
507 {
508 tool: "Bash",
509 permission: "exclude",
510 argumentMatches: { command: "rm*" },
511 },
512 {
513 tool: "Write",
514 permission: "ask",
515 argumentMatches: { file_path: "**/*.ts" },
516 },
517 {
518 tool: "Write",
519 permission: "allow",
520 },
521 ],
522 };
523
524 // Should exclude dangerous rm commands
525 const rmResult = checkToolPermission(
526 { name: "Bash", arguments: { command: "rm -rf /" } },
527 permissions,
528 );
529 expect(rmResult.permission).toBe("exclude");
530 expect(rmResult.matchedPolicy?.argumentMatches?.command).toBe("rm*");
531
532 // Should ask for TypeScript files
533 const tsResult = checkToolPermission(
534 {
535 name: "Write",
536 arguments: { file_path: "/src/components/Button.ts" },
537 },
538 permissions,
539 );
540 expect(tsResult.permission).toBe("ask");
541 expect(tsResult.matchedPolicy?.argumentMatches?.file_path).toBe(
542 "**/*.ts",
543 );
544
545 // Should allow other file writes
546 const jsResult = checkToolPermission(
547 { name: "Write", arguments: { file_path: "/src/utils/helper.js" } },
548 permissions,
549 );
550 expect(jsResult.permission).toBe("allow");
551 expect(jsResult.matchedPolicy?.argumentMatches).toBeUndefined();
552 });
553 });
554
555 describe("filterExcludedTools", () => {
556 it("should filter out excluded tools", () => {
557 const permissions: ToolPermissions = {
558 policies: [
559 { tool: "runTerminalCommand", permission: "exclude" },
560 { tool: "writeFile", permission: "ask" },
561 { tool: "*", permission: "allow" },
562 ],
563 };
564
565 const tools = [
566 "readFile",
567 "writeFile",
568 "runTerminalCommand",
569 "searchCode",
570 ];
571 const filtered = filterExcludedTools(tools, permissions);
572
573 expect(filtered).toEqual(["readFile", "writeFile", "searchCode"]);
574 expect(filtered).not.toContain("runTerminalCommand");
575 });
576
577 it("should return all tools when none are excluded", () => {
578 const permissions: ToolPermissions = {
579 policies: [{ tool: "*", permission: "allow" }],
580 };
581
582 const tools = ["readFile", "writeFile", "runTerminalCommand"];
583 const filtered = filterExcludedTools(tools, permissions);
584
585 expect(filtered).toEqual(tools);
586 });
587
588 it("should use default policies when no permissions provided", () => {
589 const tools = ["readFile", "writeFile", "runTerminalCommand"];
590 const filtered = filterExcludedTools(tools);
591
592 // All tools should be included (none excluded by default)
593 expect(filtered).toEqual(tools);
594 });
595 });
596});