| 1 | //! Tool-call risk policy. |
| 2 | //! |
| 3 | //! Classifies a call so the engine's gates and the approval views agree on |
| 4 | //! how much the call can touch. Intentionally UI-free. |
| 5 | |
| 6 | use codewhale_execpolicy::canonical_action::canonical_action_alias; |
| 7 | use codewhale_execpolicy::command_safety::is_parallel_readonly_command; |
| 8 | use serde_json::Value; |
| 9 | |
| 10 | use super::ToolCategory; |
| 11 | |
| 12 | /// Stakes-based variant for the takeover modal. |
| 13 | /// |
| 14 | /// `RiskLevel::Benign` lets a single keystroke commit the approval. |
| 15 | /// `RiskLevel::Destructive` keeps stronger warning copy and styling |
| 16 | /// around approvals that can touch files, shell, or remote state. |
| 17 | /// |
| 18 | /// Routing rules live in [`classify_risk`] - when in doubt, route to |
| 19 | /// `Destructive`. |
| 20 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 21 | pub enum RiskLevel { |
| 22 | Benign, |
| 23 | Destructive, |
| 24 | } |
| 25 | |
| 26 | /// Decide the stakes variant for an approval request. |
| 27 | /// |
| 28 | /// The bias is conservative: a category we don't recognise routes to |
| 29 | /// `Destructive`, and any shell command that `command_safety` flags as |
| 30 | /// `Dangerous` is forced to `Destructive` even when the rest of the |
| 31 | /// request looks calm. The split lets the modal render stronger warning |
| 32 | /// copy on anything that can touch state outside this turn. |
| 33 | #[must_use] |
| 34 | pub fn classify_risk(tool_name: &str, category: ToolCategory, params: &Value) -> RiskLevel { |
| 35 | let tool_name = canonical_action_alias(tool_name, params); |
| 36 | match category { |
| 37 | // Read paths and discovery. |
| 38 | ToolCategory::Safe | ToolCategory::McpRead => RiskLevel::Benign, |
| 39 | // Query-only network is benign; opening a URL pulls arbitrary |
| 40 | // remote content, so it stays destructive. |
| 41 | ToolCategory::Network => match tool_name { |
| 42 | "web_search" | "wait_for_dev_server" | "registry_sync" => RiskLevel::Benign, |
| 43 | // web_run is benign for search/query, but its `open`/`click` |
| 44 | // actions fetch model-supplied URLs (arbitrary remote content) - |
| 45 | // destructive, consistent with fetch_url. |
| 46 | "web_run" => { |
| 47 | let fetches_url = params |
| 48 | .get("open") |
| 49 | .and_then(Value::as_array) |
| 50 | .is_some_and(|a| !a.is_empty()) |
| 51 | || params |
| 52 | .get("click") |
| 53 | .and_then(Value::as_array) |
| 54 | .is_some_and(|a| !a.is_empty()); |
| 55 | if fetches_url { |
| 56 | RiskLevel::Destructive |
| 57 | } else { |
| 58 | RiskLevel::Benign |
| 59 | } |
| 60 | } |
| 61 | _ => RiskLevel::Destructive, |
| 62 | }, |
| 63 | // Shell stays destructive unless the existing command-safety analyzer |
| 64 | // can prove the concrete command is read-only. |
| 65 | ToolCategory::Shell => { |
| 66 | if let Some(cmd) = params.get("command").and_then(Value::as_str) |
| 67 | && is_parallel_readonly_command(cmd) |
| 68 | { |
| 69 | return RiskLevel::Benign; |
| 70 | } |
| 71 | RiskLevel::Destructive |
| 72 | } |
| 73 | // Sub-agent lifecycle: status/peek are inspection-only. Starts and |
| 74 | // other actions keep the explicit-options keymap (the child's own |
| 75 | // gates govern what it may do once running). |
| 76 | ToolCategory::Agent => match params.get("action").and_then(Value::as_str) { |
| 77 | Some("status" | "peek" | "list") => RiskLevel::Benign, |
| 78 | _ => RiskLevel::Destructive, |
| 79 | }, |
| 80 | // File writes, MCP actions, unclassified surfaces - all require |
| 81 | // explicit confirmation. |
| 82 | ToolCategory::FileWrite | ToolCategory::McpAction | ToolCategory::Unknown => { |
| 83 | RiskLevel::Destructive |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[cfg(test)] |
| 89 | mod tests { |
| 90 | use super::*; |
| 91 | use crate::core::authority::{get_tool_category, get_tool_category_for_call}; |
| 92 | use serde_json::json; |
| 93 | |
| 94 | #[test] |
| 95 | fn classifies_read_only_surfaces_as_benign() { |
| 96 | for name in ["read_file", "list_dir", "list_mcp_tools", "web_search"] { |
| 97 | let category = get_tool_category(name); |
| 98 | assert_eq!( |
| 99 | classify_risk(name, category, &json!({})), |
| 100 | RiskLevel::Benign, |
| 101 | "{name}" |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn classifies_stateful_or_unknown_surfaces_as_destructive() { |
| 108 | for name in [ |
| 109 | "write_file", |
| 110 | "edit_file", |
| 111 | "apply_patch", |
| 112 | "mcp_linear_save_issue", |
| 113 | "fetch_url", |
| 114 | "unknown_tool", |
| 115 | ] { |
| 116 | let category = get_tool_category(name); |
| 117 | assert_eq!( |
| 118 | classify_risk(name, category, &json!({})), |
| 119 | RiskLevel::Destructive, |
| 120 | "{name}" |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn shell_risk_uses_command_safety_analysis() { |
| 127 | let category = get_tool_category("exec_shell"); |
| 128 | assert_eq!( |
| 129 | classify_risk( |
| 130 | "exec_shell", |
| 131 | category, |
| 132 | &json!({"command": "git status --short"}) |
| 133 | ), |
| 134 | RiskLevel::Benign |
| 135 | ); |
| 136 | assert_eq!( |
| 137 | classify_risk( |
| 138 | "exec_shell", |
| 139 | category, |
| 140 | &json!({"command": "rm -rf /tmp/example"}) |
| 141 | ), |
| 142 | RiskLevel::Destructive |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn shell_exec_flags_are_not_benign() { |
| 148 | let category = get_tool_category("exec_shell"); |
| 149 | for command in [ |
| 150 | "fd -x ./pwn.sh", |
| 151 | "fd -uHtx ./pwn.sh", |
| 152 | "rg --pre /tmp/evil.sh needle .", |
| 153 | "git grep -O needle", |
| 154 | "git grep -nO needle", |
| 155 | ] { |
| 156 | assert_eq!( |
| 157 | classify_risk("exec_shell", category, &json!({"command": command})), |
| 158 | RiskLevel::Destructive, |
| 159 | "{command} should not be classified as benign" |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | for command in [ |
| 164 | "fd -e rs .", |
| 165 | "fd -H --type f src", |
| 166 | "rg needle crates/", |
| 167 | "git grep needle crates/", |
| 168 | "git grep -n needle crates/", |
| 169 | ] { |
| 170 | assert_eq!( |
| 171 | classify_risk("exec_shell", category, &json!({"command": command})), |
| 172 | RiskLevel::Benign, |
| 173 | "{command} should remain benign" |
| 174 | ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn web_run_open_and_click_fetch_remote_content() { |
| 180 | let category = get_tool_category("web_run"); |
| 181 | assert_eq!( |
| 182 | classify_risk( |
| 183 | "web_run", |
| 184 | category, |
| 185 | &json!({"search_query": [{"q": "rust"}]}) |
| 186 | ), |
| 187 | RiskLevel::Benign |
| 188 | ); |
| 189 | assert_eq!( |
| 190 | classify_risk("web_run", category, &json!({"open": [{"ref_id": "x"}]})), |
| 191 | RiskLevel::Destructive |
| 192 | ); |
| 193 | assert_eq!( |
| 194 | classify_risk( |
| 195 | "web_run", |
| 196 | category, |
| 197 | &json!({"click": [{"ref_id": "x", "id": 1}]}) |
| 198 | ), |
| 199 | RiskLevel::Destructive |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn canonical_actions_keep_legacy_approval_categories_and_risk() { |
| 205 | let cases = [ |
| 206 | ("Bash", "run", ToolCategory::Shell, RiskLevel::Destructive), |
| 207 | ("Bash", "wait", ToolCategory::Shell, RiskLevel::Destructive), |
| 208 | ( |
| 209 | "Bash", |
| 210 | "interact", |
| 211 | ToolCategory::Shell, |
| 212 | RiskLevel::Destructive, |
| 213 | ), |
| 214 | ( |
| 215 | "Bash", |
| 216 | "cancel", |
| 217 | ToolCategory::Shell, |
| 218 | RiskLevel::Destructive, |
| 219 | ), |
| 220 | ("File", "read", ToolCategory::Safe, RiskLevel::Benign), |
| 221 | ("File", "list", ToolCategory::Safe, RiskLevel::Benign), |
| 222 | ("File", "search_name", ToolCategory::Safe, RiskLevel::Benign), |
| 223 | ( |
| 224 | "File", |
| 225 | "search_content", |
| 226 | ToolCategory::Safe, |
| 227 | RiskLevel::Benign, |
| 228 | ), |
| 229 | ( |
| 230 | "File", |
| 231 | "write", |
| 232 | ToolCategory::FileWrite, |
| 233 | RiskLevel::Destructive, |
| 234 | ), |
| 235 | ( |
| 236 | "File", |
| 237 | "edit", |
| 238 | ToolCategory::FileWrite, |
| 239 | RiskLevel::Destructive, |
| 240 | ), |
| 241 | ( |
| 242 | "File", |
| 243 | "patch", |
| 244 | ToolCategory::FileWrite, |
| 245 | RiskLevel::Destructive, |
| 246 | ), |
| 247 | ("Git", "status", ToolCategory::Safe, RiskLevel::Benign), |
| 248 | ("Git", "diff", ToolCategory::Safe, RiskLevel::Benign), |
| 249 | ("Git", "log", ToolCategory::Safe, RiskLevel::Benign), |
| 250 | ("Git", "show", ToolCategory::Safe, RiskLevel::Benign), |
| 251 | ("Git", "blame", ToolCategory::Safe, RiskLevel::Benign), |
| 252 | ("Git", "commit_plan", ToolCategory::Safe, RiskLevel::Benign), |
| 253 | ( |
| 254 | "Run", |
| 255 | "tests", |
| 256 | ToolCategory::Unknown, |
| 257 | RiskLevel::Destructive, |
| 258 | ), |
| 259 | ( |
| 260 | "Run", |
| 261 | "verifiers", |
| 262 | ToolCategory::Unknown, |
| 263 | RiskLevel::Destructive, |
| 264 | ), |
| 265 | ("Web", "search", ToolCategory::Network, RiskLevel::Benign), |
| 266 | ( |
| 267 | "Web", |
| 268 | "fetch", |
| 269 | ToolCategory::Network, |
| 270 | RiskLevel::Destructive, |
| 271 | ), |
| 272 | ("Web", "wait", ToolCategory::Network, RiskLevel::Benign), |
| 273 | ]; |
| 274 | |
| 275 | for (family, action, expected_category, expected_risk) in cases { |
| 276 | let params = json!({"action": action}); |
| 277 | let category = get_tool_category_for_call(family, ¶ms); |
| 278 | assert_eq!(category, expected_category, "{family}.{action}"); |
| 279 | assert_eq!( |
| 280 | classify_risk(family, category, ¶ms), |
| 281 | expected_risk, |
| 282 | "{family}.{action}" |
| 283 | ); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 |