From 33e330a4eb7ece85d95330987692c6343f52abc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Sat, 7 Nov 2026 09:02:27 +0100 Subject: [PATCH] fix(fermata): a path call's leading ~/ is expanded, so Grep with path ~/.ssh/id_rsa meets the gate --- src/bin/fermata.rs | 6 ++++-- src/core/paths.rs | 15 ++++++++++++--- src/core/policy.rs | 5 ++--- tests/cli_command_paths.rs | 12 ++++++++++++ tests/known_limits.rs | 9 --------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/bin/fermata.rs b/src/bin/fermata.rs index 95b07cc..c933be4 100644 --- a/src/bin/fermata.rs +++ b/src/bin/fermata.rs @@ -1,6 +1,6 @@ use clap::{Parser, Subcommand, ValueEnum}; use dirigent_fermata::core::secrets::{config::ParseErrorAction, SecretsConfig}; -use dirigent_fermata::core::{paths::absolute, project::find_project_root, Decision, Op, Policy, Reason}; +use dirigent_fermata::core::{paths::{absolute, expand_home, home_dir}, project::find_project_root, Decision, Op, Policy, Reason}; use dirigent_fermata::harness::HookEvent; use std::io::{Read, Write}; use std::path::{Path, PathBuf}; @@ -265,7 +265,9 @@ fn run_pre_tool_use( let session = session_dir(call.cwd.as_deref()); let decision = match &call.op { ToolOp::Path { path, kind } => { - let path = session.as_deref().map_or_else(|| path.clone(), |dir| absolute(path, dir)); + // Claude Code expands `~` for its file tools before the hook, and not for Grep's `path`. + let path = PathBuf::from(expand_home(&path.to_string_lossy(), home_dir().as_deref())); + let path = session.as_deref().map_or_else(|| path.clone(), |dir| absolute(&path, dir)); let op = match kind { PathKind::Read => Op::Read, PathKind::Write => Op::Write, diff --git a/src/core/paths.rs b/src/core/paths.rs index 351fcdb..71991df 100644 --- a/src/core/paths.rs +++ b/src/core/paths.rs @@ -66,6 +66,17 @@ pub fn absolute(path: &Path, base: &Path) -> PathBuf { out } +/// The home directory the hook runs under. +pub fn home_dir() -> Option { + std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(PathBuf::from) +} + +/// `word` with a leading `~/`, `$HOME/` or `${HOME}/` expanded. +pub fn expand_home(word: &str, home: Option<&Path>) -> String { + home.and_then(|home| Some(home.join(["~/", "$HOME/", "${HOME}/"].iter().find_map(|p| word.strip_prefix(p))?))) + .map_or_else(|| word.to_string(), |path| path.to_string_lossy().into_owned()) +} + #[derive(Default)] struct Splitter { segments: Vec>, @@ -302,9 +313,7 @@ fn named_words<'a>(args: &[&'a str]) -> (Verb, Vec<&'a str>, Vec<&'a str>, Optio /// count only as a real file. A glob reaches the files of its one directory and, as in /// a shell, a dotfile only when it starts with `.`. fn word_paths(word: &str, new: bool, cwd: &Path, home: Option<&Path>) -> Vec { - let expanded = home - .and_then(|home| Some(home.join(["~/", "$HOME/", "${HOME}/"].iter().find_map(|p| word.strip_prefix(p))?))) - .map_or_else(|| word.to_string(), |path| path.to_string_lossy().into_owned()); + let expanded = expand_home(word, home); let (dir, name) = expanded.rsplit_once('/').unwrap_or(("", &expanded)); if let Some(glob) = Glob::new(name).ok().filter(|_| name.contains(GLOB_CHARS) && !dir.contains(GLOB_CHARS)) { let matcher = glob.compile_matcher(); diff --git a/src/core/policy.rs b/src/core/policy.rs index 9503aca..138a7b4 100644 --- a/src/core/policy.rs +++ b/src/core/policy.rs @@ -1,7 +1,7 @@ use crate::core::botignore::{BotignoreError, BotignoreSet}; use crate::core::decision::{Decision, Reason, Rule}; use crate::core::op::Op; -use crate::core::paths::{absolute, command_paths, Dialect, Verb}; +use crate::core::paths::{absolute, command_paths, home_dir, Dialect, Verb}; use crate::core::secrets::config::{GuardAction, PolicyConfig, SecretsConfig}; use crate::core::secrets::{unmaskable_reason, FileFormat, SecretFileSet}; use std::path::{Path, PathBuf}; @@ -105,8 +105,7 @@ impl Policy { /// names them, the read rules for any other, the read gate for one that prints them, /// and the write rules and the read rules for one that writes them. pub fn check_command_paths(&self, command: &str, dialect: Dialect, cwd: &Path) -> Result { - let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(PathBuf::from); - for named in command_paths(command, dialect, cwd, home.as_deref()) { + for named in command_paths(command, dialect, cwd, home_dir().as_deref()) { let decision = match named.verb { Verb::Prints => self.check(Op::Read, &named.path)?, Verb::Other => self.check_path_rules(Op::Read, &named.path)?, diff --git a/tests/cli_command_paths.rs b/tests/cli_command_paths.rs index 5bef38e..b46faf8 100644 --- a/tests/cli_command_paths.rs +++ b/tests/cli_command_paths.rs @@ -221,6 +221,18 @@ fn a_command_naming_a_file_in_home_meets_the_read_gate() { assert_eq!(call("ls ~/.ssh/id_rsa"), json!({})); } +/// A path call's leading `~/` is the same home. +#[test] +fn a_path_call_naming_a_file_in_home_meets_the_read_gate() { + let p = project(&[]); + let home = tempfile::tempdir().unwrap(); + write(home.path(), &[(".ssh/id_rsa", PEM), (".ssh/config", "Host x\n")]); + let call = |tool: &str, input: Value| hook_in(p.path(), home.path(), json!({"tool_name": tool, "tool_input": input})); + assert_eq!(verdict(&call("Grep", json!({"pattern": "x", "path": "~/.ssh/id_rsa"}))), "deny"); + assert_eq!(verdict(&call("Read", json!({"file_path": "~/.ssh/id_rsa"}))), "deny"); + assert_eq!(call("Grep", json!({"pattern": "x", "path": "~/.ssh/config"})), json!({})); +} + /// The session's directory resolves the words, not this process's. #[test] fn the_payloads_cwd_resolves_a_commands_words() { diff --git a/tests/known_limits.rs b/tests/known_limits.rs index 27faa32..fabb556 100644 --- a/tests/known_limits.rs +++ b/tests/known_limits.rs @@ -466,15 +466,6 @@ fn a_comment_or_an_attribute_line_beside_a_certificate_passes() { } } -/// Only `~` in a command is expanded. In Grep's `path` it stays text, so the file is not found. -#[test] -fn a_tilde_in_a_greps_path_is_not_expanded() { - let p = project(&[]); - put(p.home.path(), &[(".ssh/id_rsa", b"fake key text\n")]); - assert!(denied(&p.pre("Grep", json!({"pattern": "x", "path": p.home.path().join(".ssh/id_rsa")}))), "control"); - assert!(passes(&p.pre("Grep", json!({"pattern": "x", "path": "~/.ssh/id_rsa"})))); -} - /// The default list names `terraform.tfvars` and `*.auto.tfvars`, no other `*.tfvars`. #[test] fn a_tfvars_file_off_the_list_is_not_masked() {