Elffuss BraidHub

kikocis/continue public

1d6a8dcaaf43 · 4.9 KiBRawPermalink
1import { getServiceSync } from "../services/index.js";
2import type { ToolPermissionServiceState } from "../services/types.js";
3import { SERVICE_NAMES } from "../services/types.js";
4
5import { DEFAULT_TOOL_POLICIES } from "./defaultPolicies.js";
6import {
7 PermissionCheckResult,
8 ToolCallRequest,
9 ToolPermissions,
10} from "./types.js";
11
12/**
13 * Checks if a tool name matches a pattern.
14 * Supports wildcards (*) for pattern matching.
15 * Also handles special Bash command patterns like "Bash(ls*)"
16 */
17export function matchesToolPattern(
18 toolName: string,
19 pattern: string,
20 toolArguments?: Record<string, any>,
21): boolean {
22 if (pattern === "*") return true;
23 if (pattern === toolName) return true;
24
25 // Handle special Bash command patterns like "Bash(ls*)"
26 const bashCommandMatch = pattern.match(/^Bash\((.+)\)$/);
27 if (bashCommandMatch) {
28 // Check if this is a bash/terminal tool
29 const isBashTool = toolName === "Bash";
30 if (isBashTool && toolArguments?.command) {
31 const commandPattern = bashCommandMatch[1];
32 const command = toolArguments.command;
33
34 // Handle command patterns with wildcards
35 if (commandPattern.includes("*") || commandPattern.includes("?")) {
36 // Escape all regex metacharacters except * and ?
37 const escaped = commandPattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
38 // Convert * and ? to their regex equivalents
39 const regexPattern = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
40 const regex = new RegExp(`^${regexPattern}$`);
41 return regex.test(command);
42 }
43
44 // Exact command match
45 return command === commandPattern;
46 }
47 return false;
48 }
49
50 // Handle regular wildcard patterns like "mcp__*"
51 if (pattern.includes("*") || pattern.includes("?")) {
52 // Escape all regex metacharacters except * and ?
53 const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
54 // Convert * and ? to their regex equivalents
55 const regexPattern = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
56 const regex = new RegExp(`^${regexPattern}$`);
57 return regex.test(toolName);
58 }
59
60 return false;
61}
62
63/**
64 * Checks if tool call arguments match the specified patterns.
65 * Supports glob patterns with * and ? wildcards.
66 */
67export function matchesArguments(
68 args: Record<string, any>,
69 patterns?: Record<string, any>,
70): boolean {
71 if (!patterns) return true;
72
73 for (const [key, pattern] of Object.entries(patterns)) {
74 const argValue = args[key];
75
76 if (pattern === "*") continue; // Wildcard matches anything
77
78 // Handle glob patterns with wildcards (only for string patterns)
79 if (
80 typeof pattern === "string" &&
81 (pattern.includes("*") || pattern.includes("?"))
82 ) {
83 // Convert argValue to string for pattern matching
84 const stringValue = String(argValue ?? "");
85
86 // Escape all regex metacharacters except * and ?
87 const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
88 // Convert * and ? to their regex equivalents
89 const regexPattern = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
90 const regex = new RegExp(`^${regexPattern}$`);
91
92 if (!regex.test(stringValue)) {
93 return false;
94 }
95 } else {
96 // Exact match for non-glob patterns (preserve original behavior)
97 if (argValue !== pattern) {
98 return false;
99 }
100 }
101 }
102
103 return true;
104}
105
106/**
107 * Evaluates a tool call request against a set of permission policies.
108 * Returns the permission for the first matching policy.
109 */
110export function checkToolPermission(
111 toolCall: ToolCallRequest,
112 permissions?: ToolPermissions,
113): PermissionCheckResult {
114 // Get permissions from service if not provided
115 let policies = permissions?.policies;
116
117 if (!policies) {
118 const serviceResult = getServiceSync<ToolPermissionServiceState>(
119 SERVICE_NAMES.TOOL_PERMISSIONS,
120 );
121
122 // Only use defaults if service is not ready, not registered, or has no value
123 // This allows critical runtime errors to propagate properly
124 if (serviceResult.state !== "ready" || !serviceResult.value) {
125 policies = DEFAULT_TOOL_POLICIES;
126 } else {
127 policies =
128 serviceResult.value.permissions.policies || DEFAULT_TOOL_POLICIES;
129 }
130 }
131
132 for (const policy of policies) {
133 if (
134 matchesToolPattern(toolCall.name, policy.tool, toolCall.arguments) &&
135 matchesArguments(toolCall.arguments, policy.argumentMatches)
136 ) {
137 return {
138 permission: policy.permission,
139 matchedPolicy: policy,
140 };
141 }
142 }
143
144 // Fallback to "ask" if no policy matches
145 return {
146 permission: "ask",
147 };
148}
149
150/**
151 * Filters out tools that have "exclude" permission from a list of tool names.
152 */
153export function filterExcludedTools(
154 toolNames: string[],
155 permissions?: ToolPermissions,
156): string[] {
157 return toolNames.filter((toolName) => {
158 const result = checkToolPermission(
159 { name: toolName, arguments: {} },
160 permissions,
161 );
162 return result.permission !== "exclude";
163 });
164}