| 1 | import fs from "fs"; |
| 2 | import path from "path"; |
| 3 | |
| 4 | import YAML from "yaml"; |
| 5 | |
| 6 | import { env } from "../env.js"; |
| 7 | import { logger } from "../util/logger.js"; |
| 8 | |
| 9 | import { PermissionPolicy, ToolPermissionPolicy } from "./types.js"; |
| 10 | |
| 11 | export const PERMISSIONS_YAML_PATH = path.resolve( |
| 12 | path.join(env.continueHome, "permissions.yaml"), |
| 13 | ); |
| 14 | |
| 15 | export interface PermissionsYamlConfig { |
| 16 | allow?: string[]; |
| 17 | ask?: string[]; |
| 18 | exclude?: string[]; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Loads permissions from ~/.continue/permissions.yaml |
| 23 | * Returns null if file doesn't exist or can't be parsed |
| 24 | */ |
| 25 | export function loadPermissionsYaml(): PermissionsYamlConfig | null { |
| 26 | try { |
| 27 | if (!fs.existsSync(PERMISSIONS_YAML_PATH)) { |
| 28 | logger.debug("Permissions YAML file does not exist", { |
| 29 | path: PERMISSIONS_YAML_PATH, |
| 30 | }); |
| 31 | return null; |
| 32 | } |
| 33 | |
| 34 | const content = fs.readFileSync(PERMISSIONS_YAML_PATH, "utf-8"); |
| 35 | const parsed = YAML.parse(content) as PermissionsYamlConfig; |
| 36 | |
| 37 | // Validate the structure |
| 38 | if (parsed && typeof parsed === "object") { |
| 39 | const validKeys = ["allow", "ask", "exclude"]; |
| 40 | const hasValidStructure = Object.keys(parsed).every( |
| 41 | (key) => |
| 42 | validKeys.includes(key) && |
| 43 | (!parsed[key as keyof PermissionsYamlConfig] || |
| 44 | Array.isArray(parsed[key as keyof PermissionsYamlConfig])), |
| 45 | ); |
| 46 | |
| 47 | if (hasValidStructure) { |
| 48 | logger.debug("Loaded permissions from YAML", { |
| 49 | allow: parsed.allow?.length || 0, |
| 50 | ask: parsed.ask?.length || 0, |
| 51 | exclude: parsed.exclude?.length || 0, |
| 52 | }); |
| 53 | return parsed; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | logger.warn("Invalid permissions YAML structure", { |
| 58 | path: PERMISSIONS_YAML_PATH, |
| 59 | }); |
| 60 | return null; |
| 61 | } catch (error) { |
| 62 | logger.error("Failed to load permissions YAML", { |
| 63 | error, |
| 64 | path: PERMISSIONS_YAML_PATH, |
| 65 | }); |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Parses a pattern string into a ToolPermissionPolicy |
| 72 | * Supports formats like: |
| 73 | * - "Write" -> { tool: "Write", permission } |
| 74 | * - "Write(pattern)" -> { tool: "Write", permission, argumentMatches: { file_path: "pattern" } } |
| 75 | * - "Bash(npm install)" -> { tool: "Bash", permission, argumentMatches: { command: "npm install" } } |
| 76 | */ |
| 77 | export function parseToolPattern( |
| 78 | pattern: string, |
| 79 | permission: PermissionPolicy, |
| 80 | ): ToolPermissionPolicy { |
| 81 | const match = pattern.match(/^([^(]+)(?:\(([^)]*)\))?$/); |
| 82 | if (!match) { |
| 83 | throw new Error(`Invalid tool pattern: ${pattern}`); |
| 84 | } |
| 85 | |
| 86 | const [, toolName, args] = match; |
| 87 | const normalizedName = toolName.trim(); |
| 88 | |
| 89 | const policy: ToolPermissionPolicy = { |
| 90 | tool: normalizedName, |
| 91 | permission, |
| 92 | }; |
| 93 | |
| 94 | if (args) { |
| 95 | const trimmedArgs = args.trim(); |
| 96 | if (trimmedArgs) { |
| 97 | // Map tool names to their primary argument parameter |
| 98 | const toolArgMappings: Record<string, string> = { |
| 99 | Write: "file_path", |
| 100 | Edit: "file_path", |
| 101 | Read: "file_path", |
| 102 | List: "path", |
| 103 | Search: "query", |
| 104 | Bash: "command", |
| 105 | Fetch: "url", |
| 106 | Diff: "file_path", |
| 107 | }; |
| 108 | |
| 109 | const argKey = toolArgMappings[normalizedName] || "pattern"; |
| 110 | policy.argumentMatches = { |
| 111 | [argKey]: trimmedArgs, |
| 112 | }; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return policy; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Converts permissions YAML config to ToolPermissionPolicy array |
| 121 | */ |
| 122 | export function yamlConfigToPolicies( |
| 123 | config: PermissionsYamlConfig, |
| 124 | ): ToolPermissionPolicy[] { |
| 125 | const policies: ToolPermissionPolicy[] = []; |
| 126 | |
| 127 | // Order matters: more restrictive policies first |
| 128 | if (config.exclude) { |
| 129 | for (const pattern of config.exclude) { |
| 130 | policies.push(parseToolPattern(pattern, "exclude")); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (config.ask) { |
| 135 | for (const pattern of config.ask) { |
| 136 | policies.push(parseToolPattern(pattern, "ask")); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (config.allow) { |
| 141 | for (const pattern of config.allow) { |
| 142 | policies.push(parseToolPattern(pattern, "allow")); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return policies; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Creates the permissions.yaml file with default content if it doesn't exist |
| 151 | */ |
| 152 | export async function ensurePermissionsYamlExists(): Promise<void> { |
| 153 | const dir = path.dirname(PERMISSIONS_YAML_PATH); |
| 154 | |
| 155 | // Ensure directory exists |
| 156 | if (!fs.existsSync(dir)) { |
| 157 | await fs.promises.mkdir(dir, { recursive: true }); |
| 158 | } |
| 159 | |
| 160 | // Create file if it doesn't exist |
| 161 | if (!fs.existsSync(PERMISSIONS_YAML_PATH)) { |
| 162 | const defaultContent = `# cn tool permissions |
| 163 | |
| 164 | # Tools that are automatically allowed without prompting |
| 165 | allow: [] |
| 166 | |
| 167 | # Tools that require user confirmation before execution |
| 168 | ask: [] |
| 169 | |
| 170 | # Tools that are completely excluded (model won't know they exist) |
| 171 | exclude: [] |
| 172 | `; |
| 173 | |
| 174 | await fs.promises.writeFile(PERMISSIONS_YAML_PATH, defaultContent, "utf-8"); |
| 175 | logger.info("Created default permissions.yaml", { |
| 176 | path: PERMISSIONS_YAML_PATH, |
| 177 | }); |
| 178 | } |
| 179 | } |