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 "bunx",
125 "npx",
126 "go",
128 "go build",
129 "go test",
130 "gcc",
132 "g++",
133 "clang",
134 "clang++",
135 "javac",
137 "java",
138 "mvn",
139 "gradle",
140 "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
160fn default_allow_list() -> Vec<String> {
162 DEFAULT_ALLOW_LIST.iter().map(|s| (*s).into()).collect()
163}
164
165fn default_deny_list() -> Vec<String> {
167 vec![
168 "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 "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 "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 "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 "wget --no-check-certificate".into(),
213 ":(){ :|:& };:".into(), "nohup bash -i".into(),
215 "exec bash -i".into(),
216 "eval".into(),
217 "source /etc/bashrc".into(),
219 "source ~/.bashrc".into(),
220 "chmod 777".into(),
222 "chmod -R 777".into(),
223 "chown -R".into(),
224 "chgrp -R".into(),
225 "rm ~/.ssh/*".into(),
227 "rm -r ~/.ssh".into(),
228 "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
237fn default_allow_glob() -> Vec<String> {
239 vec![
240 "git *".into(),
242 "cargo *".into(),
244 "rustc *".into(),
245 "python *".into(),
247 "python3 *".into(),
248 "pip *".into(),
249 "pip3 *".into(),
250 "virtualenv *".into(),
251 "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 *".into(),
265 "gcc *".into(),
267 "g++ *".into(),
268 "clang *".into(),
269 "clang++ *".into(),
270 "javac *".into(),
272 "java *".into(),
273 "mvn *".into(),
274 "gradle *".into(),
275 "make *".into(),
277 "cmake *".into(),
278 "ninja *".into(),
279 "docker *".into(),
281 "docker-compose *".into(),
282 "tar *".into(),
284 "zip *".into(),
285 "unzip *".into(),
286 "gzip *".into(),
287 "gunzip *".into(),
288 ]
289}
290
291fn default_deny_glob() -> Vec<String> {
293 vec![
294 "rm *".into(),
296 "sudo *".into(),
298 "chmod *".into(),
300 "chown *".into(),
301 "kill *".into(),
303 "pkill *".into(),
304 "systemctl *".into(),
306 "service *".into(),
307 "mount *".into(),
309 "umount *".into(),
310 "docker run *".into(),
312 "kubectl *".into(),
314 ]
315}
316
317fn default_allow_regex() -> Vec<String> {
319 vec![
320 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 r"^git (status|diff|log|show|branch|remote)\b".into(),
324 r"^cargo (check|build|test|run|doc|clippy|fmt|tree|metadata)\b".into(),
326 r"^rustc\b".into(),
327 r"^(python|python3) (-m | )?\w*".into(),
329 r"^(pip|pip3)\b".into(),
330 r"^virtualenv\b".into(),
331 r"^(node|npm|yarn|pnpm|bun|bunx|npx)\b".into(),
333 r"^go\b".into(),
335 r"^(gcc|g\+\+|clang|clang\++)\b".into(),
337 r"^(javac|java)\b".into(),
339 r"^(mvn|gradle)\b".into(),
340 r"^(make|cmake)\b".into(),
342 r"^(docker|docker-compose)\b".into(),
344 ]
345}
346
347fn default_deny_regex() -> Vec<String> {
349 vec![
350 r"rm\s+(-rf|--force)".into(),
352 r"sudo\s+.*".into(),
354 r"chmod\s+.*".into(),
356 r"chown\s+.*".into(),
357 r"docker\s+run\s+.*--privileged".into(),
359 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}