Skip to main content

safe_chains/targets/
mod.rs

1use std::path::{Path, PathBuf};
2
3use crate::verdict::{SafetyLevel, Verdict};
4
5pub mod agy;
6pub mod claude;
7pub mod codex;
8pub mod copilot;
9pub mod cursor;
10pub mod droid;
11pub mod gemini;
12pub mod grok;
13pub mod opencode;
14pub mod qwen;
15
16pub trait Target: Send + Sync {
17    fn name(&self) -> &'static str;
18
19    /// The harness's SHELL tool — the only tool this hook should decide about. Droid's is
20    /// `Execute`, Gemini's `run_shell_command`; most are `Bash`. Defaults to `Bash`, the common
21    /// case.
22    fn shell_tool_name(&self) -> &'static str {
23        "Bash"
24    }
25
26    /// A sample envelope this target's `parse_input` accepts, naming `tool`, or `None` when the
27    /// harness's envelope carries NO tool identifier.
28    ///
29    /// `None` is a researched claim, not a default: it says the envelope has no field naming the
30    /// tool, so the hook cannot tell a shell call from any other and must rely on its configured
31    /// matcher alone. `Some` obliges the target to abstain on a foreign tool —
32    /// `no_target_decides_on_a_foreign_tool` holds it to that in both directions.
33    ///
34    /// Test-only; each target knows its own envelope shape, and a generic one cannot stand in for
35    /// nine different schemas (Copilot nests `toolArgs` as a JSON STRING, Antigravity uses
36    /// `toolCall.args.commandLine`, Grok is camelCase).
37    #[cfg(test)]
38    fn sample_envelope(&self, _tool: &str, _command: &str) -> Option<String> {
39        None
40    }
41
42    fn display_name(&self) -> &'static str;
43
44    fn detect_paths(&self, home: &Path) -> Vec<PathBuf>;
45
46    fn install(&self, home: &Path) -> Result<InstallOutcome, String>;
47
48    fn hook_format(&self) -> Option<&dyn HookFormat> {
49        None
50    }
51}
52
53pub trait HookFormat: Send + Sync {
54    fn parse_input(&self, stdin: &str) -> Result<HookInput, ParseError>;
55
56    fn render_response(&self, verdict: Verdict) -> HookResponse;
57
58    /// The JSON pointer this harness reads its decision from.
59    ///
60    /// Deliberately has NO default: getting the field wrong fails SILENTLY — the harness ignores
61    /// the unknown key and falls back to its own permissions, so a mis-wired target still lets
62    /// commands run and looks like it works while never deciding anything. Requiring the
63    /// declaration means a new target cannot be added without stating its contract, and
64    /// `every_target_emits_its_decision_at_the_declared_field` checks every emission against it —
65    /// including that the decision does NOT appear at another harness's pointer, which is what a
66    /// copy-pasted target looks like.
67    ///
68    /// Note the leaf name alone is not the contract: Claude nests
69    /// `/hookSpecificOutput/permissionDecision` while Copilot uses a flat `/permissionDecision`.
70    fn decision_pointer(&self) -> &'static str;
71
72    /// Surface explanatory context to the model on a non-approval *without*
73    /// changing the permission decision (the command still flows through the
74    /// tool's normal approval path, and the user's own allowlist still applies).
75    ///
76    /// The default abstains silently — same as today's empty deny body. A target
77    /// overrides this only when its hook schema has a verified field for
78    /// injecting model-visible context without a permission decision.
79    fn render_context(&self, _context: &str) -> HookResponse {
80        HookResponse {
81            stdout: String::new(),
82            exit_code: 0,
83        }
84    }
85
86    /// How this harness's hook must handle a GATED command (one safe-chains does not auto-approve),
87    /// derived from its capabilities (`docs/design/harness-capability-model.md`):
88    /// - `Defer` — stay silent; the harness's own per-command human review is the check (Claude).
89    /// - `Deny` — veto it; the harness has no human review and no escalate (Codex).
90    /// - `Ask` — escalate to an in-the-moment human prompt (Antigravity's `ask`).
91    fn gated_policy(&self) -> GatedPolicy {
92        GatedPolicy::Defer
93    }
94
95    /// The hook output that VETOES a gated command, for a `Deny` harness. Default abstains (so a
96    /// stray call can't fail open). The shape must be exactly what the harness supports, or a
97    /// harness that "continues on malformed output" (e.g. Codex) fails open.
98    fn render_deny(&self, _reason: &str) -> HookResponse {
99        HookResponse {
100            stdout: String::new(),
101            exit_code: 0,
102        }
103    }
104
105    /// The hook output that ESCALATES a gated command to a human prompt, for an `Ask` harness.
106    /// Default abstains. (Antigravity fails CLOSED on a malformed/absent decision, so an Ask target
107    /// must always emit a valid decision.)
108    fn render_ask(&self, _reason: &str) -> HookResponse {
109        HookResponse {
110            stdout: String::new(),
111            exit_code: 0,
112        }
113    }
114}
115
116/// How a harness's hook handles a gated command — see `HookFormat::gated_policy`.
117#[derive(Clone, Copy, PartialEq, Eq, Debug)]
118pub enum GatedPolicy {
119    Defer,
120    Deny,
121    Ask,
122}
123
124#[derive(Debug)]
125pub struct ParseError {
126    pub message: String,
127}
128
129impl std::fmt::Display for ParseError {
130    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131        f.write_str(&self.message)
132    }
133}
134
135impl std::error::Error for ParseError {}
136
137pub struct HookInput {
138    pub command: String,
139    pub cwd: Option<String>,
140    /// The project root, when the harness supplies one (HP-19) — a `*_PROJECT_DIR` env var
141    /// for most, `workspace_roots` in the payload for cursor. Absent for codex/copilot.
142    pub root: Option<String>,
143    /// The harness's session/conversation id, when it supplies one (`session_id` for
144    /// Claude/Gemini/Qwen/Droid, `sessionId` for grok, `conversation_id` for cursor). It comes from
145    /// the harness's own envelope, so the agent cannot forge it — which is what makes it usable as
146    /// the anchor for recognizing the session's scratchpad (see `pathctx::session_scratchpad`).
147    pub session_id: Option<String>,
148}
149
150/// May a GRANT be emitted for this command?
151///
152/// A blank command classifies as `Allowed(Inert)` — an empty script really is inert — but rendering
153/// that as `permissionDecision: "allow"` asserts "every command in this chain is safe" about ZERO
154/// commands, and on the harnesses whose allow is authoritative it replaces the user's prompt.
155///
156/// The check lives HERE, next to the decision contract, rather than in the binary. It was in
157/// `main.rs` first: the shipped hook was safe, but `render_response` is public and knew nothing
158/// about blankness, so any second caller reintroduced the bug — and the integration guard passed
159/// only because it drives the binary. The `hook_envelope` fuzz target found exactly that by calling
160/// the format directly.
161pub fn may_grant(command: &str, verdict: crate::Verdict) -> bool {
162    verdict.is_allowed() && !command.trim().is_empty()
163}
164
165/// The decision for one parsed envelope: the response to emit, or `None` to abstain.
166///
167/// The single seam every caller goes through, so the blank-command rule cannot be bypassed by
168/// reaching for `render_response` directly.
169pub fn respond(format: &dyn HookFormat, command: &str, verdict: crate::Verdict) -> Option<HookResponse> {
170    may_grant(command, verdict).then(|| format.render_response(verdict))
171}
172
173/// Append `entry` to `settings[outer][event]`, creating the path when absent.
174///
175/// Refuses — rather than overwriting — when an existing key has the wrong TYPE. Four targets wrote
176/// this by hand as `entry(k).or_insert_with(…).as_object_mut().expect("created above as an
177/// object")`, and the message says why it looked safe: it reads as if the key had just been
178/// created. `or_insert_with` returns the EXISTING value, so a settings file carrying
179/// `"hooks": "something"` made `--setup` PANIC — and under `--auto-detect` that aborts the whole
180/// run, so every target after it goes uninstalled.
181///
182/// Erroring beats replacing. The value is the user's, an unreadable one usually means a
183/// hand-edit or a schema we don't know, and silently rewriting config we did not understand is
184/// not ours to do. `install` writes only on `Ok`, so the file is left untouched either way.
185pub(crate) fn append_hook_entry(
186    settings: &mut serde_json::Value,
187    outer: &str,
188    event: &str,
189    entry: serde_json::Value,
190) -> Result<(), String> {
191    use serde_json::json;
192    // Refuse a non-object ROOT rather than replace it, for the same reason a wrong-typed inner key
193    // is refused below: an unreadable value is usually a hand-edit or a schema we do not know, and
194    // rewriting config we did not understand is not ours to do.
195    //
196    // This only ever fires on a file that EXISTS and parses to something that is not an object
197    // (`[1,2,3]`, `"a string"`, `42`). Every caller turns a MISSING file into an empty object
198    // before reaching here, so refusing cannot break a first-time `--setup`.
199    if !settings.is_object() {
200        return Err(format!(
201            "the settings file is {}, expected an object. Leaving the file unchanged.",
202            json_kind(settings)
203        ));
204    }
205    let Some(obj) = settings.as_object_mut() else {
206        unreachable!("just checked it is an object");
207    };
208    let hooks = obj.entry(outer).or_insert_with(|| json!({}));
209    let Some(hooks) = hooks.as_object_mut() else {
210        return Err(format!(
211            "`{outer}` is {}, expected an object. Leaving the file unchanged.",
212            json_kind(&obj[outer])
213        ));
214    };
215    let slot = hooks.entry(event).or_insert_with(|| json!([]));
216    if !slot.is_array() {
217        return Err(format!(
218            "`{outer}.{event}` is {}, expected an array. Leaving the file unchanged.",
219            json_kind(slot)
220        ));
221    }
222    let Some(arr) = slot.as_array_mut() else {
223        unreachable!("just checked it is an array");
224    };
225    arr.push(entry);
226    Ok(())
227}
228
229fn json_kind(v: &serde_json::Value) -> &'static str {
230    match v {
231        serde_json::Value::Null => "null",
232        serde_json::Value::Bool(_) => "a boolean",
233        serde_json::Value::Number(_) => "a number",
234        serde_json::Value::String(_) => "a string",
235        serde_json::Value::Array(_) => "an array",
236        serde_json::Value::Object(_) => "an object",
237    }
238}
239
240/// Read a harness project-root env var from the hook process environment (set by the
241/// harness, not the agent's shell — see HARNESS-BEHAVIORS.md). Empty → `None`.
242pub(crate) fn env_root(var: &str) -> Option<String> {
243    std::env::var(var).ok().filter(|s| !s.is_empty())
244}
245
246pub struct HookResponse {
247    pub stdout: String,
248    pub exit_code: i32,
249}
250
251pub enum InstallOutcome {
252    Installed { path: PathBuf },
253    AlreadyConfigured { path: PathBuf },
254    Skipped { reason: String },
255}
256
257impl InstallOutcome {
258    pub fn message(&self, target_display: &str) -> String {
259        match self {
260            InstallOutcome::Installed { path } => {
261                format!("{target_display}: installed → {}", path.display())
262            }
263            InstallOutcome::AlreadyConfigured { path } => {
264                format!("{target_display}: already configured at {}", path.display())
265            }
266            InstallOutcome::Skipped { reason } => {
267                format!("{target_display}: skipped, {reason}")
268            }
269        }
270    }
271}
272
273pub fn registry() -> Vec<Box<dyn Target>> {
274    vec![
275        Box::new(claude::ClaudeTarget),
276        Box::new(codex::CodexTarget),
277        Box::new(agy::AntigravityTarget),
278        Box::new(cursor::CursorTarget),
279        Box::new(gemini::GeminiTarget),
280        Box::new(grok::GrokTarget),
281        Box::new(copilot::CopilotTarget),
282        Box::new(qwen::QwenTarget),
283        Box::new(droid::DroidTarget),
284        Box::new(opencode::OpenCodeTarget),
285    ]
286}
287
288pub fn find(name: &str) -> Option<Box<dyn Target>> {
289    registry().into_iter().find(|t| t.name() == name)
290}
291
292pub fn detect_installed(home: &Path) -> Vec<Box<dyn Target>> {
293    registry()
294        .into_iter()
295        .filter(|t| t.detect_paths(home).iter().any(|p| p.exists()))
296        .collect()
297}
298
299pub fn allow_reason(verdict: Verdict) -> &'static str {
300    match verdict {
301        Verdict::Allowed(SafetyLevel::SafeWrite) => {
302            "All commands in chain are safe utilities (includes file writes)"
303        }
304        Verdict::Allowed(SafetyLevel::SafeRead) => {
305            "All commands in chain are safe utilities (includes code execution)"
306        }
307        _ => "All commands in chain are safe utilities",
308    }
309}
310
311#[cfg(test)]
312mod append_hook_entry_tests {
313    use super::*;
314    use serde_json::json;
315
316    #[test]
317    fn creates_the_path_when_absent() {
318        let mut s = json!({});
319        append_hook_entry(&mut s, "hooks", "PreToolUse", json!({"matcher": "Bash"})).unwrap();
320        assert_eq!(s["hooks"]["PreToolUse"][0]["matcher"], "Bash");
321    }
322
323    #[test]
324    fn appends_beside_an_existing_entry() {
325        let mut s = json!({"hooks": {"PreToolUse": [{"matcher": "Other"}]}});
326        append_hook_entry(&mut s, "hooks", "PreToolUse", json!({"matcher": "Bash"})).unwrap();
327        let arr = s["hooks"]["PreToolUse"].as_array().unwrap();
328        assert_eq!(arr.len(), 2, "the user's existing hook must survive");
329        assert_eq!(arr[0]["matcher"], "Other");
330    }
331
332    /// The panic this replaced: `entry(k).or_insert_with(…).as_object_mut().expect(…)` reads as if
333    /// the key was just created, but `or_insert_with` returns the EXISTING value. A settings file
334    /// with `"hooks": "x"` crashed `--setup` — and under `--auto-detect` that aborted the whole run.
335    #[test]
336    fn refuses_a_wrong_typed_outer_key_without_panicking() {
337        for wrong in [json!("a string"), json!([1, 2]), json!(7), json!(null)] {
338            let mut s = json!({ "hooks": wrong });
339            let before = s.clone();
340            let err = append_hook_entry(&mut s, "hooks", "PreToolUse", json!({})).unwrap_err();
341            assert!(err.contains("expected an object"), "unhelpful error: {err}");
342            assert_eq!(s, before, "the user's value must be left alone, not replaced");
343        }
344    }
345
346    #[test]
347    fn refuses_a_wrong_typed_event_key_without_panicking() {
348        let mut s = json!({"hooks": {"PreToolUse": "a string"}});
349        let before = s.clone();
350        let err = append_hook_entry(&mut s, "hooks", "PreToolUse", json!({})).unwrap_err();
351        assert!(err.contains("expected an array"), "unhelpful error: {err}");
352        assert_eq!(s, before, "the user's value must be left alone, not replaced");
353    }
354
355    /// A non-object ROOT is refused, not replaced.
356    ///
357    /// This test previously asserted the opposite, on the reasoning that "a file whose ROOT is not
358    /// an object carries nothing to preserve". That is the same argument this module already
359    /// rejected one level in, where a wrong-typed `hooks` value is refused because an unreadable
360    /// value usually means a hand-edit or a schema we do not know. A root we cannot read is not
361    /// more disposable than a key we cannot read — it is less, since it is the whole file.
362    ///
363    /// Refusing is safe for a first-time `--setup`: every caller turns a MISSING file into an empty
364    /// object before reaching here, so this fires only for a file that exists and parses to a
365    /// non-object.
366    #[test]
367    fn refuses_a_non_object_root_without_replacing_it() {
368        for root in [json!("garbage"), json!([1, 2, 3]), json!(42), json!(null)] {
369            let mut s = root.clone();
370            let err = append_hook_entry(&mut s, "hooks", "PreToolUse", json!({"matcher": "Bash"}))
371                .expect_err("a non-object root must be refused");
372            assert!(err.contains("expected an object"), "unhelpful error: {err}");
373            assert_eq!(s, root, "the user's file must be left alone, not replaced");
374        }
375
376        // An object root is still the ordinary path.
377        let mut s = json!({"unrelated": true});
378        append_hook_entry(&mut s, "hooks", "PreToolUse", json!({"matcher": "Bash"})).unwrap();
379        assert_eq!(s["hooks"]["PreToolUse"][0]["matcher"], "Bash");
380        assert_eq!(s["unrelated"], true, "unrelated keys survive");
381    }
382}
383
384#[cfg(test)]
385mod tool_filter_tests {
386    use super::*;
387
388    /// No target decides on a tool that is not its shell tool.
389    ///
390    /// The hook is wired with a matcher (`Bash`, `Execute`, `run_shell_command`), so normally only
391    /// shell calls arrive. But a matcher is configuration: it can be hand-edited, and grok is
392    /// documented to auto-load `~/.claude/settings.json`, which hands Claude's hook a foreign
393    /// envelope. Deciding on a `Read`/`Write`/`Edit` call grants or vetoes a tool whose semantics
394    /// were never analysed — and for the ALLOW-capable targets that is a grant, issued on the
395    /// strength of a `command` field the tool does not even have. Four targets did exactly that.
396    ///
397    /// Driven by each target's OWN `sample_envelope`, because nine harnesses have nine schemas and
398    /// a generic probe silently fails to parse (which looks like a pass). A target whose envelope
399    /// carries no tool identifier returns `None` and is exempt — a researched claim, recorded per
400    /// target, not a default.
401    #[test]
402    fn no_target_decides_on_a_foreign_tool() {
403        let mut failures = Vec::new();
404        let mut checked = 0usize;
405        for target in registry() {
406            let Some(fmt) = target.hook_format() else { continue };
407            let Some(shell) = target.sample_envelope(target.shell_tool_name(), "ls") else {
408                continue; // envelope carries no tool identifier — cannot self-filter
409            };
410            let name = target.name();
411            // The shell tool must still parse, or "reject everything" would satisfy the negative
412            // half and look like a working filter.
413            if let Err(e) = fmt.parse_input(&shell) {
414                failures.push(format!(
415                    "{name}: rejected its own shell tool `{}`: {}",
416                    target.shell_tool_name(),
417                    e.message
418                ));
419            }
420            for foreign in ["Read", "Write", "Edit", "WebFetch"] {
421                let Some(env) = target.sample_envelope(foreign, "rm -rf /") else { continue };
422                checked += 1;
423                if fmt.parse_input(&env).is_ok() {
424                    failures.push(format!(
425                        "{name}: parsed a `{foreign}` envelope instead of abstaining"
426                    ));
427                }
428            }
429        }
430        assert!(checked > 0, "no target was probed — the guard is vacuous");
431        assert!(failures.is_empty(), "foreign-tool decisions:\n{}", failures.join("\n"));
432    }
433}