1use serde::{Deserialize, Serialize};
2
3use crate::constants::commands as command_constants;
4
5#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Deserialize, Serialize)]
8pub struct CommandsConfig {
9 #[serde(default = "default_allow_list")]
11 pub allow_list: Vec<String>,
12
13 #[serde(default)]
15 pub approval_prefixes: Vec<String>,
16
17 #[serde(default = "default_extra_path_entries")]
19 pub extra_path_entries: Vec<String>,
20
21 #[serde(default = "default_deny_list")]
23 pub deny_list: Vec<String>,
24
25 #[serde(default = "default_allow_glob")]
27 pub allow_glob: Vec<String>,
28
29 #[serde(default = "default_deny_glob")]
31 pub deny_glob: Vec<String>,
32
33 #[serde(default = "default_allow_regex")]
35 pub allow_regex: Vec<String>,
36
37 #[serde(default = "default_deny_regex")]
39 pub deny_regex: Vec<String>,
40}
41
42const DEFAULT_ALLOW_LIST: &[&str] = &[
43 "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 "tar",
62 "zip",
63 "unzip",
64 "gzip",
65 "gunzip",
66 "make",
68 "cmake",
69 "ninja",
70 "which",
71 "echo",
72 "printf",
73 "read",
74 "date",
75 "sleep",
76 "git status",
78 "git diff",
79 "git log",
80 "git show",
81 "git branch",
82 "git remote",
83 "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 "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",
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",
127 "go build",
128 "go test",
129 "gcc",
131 "g++",
132 "clang",
133 "clang++",
134 "javac",
136 "java",
137 "mvn",
138 "gradle",
139 "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
159fn default_allow_list() -> Vec<String> {
161 DEFAULT_ALLOW_LIST.iter().map(|s| (*s).into()).collect()
162}
163
164fn default_deny_list() -> Vec<String> {
166 vec![
167 "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 "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 "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 "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 "wget --no-check-certificate".into(),
212 ":(){ :|:& };:".into(), "nohup bash -i".into(),
214 "exec bash -i".into(),
215 "eval".into(),
216 "source /etc/bashrc".into(),
218 "source ~/.bashrc".into(),
219 "chmod 777".into(),
221 "chmod -R 777".into(),
222 "chown -R".into(),
223 "chgrp -R".into(),
224 "rm ~/.ssh/*".into(),
226 "rm -r ~/.ssh".into(),
227 "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
236fn default_allow_glob() -> Vec<String> {
238 vec![
239 "git *".into(),
241 "cargo *".into(),
243 "rustc *".into(),
244 "python *".into(),
246 "python3 *".into(),
247 "pip *".into(),
248 "pip3 *".into(),
249 "virtualenv *".into(),
250 "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 *".into(),
263 "gcc *".into(),
265 "g++ *".into(),
266 "clang *".into(),
267 "clang++ *".into(),
268 "javac *".into(),
270 "java *".into(),
271 "mvn *".into(),
272 "gradle *".into(),
273 "make *".into(),
275 "cmake *".into(),
276 "ninja *".into(),
277 "docker *".into(),
279 "docker-compose *".into(),
280 "tar *".into(),
282 "zip *".into(),
283 "unzip *".into(),
284 "gzip *".into(),
285 "gunzip *".into(),
286 ]
287}
288
289fn default_deny_glob() -> Vec<String> {
291 vec![
292 "rm *".into(),
294 "sudo *".into(),
296 "chmod *".into(),
298 "chown *".into(),
299 "kill *".into(),
301 "pkill *".into(),
302 "systemctl *".into(),
304 "service *".into(),
305 "mount *".into(),
307 "umount *".into(),
308 "docker run *".into(),
310 "kubectl *".into(),
312 ]
313}
314
315fn default_allow_regex() -> Vec<String> {
317 vec![
318 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 r"^git (status|diff|log|show|branch|remote)\b".into(),
322 r"^cargo (check|build|test|run|doc|clippy|fmt|tree|metadata)\b".into(),
324 r"^rustc\b".into(),
325 r"^(python|python3) (-m | )?\w*".into(),
327 r"^(pip|pip3)\b".into(),
328 r"^virtualenv\b".into(),
329 r"^(node|npm|yarn|pnpm|bun|npx)\b".into(),
331 r"^go\b".into(),
333 r"^(gcc|g\+\+|clang|clang\++)\b".into(),
335 r"^(javac|java)\b".into(),
337 r"^(mvn|gradle)\b".into(),
338 r"^(make|cmake)\b".into(),
340 r"^(docker|docker-compose)\b".into(),
342 ]
343}
344
345fn default_deny_regex() -> Vec<String> {
347 vec![
348 r"rm\s+(-rf|--force)".into(),
350 r"sudo\s+.*".into(),
352 r"chmod\s+.*".into(),
354 r"chown\s+.*".into(),
355 r"docker\s+run\s+.*--privileged".into(),
357 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}