safe-chains 0.243.6

Auto-allow safe bash commands in agentic coding tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! A tool that takes code or configuration from the folder it runs in (`folder_code` in
//! `pathgates.toml`) is judged by that folder's origin, the way a script there would be.
//!
//! `cargo --version` from a folder holding `rust-toolchain.toml` with `path = "./tc"` runs
//! `./tc/bin/cargo`; `cargo build` there reads `.cargo/config.toml`, whose `rustc-wrapper` runs; and
//! `npm test` runs the folder's `package.json`. From a folder outside this project whose code
//! would not be executed — a temporary directory, a system directory, or another user's folder —
//! that is foreign code, so these ask just as `sh ./x.sh` there does. The project root, any folder
//! inside it, and a folder the user granted are unaffected.

#[derive(serde::Deserialize)]
struct Lists {
    folder_code: std::collections::HashSet<String>,
}

pub(super) static NAMES: std::sync::LazyLock<std::collections::HashSet<String>> = std::sync::LazyLock::new(|| {
    toml::from_str::<Lists>(include_str!("../../pathgates.toml"))
        .expect("pathgates.toml is invalid TOML")
        .folder_code
});

/// Whether `cmd` is a `folder_code` tool and the folder it would run in is foreign.
pub(super) fn denies(cmd: &str) -> bool {
    NAMES.contains(cmd) && is_foreign()
}

/// A read or an inert command in the unknown folder at `reads`. The ceiling would approve it,
/// so it asks here. A write stays allowed: that ceiling is what sends it to the prompt, and it
/// says why.
pub(super) fn denies_read(cmd: &str, tokens: &[crate::parse::Token]) -> bool {
    if tokens.is_empty() || !NAMES.contains(cmd) || !unknown_at_reads() || READING_UNSCOPED.with(std::cell::Cell::get) {
        return false;
    }
    let verdict = unscoped_verdict(tokens);
    matches!(verdict, crate::verdict::Verdict::Allowed(level) if level <= crate::verdict::SafetyLevel::SafeRead)
}

fn unknown_at_reads() -> bool {
    let Some(cwd) = crate::pathctx::cwd() else { return false };
    crate::pathctx::folder::is_unknown(&cwd) && crate::pathctx::folder::level() == Some(crate::pathctx::anchor::FolderLevel::Reads)
}

std::thread_local! {
    static READING_UNSCOPED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

/// The verdict before this read check. The engine's own verdict does not consult the gate; the
/// legacy one does, so a second look skips this check and cannot recurse.
fn unscoped_verdict(tokens: &[crate::parse::Token]) -> crate::verdict::Verdict {
    struct Restore(bool);
    impl Drop for Restore {
        fn drop(&mut self) {
            READING_UNSCOPED.with(|cell| cell.set(self.0));
        }
    }
    let previous = READING_UNSCOPED.with(|cell| cell.replace(true));
    let _restore = Restore(previous);
    crate::engine::bridge::engine_verdict(tokens).unwrap_or_else(|| crate::handlers::dispatch(tokens))
}

/// Whether the ambient folder's code would not be run. A directory inside the physically resolved
/// project root is the project. Anywhere else is foreign when executing a file there would be
/// refused, and a temporary directory is foreign even when its spelling is not the exact temp
/// prefix, except this session's scratchpad. A `cd` to a place nobody can pin counts as foreign.
fn is_foreign() -> bool {
    let Some(cwd) = crate::pathctx::cwd() else { return false };
    if cwd == crate::pathctx::UNRESOLVED_CWD {
        return true;
    }
    // The harness places a command here when it does not know the folder. With no level in
    // force, a tool that runs the folder's own code asks. Developer and workspace treat the
    // sentinel as that folder. At reads a write stays a write, so the ceiling can explain it.
    if crate::pathctx::folder::is_unknown(&cwd) {
        return matches!(crate::pathctx::root(), crate::pathctx::Root::Unknown) || crate::pathctx::folder::level().is_none();
    }
    if !cwd.starts_with('/') {
        return false;
    }
    let cwd = crate::pathctx::physical_directory(&cwd);
    if cwd == crate::pathctx::UNRESOLVED_CWD {
        return true;
    }
    if let crate::pathctx::Root::Known(root) = crate::pathctx::root()
        && inside(&cwd, &crate::pathctx::physical_directory(&root))
    {
        return false;
    }
    let dir = format!("{}/", cwd.trim_end_matches('/'));
    if crate::engine::resolve::locus::under_temp(&dir) && !crate::pathctx::in_session_scratchpad(&cwd) {
        return true;
    }
    !crate::engine::resolve::execute_file_verdict(&cwd).is_allowed()
}

fn inside(path: &str, root: &str) -> bool {
    let root = root.trim_end_matches('/');
    let path = path.trim_end_matches('/');
    !root.is_empty() && (path == root || path.strip_prefix(root).is_some_and(|rest| rest.starts_with('/')))
}

#[cfg(test)]
mod tests {
    use crate::pathctx::{PathCtx, enter};
    use proptest::prelude::*;

    const SESSION: &str = "sc0000folder0000code";
    const PROBES: &[&str] = &["--version", "-V", "version", "--help", "help", "-v"];

    fn project() -> String {
        format!("{}/projects/scproj", crate::process_home::var().expect("HOME"))
    }

    fn judged_from(cwd: &str, command: &str) -> bool {
        let root = project();
        let _g = enter(PathCtx { cwd: Some(cwd.to_string()), root: Some(root), session_id: Some(SESSION.to_string()) });
        crate::is_safe_command(command)
    }

    fn approved_probe(name: &str) -> Option<String> {
        PROBES.iter().map(|p| format!("{name} {p}")).find(|c| judged_from(&project(), c))
    }

    fn known_names() -> Vec<String> {
        let mut names: Vec<String> = crate::handlers::handler_docs()
            .into_iter()
            .flat_map(|d| d.name.split(" / ").map(str::to_string).collect::<Vec<_>>())
            .collect();
        names.extend(crate::registry::toml_command_names().into_iter().map(str::to_string));
        names
    }

    #[test]
    fn every_folder_code_name_is_a_command_the_classifier_knows() {
        let known = known_names();
        for name in super::super::folder_code_names() {
            assert!(known.iter().any(|k| k == name), "folder_code lists `{name}`, which no handler or TOML entry defines");
        }
    }

    #[test]
    fn the_reported_bypasses_ask_from_a_foreign_temp_folder() {
        let root = project();
        for command in [
            "cd /tmp/x && cargo --version",
            "cd /tmp/x && rustc --version",
            "cd /tmp/x && rustup show",
            &format!("cd /tmp/y && cargo check --manifest-path {root}/Cargo.toml"),
            "cd /tmp/y && cargo build",
            "cd /tmp/y && cargo test",
            "cd /tmp/y && npm test",
            "cd /tmp/y && go test ./...",
            "cd /private/tmp/y && cargo build",
            "cd /var/tmp/y && cargo build",
            "pushd /tmp/y && cargo build",
            "cd /tmp/y; cargo build",
            "cd /tmp/y && env cargo build",
            "cd /tmp/y && timeout 60 cargo build",
            "cd /tmp/y && rustup run stable cargo build",
        ] {
            assert!(!judged_from(&root, command), "{command}");
        }
        for command in ["cargo --version", "cargo build", "npm test", "go test ./..."] {
            assert!(!judged_from("/tmp/y", command), "ambient /tmp/y: {command}");
            assert!(judged_from(&root, command), "{command}");
            assert!(judged_from(&format!("{root}/sub"), command), "below the root: {command}");
            assert!(judged_from(&format!("/tmp/claude-501/x/{SESSION}/scratchpad"), command), "scratchpad: {command}");
            crate::engine::resolve::regions::with_os("macos", || {
                assert!(judged_from(&format!("/private/tmp/claude-501/x/{SESSION}/scratchpad"), command), "scratchpad: {command}");
            });
            crate::engine::resolve::regions::with_os("linux", || {
                assert!(!judged_from(&format!("/private/tmp/claude-501/x/{SESSION}/scratchpad"), command), "linux: {command}");
            });
        }
        assert!(judged_from(&root, "cd /tmp/y && cat notes.txt"), "a folder in temp is still readable");
    }

    #[test]
    fn a_folder_code_tool_asks_from_a_folder_outside_the_project() {
        let root = project();
        let outside = [
            "cd /etc && cargo test",
            "cd /etc && pytest",
            "cd /TMP/y && cargo build",
            "cd /Tmp/y && cargo build",
            "cd /PRIVATE/TMP/y && cargo build",
            "cd /private/TMP/y && cargo build",
            "cd /System/Volumes/Data/tmp/y && cargo build",
            "cd /System/Volumes/Data/private/tmp/y && cargo build",
            "cd /private/var/folders/xx/yy/T/y && cargo build",
            "cd /var/folders/xx/yy/T/y && cargo build",
            "cd /VAR/folders/xx/yy/T/y && cargo build",
            "cd /Var/Folders/xx/yy/T/y && cargo build",
            "cd ~/Downloads && cargo test",
            "cd /Users/Shared/evil && pytest",
            "cd /opt/y && npm test",
        ];
        let still: Vec<_> = outside.into_iter().filter(|command| judged_from(&root, command)).collect();
        assert!(still.is_empty(), "still approved outside the project: {still:?}");
        for command in ["cargo build", "cargo test", "npm test", "pytest", "rustup show", "jest", "eslint src/"] {
            assert!(judged_from(&root, command), "{command}");
            assert!(judged_from(&root, &format!("cd sub && {command}")), "cd sub && {command}");
        }
        assert!(judged_from(&root, "cd /tmp/y && cat notes.txt"));
    }

    #[test]
    fn a_sibling_runner_asks_outside_the_project() {
        let root = project();
        for command in [
            "cd /tmp/y && jasmine", "cd /tmp/y && cucumber-js", "cd /tmp/y && gotestsum", "cd /tmp/y && karma start --single-run",
            "cd /tmp/y && turbo run build --dry-run", "cd /tmp/y && artisan test", "cd /tmp/y && please list",
        ] {
            assert!(!judged_from(&root, command), "{command}");
        }
        for command in
            ["jasmine", "cucumber-js", "gotestsum", "karma start --single-run", "turbo run build --dry-run", "artisan test", "please list"]
        {
            assert!(judged_from(&root, command), "{command}");
        }
    }

    /// The unknown-folder sentinel is not a directory the harness has pinned. A folder-code tool
    /// asks there until a level that approves writes in that folder is in force. At `reads` a
    /// write stays allowed in the library, because that ceiling sends it to the prompt and says
    /// why; a read asks here, because the ceiling would approve it.
    #[test]
    fn a_folder_code_tool_asks_in_the_unknown_folder_until_a_write_level() {
        use crate::pathctx::anchor::FolderLevel;
        use crate::pathctx::folder::enter as enter_folder;
        let root = project();
        let cwd = crate::targets::UNKNOWN_WORKDIR.to_string();
        let reads = ["pytest", "npm test", "cargo --version"];
        {
            let _unknown = enter(PathCtx { cwd: Some(cwd.clone()), root: Some(root.clone()), session_id: None });
            for command in reads {
                assert!(!crate::is_safe_command(command), "no level: {command}");
            }
            assert!(!crate::is_safe_command("cargo fmt"), "no level: cargo fmt");
            assert!(crate::is_safe_command("cat notes.txt"));
        }
        {
            let _unknown = enter(PathCtx { cwd: Some(cwd.clone()), root: Some(root.clone()), session_id: None });
            let _reads = enter_folder(FolderLevel::Reads);
            for command in reads {
                assert!(!crate::is_safe_command(command), "reads: {command}");
            }
            assert!(crate::is_safe_command("cargo fmt"), "reads: cargo fmt stays a write");
            assert!(crate::is_safe_command("cat notes.txt"));
        }
        {
            let _unknown = enter(PathCtx { cwd: Some(cwd.clone()), root: Some(root.clone()), session_id: None });
            let _developer = enter_folder(FolderLevel::Developer);
            for command in reads {
                assert!(crate::is_safe_command(command), "developer: {command}");
            }
            assert!(crate::is_safe_command("cargo fmt"), "developer: cargo fmt");
        }
        let _project = enter(PathCtx { cwd: Some(root.clone()), root: Some(root), session_id: None });
        assert!(!crate::is_safe_command(&format!("cd {cwd} && cargo fmt")));
        assert!(crate::is_safe_command(&format!("cd {cwd} && cat notes.txt")));
        assert!(!crate::is_safe_command("cd /tmp/y && cargo fmt"));
    }

    #[test]
    fn a_symlinked_temp_directory_is_foreign_for_folder_code() {
        let proj = tempfile::tempdir().expect("project dir");
        let root = proj.path().canonicalize().expect("project path").to_string_lossy().into_owned();
        let target = format!("/tmp/sc-fix-02413-{}", std::process::id());
        std::fs::create_dir_all(&target).expect("temp target");
        let _cleanup = RemoveDir(target.clone());
        std::os::unix::fs::symlink(&target, format!("{root}/tmp_link")).expect("tmp_link");
        let _g = enter(PathCtx { cwd: Some(root.clone()), root: Some(root), session_id: Some(SESSION.to_string()) });
        assert!(crate::is_safe_command("cargo build"), "the project itself stays the project");
        assert!(!crate::is_safe_command("cd ./tmp_link && cargo build"), "cd ./tmp_link && cargo build");
        assert!(!crate::is_safe_command("cd tmp_link && cargo test"), "cd tmp_link && cargo test");
    }

    struct RemoveDir(String);

    impl Drop for RemoveDir {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn cd_pwd_after_leaving_the_project_does_not_count_as_the_project() {
        let root = project();
        let outside = [
            "cd /tmp/y && cd $(pwd) && cargo build",
            "cd /tmp/y && cd \"$(pwd)\" && cargo build",
            "cd /tmp/y && cd `pwd` && cargo build",
            "cd /tmp/y && cd $(pwd) && cargo test",
            "cd /tmp/y && cd $(pwd) && pytest",
            "cd /TMP/y && cd $(pwd) && cargo build",
            "cd /etc && cd $(pwd) && cargo test",
            "cd /etc && cd $(pwd) && pytest",
            "cd /private/var/folders/xx/yy/T/y && cd $(pwd) && cargo build",
            "cd /System/Volumes/Data/tmp/y && cd $(pwd) && cargo build",
            "cd /tmp/y && cd $(pwd)/src && cargo build",
            "cd /tmp/y && cd $(pwd) && sh ./x.sh",
            "cd /tmp/y && cd $(pwd) && bash ./x.sh",
            "cd /tmp/y && cd $(pwd) && python3 ./x.py",
            "cd /tmp/y && cd $(pwd) && node ./x.js",
        ];
        let still: Vec<_> = outside.into_iter().filter(|command| judged_from(&root, command)).collect();
        assert!(still.is_empty(), "still approved: {still:?}");
        for command in [
            "cd $(pwd) && cargo build", "cd $(pwd) && cargo test", "cd $(pwd) && npm test", "cd $(pwd) && pytest",
            "cd $(pwd) && rustup show", "cd $(pwd) && jest", "cd \"$(pwd)\" && eslint src/", "cd sub && cd $(pwd) && cargo build",
            "cd $(pwd) && sh ./x.sh", "cd $(pwd) && bash ./x.sh", "cd $(pwd) && node ./x.js",
        ] {
            assert!(!judged_from(&root, command), "{command}");
        }
        for command in [
            "cd ./sub && cargo build", "cd ./sub && cargo test", "cd ./sub && cat f", "cd ./sub && npm test", "cd ./sub && pytest",
            "cd ./sub && rustup show", "cd ./sub && jest", "cd ./sub && eslint src/", "cd sub && cd .. && cargo build",
            "cd /tmp/y && cat notes.txt", "sh ./x.sh", "cd ./sub && sh ./x.sh", "cd ./sub && bash ./x.sh", "cd ./sub && node ./x.js",
            "cargo build", "cargo test", "npm test", "pytest", "rustup show", "jest", "eslint src/",
        ] {
            assert!(judged_from(&root, command), "{command}");
        }
    }

    #[test]
    fn a_folder_code_tool_asks_where_the_folder_it_runs_in_is_unknown() {
        let root = project();
        for command in [
            "CDPATH=/tmp; cd y && cargo --version", "CDPATH=/tmp cd y && cargo build", "export CDPATH=/tmp; cd y && npm test",
            "cdpath=/tmp; cd y && go test ./...",
        ] {
            assert!(!judged_from(&root, command), "{command}");
        }
        for command in ["CDPATH=/tmp; cd ./sub && cargo build", "cd sub && cargo build", "cargo build"] {
            assert!(judged_from(&root, command), "{command}");
        }
    }

    #[test]
    fn tools_that_run_their_folders_config_or_tests_ask_from_a_foreign_temp_folder() {
        for command in [
            "vite build", "webpack", "rollup -c", "tsup", "mocha", "ava", "stylelint a.css", "pylint x.py", "flake8", "phpunit",
            "phpstan analyse", "rubocop", "mypy .", "next build", "astro build", "coverage run -m pytest", "cucumber", "php artisan test",
            "pod install", "tuist build", "mvn test", "sphinx-build docs out", "mkdocs build", "ansible-lint",
        ] {
            assert!(judged_from(&project(), command), "in the project: {command}");
            assert!(!judged_from("/tmp/y", command), "from /tmp/y: {command}");
        }
    }

    #[test]
    fn a_project_root_under_temp_is_still_the_project() {
        let root = "/tmp/ci/work/proj";
        let _g = enter(PathCtx { cwd: Some(root.to_string()), root: Some(root.to_string()), session_id: None });
        assert!(crate::is_safe_command("cargo build"));
        assert!(crate::is_safe_command("cd sub && cargo build"));
        assert!(!crate::is_safe_command("cd /tmp/other && cargo build"));
    }

    proptest! {
        #[test]
        fn a_folder_code_tool_asks_from_a_directory_outside_the_project(
            place in prop::sample::select(vec![
                "/etc", "/TMP/y", "/Tmp/z", "/PRIVATE/TMP/y", "/System/Volumes/Data/tmp/y",
                "/private/var/folders/xx/yy/T/y", "/VAR/folders/aa/bb/T/y", "/opt/y", "/Users/Shared/y",
            ]),
        ) {
            let root = project();
            let cargo_test = format!("cd {place} && cargo test");
            let pytest_cmd = format!("cd {place} && pytest");
            prop_assert!(!judged_from(&root, &cargo_test));
            prop_assert!(!judged_from(&root, &pytest_cmd));
            prop_assert!(judged_from(&root, "cargo build"));
            prop_assert!(judged_from(&root, "cargo test"));
            prop_assert!(judged_from(&root, "cd sub && cargo test"));
            prop_assert!(judged_from(&root, "npm test"));
            prop_assert!(judged_from(&root, "pytest"));
            prop_assert!(judged_from(&root, "rustup show"));
            prop_assert!(judged_from(&root, "jest"));
            prop_assert!(judged_from(&root, "eslint src/"));
        }

        #[test]
        fn a_folder_code_tool_asks_from_a_foreign_temp_folder_and_not_from_the_project(
            pick in any::<prop::sample::Index>(),
            temp in prop::sample::select(vec!["/tmp", "/private/tmp", "/var/tmp"]),
            dir in "[a-z]{1,8}",
        ) {
            let names = super::super::folder_code_names();
            let name = pick.get(&names);
            if let Some(probe) = approved_probe(name) {
                let foreign = format!("{temp}/{dir}");
                prop_assert!(!judged_from(&foreign, &probe), "ambient {foreign}: {probe}");
                prop_assert!(!judged_from(&project(), &format!("cd {foreign} && {probe}")), "cd {foreign} && {probe}");
                prop_assert!(judged_from(&format!("{}/{dir}", project()), &probe), "inside the project: {probe}");
            }
        }
    }

    #[test]
    fn most_folder_code_tools_have_an_approved_probe() {
        let names = super::super::folder_code_names();
        let probed = names.iter().filter(|n| approved_probe(n).is_some()).count();
        assert!(probed * 2 >= names.len(), "only {probed} of {} folder_code tools have an approved probe", names.len());
    }
}