lean_ctx_cli/cli/
allow_cmd.rs1use lean_ctx::core::config;
11use lean_ctx_shell_policy as shell_allowlist;
12
13pub fn cmd_allow(args: &[String]) {
14 if args.iter().any(|a| a == "--help" || a == "-h") {
17 print_usage();
18 return;
19 }
20 match args.first().map(std::string::String::as_str) {
21 None => print_usage(),
22 Some("--list" | "list" | "ls") => print_effective(),
23 Some("--remove" | "-r" | "remove" | "rm") => remove(&args[1..]),
24 _ => add(args),
25 }
26}
27
28fn add(cmds: &[String]) {
30 let requested: Vec<String> = cmds
31 .iter()
32 .map(|c| c.trim().to_string())
33 .filter(|c| !c.is_empty())
34 .collect();
35
36 if requested.is_empty() {
37 print_usage();
38 return;
39 }
40
41 let mut extra = current_extra_from_global();
42 let mut added = Vec::new();
43 for cmd in requested {
44 if extra.iter().any(|e| e == &cmd) {
45 println!(" already allowed: {cmd}");
46 } else {
47 extra.push(cmd.clone());
48 added.push(cmd);
49 }
50 }
51
52 if added.is_empty() {
53 println!("\nNothing to add — all commands were already in the allowlist.");
54 print_effective();
55 return;
56 }
57
58 if let Err(e) = write_extra(&extra) {
59 eprintln!("Error: {e}");
60 std::process::exit(1);
61 }
62
63 println!("Allowed (additive): {}", added.join(", "));
64 println!("These are merged on top of the defaults — nothing else was removed.");
65 println!("Takes effect immediately; no MCP/daemon restart needed.");
66
67 let effective = shell_allowlist::effective_allowlist_pub();
72 for cmd in &added {
73 if let Some(covering) = shell_allowlist::shadowing_entry(&effective, cmd) {
74 println!(
75 " \x1b[33m⚠ '{cmd}' has no effect — it's already covered by '{covering}'.\x1b[0m"
76 );
77 println!(
78 " To scope a default binary like '{covering}', replace `shell_allowlist` \
79 directly — `shell_allowlist_extra` can only add, never narrow."
80 );
81 }
82 }
83 print_effective();
84}
85
86fn remove(cmds: &[String]) {
88 let to_remove: Vec<String> = cmds
89 .iter()
90 .map(|c| c.trim().to_string())
91 .filter(|c| !c.is_empty())
92 .collect();
93
94 if to_remove.is_empty() {
95 eprintln!("Usage: lean-ctx allow --remove <cmd> [<cmd>...]");
96 std::process::exit(1);
97 }
98
99 let before = current_extra_from_global();
100 let after: Vec<String> = before
101 .iter()
102 .filter(|e| !to_remove.iter().any(|r| r == *e))
103 .cloned()
104 .collect();
105
106 let removed: Vec<&String> = before.iter().filter(|e| !after.contains(e)).collect();
107 if removed.is_empty() {
108 println!("None of those were in shell_allowlist_extra (nothing changed).");
109 println!(
110 "Note: built-in defaults can't be removed here — set `shell_allowlist` explicitly to override the whole list."
111 );
112 return;
113 }
114
115 if let Err(e) = write_extra(&after) {
116 eprintln!("Error: {e}");
117 std::process::exit(1);
118 }
119
120 let names: Vec<&str> = removed.iter().map(|s| s.as_str()).collect();
121 println!("Removed from extra allowlist: {}", names.join(", "));
122 print_effective();
123}
124
125fn print_effective() {
129 let effective = shell_allowlist::effective_allowlist_pub();
130 let parse_err = config::last_config_parse_error();
131 let path = config::Config::path().map_or_else(
132 || "~/.lean-ctx/config.toml".to_string(),
133 |p| p.display().to_string(),
134 );
135
136 println!("\nShell allowlist (enforced by the MCP tools):");
137 println!(" Config: {path}");
138
139 if let Some(err) = parse_err {
140 println!(" \x1b[31m⚠ config.toml FAILED to parse — running on DEFAULTS.\x1b[0m");
141 println!(" {err}");
142 println!(" Fix the TOML above, then re-run `lean-ctx allow --list`.");
143 }
144
145 if effective.is_empty() {
146 println!(" Mode: disabled (every command is allowed)");
147 return;
148 }
149
150 println!(
151 " Mode: restricted — {} command(s) permitted",
152 effective.len()
153 );
154
155 let extra = current_extra_from_global();
156 if extra.is_empty() {
157 println!(" Extra (additive, via `lean-ctx allow`): none");
158 } else {
159 println!(
160 " Extra (additive, via `lean-ctx allow`): {}",
161 extra.join(", ")
162 );
163 }
164}
165
166fn current_extra_from_global() -> Vec<String> {
169 global_list("shell_allowlist_extra")
170}
171
172fn global_list(key: &str) -> Vec<String> {
174 let Some(path) = config::Config::path() else {
175 return Vec::new();
176 };
177 let Ok(raw) = std::fs::read_to_string(&path) else {
178 return Vec::new();
179 };
180 let Ok(table) = raw.parse::<toml::Table>() else {
181 return Vec::new();
182 };
183 table
184 .get(key)
185 .and_then(toml::Value::as_array)
186 .map(|arr| {
187 arr.iter()
188 .filter_map(|v| v.as_str().map(str::to_string))
189 .collect()
190 })
191 .unwrap_or_default()
192}
193
194fn write_extra(extra: &[String]) -> Result<(), String> {
196 config::setter::set_by_key("shell_allowlist_extra", &extra.join(","))
197 .map(|_| ())
198 .map_err(|e| e.to_string())
199}
200
201fn print_usage() {
202 println!(
203 "Usage: lean-ctx allow <cmd> [<cmd>...] Add command(s) to the shell allowlist (additive)\n\
204 \x20 lean-ctx allow --list Show the effective allowlist + config path\n\
205 \x20 lean-ctx allow --remove <cmd> Remove command(s) you previously added\n\
206 \n\
207 Why this exists: editing `shell_allowlist` replaces the whole built-in list.\n\
208 `lean-ctx allow` appends to `shell_allowlist_extra`, keeping git/cargo/npm/… intact.\n\
209 \n\
210 Entry grammar: one or more words, optionally ending in a lone `*`.\n\
211 A single word (\"cargo\") always allows any subcommand/args. A multi-word\n\
212 entry with no `*` (\"git status\") matches that exact command only. A\n\
213 trailing `*` (\"terraform plan *\") matches that subcommand plus any\n\
214 (or no) further arguments.\n\
215 Example: lean-ctx allow acli"
216 );
217 print_effective();
218}