fix(fermata): a path call's leading ~/ is expanded, so Grep with path ~/.ssh/id_rsa meets the gate
This commit is contained in:
+4
-2
@@ -1,6 +1,6 @@
|
|||||||
use clap::{Parser, Subcommand, ValueEnum};
|
use clap::{Parser, Subcommand, ValueEnum};
|
||||||
use dirigent_fermata::core::secrets::{config::ParseErrorAction, SecretsConfig};
|
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 dirigent_fermata::harness::HookEvent;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -265,7 +265,9 @@ fn run_pre_tool_use(
|
|||||||
let session = session_dir(call.cwd.as_deref());
|
let session = session_dir(call.cwd.as_deref());
|
||||||
let decision = match &call.op {
|
let decision = match &call.op {
|
||||||
ToolOp::Path { path, kind } => {
|
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 {
|
let op = match kind {
|
||||||
PathKind::Read => Op::Read,
|
PathKind::Read => Op::Read,
|
||||||
PathKind::Write => Op::Write,
|
PathKind::Write => Op::Write,
|
||||||
|
|||||||
+12
-3
@@ -66,6 +66,17 @@ pub fn absolute(path: &Path, base: &Path) -> PathBuf {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The home directory the hook runs under.
|
||||||
|
pub fn home_dir() -> Option<PathBuf> {
|
||||||
|
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)]
|
#[derive(Default)]
|
||||||
struct Splitter {
|
struct Splitter {
|
||||||
segments: Vec<Vec<String>>,
|
segments: Vec<Vec<String>>,
|
||||||
@@ -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
|
/// 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 `.`.
|
/// a shell, a dotfile only when it starts with `.`.
|
||||||
fn word_paths(word: &str, new: bool, cwd: &Path, home: Option<&Path>) -> Vec<PathBuf> {
|
fn word_paths(word: &str, new: bool, cwd: &Path, home: Option<&Path>) -> Vec<PathBuf> {
|
||||||
let expanded = home
|
let expanded = expand_home(word, 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 (dir, name) = expanded.rsplit_once('/').unwrap_or(("", &expanded));
|
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)) {
|
if let Some(glob) = Glob::new(name).ok().filter(|_| name.contains(GLOB_CHARS) && !dir.contains(GLOB_CHARS)) {
|
||||||
let matcher = glob.compile_matcher();
|
let matcher = glob.compile_matcher();
|
||||||
|
|||||||
+2
-3
@@ -1,7 +1,7 @@
|
|||||||
use crate::core::botignore::{BotignoreError, BotignoreSet};
|
use crate::core::botignore::{BotignoreError, BotignoreSet};
|
||||||
use crate::core::decision::{Decision, Reason, Rule};
|
use crate::core::decision::{Decision, Reason, Rule};
|
||||||
use crate::core::op::Op;
|
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::config::{GuardAction, PolicyConfig, SecretsConfig};
|
||||||
use crate::core::secrets::{unmaskable_reason, FileFormat, SecretFileSet};
|
use crate::core::secrets::{unmaskable_reason, FileFormat, SecretFileSet};
|
||||||
use std::path::{Path, PathBuf};
|
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,
|
/// 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.
|
/// 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<Decision, PolicyError> {
|
pub fn check_command_paths(&self, command: &str, dialect: Dialect, cwd: &Path) -> Result<Decision, PolicyError> {
|
||||||
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_dir().as_deref()) {
|
||||||
for named in command_paths(command, dialect, cwd, home.as_deref()) {
|
|
||||||
let decision = match named.verb {
|
let decision = match named.verb {
|
||||||
Verb::Prints => self.check(Op::Read, &named.path)?,
|
Verb::Prints => self.check(Op::Read, &named.path)?,
|
||||||
Verb::Other => self.check_path_rules(Op::Read, &named.path)?,
|
Verb::Other => self.check_path_rules(Op::Read, &named.path)?,
|
||||||
|
|||||||
@@ -221,6 +221,18 @@ fn a_command_naming_a_file_in_home_meets_the_read_gate() {
|
|||||||
assert_eq!(call("ls ~/.ssh/id_rsa"), json!({}));
|
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.
|
/// The session's directory resolves the words, not this process's.
|
||||||
#[test]
|
#[test]
|
||||||
fn the_payloads_cwd_resolves_a_commands_words() {
|
fn the_payloads_cwd_resolves_a_commands_words() {
|
||||||
|
|||||||
@@ -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`.
|
/// The default list names `terraform.tfvars` and `*.auto.tfvars`, no other `*.tfvars`.
|
||||||
#[test]
|
#[test]
|
||||||
fn a_tfvars_file_off_the_list_is_not_masked() {
|
fn a_tfvars_file_off_the_list_is_not_masked() {
|
||||||
|
|||||||
Reference in New Issue
Block a user