safe_chains/handlers/
mod.rs1macro_rules! handler_module {
2 ($($sub:ident),+ $(,)?) => {
3 $(mod $sub;)+
4
5 pub(crate) fn dispatch(cmd: &str, tokens: &[crate::parse::Token]) -> Option<crate::verdict::Verdict> {
6 None$(.or_else(|| $sub::dispatch(cmd, tokens)))+
7 }
8
9 pub fn command_docs() -> Vec<crate::docs::CommandDoc> {
10 let mut docs = Vec::new();
11 $(docs.extend($sub::command_docs());)+
12 docs
13 }
14
15 #[cfg(test)]
16 pub(super) fn full_registry() -> Vec<&'static super::CommandEntry> {
17 let mut v = Vec::new();
18 $(v.extend($sub::REGISTRY);)+
19 v
20 }
21 };
22}
23
24pub mod android;
25pub mod coreutils;
26pub mod forges;
27pub mod fuzzy;
28pub mod jvm;
29pub mod magick;
30pub mod network;
31pub mod node;
32pub mod perl;
33pub mod php;
34pub mod ruby;
35pub mod shell;
36pub mod system;
37pub mod tilt;
38pub mod vcs;
39pub mod wrappers;
40
41use std::collections::HashMap;
42
43use crate::parse::Token;
44use crate::verdict::Verdict;
45
46type HandlerFn = fn(&[Token]) -> Verdict;
47
48pub fn custom_cmd_handlers() -> HashMap<&'static str, HandlerFn> {
49 HashMap::from([
50 ("magick", magick::is_safe_magick as HandlerFn),
51 ("php", php::is_safe_php as HandlerFn),
52 ("sysctl", system::sysctl::is_safe_sysctl as HandlerFn),
53 ("tilt", tilt::check_tilt as HandlerFn),
54 ])
55}
56
57pub fn custom_sub_handlers() -> HashMap<&'static str, HandlerFn> {
58 HashMap::from([
59 ("bun_x", node::bun::check_bun_x as HandlerFn),
60 ("bundle_config", ruby::bundle::check_bundle_config as HandlerFn),
61 ("bundle_exec", ruby::bundle::check_bundle_exec as HandlerFn),
62 ("git_remote", vcs::git::check_git_remote as HandlerFn),
63 ("laravel_cache_clear", php::check_laravel_cache_clear as HandlerFn),
64 ("plutil_convert", system::plutil::check_plutil_convert as HandlerFn),
65 ])
66}
67
68pub fn dispatch(tokens: &[Token]) -> Verdict {
69 let cmd = tokens[0].command_name();
70 None
71 .or_else(|| crate::registry::custom_dispatch(tokens))
72 .or_else(|| shell::dispatch(cmd, tokens))
73 .or_else(|| wrappers::dispatch(cmd, tokens))
74 .or_else(|| forges::dispatch(cmd, tokens))
75 .or_else(|| node::dispatch(cmd, tokens))
76 .or_else(|| jvm::dispatch(cmd, tokens))
77 .or_else(|| android::dispatch(cmd, tokens))
78 .or_else(|| network::dispatch(cmd, tokens))
79 .or_else(|| system::dispatch(cmd, tokens))
80 .or_else(|| perl::dispatch(cmd, tokens))
81 .or_else(|| coreutils::dispatch(cmd, tokens))
82 .or_else(|| fuzzy::dispatch(cmd, tokens))
83 .or_else(|| vcs::dispatch(cmd, tokens))
84 .or_else(|| crate::registry::toml_dispatch(tokens))
85 .unwrap_or(Verdict::Denied)
86}
87
88pub fn handler_docs() -> Vec<crate::docs::CommandDoc> {
89 let mut docs = Vec::new();
90 docs.extend(forges::command_docs());
91 docs.extend(node::command_docs());
92 docs.extend(jvm::command_docs());
93 docs.extend(android::command_docs());
94 docs.extend(network::command_docs());
95 docs.extend(system::command_docs());
96 docs.extend(perl::command_docs());
97 docs.extend(coreutils::command_docs());
98 docs.extend(fuzzy::command_docs());
99 docs.extend(shell::command_docs());
100 docs.extend(wrappers::command_docs());
101 docs.extend(vcs::command_docs());
102 docs.extend(crate::registry::toml_command_docs());
103 docs
104}
105
106#[cfg(test)]
107#[derive(Debug)]
108pub(crate) enum CommandEntry {
109 Custom { cmd: &'static str, valid_prefix: Option<&'static str> },
110 Paths { cmd: &'static str, bare_ok: bool, paths: &'static [&'static str] },
111}
112
113pub fn all_opencode_patterns() -> Vec<String> {
114 let mut patterns = Vec::new();
115 patterns.sort();
116 patterns.dedup();
117 patterns
118}
119
120#[cfg(test)]
121fn full_registry() -> Vec<&'static CommandEntry> {
122 let mut entries = Vec::new();
123 entries.extend(forges::full_registry());
124 entries.extend(jvm::full_registry());
125 entries.extend(android::full_registry());
126 entries.extend(network::REGISTRY);
127 entries.extend(coreutils::full_registry());
128 entries.extend(fuzzy::full_registry());
129 entries
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135
136 const UNKNOWN_FLAG: &str = "--xyzzy-unknown-42";
137 const UNKNOWN_SUB: &str = "xyzzy-unknown-42";
138
139 fn check_entry(entry: &CommandEntry, failures: &mut Vec<String>) {
140 match entry {
141 CommandEntry::Custom { cmd, valid_prefix } => {
142 let base = valid_prefix.unwrap_or(cmd);
143 let test = format!("{base} {UNKNOWN_FLAG}");
144 if crate::is_safe_command(&test) {
145 failures.push(format!("{cmd}: accepted unknown flag: {test}"));
146 }
147 }
148 CommandEntry::Paths { cmd, bare_ok, paths } => {
149 if !bare_ok && crate::is_safe_command(cmd) {
150 failures.push(format!("{cmd}: accepted bare invocation"));
151 }
152 let test = format!("{cmd} {UNKNOWN_SUB}");
153 if crate::is_safe_command(&test) {
154 failures.push(format!("{cmd}: accepted unknown subcommand: {test}"));
155 }
156 for path in *paths {
157 let test = format!("{path} {UNKNOWN_FLAG}");
158 if crate::is_safe_command(&test) {
159 failures.push(format!("{path}: accepted unknown flag: {test}"));
160 }
161 }
162 }
163 }
164 }
165
166 #[test]
167 fn all_commands_reject_unknown() {
168 let registry = full_registry();
169 let mut failures = Vec::new();
170 for entry in ®istry {
171 check_entry(entry, &mut failures);
172 }
173 assert!(
174 failures.is_empty(),
175 "unknown flags/subcommands accepted:\n{}",
176 failures.join("\n")
177 );
178 }
179
180 #[test]
181 fn process_substitution_safe_inner() {
182 let safe = ["echo <(cat /etc/passwd)", "grep pattern <(ls)", "diff <(sort a.txt) <(sort b.txt)", "comm -23 file.txt <(sort other.txt)"];
183 for cmd in &safe {
184 assert!(crate::is_safe_command(cmd), "safe process substitution rejected: {cmd}");
185 }
186 }
187
188 #[test]
189 fn process_substitution_unsafe_inner() {
190 let unsafe_cmds = ["echo >(rm -rf /)", "diff <(sort a.txt) <(rm -rf /)"];
191 for cmd in &unsafe_cmds {
192 assert!(!crate::is_safe_command(cmd), "unsafe process substitution approved: {cmd}");
193 }
194 }
195
196}