Skip to main content

safe_chains/targets/
copilot.rs

1use std::path::{Path, PathBuf};
2
3use serde::Deserialize;
4use serde_json::{Value, json};
5
6use super::{HookFormat, HookInput, HookResponse, InstallOutcome, ParseError, Target, allow_reason};
7use crate::verdict::Verdict;
8
9pub struct CopilotTarget;
10
11impl Target for CopilotTarget {
12    fn name(&self) -> &'static str {
13        "copilot"
14    }
15
16    fn display_name(&self) -> &'static str {
17        "GitHub Copilot CLI"
18    }
19
20    fn detect_paths(&self, home: &Path) -> Vec<PathBuf> {
21        // Copilot reads hooks from per-repo `<repo>/.github/hooks/*.json` OR the user-global
22        // `~/.copilot/hooks/` (or `$COPILOT_HOME/hooks/`); upstream's hooks-configuration docs say
23        // both are loaded and all entries run. We install to the user-global dir; a per-repo install
24        // would need a project path. The live drive (v1.0.71) fired the per-repo `.github/hooks/`
25        // config and confirmed `~/.copilot/` is the real config root — the earlier `~/.github/hooks/`
26        // guess was wrong and never fired.
27        vec![home.join(".copilot").join("hooks")]
28    }
29
30    fn install(&self, home: &Path) -> Result<InstallOutcome, String> {
31        let dir = home.join(".copilot").join("hooks");
32        if let Err(e) = std::fs::create_dir_all(&dir) {
33            return Err(format!("Could not create {}: {e}", dir.display()));
34        }
35
36        let path = dir.join("safe-chains.json");
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 settings: Value = serde_json::from_str(&contents)
42                .map_err(|e| format!("Could not parse {}: {e}", path.display()))?;
43            if has_safe_chains_hook(&settings) {
44                return Ok(InstallOutcome::AlreadyConfigured { path });
45            }
46        }
47
48        let settings = build_settings();
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    }
54
55    fn hook_format(&self) -> Option<&dyn HookFormat> {
56        Some(&CopilotHookFormat)
57    }
58}
59
60struct CopilotHookFormat;
61
62#[derive(Deserialize)]
63struct CopilotHookEnvelope {
64    #[serde(default)]
65    #[serde(rename = "toolName")]
66    tool_name: Option<String>,
67    #[serde(default)]
68    #[serde(rename = "toolArgs")]
69    tool_args: Option<String>,
70    #[serde(default)]
71    cwd: Option<String>,
72}
73
74#[derive(Deserialize)]
75struct CopilotToolArgs {
76    #[serde(default)]
77    command: Option<String>,
78}
79
80impl HookFormat for CopilotHookFormat {
81    fn parse_input(&self, stdin: &str) -> Result<HookInput, ParseError> {
82        // Copilot's quirk: toolArgs is a JSON-encoded *string*, not a
83        // nested object. We must parse the outer envelope, then parse
84        // the toolArgs string a second time to recover {command}.
85        let envelope: CopilotHookEnvelope =
86            serde_json::from_str(stdin).map_err(|e| ParseError {
87                message: e.to_string(),
88            })?;
89
90        // The hook fires for every tool by default — Copilot's config
91        // has no matcher. Self-filter to the bash tool here; for other
92        // tools, return Err so the runtime exits silently and Copilot
93        // falls back to its own permission rules.
94        let is_bash_tool = envelope
95            .tool_name
96            .as_deref()
97            .is_some_and(|n| n == "bash");
98        if !is_bash_tool {
99            return Err(ParseError {
100                message: format!(
101                    "not a bash tool: {:?}",
102                    envelope.tool_name.as_deref().unwrap_or("<missing>")
103                ),
104            });
105        }
106
107        let raw_args = envelope.tool_args.unwrap_or_default();
108        let inner: CopilotToolArgs =
109            serde_json::from_str(&raw_args).map_err(|e| ParseError {
110                message: format!("toolArgs not a parseable JSON string: {e}"),
111            })?;
112        Ok(HookInput {
113            command: inner.command.unwrap_or_default(),
114            cwd: envelope.cwd,
115            root: None, // copilot sends cwd but no documented project root
116        })
117    }
118
119    fn render_response(&self, verdict: Verdict) -> HookResponse {
120        // Copilot honors BOTH `allow` and `deny` — VERIFIED LIVE (v1.0.71): a hook `allow` auto-runs
121        // the command and even suppresses copilot's own directory-access prompt; a hook `deny` blocks
122        // it and surfaces our `permissionDecisionReason`. (The earlier "only deny is processed" note
123        // is stale.) So a safe command gets `allow` (a real grant) and a gated one ABSTAINS (empty),
124        // deferring to copilot's own per-command prompt — copilot has interactive review, so we don't
125        // hard-deny. Copilot's response is a FLAT object (no `hookSpecificOutput` wrapper).
126        if verdict.is_allowed() {
127            let reason = allow_reason(verdict);
128            let body = json!({
129                "permissionDecision": "allow",
130                "permissionDecisionReason": reason,
131            });
132            HookResponse {
133                stdout: serde_json::to_string(&body).unwrap_or_default(),
134                exit_code: 0,
135            }
136        } else {
137            HookResponse {
138                stdout: String::new(),
139                exit_code: 0,
140            }
141        }
142    }
143}
144
145fn build_settings() -> Value {
146    // Copilot's config: flat object with `version` and `hooks.preToolUse[]`.
147    // No `matcher` in entries — script self-filters on `toolName`. Field
148    // name is `bash` (the script path), NOT `command`.
149    let resolved = std::env::current_exe()
150        .ok()
151        .and_then(|p| p.canonicalize().ok())
152        .map(|p| format!("{} hook copilot", p.display()))
153        .unwrap_or_else(|| "safe-chains hook copilot".to_string());
154    json!({
155        "version": 1,
156        "hooks": {
157            "preToolUse": [
158                {
159                    "type": "command",
160                    "bash": resolved,
161                    "comment": "safe-chains: validate every Bash tool call before it runs.",
162                    "timeoutSec": 60,
163                }
164            ]
165        }
166    })
167}
168
169fn has_safe_chains_hook(settings: &Value) -> bool {
170    settings
171        .pointer("/hooks/preToolUse")
172        .and_then(|arr| arr.as_array())
173        .is_some_and(|entries| {
174            entries.iter().any(|entry| {
175                entry
176                    .get("bash")
177                    .and_then(|c| c.as_str())
178                    .is_some_and(|cmd| cmd.contains("safe-chains"))
179            })
180        })
181}
182
183#[cfg(test)]
184mod tests {
185    use super::*;
186    use crate::verdict::SafetyLevel;
187
188    fn target() -> CopilotTarget {
189        CopilotTarget
190    }
191
192    /// Verbatim shape from docs.github.com/en/copilot/reference/
193    /// hooks-configuration. The `toolArgs` field is a JSON-encoded
194    /// STRING, not a nested object — must be parsed twice.
195    const COPILOT_DOCS_SAMPLE: &str = r#"{
196        "timestamp": 1704614600000,
197        "cwd": "/path/to/project",
198        "toolName": "bash",
199        "toolArgs": "{\"command\":\"ls -la\",\"description\":\"list files\"}"
200    }"#;
201
202    #[test]
203    fn install_creates_hooks_file() {
204        let dir = tempfile::tempdir().unwrap();
205        let outcome = target().install(dir.path()).unwrap();
206        assert!(matches!(outcome, InstallOutcome::Installed { .. }));
207        let path = dir.path().join(".copilot/hooks/safe-chains.json");
208        assert!(path.exists());
209        let contents = std::fs::read_to_string(&path).unwrap();
210        let settings: Value = serde_json::from_str(&contents).unwrap();
211        assert!(has_safe_chains_hook(&settings));
212    }
213
214    #[test]
215    fn install_uses_bash_field_not_command() {
216        // Copilot's script-path field is `bash`, not `command`. Wiring
217        // this to `command` would silently mis-configure the hook.
218        let dir = tempfile::tempdir().unwrap();
219        target().install(dir.path()).unwrap();
220        let contents = std::fs::read_to_string(
221            dir.path().join(".copilot/hooks/safe-chains.json"),
222        )
223        .unwrap();
224        let settings: Value = serde_json::from_str(&contents).unwrap();
225        let entry = settings.pointer("/hooks/preToolUse/0").unwrap();
226        assert!(entry.get("bash").is_some(), "must use `bash` key");
227        assert!(entry.get("command").is_none(), "must NOT use `command` key");
228    }
229
230    #[test]
231    fn install_uses_subcommand_invocation() {
232        let dir = tempfile::tempdir().unwrap();
233        target().install(dir.path()).unwrap();
234        let contents = std::fs::read_to_string(
235            dir.path().join(".copilot/hooks/safe-chains.json"),
236        )
237        .unwrap();
238        assert!(contents.contains("hook copilot"));
239    }
240
241    #[test]
242    fn install_idempotent() {
243        let dir = tempfile::tempdir().unwrap();
244        target().install(dir.path()).unwrap();
245        let outcome = target().install(dir.path()).unwrap();
246        assert!(matches!(outcome, InstallOutcome::AlreadyConfigured { .. }));
247    }
248
249    #[test]
250    fn parse_input_double_decodes_tool_args() {
251        // The headline quirk: outer JSON, then `toolArgs` is itself a
252        // JSON-encoded string that must be parsed again to recover the
253        // bash command. Tested with the verbatim docs payload.
254        let parsed = CopilotHookFormat.parse_input(COPILOT_DOCS_SAMPLE).unwrap();
255        assert_eq!(parsed.command, "ls -la");
256        assert_eq!(parsed.cwd.as_deref(), Some("/path/to/project"));
257    }
258
259    #[test]
260    fn parse_input_skips_non_bash_tools() {
261        // No matcher in Copilot's config — every tool dispatches
262        // through. Self-filter to "bash"; for others, return Err so
263        // the runtime exits silently and Copilot's own perms apply.
264        let stdin = r#"{
265            "timestamp": 1,
266            "cwd": "/p",
267            "toolName": "edit",
268            "toolArgs": "{\"path\":\"x\"}"
269        }"#;
270        assert!(CopilotHookFormat.parse_input(stdin).is_err());
271    }
272
273    #[test]
274    fn parse_input_rejects_garbage() {
275        assert!(CopilotHookFormat.parse_input("not json").is_err());
276    }
277
278    #[test]
279    fn parse_input_rejects_unparseable_tool_args() {
280        let stdin = r#"{"toolName": "bash", "toolArgs": "not-json"}"#;
281        let result = CopilotHookFormat.parse_input(stdin);
282        assert!(result.is_err());
283    }
284
285    #[test]
286    fn render_response_emits_flat_object_no_wrapper() {
287        // Copilot uses a FLAT response object — no
288        // hookSpecificOutput wrapper key, unlike Claude/Codex/Qwen/
289        // Droid. Wrapping would be silently rejected.
290        let r = CopilotHookFormat.render_response(Verdict::Allowed(SafetyLevel::Inert));
291        let v: Value = serde_json::from_str(&r.stdout).unwrap();
292        assert_eq!(
293            v.get("permissionDecision").and_then(|s| s.as_str()),
294            Some("allow"),
295        );
296        assert!(
297            v.get("hookSpecificOutput").is_none(),
298            "must NOT wrap in hookSpecificOutput",
299        );
300    }
301
302    #[test]
303    fn render_response_includes_reason() {
304        let r = CopilotHookFormat.render_response(Verdict::Allowed(SafetyLevel::SafeWrite));
305        let v: Value = serde_json::from_str(&r.stdout).unwrap();
306        assert!(
307            v.get("permissionDecisionReason")
308                .and_then(|s| s.as_str())
309                .is_some()
310        );
311    }
312
313    #[test]
314    fn render_response_deny_emits_empty_body() {
315        let r = CopilotHookFormat.render_response(Verdict::Denied);
316        assert_eq!(r.stdout, "");
317    }
318}