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