safe-chains 0.243.0

Auto-allow safe bash commands in agentic coding tools
Documentation
use crate::verdict::{SafetyLevel, Verdict};

pub(crate) fn names_command_search(name: &str) -> bool {
    super::rule_of(name).is_some_and(|rule| rule.shape == super::Shape::CommandSearch)
}

pub(super) fn command_search_verdict(name: &str, appends: bool, value: &str) -> Verdict {
    let value = if name == "path" { value.strip_prefix("${path}").unwrap_or(value) } else { value };
    if appends && name == "PATH" {
        return search_path_verdict(&format!("$PATH{value}"));
    }
    search_path_verdict(value)
}

fn search_path_verdict(value: &str) -> Verdict {
    value.split(':').map(element_verdict).fold(Verdict::Allowed(SafetyLevel::Inert), Verdict::combine)
}

fn element_verdict(dir: &str) -> Verdict {
    if matches!(dir, "$PATH" | "${PATH}") {
        return Verdict::Allowed(SafetyLevel::Inert);
    }
    if !dir.starts_with('/') {
        return Verdict::Denied;
    }
    if crate::handlers::direct_script::trusted_command_path(&format!("{}/x", dir.trim_end_matches('/'))) {
        return Verdict::Allowed(SafetyLevel::Inert);
    }
    crate::engine::resolve::execute_file_verdict(dir)
}

#[cfg(test)]
#[path = "command_search_tests.rs"]
mod tests;