Skip to main content

safe_chains/targets/
gemini.rs

1use std::path::{Path, PathBuf};
2
3use serde::Deserialize;
4use serde_json::{Map, Value, json};
5
6use super::{HookFormat, HookInput, HookResponse, InstallOutcome, ParseError, Target, allow_reason};
7use crate::verdict::Verdict;
8
9pub struct GeminiTarget;
10
11impl Target for GeminiTarget {
12    fn name(&self) -> &'static str {
13        "gemini"
14    }
15
16    fn display_name(&self) -> &'static str {
17        "Gemini CLI"
18    }
19
20    fn detect_paths(&self, home: &Path) -> Vec<PathBuf> {
21        vec![home.join(".gemini")]
22    }
23
24    fn install(&self, home: &Path) -> Result<InstallOutcome, String> {
25        let dir = home.join(".gemini");
26        if !dir.exists() {
27            return Ok(InstallOutcome::Skipped {
28                reason: format!(
29                    "~/.gemini not found at {} (Gemini CLI not installed)",
30                    dir.display()
31                ),
32            });
33        }
34
35        let path = dir.join("settings.json");
36        let binary = "safe-chains hook gemini";
37
38        if path.exists() {
39            let contents = std::fs::read_to_string(&path)
40                .map_err(|e| format!("Could not read {}: {e}", path.display()))?;
41            let mut settings: Value = serde_json::from_str(&contents)
42                .map_err(|e| format!("Could not parse {}: {e}", path.display()))?;
43
44            if has_safe_chains_hook(&settings) {
45                return Ok(InstallOutcome::AlreadyConfigured { path });
46            }
47
48            add_hook(&mut settings, binary);
49            let output = serde_json::to_string_pretty(&settings).expect("serializing valid JSON");
50            std::fs::write(&path, format!("{output}\n"))
51                .map_err(|e| format!("Could not write {}: {e}", path.display()))?;
52            Ok(InstallOutcome::Installed { path })
53        } else {
54            let mut settings = Value::Object(Map::new());
55            add_hook(&mut settings, binary);
56            let output = serde_json::to_string_pretty(&settings).expect("serializing valid JSON");
57            std::fs::write(&path, format!("{output}\n"))
58                .map_err(|e| format!("Could not write {}: {e}", path.display()))?;
59            Ok(InstallOutcome::Installed { path })
60        }
61    }
62
63    fn hook_format(&self) -> Option<&dyn HookFormat> {
64        Some(&GeminiHookFormat)
65    }
66}
67
68struct GeminiHookFormat;
69
70#[derive(Deserialize)]
71struct ToolInput {
72    command: String,
73}
74
75#[derive(Deserialize)]
76struct GeminiHookEnvelope {
77    #[serde(default)]
78    tool_name: Option<String>,
79    tool_input: ToolInput,
80    #[serde(default)]
81    cwd: Option<String>,
82}
83
84impl HookFormat for GeminiHookFormat {
85    fn parse_input(&self, stdin: &str) -> Result<HookInput, ParseError> {
86        let envelope: GeminiHookEnvelope = serde_json::from_str(stdin).map_err(|e| ParseError {
87            message: e.to_string(),
88        })?;
89        // Gemini's matcher narrows to run_shell_command in config, but
90        // some setups may dispatch all tools through the same hook.
91        // For non-shell tools, return Err so the runtime exits 0
92        // silently — equivalent to "no opinion" — and Gemini falls
93        // back to its own permission rules.
94        if let Some(name) = &envelope.tool_name
95            && name != "run_shell_command"
96            && name != "Shell"
97        {
98            return Err(ParseError {
99                message: format!("not a shell tool: {name}"),
100            });
101        }
102        Ok(HookInput {
103            command: envelope.tool_input.command,
104            cwd: envelope.cwd,
105        })
106    }
107
108    fn render_response(&self, verdict: Verdict) -> HookResponse {
109        if verdict.is_allowed() {
110            let reason = allow_reason(verdict);
111            // Gemini contract: `decision` (not permission /
112            // permissionDecision). Values: "allow" or "deny" only —
113            // no "ask".
114            let body = json!({
115                "decision": "allow",
116                "reason": reason,
117            });
118            HookResponse {
119                stdout: serde_json::to_string(&body).unwrap_or_default(),
120                exit_code: 0,
121            }
122        } else {
123            // Empty stdout is "no opinion" — Gemini's docs note that
124            // exit code drives the outcome and an unparseable stdout
125            // is a warning. Exit 0 + empty body lets Gemini's own
126            // permission system handle it.
127            HookResponse {
128                stdout: String::new(),
129                exit_code: 0,
130            }
131        }
132    }
133}
134
135fn hook_entry(binary: &str) -> Value {
136    json!({
137        "matcher": "^run_shell_command$",
138        "hooks": [{
139            "type": "command",
140            "command": binary,
141            "timeout": 60_000,
142        }]
143    })
144}
145
146fn has_safe_chains_hook(settings: &Value) -> bool {
147    settings
148        .get("hooks")
149        .and_then(|h| h.get("BeforeTool"))
150        .and_then(|arr| arr.as_array())
151        .is_some_and(|entries| {
152            entries.iter().any(|entry| {
153                entry
154                    .get("hooks")
155                    .and_then(|h| h.as_array())
156                    .is_some_and(|hooks| {
157                        hooks.iter().any(|hook| {
158                            hook.get("command")
159                                .and_then(|c| c.as_str())
160                                .is_some_and(|cmd| cmd.contains("safe-chains"))
161                        })
162                    })
163            })
164        })
165}
166
167fn add_hook(settings: &mut Value, binary: &str) {
168    if !settings.is_object() {
169        *settings = json!({});
170    }
171    let Some(obj) = settings.as_object_mut() else {
172        unreachable!("settings was just set to an object");
173    };
174    let hooks = obj
175        .entry("hooks")
176        .or_insert_with(|| json!({}))
177        .as_object_mut()
178        .expect("hooks key was created above as an object");
179    let before_tool = hooks
180        .entry("BeforeTool")
181        .or_insert_with(|| json!([]))
182        .as_array_mut()
183        .expect("BeforeTool was created above as an array");
184    before_tool.push(hook_entry(binary));
185}
186
187#[cfg(test)]
188mod tests {
189    use super::*;
190    use crate::verdict::SafetyLevel;
191
192    fn target() -> GeminiTarget {
193        GeminiTarget
194    }
195
196    /// Verbatim shape from the Gemini CLI hooks reference. The bash
197    /// command lives in tool_input.command, matched by tool_name.
198    const GEMINI_DOCS_SAMPLE: &str = r#"{
199        "session_id": "abc123",
200        "transcript_path": "/Users/me/.gemini/transcripts/abc.json",
201        "cwd": "/Users/me/project",
202        "hook_event_name": "BeforeTool",
203        "timestamp": "2026-05-06T12:00:00Z",
204        "tool_name": "run_shell_command",
205        "tool_input": {"command": "ls -la"}
206    }"#;
207
208    #[test]
209    fn install_no_gemini_dir_skips() {
210        let dir = tempfile::tempdir().unwrap();
211        let outcome = target().install(dir.path()).unwrap();
212        assert!(matches!(outcome, InstallOutcome::Skipped { .. }));
213    }
214
215    #[test]
216    fn install_creates_settings_file() {
217        let dir = tempfile::tempdir().unwrap();
218        std::fs::create_dir(dir.path().join(".gemini")).unwrap();
219        let outcome = target().install(dir.path()).unwrap();
220        assert!(matches!(outcome, InstallOutcome::Installed { .. }));
221        let contents = std::fs::read_to_string(dir.path().join(".gemini/settings.json")).unwrap();
222        let settings: Value = serde_json::from_str(&contents).unwrap();
223        assert!(has_safe_chains_hook(&settings));
224    }
225
226    #[test]
227    fn install_uses_subcommand_invocation() {
228        let dir = tempfile::tempdir().unwrap();
229        std::fs::create_dir(dir.path().join(".gemini")).unwrap();
230        target().install(dir.path()).unwrap();
231        let contents = std::fs::read_to_string(dir.path().join(".gemini/settings.json")).unwrap();
232        assert!(contents.contains("safe-chains hook gemini"));
233    }
234
235    #[test]
236    fn install_idempotent() {
237        let dir = tempfile::tempdir().unwrap();
238        std::fs::create_dir(dir.path().join(".gemini")).unwrap();
239        target().install(dir.path()).unwrap();
240        let outcome = target().install(dir.path()).unwrap();
241        assert!(matches!(outcome, InstallOutcome::AlreadyConfigured { .. }));
242    }
243
244    #[test]
245    fn parse_input_extracts_command_from_tool_input() {
246        let parsed = GeminiHookFormat.parse_input(GEMINI_DOCS_SAMPLE).unwrap();
247        assert_eq!(parsed.command, "ls -la");
248        assert_eq!(parsed.cwd.as_deref(), Some("/Users/me/project"));
249    }
250
251    #[test]
252    fn parse_input_skips_non_shell_tool_names() {
253        // If the matcher in config doesn't narrow to run_shell_command,
254        // a non-shell tool may dispatch through. We return Err so the
255        // runtime exits silently — Gemini falls back to its own perms.
256        let stdin = r#"{"tool_name": "list_files", "tool_input": {"command": "ignored"}}"#;
257        assert!(GeminiHookFormat.parse_input(stdin).is_err());
258    }
259
260    #[test]
261    fn parse_input_rejects_garbage() {
262        assert!(GeminiHookFormat.parse_input("not json").is_err());
263        assert!(GeminiHookFormat.parse_input("{}").is_err());
264    }
265
266    #[test]
267    fn render_response_uses_decision_key_not_permission() {
268        // Gemini contract is `decision`, NOT `permission` /
269        // `permissionDecision`. Wiring this wrong silently fails the
270        // hook (warning, action proceeds) rather than blocking.
271        let r = GeminiHookFormat.render_response(Verdict::Allowed(SafetyLevel::Inert));
272        let v: Value = serde_json::from_str(&r.stdout).unwrap();
273        assert_eq!(v.get("decision").and_then(|s| s.as_str()), Some("allow"));
274        assert!(v.get("permission").is_none());
275        assert!(v.get("permissionDecision").is_none());
276    }
277
278    #[test]
279    fn render_response_includes_reason() {
280        let r = GeminiHookFormat.render_response(Verdict::Allowed(SafetyLevel::SafeWrite));
281        let v: Value = serde_json::from_str(&r.stdout).unwrap();
282        assert!(v.get("reason").and_then(|s| s.as_str()).is_some());
283    }
284
285    #[test]
286    fn render_response_deny_emits_empty_body() {
287        let r = GeminiHookFormat.render_response(Verdict::Denied);
288        assert_eq!(r.stdout, "");
289    }
290
291    #[test]
292    fn install_uses_correct_matcher() {
293        // Gemini's matcher is regex on tool name; `^run_shell_command$`
294        // is the canonical shell-tool matcher.
295        let dir = tempfile::tempdir().unwrap();
296        std::fs::create_dir(dir.path().join(".gemini")).unwrap();
297        target().install(dir.path()).unwrap();
298        let contents = std::fs::read_to_string(dir.path().join(".gemini/settings.json")).unwrap();
299        assert!(contents.contains("run_shell_command"));
300    }
301}