Skip to main content

vtcode_config/core/
commands.rs

1use serde::{Deserialize, Serialize};
2
3use crate::constants::commands as command_constants;
4
5/// Command execution configuration
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct CommandsConfig {
9    /// Commands that can be executed without prompting
10    #[serde(default = "default_allow_list")]
11    pub allow_list: Vec<String>,
12
13    /// Command prefixes that skip future shell approval prompts when matched
14    #[serde(default)]
15    pub approval_prefixes: Vec<String>,
16
17    /// Additional directories that should be searched/prepended to PATH for command execution
18    #[serde(default = "default_extra_path_entries")]
19    pub extra_path_entries: Vec<String>,
20
21    /// Commands that are always denied
22    #[serde(default = "default_deny_list")]
23    pub deny_list: Vec<String>,
24
25    /// Glob patterns allowed for shell commands (applies to Bash)
26    #[serde(default = "default_allow_glob")]
27    pub allow_glob: Vec<String>,
28
29    /// Glob patterns denied for shell commands
30    #[serde(default = "default_deny_glob")]
31    pub deny_glob: Vec<String>,
32
33    /// Regex allow patterns for shell commands
34    #[serde(default = "default_allow_regex")]
35    pub allow_regex: Vec<String>,
36
37    /// Regex deny patterns for shell commands
38    #[serde(default = "default_deny_regex")]
39    pub deny_regex: Vec<String>,
40}
41
42const DEFAULT_ALLOW_LIST: &[&str] = &[
43    // File and directory operations
44    "ls",
45    "pwd",
46    "cat",
47    "grep",
48    "find",
49    "head",
50    "tail",
51    "wc",
52    "tree",
53    "stat",
54    "file",
55    "sort",
56    "uniq",
57    "cut",
58    "awk",
59    "sed",
60    // Archive operations
61    "tar",
62    "zip",
63    "unzip",
64    "gzip",
65    "gunzip",
66    // Build tools
67    "make",
68    "cmake",
69    "ninja",
70    "which",
71    "echo",
72    "printf",
73    "read",
74    "date",
75    "sleep",
76    // Version control
77    "git status",
78    "git diff",
79    "git log",
80    "git show",
81    "git branch",
82    "git remote",
83    // Rust ecosystem
84    "cargo check",
85    "cargo build",
86    "cargo build --release",
87    "cargo build --profile release",
88    "cargo test",
89    "cargo run",
90    "cargo clippy",
91    "cargo fmt",
92    "cargo tree",
93    "cargo metadata",
94    "cargo doc",
95    "rustc",
96    // Python ecosystem
97    "python3",
98    "python3 -m pip install",
99    "python3 -m pytest",
100    "python3 -m build",
101    "python",
102    "pip3",
103    "pip",
104    "uv",
105    "uv run",
106    "uvx",
107    "virtualenv",
108    // Node.js ecosystem
109    "node",
110    "npm",
111    "npm run build",
112    "npm run test",
113    "npm install",
114    "yarn",
115    "yarn build",
116    "yarn test",
117    "pnpm",
118    "pnpm build",
119    "pnpm test",
120    "bun",
121    "bun install",
122    "bun run",
123    "bun test",
124    "bunx",
125    "npx",
126    // Go ecosystem
127    "go",
128    "go build",
129    "go test",
130    // C/C++
131    "gcc",
132    "g++",
133    "clang",
134    "clang++",
135    // Java ecosystem
136    "javac",
137    "java",
138    "mvn",
139    "gradle",
140    // Container operations
141    "docker",
142    "docker-compose",
143];
144
145impl Default for CommandsConfig {
146    fn default() -> Self {
147        Self {
148            allow_list: default_allow_list(),
149            approval_prefixes: Vec::new(),
150            extra_path_entries: default_extra_path_entries(),
151            deny_list: default_deny_list(),
152            allow_glob: default_allow_glob(),
153            deny_glob: default_deny_glob(),
154            allow_regex: default_allow_regex(),
155            deny_regex: default_deny_regex(),
156        }
157    }
158}
159
160/// Built-in `allow_list` applied when the key is absent from a `[commands]` section.
161fn default_allow_list() -> Vec<String> {
162    DEFAULT_ALLOW_LIST.iter().map(|s| (*s).into()).collect()
163}
164
165/// Built-in `deny_list` applied when the key is absent from a `[commands]` section.
166fn default_deny_list() -> Vec<String> {
167    vec![
168        // Dangerous file deletion
169        "rm".into(),
170        "rm -rf /".into(),
171        "rm -rf ~".into(),
172        "rm -rf /*".into(),
173        "rm -rf /home".into(),
174        "rm -rf /usr".into(),
175        "rm -rf /etc".into(),
176        "rm -rf /var".into(),
177        "rm -rf /opt".into(),
178        "rmdir /".into(),
179        "rmdir /home".into(),
180        "rmdir /usr".into(),
181        // System control
182        "shutdown".into(),
183        "reboot".into(),
184        "halt".into(),
185        "poweroff".into(),
186        "init 0".into(),
187        "init 6".into(),
188        "systemctl poweroff".into(),
189        "systemctl reboot".into(),
190        "systemctl halt".into(),
191        // Privilege escalation
192        "sudo rm".into(),
193        "sudo chmod 777".into(),
194        "sudo chown".into(),
195        "sudo passwd".into(),
196        "sudo su".into(),
197        "sudo -i".into(),
198        "sudo bash".into(),
199        "su root".into(),
200        "su -".into(),
201        // Disk operations
202        "format".into(),
203        "fdisk".into(),
204        "mkfs".into(),
205        "mkfs.ext4".into(),
206        "mkfs.xfs".into(),
207        "mkfs.vfat".into(),
208        "dd if=/dev/zero".into(),
209        "dd if=/dev/random".into(),
210        "dd if=/dev/urandom".into(),
211        // Security risks
212        "wget --no-check-certificate".into(),
213        ":(){ :|:& };:".into(), // Fork bomb
214        "nohup bash -i".into(),
215        "exec bash -i".into(),
216        "eval".into(),
217        // Shell configuration
218        "source /etc/bashrc".into(),
219        "source ~/.bashrc".into(),
220        // Permission changes
221        "chmod 777".into(),
222        "chmod -R 777".into(),
223        "chown -R".into(),
224        "chgrp -R".into(),
225        // SSH key destruction
226        "rm ~/.ssh/*".into(),
227        "rm -r ~/.ssh".into(),
228        // Sensitive file access
229        "cat /etc/passwd".into(),
230        "cat /etc/shadow".into(),
231        "cat ~/.ssh/id_*".into(),
232        "tail -f /var/log".into(),
233        "head -n 1 /var/log".into(),
234    ]
235}
236
237/// Built-in `allow_glob` applied when the key is absent from a `[commands]` section.
238fn default_allow_glob() -> Vec<String> {
239    vec![
240        // Version control
241        "git *".into(),
242        // Rust ecosystem
243        "cargo *".into(),
244        "rustc *".into(),
245        // Python ecosystem
246        "python *".into(),
247        "python3 *".into(),
248        "pip *".into(),
249        "pip3 *".into(),
250        "virtualenv *".into(),
251        // Node.js ecosystem
252        "node *".into(),
253        "npm *".into(),
254        "npm run *".into(),
255        "yarn *".into(),
256        "yarn run *".into(),
257        "pnpm *".into(),
258        "pnpm run *".into(),
259        "bun *".into(),
260        "bun run *".into(),
261        "bunx *".into(),
262        "npx *".into(),
263        // Go
264        "go *".into(),
265        // C/C++
266        "gcc *".into(),
267        "g++ *".into(),
268        "clang *".into(),
269        "clang++ *".into(),
270        // Java
271        "javac *".into(),
272        "java *".into(),
273        "mvn *".into(),
274        "gradle *".into(),
275        // Build tools
276        "make *".into(),
277        "cmake *".into(),
278        "ninja *".into(),
279        // Containers
280        "docker *".into(),
281        "docker-compose *".into(),
282        // Archive tools
283        "tar *".into(),
284        "zip *".into(),
285        "unzip *".into(),
286        "gzip *".into(),
287        "gunzip *".into(),
288    ]
289}
290
291/// Built-in `deny_glob` applied when the key is absent from a `[commands]` section.
292fn default_deny_glob() -> Vec<String> {
293    vec![
294        // File deletion
295        "rm *".into(),
296        // Privilege escalation
297        "sudo *".into(),
298        // Permission changes
299        "chmod *".into(),
300        "chown *".into(),
301        // Process termination
302        "kill *".into(),
303        "pkill *".into(),
304        // System services
305        "systemctl *".into(),
306        "service *".into(),
307        // Mount operations
308        "mount *".into(),
309        "umount *".into(),
310        // Dangerous container operations
311        "docker run *".into(),
312        // Kubernetes (admin access)
313        "kubectl *".into(),
314    ]
315}
316
317/// Built-in `allow_regex` applied when the key is absent from a `[commands]` section.
318fn default_allow_regex() -> Vec<String> {
319    vec![
320                // File and text utilities
321                r"^(ls|pwd|cat|grep|find|head|tail|wc|echo|printf|read|date|sleep|tree|stat|file|sort|uniq|cut|awk|sed|tar|zip|unzip|gzip|gunzip)\b".into(),
322                // Version control
323                r"^git (status|diff|log|show|branch|remote)\b".into(),
324                // Rust
325                r"^cargo (check|build|test|run|doc|clippy|fmt|tree|metadata)\b".into(),
326                r"^rustc\b".into(),
327                // Python
328                r"^(python|python3) (-m | )?\w*".into(),
329                r"^(pip|pip3)\b".into(),
330                r"^virtualenv\b".into(),
331                // Node.js
332                r"^(node|npm|yarn|pnpm|bun|bunx|npx)\b".into(),
333                // Go
334                r"^go\b".into(),
335                // C/C++
336                r"^(gcc|g\+\+|clang|clang\++)\b".into(),
337                // Java
338                r"^(javac|java)\b".into(),
339                r"^(mvn|gradle)\b".into(),
340                // Build tools
341                r"^(make|cmake)\b".into(),
342                // Containers
343                r"^(docker|docker-compose)\b".into(),
344            ]
345}
346
347/// Built-in `deny_regex` applied when the key is absent from a `[commands]` section.
348fn default_deny_regex() -> Vec<String> {
349    vec![
350        // Force removal
351        r"rm\s+(-rf|--force)".into(),
352        // Sudo commands
353        r"sudo\s+.*".into(),
354        // Permission changes
355        r"chmod\s+.*".into(),
356        r"chown\s+.*".into(),
357        // Privileged containers
358        r"docker\s+run\s+.*--privileged".into(),
359        // Dangerous kubectl operations
360        r"kubectl\s+(delete|drain|uncordon)".into(),
361    ]
362}
363
364fn default_extra_path_entries() -> Vec<String> {
365    command_constants::DEFAULT_EXTRA_PATH_ENTRIES
366        .iter()
367        .map(|value| (*value).into())
368        .collect()
369}