返回 CodeWhale
tool_rules.rs
根目录 / crates / execpolicy / src / tool_rules.rs
1 pub fn tool_denied(disallowed_tools: Option<&[String]>, tool_name: &str) -> bool {
2 disallowed_tools.is_some_and(|rules| {
3 tool_matches_any_rule(rules, tool_name)
4 || (requires_raw_shell(tool_name) && tool_matches_any_rule(rules, "Bash"))
5 })
6 }
7
8 /// Execution dependencies narrow denials only. Treating these as symmetric
9 /// aliases would also grant task/terminal execution to an allowlist of Bash.
10 fn requires_raw_shell(name: &str) -> bool {
11 matches!(
12 name.to_ascii_lowercase().as_str(),
13 "bash"
14 | "exec_shell"
15 | "exec_shell_interact"
16 | "exec_interact"
17 | "task_shell_start"
18 | "task_gate_run"
19 | "task_create"
20 | "automation_create"
21 | "automation_update"
22 | "automation_resume"
23 | "automation_adopt"
24 | "automation_run"
25 | "terminal_run"
26 | "terminal_send"
27 | "terminal_reset"
28 | "terminal/run"
29 | "terminal/send"
30 | "terminal/reset"
31 | "code_execution"
32 | "js_execution"
33 | "rlm_eval"
34 // Launching an MCP server spawns an arbitrary local process.
35 | "start_mcp_server"
36 | "start_registry_mcp_server"
37 )
38 }
39
40 pub fn tool_matches_any_rule(rules: &[String], tool_name: &str) -> bool {
41 let tool_name = tool_name.to_ascii_lowercase();
42 rules.iter().any(|rule| {
43 let rule = rule.to_ascii_lowercase();
44 let (rule_body, is_prefix) = rule
45 .strip_suffix('*')
46 .map_or((rule.as_str(), false), |prefix| (prefix, true));
47 std::iter::once(tool_name.as_str())
48 .chain(policy_tool_aliases(&tool_name).iter().copied())
49 .any(|candidate| {
50 if is_prefix {
51 candidate.starts_with(rule_body)
52 } else {
53 candidate == rule_body
54 }
55 })
56 })
57 }
58
59 fn policy_tool_aliases(name: &str) -> &'static [&'static str] {
60 match name {
61 "read" | "read_file" => &["read", "read_file", "file"],
62 "write" | "write_file" => &["write", "write_file", "file"],
63 "edit" | "edit_file" => &["edit", "edit_file", "file"],
64 "file" => &[
65 "file",
66 "read",
67 "read_file",
68 "write",
69 "write_file",
70 "edit",
71 "edit_file",
72 ],
73 "bash" | "exec_shell" => &["bash", "exec_shell"],
74 "terminal_run" | "terminal/run" => &["terminal_run", "terminal/run"],
75 "terminal_send" | "terminal/send" => &["terminal_send", "terminal/send"],
76 "terminal_wait" | "terminal/wait" => &["terminal_wait", "terminal/wait"],
77 "terminal_cancel" | "terminal/cancel" => &["terminal_cancel", "terminal/cancel"],
78 "terminal_reset" | "terminal/reset" => &["terminal_reset", "terminal/reset"],
79 "mcp_read_resource" | "read_mcp_resource" => &["mcp_read_resource", "read_mcp_resource"],
80 _ => &[],
81 }
82 }
83
83 lines RUST