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    "npx",
125    // Go ecosystem
126    "go",
127    "go build",
128    "go test",
129    // C/C++
130    "gcc",
131    "g++",
132    "clang",
133    "clang++",
134    // Java ecosystem
135    "javac",
136    "java",
137    "mvn",
138    "gradle",
139    // Container operations
140    "docker",
141    "docker-compose",
142];
143
144impl Default for CommandsConfig {
145    fn default() -> Self {
146        Self {
147            allow_list: default_allow_list(),
148            approval_prefixes: Vec::new(),
149            extra_path_entries: default_extra_path_entries(),
150            deny_list: default_deny_list(),
151            allow_glob: default_allow_glob(),
152            deny_glob: default_deny_glob(),
153            allow_regex: default_allow_regex(),
154            deny_regex: default_deny_regex(),
155        }
156    }
157}
158
159/// Built-in `allow_list` applied when the key is absent from a `[commands]` section.
160fn default_allow_list() -> Vec<String> {
161    DEFAULT_ALLOW_LIST.iter().map(|s| (*s).into()).collect()
162}
163
164/// Built-in `deny_list` applied when the key is absent from a `[commands]` section.
165fn default_deny_list() -> Vec<String> {
166    vec![
167        // Dangerous file deletion
168        "rm".into(),
169        "rm -rf /".into(),
170        "rm -rf ~".into(),
171        "rm -rf /*".into(),
172        "rm -rf /home".into(),
173        "rm -rf /usr".into(),
174        "rm -rf /etc".into(),
175        "rm -rf /var".into(),
176        "rm -rf /opt".into(),
177        "rmdir /".into(),
178        "rmdir /home".into(),
179        "rmdir /usr".into(),
180        // System control
181        "shutdown".into(),
182        "reboot".into(),
183        "halt".into(),
184        "poweroff".into(),
185        "init 0".into(),
186        "init 6".into(),
187        "systemctl poweroff".into(),
188        "systemctl reboot".into(),
189        "systemctl halt".into(),
190        // Privilege escalation
191        "sudo rm".into(),
192        "sudo chmod 777".into(),
193        "sudo chown".into(),
194        "sudo passwd".into(),
195        "sudo su".into(),
196        "sudo -i".into(),
197        "sudo bash".into(),
198        "su root".into(),
199        "su -".into(),
200        // Disk operations
201        "format".into(),
202        "fdisk".into(),
203        "mkfs".into(),
204        "mkfs.ext4".into(),
205        "mkfs.xfs".into(),
206        "mkfs.vfat".into(),
207        "dd if=/dev/zero".into(),
208        "dd if=/dev/random".into(),
209        "dd if=/dev/urandom".into(),
210        // Security risks
211        "wget --no-check-certificate".into(),
212        ":(){ :|:& };:".into(), // Fork bomb
213        "nohup bash -i".into(),
214        "exec bash -i".into(),
215        "eval".into(),
216        // Shell configuration
217        "source /etc/bashrc".into(),
218        "source ~/.bashrc".into(),
219        // Permission changes
220        "chmod 777".into(),
221        "chmod -R 777".into(),
222        "chown -R".into(),
223        "chgrp -R".into(),
224        // SSH key destruction
225        "rm ~/.ssh/*".into(),
226        "rm -r ~/.ssh".into(),
227        // Sensitive file access
228        "cat /etc/passwd".into(),
229        "cat /etc/shadow".into(),
230        "cat ~/.ssh/id_*".into(),
231        "tail -f /var/log".into(),
232        "head -n 1 /var/log".into(),
233    ]
234}
235
236/// Built-in `allow_glob` applied when the key is absent from a `[commands]` section.
237fn default_allow_glob() -> Vec<String> {
238    vec![
239        // Version control
240        "git *".into(),
241        // Rust ecosystem
242        "cargo *".into(),
243        "rustc *".into(),
244        // Python ecosystem
245        "python *".into(),
246        "python3 *".into(),
247        "pip *".into(),
248        "pip3 *".into(),
249        "virtualenv *".into(),
250        // Node.js ecosystem
251        "node *".into(),
252        "npm *".into(),
253        "npm run *".into(),
254        "yarn *".into(),
255        "yarn run *".into(),
256        "pnpm *".into(),
257        "pnpm run *".into(),
258        "bun *".into(),
259        "bun run *".into(),
260        "npx *".into(),
261        // Go
262        "go *".into(),
263        // C/C++
264        "gcc *".into(),
265        "g++ *".into(),
266        "clang *".into(),
267        "clang++ *".into(),
268        // Java
269        "javac *".into(),
270        "java *".into(),
271        "mvn *".into(),
272        "gradle *".into(),
273        // Build tools
274        "make *".into(),
275        "cmake *".into(),
276        "ninja *".into(),
277        // Containers
278        "docker *".into(),
279        "docker-compose *".into(),
280        // Archive tools
281        "tar *".into(),
282        "zip *".into(),
283        "unzip *".into(),
284        "gzip *".into(),
285        "gunzip *".into(),
286    ]
287}
288
289/// Built-in `deny_glob` applied when the key is absent from a `[commands]` section.
290fn default_deny_glob() -> Vec<String> {
291    vec![
292        // File deletion
293        "rm *".into(),
294        // Privilege escalation
295        "sudo *".into(),
296        // Permission changes
297        "chmod *".into(),
298        "chown *".into(),
299        // Process termination
300        "kill *".into(),
301        "pkill *".into(),
302        // System services
303        "systemctl *".into(),
304        "service *".into(),
305        // Mount operations
306        "mount *".into(),
307        "umount *".into(),
308        // Dangerous container operations
309        "docker run *".into(),
310        // Kubernetes (admin access)
311        "kubectl *".into(),
312    ]
313}
314
315/// Built-in `allow_regex` applied when the key is absent from a `[commands]` section.
316fn default_allow_regex() -> Vec<String> {
317    vec![
318                // File and text utilities
319                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(),
320                // Version control
321                r"^git (status|diff|log|show|branch|remote)\b".into(),
322                // Rust
323                r"^cargo (check|build|test|run|doc|clippy|fmt|tree|metadata)\b".into(),
324                r"^rustc\b".into(),
325                // Python
326                r"^(python|python3) (-m | )?\w*".into(),
327                r"^(pip|pip3)\b".into(),
328                r"^virtualenv\b".into(),
329                // Node.js
330                r"^(node|npm|yarn|pnpm|bun|npx)\b".into(),
331                // Go
332                r"^go\b".into(),
333                // C/C++
334                r"^(gcc|g\+\+|clang|clang\++)\b".into(),
335                // Java
336                r"^(javac|java)\b".into(),
337                r"^(mvn|gradle)\b".into(),
338                // Build tools
339                r"^(make|cmake)\b".into(),
340                // Containers
341                r"^(docker|docker-compose)\b".into(),
342            ]
343}
344
345/// Built-in `deny_regex` applied when the key is absent from a `[commands]` section.
346fn default_deny_regex() -> Vec<String> {
347    vec![
348        // Force removal
349        r"rm\s+(-rf|--force)".into(),
350        // Sudo commands
351        r"sudo\s+.*".into(),
352        // Permission changes
353        r"chmod\s+.*".into(),
354        r"chown\s+.*".into(),
355        // Privileged containers
356        r"docker\s+run\s+.*--privileged".into(),
357        // Dangerous kubectl operations
358        r"kubectl\s+(delete|drain|uncordon)".into(),
359    ]
360}
361
362fn default_extra_path_entries() -> Vec<String> {
363    command_constants::DEFAULT_EXTRA_PATH_ENTRIES
364        .iter()
365        .map(|value| (*value).into())
366        .collect()
367}