Files
fermata/tests/cli_hook_paths.rs
g4borg 11328da30f fix(fermata): vendor/ covers what lies below it; relative paths and links meet the rules; the payload's cwd decides
.botignore matches a path or any parent, with the path tried first, so
!vendor/keep beats vendor/. Policy::check takes a relative path against the
root and also checks the path as the file system resolves it, spelled from the
root as given, so a link or a root behind a link hides nothing. session_dir: the
payload's cwd if absolute, else the process directory, for both hooks; fermata
check and the hook make paths absolute.
2026-11-07 00:59:04 +01:00

137 lines
5.3 KiB
Rust

//! The session's directory (the payload's `cwd`) decides which project a call is in,
//! not the directory the hook was started in: Claude Code can spawn a hook elsewhere
//! (`--project-config-root`, remote mode, a deleted directory). All values are fake.
use assert_cmd::Command;
use serde_json::{json, Value};
use std::fs;
use std::path::Path;
use tempfile::TempDir;
const SESSION_VALUE: &str = "fake-value-of-the-session";
const PROCESS_VALUE: &str = "fake-value-of-the-process";
/// A project with a `.env` value of its own and a command rule
/// that denies a word of its own.
fn project(word: &str, value: &str, botignore: &str) -> TempDir {
let dir = tempfile::tempdir().unwrap();
fs::create_dir(dir.path().join(".git")).unwrap();
fs::write(dir.path().join(".botignore"), botignore).unwrap();
fs::write(dir.path().join(".botsecrets"), format!("[policy.bash]\ndeny = [\"{word}\"]\n")).unwrap();
fs::write(dir.path().join(".env"), format!("API_TOKEN={value}\n")).unwrap();
dir
}
struct Setup {
process: TempDir,
session: TempDir,
home: TempDir,
}
/// The hook runs in `process`. Only `session` blocks `.env` and `vendor/`, and each
/// denies its own word.
fn setup() -> Setup {
Setup {
process: project("process-word", PROCESS_VALUE, ""),
session: project("session-word", SESSION_VALUE, ".env\nvendor/\n"),
home: tempfile::tempdir().unwrap(),
}
}
impl Setup {
fn hook(&self, event: &str, mut payload: Value, cwd: Option<&Path>) -> Value {
if let Some(cwd) = cwd {
payload["cwd"] = json!(cwd);
}
let output = Command::cargo_bin("fermata")
.unwrap()
.args(["hook", "--event", event, "--harness", "claude"])
.current_dir(self.process.path())
.env("HOME", self.home.path())
.write_stdin(payload.to_string())
.assert()
.success()
.get_output()
.clone();
serde_json::from_slice(&output.stdout).unwrap()
}
fn bash(&self, command: &str, cwd: Option<&Path>) -> Value {
self.hook("pre-tool-use", json!({"tool_name": "Bash", "tool_input": {"command": command}}), cwd)
}
fn pre_path(&self, tool: &str, file: &str, cwd: Option<&Path>) -> Value {
self.hook("pre-tool-use", json!({"tool_name": tool, "tool_input": {"file_path": file}}), cwd)
}
/// What the model gets of an output holding both projects' values.
fn post(&self, cwd: Option<&Path>) -> String {
let text = format!("{SESSION_VALUE} {PROCESS_VALUE}");
let out = self.hook("post-tool-use", json!({"tool_name": "Bash", "tool_input": {"command": "x"}, "tool_response": text}), cwd);
out["hookSpecificOutput"]["updatedToolOutput"].as_str().unwrap_or(&text).to_string()
}
}
fn is_denied(out: &Value) -> bool {
out["hookSpecificOutput"]["permissionDecision"] == "deny"
}
#[test]
fn the_payloads_directory_decides_a_command() {
let s = setup();
let session = Some(s.session.path());
assert!(is_denied(&s.bash("run session-word", session)));
assert_eq!(s.bash("run process-word", session), json!({}));
}
#[test]
fn the_payloads_directory_decides_what_post_tool_use_masks() {
let s = setup();
assert_eq!(s.post(Some(s.session.path())), format!("***** {PROCESS_VALUE}"));
}
#[test]
fn a_relative_file_path_is_taken_against_the_payloads_directory() {
let s = setup();
let session = Some(s.session.path());
assert!(is_denied(&s.pre_path("Read", ".env", session)));
assert!(is_denied(&s.pre_path("Read", "./.env", session)));
assert!(is_denied(&s.pre_path("Write", "vendor/a", session)));
assert!(is_denied(&s.pre_path("Edit", "vendor/b/c", session)));
assert_eq!(s.pre_path("Read", "src/a.rs", session), json!({}));
}
/// A subdirectory and a deleted directory are still in their project.
#[test]
fn a_payloads_directory_that_is_gone_is_still_in_its_project() {
let s = setup();
let gone = s.session.path().join("gone/sub");
assert!(is_denied(&s.bash("run session-word", Some(&gone))));
assert_eq!(s.post(Some(&gone)), format!("***** {PROCESS_VALUE}"));
assert!(is_denied(&s.pre_path("Read", "../../.env", Some(&gone))));
}
/// No `cwd` and a `cwd` that is not absolute both leave the process directory, as
/// every existing test and an older Claude Code do.
#[test]
fn without_a_usable_directory_the_process_directory_decides() {
let s = setup();
for cwd in [None, Some(Path::new("relative/dir"))] {
assert!(is_denied(&s.bash("run process-word", cwd)), "{cwd:?}");
assert_eq!(s.bash("run session-word", cwd), json!({}), "{cwd:?}");
assert_eq!(s.post(cwd), format!("{SESSION_VALUE} *****"), "{cwd:?}");
assert_eq!(s.pre_path("Read", ".env", cwd), json!({}), "{cwd:?}: the process project has no such rule");
}
fs::write(s.process.path().join(".botignore"), ".env\n").unwrap();
assert!(is_denied(&s.pre_path("Read", ".env", None)));
}
/// An absolute path decides its own project, wherever the session is.
#[test]
fn an_absolute_file_path_stays_in_its_own_project() {
let s = setup();
let env = s.session.path().join(".env");
assert!(is_denied(&s.pre_path("Read", env.to_str().unwrap(), None)));
assert!(is_denied(&s.pre_path("Read", s.session.path().join("x/../.env").to_str().unwrap(), Some(s.process.path()))));
}