safe-chains 0.243.0

Auto-allow safe bash commands in agentic coding tools
Documentation
//! Property tests for `workspace_overreach`, the reason a denied command is given.

use proptest::prelude::*;

use crate::pathctx::PathCtx;
use crate::{ReachReason, workspace_overreach};

proptest! {
    #![proptest_config(ProptestConfig::with_cases(256))]

    /// A temp path is reported as a foreign temp exactly when it is not this session's scratchpad.
    ///
    /// The same file under every temp-root spelling, once in an anonymous directory and once under
    /// the session id: the first must be named `ForeignTemp` (its remedy is a grant), the second
    /// must not be reported at all, since the scratchpad is admitted. Before this, inverting the
    /// scratchpad test in `workspace_overreach` left every test green.
    #[test]
    fn only_a_foreign_temp_path_is_reported_as_foreign_temp(
        root in proptest::sample::select(vec!["/tmp", "/private/tmp", "/var/tmp", "/private/var/tmp"]),
        dir in "[a-z]{1,8}",
        file in "[a-z]{1,8}\\.(sh|txt|py)",
        verb in proptest::sample::select(vec!["cat", "bash", "head", "rm"]),
    ) {
        const SID: &str = "7676dbc5-a265-43b3-a0f8-49666792bd9b";
        let _g = crate::pathctx::enter(PathCtx {
            cwd: Some("/work".into()),
            root: Some("/work".into()),
            session_id: Some(SID.into()),
        });
        let foreign = format!("{root}/{dir}/{file}");
        let reach = workspace_overreach(&format!("{verb} {foreign}"));
        prop_assert_eq!(reach, Some((foreign.clone(), ReachReason::ForeignTemp)), "{}", foreign);

        let scratch = format!("{root}/{dir}/{SID}/scratchpad/{file}");
        let reach = workspace_overreach(&format!("{verb} {scratch}"));
        prop_assert_eq!(reach, None, "{}", scratch);
    }

    /// A path that reaches a command through an earlier statement is reported as that path.
    ///
    /// The same command with the path written in, and with its directory held in a variable bound
    /// by an earlier statement: both reach the same place, so both must name it and give the same
    /// reason. Read one statement at a time, the second named `$P/…`, which is no place at all.
    #[test]
    fn a_path_reached_through_an_earlier_statement_is_reported_as_itself(
        root in proptest::sample::select(vec!["/opt", "/srv", "/Users/someone", "/home/someone", "/tmp", "/etc"]),
        dir in "[a-z]{1,8}",
        file in "[a-z]{1,8}\\.(sh|txt|py)",
        verb in proptest::sample::select(vec!["cat", "bash", "head", "rm", "touch"]),
        join in proptest::sample::select(vec!["; ", " && ", "; ls; "]),
    ) {
        let _g = crate::pathctx::enter(PathCtx {
            cwd: Some("/work".into()),
            root: Some("/work".into()),
            session_id: None,
        });
        let place = format!("{root}/{dir}");
        let path = format!("{place}/{file}");
        let literal = workspace_overreach(&format!("{verb} {path}"));
        prop_assert!(literal.is_some(), "{} {} is not reported at all", verb, path);
        let held = workspace_overreach(&format!("P={place}{join}{verb} $P/{file}"));
        prop_assert_eq!(held, literal, "{}", path);

        let unknown = workspace_overreach(&format!("{verb} $X/{file}"));
        let held_unknown = workspace_overreach(&format!("P=$X{join}{verb} $P/{file}"));
        prop_assert_eq!(held_unknown, unknown, "a variable holding an unknown value named a place");
        let moved_unknown = workspace_overreach(&format!("cd $(foo){join}{verb} ./{file}"));
        let named = moved_unknown.map(|(path, _)| path);
        prop_assert!(!named.as_deref().is_some_and(crate::cst::check::is_opaque_value), "{:?}", named);
    }
}