safe-chains 0.232.10

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::parse::Token;
use crate::policy::FlagSet;
use crate::verdict::{SafetyLevel, Verdict};

// find's expression is an ALLOWLIST of READ-ONLY primaries: tests (`-name`/`-type`/`-size`/…),
// read-only actions (`-print`/`-ls`/`-prune`/…), positional + global options, and operators. A
// primary NOT listed — `-ok`/`-okdir`, `-fprint*`/`-fls`, or any new/BSD write primary —
// denies by OMISSION (fail closed), where the old denylist failed open on anything it hadn't
// enumerated. `-exec`/`-execdir` and `-delete` are handled separately (they delegate, so the
// traversal locus decides them). A VALUED
// primary consumes its next token, so a `-`-prefixed VALUE (`-mtime -7`, `-perm -644`) is not
// mistaken for a primary and a filename that looks like an action (`find . -name -delete`) stays a
// value. SAFETY (as in mlr): a value-taking primary MUST be in VALUED, never STANDALONE, or the
// walk would fail to skip its value — over-denying at best, or (if the value were an action)
// swallowing it silently.

pub(in crate::handlers::coreutils) fn is_safe_find(tokens: &[Token]) -> Verdict {
    // find's `{}` placeholder is substituted with each traversed path, which lives UNDER
    // find's path operand. So the nested command's locus is find's traversal scope, not a
    // bare worktree name: `find /etc -exec rm {}` deletes /etc files, not `./file`. Bind `{}`
    // to a path under each find operand and delegate once per operand (deny-absorbing), so a
    // system/home traversal denies. Default operand `.` when none is given (find's default).
    let bases: Vec<&str> = {
        // Skip the leading GLOBAL options (`-H`/`-L`/`-P`/`-D debugopts`/`-O[level]`), which
        // precede the path operand — otherwise `find -L /etc -exec …` stops at `-L`, defaults
        // the base to the cwd, and binds `{}` to a *worktree* path while find actually
        // traverses /etc. Collect ALL leading path operands (deny-absorbing below), so even a
        // mis-counted option arg still surfaces the real path.
        let mut i = 1;
        while i < tokens.len() {
            match tokens[i].as_str() {
                "-H" | "-L" | "-P" => i += 1,
                "-D" => i += 2,
                s if s.starts_with("-O") => i += 1,
                _ => break,
            }
        }
        // `-D` advances by 2 (option + debugopts arg); a TRAILING `-D` with no arg pushes `i` past
        // the end, so slice with `get` rather than `tokens[i..]` (which would panic out of range).
        let leading: Vec<&str> = tokens
            .get(i..)
            .unwrap_or(&[])
            .iter()
            .take_while(|t| !t.as_str().starts_with('-'))
            .map(Token::as_str)
            .collect();
        if leading.is_empty() { vec!["."] } else { leading }
    };

    // The primary vocabulary is DATA and lives in commands/search/find.toml. Absent it, nothing is
    // recognised, so fail closed rather than fall through to the deny-by-omission arm and pretend
    // every primary was simply unknown.
    let Some((safe_standalone, safe_valued)) = crate::registry::fallback_flag_sets("find") else {
        return Verdict::Denied;
    };

    let mut level = SafetyLevel::Inert;
    let mut i = 1;
    while i < tokens.len() {
        let s = tokens[i].as_str();
        // `-exec`/`-execdir`: delegate to the inner command, bound to each traversal base.
        if s == "-exec" || s == "-execdir" {
            let cmd_start = i + 1;
            let cmd_end = tokens[cmd_start..]
                .iter()
                .position(|t| *t == ";" || *t == "+")
                .map(|p| cmd_start + p)
                .unwrap_or(tokens.len());
            if cmd_start >= cmd_end {
                return Verdict::Denied;
            }
            for base in &bases {
                let bound = crate::engine::resolve::locus::traversal_item(base);
                let exec_words: Vec<String> = tokens[cmd_start..cmd_end]
                    .iter()
                    .map(|t| crate::handlers::fd::synthetic_operand(&t.as_str().replace("{}", &bound), t.as_str()))
                    .collect();
                let _mark = crate::cst::netargs::enter(true);
                match crate::command_verdict(&shell_words::join(&exec_words)) {
                    Verdict::Denied => return Verdict::Denied,
                    Verdict::Allowed(l) => level = level.max(l),
                }
            }
            i = cmd_end + 1;
            continue;
        }
        // `-delete` removes every match, so it is a DESTROY bound to the traversal bases exactly
        // as `-exec rm {}` is. It deliberately does NOT live in the read-only standalone list:
        // membership there costs nothing, so it would be admitted with no locus reasoning at all.
        // Routing it through the same delegation is what makes the two spellings of one operation
        // agree STRUCTURALLY — one code path, the same bases — instead of by a test that can
        // drift, and `find / -delete` inherits its refusal the way `find / -exec rm {}` already
        // has one. Modelled as `rm -r`, not `rm`: `-delete` implies `-depth` and removes
        // directories bottom-up, so the recursive spelling is both the accurate analogue and the
        // more conservative one.
        if s == "-delete" {
            for base in &bases {
                let bound = crate::engine::resolve::locus::traversal_item(base);
                let words = vec!["rm".to_string(), "-r".to_string(), bound];
                match crate::command_verdict(&shell_words::join(&words)) {
                    Verdict::Denied => return Verdict::Denied,
                    Verdict::Allowed(l) => level = level.max(l),
                }
            }
            i += 1;
            continue;
        }
        // A path operand, a primary's value, or an operator (`(` `)` `!` `,`).
        if !s.starts_with('-') {
            i += 1;
            continue;
        }
        // Global query-optimisation level `-O2`/`-Ofind` (glued).
        if s.starts_with("-O") {
            i += 1;
            continue;
        }
        // A read-only VALUED primary consumes its value; `-newer`/`-newerXY` take a reference.
        if safe_valued.contains_flag(s) || s.starts_with("-newer") {
            i += 2;
            continue;
        }
        if safe_standalone.contains_flag(s) {
            i += 1;
            continue;
        }
        // Anything else — `-ok*`, `-fprint*`/`-fls`, or an unknown/newer primary — denies.
        return Verdict::Denied;
    }
    Verdict::Allowed(level)
}

pub(in crate::handlers::coreutils) fn dispatch(cmd: &str, tokens: &[Token]) -> Option<Verdict> {
    match cmd {
        "find" => Some(is_safe_find(tokens)),
        _ => None,
    }
}

pub(in crate::handlers::coreutils) fn command_docs() -> Vec<crate::docs::CommandDoc> {
    vec![crate::docs::CommandDoc::handler(
        "find",
        "https://www.gnu.org/software/findutils/manual/html_mono/find.html",
        "Read-only predicates and actions allowed (tests like -name/-type/-size, -print/-ls/-prune, \
             operators, positional and global options). -exec/-execdir allowed when the executed \
             command is itself safe (each `{}` binds to the traversal path).",
        "fs",
    )]
}

#[cfg(test)]
mod tests {
    use crate::is_safe_command;
    fn check(cmd: &str) -> bool {
        is_safe_command(cmd)
    }

    safe! {
        find_name: "find . -name '*.rb'",
        find_type_name: "find . -type f -name '*.py'",
        find_maxdepth: "find /tmp -maxdepth 2",
        find_print: "find . -name '*.log' -print",
        find_print0: "find . -name '*.log' -print0",
        find_exec_grep_l: "find . -name '*.rb' -exec grep -l pattern {} \\;",
        find_exec_grep_l_plus: "find . -name '*.rb' -exec grep -l pattern {} +",
        find_exec_cat: "find . -exec cat {} \\;",
        find_execdir_cat: "find . -execdir cat {} \\;",
        find_execdir_grep: "find . -execdir grep pattern {} \\;",
        find_exec_grep_safe: "find . -name '*.py' -exec grep pattern {} +",
        find_exec_nested_bash_safe: "find . -exec bash -c 'git status' \\;",
        // engine-authoritative: deleting WORKTREE files via -exec is admitted at developer
        // ({} binds to the find path, so these stay inside the worktree).
        find_exec_rm: "find . -exec rm {} \\;",
        find_exec_rm_rf: "find . -exec rm -rf {} +",
        // `-delete` delegates like `-exec rm -r {}`, so a WORKTREE base is admitted for the same
        // reason and an out-of-worktree base still refuses (see the denied list).
        find_delete: "find . -name '*.tmp' -delete",
        find_type_delete: "find . -type f -name '*.bak' -delete",
        find_delete_after_valued: "find . -mtime -7 -delete",
        find_execdir_unsafe: "find . -execdir rm {} \\;",
        // Allowlist coverage: `-`-prefixed VALUES of valued primaries aren't mistaken for primaries.
        find_size_negative: "find . -size -1M",
        find_mtime_negative: "find . -mtime -7 -type f",
        find_perm_dash: "find . -perm -644",
        find_newer: "find . -newer ref.txt",
        find_newermt: "find . -newermt 2024-01-01",
        find_mindepth_maxdepth: "find . -mindepth 1 -maxdepth 3 -type d",
        find_empty: "find . -empty",
        find_not_operator: "find . -not -name '*.tmp'",
        find_or_operator: "find . -name '*.rb' -o -name '*.py'",
        find_printf: "find . -printf '%p\\n'",
        find_help: "find --help",
        // A filename that looks like an action is just the value of `-name` (the old denylist
        // false-DENIED this; the allowlist skips the value correctly).
        find_name_dash_delete_value: "find . -name -delete",
        // Regression (fuzzer, crash-*): a TRAILING `-D` (advances the leading-option scan by 2)
        // overran `tokens[i..]` and panicked the hook. All 6 crash inputs minimize to a bare `-D`
        // (or global opts) as the last token; each is a harmless no-op find that must not panic.
        find_trailing_D: "find -D",
        find_global_L_trailing_D: "find -L -D",
        find_global_H_P_trailing_D: "find -H -P -D",
    }

    denied! {
        find_delete_outside_denied: "find /etc -name '*.tmp' -delete",
        // a leading global option must not hide the real path: base is /etc, not the cwd.
        find_global_opt_L_system: "find -L /etc -exec rm -rf {} \\;",
        find_global_opt_P_system: "find -P / -exec rm {} \\;",
        find_global_opt_O_system: "find -O3 /etc -exec rm {} \\;",
        find_global_opt_D_system: "find -D tree /etc -exec rm {} \\;",
        find_ok_denied: "find . -ok rm {} \\;",
        find_okdir_denied: "find . -okdir rm {} \\;",
        find_exec_nested_bash_chain_denied: "find . -exec bash -c 'ls && rm -rf /' \\;",
        find_type_delete_home_denied: "find ~ -type f -name '*.bak' -delete",
        find_fprint_denied: "find . -fprint /tmp/list.txt",
        find_fprint0_denied: "find . -fprint0 /tmp/list.txt",
        find_fls_denied: "find . -fls /tmp/list.txt",
        find_fprintf_denied: "find . -fprintf /tmp/list.txt '%p'",
        // Allowlist fail-closed wins: an unknown/newer/BSD primary denies by OMISSION (the old
        // denylist would have allowed anything it hadn't enumerated).
        find_unknown_primary_denied: "find . -frobnicate",
        find_delete_after_valued_outside_denied: "find /etc -mtime -7 -delete",
        find_unknown_action_after_test_denied: "find . -type f -flushcache",
    }
}

#[cfg(test)]
mod proptests {
    // Property tests for the pure-logic functions in this file: `dispatch` and `is_safe_find`.
    // Each property asserts a real invariant of one of those functions:
    //   - `dispatch` is a thin router: a property states its exact routing contract.
    //   - `is_safe_find` is the read-only allowlist walk; properties state the deny-by-omission
    //     arm (a non-allow-listed primary forces Denied), and the read-only-monotone case
    //     (a known-safe primary appended to an already-allowed expression does not flip it).
    use super::{dispatch, is_safe_find};
    use crate::parse::Token;
    use crate::verdict::Verdict;
    use proptest::prelude::*;

    /// A token composed solely of read-only allow-listed standalone primaries (`-print`,
    /// `-print0`, `-ls`, `-prune`, `-quit`, `-readable`, `-writable`, `-executable`, `-empty`,
    /// `-true`, `-false`, `-depth`, `-xdev`, …) and a benign `-`-free tail. None of these
    /// performs a write that crosses the worktree, none takes a value, and none changes the
    /// predicate's verdict beyond the read-only band. Composing them is the property the
    /// walk's "standalone advances by 1" branch promises.
    fn arb_read_only_primary() -> impl Strategy<Value = String> {
        prop_oneof![
            Just("-print".to_string()),
            Just("-print0".to_string()),
            Just("-ls".to_string()),
            Just("-prune".to_string()),
            Just("-quit".to_string()),
            Just("-readable".to_string()),
            Just("-writable".to_string()),
            Just("-executable".to_string()),
            Just("-empty".to_string()),
            Just("-true".to_string()),
            Just("-false".to_string()),
            Just("-depth".to_string()),
            Just("-xdev".to_string()),
        ]
    }

    /// A token that the deny-by-omission arm catches: starts with `-`, is NOT a leading global
    /// option (`-H`/`-L`/`-P`/`-D`/`-O*`), is NOT `-exec`/`-execdir`/`-delete` (which have
    /// dedicated delegation branches), and is NOT in the standalone or valued allowlists in
    /// `commands/search/find.toml`. Such a token is a primary the registry doesn't know, so
    /// the walk must hit the explicit `Verdict::Denied` arm at the bottom of the loop.
    fn arb_unknown_primary() -> impl Strategy<Value = String> {
        prop_oneof![
            // outright unknowns (the BSD/macOS `-fprint` family is in the engine's interceptor
            // layer too, but here we hit the handler's own allowlist-fail-closed branch).
            Just("-frobnicate".to_string()),
            Just("-flushcache".to_string()),
            Just("-z".to_string()),
            Just("-E".to_string()),
            // well-known deny-by-omission primaries documented in find's toml `[command.output]`.
            Just("-fprint".to_string()),
            Just("-fprint0".to_string()),
            Just("-fprintf".to_string()),
            Just("-fls".to_string()),
            Just("-ok".to_string()),
            Just("-okdir".to_string()),
        ]
    }

    /// An arbitrary token the walk must accept as a benign operand: arbitrary-but-non-empty text
    /// that does not start with `-`, so it gets classified as a path operand or a valued
    /// primary's value rather than a primary. Used to vary the suffix without forcing the walk
    /// into a deny branch.
    fn arb_benign_operand() -> impl Strategy<Value = String> {
        "[a-zA-Z0-9_./]{1,16}".prop_filter("must not start with a dash", |s| !s.starts_with('-'))
    }

    proptest! {
        // Differential. `dispatch`'s contract is a one-arm router on `cmd`: it forwards
        // `find` to `is_safe_find` and returns `None` for anything else. Wrapping the
        // forward in `Some(_)` must therefore mirror `is_safe_find` exactly. A regression
        // that lost the `Some(...)` wrapper, swapped arms, or skipped the call would fail
        // here on the very first generated input.
        #[test]
        fn dispatch_routes_find_to_is_safe_find_with_some_wrapper(
            args in proptest::collection::vec(any::<String>(), 0..16),
        ) {
            let mut tokens: Vec<Token> = Vec::with_capacity(args.len() + 1);
            tokens.push(Token::from_test("find"));
            for a in &args {
                tokens.push(Token::from_test(a));
            }
            proptest::prop_assert_eq!(
                dispatch("find", &tokens),
                Some(is_safe_find(&tokens)),
                "tokens after program name: {:?}",
                &args[..],
            );
        }

        // Differential (complement). The non-`find` arm of `dispatch` returns `None` for
        // every input — there is no path by which a non-`find` command name ever reaches a
        // verdict. A regression that defaulted to `Some(Allowed(Inert))` instead of `None`
        // would let every command through `find.rs` and silently bypass the rest of the
        // classifier. The cmd list is built as `String` (`select` over `&str` would yield
        // `&str` and trip `needless_borrow`); `cmd.as_str()` then gives the uniform `&str`
        // both consumers need.
        #[test]
        fn dispatch_returns_none_for_every_non_find_command(
            cmd in proptest::sample::select(vec![
                "".to_string(), "ls".to_string(), "cat".to_string(),
                "grep".to_string(), "tar".to_string(),
                "FIND".to_string(), "FIND ".to_string(), "Find".to_string(),
                "findutils".to_string(), "-find".to_string(), "_find".to_string(),
                "find-extra".to_string(), "xfind".to_string(),
            ]),
            args in proptest::collection::vec(any::<String>(), 0..8),
        ) {
            let mut tokens: Vec<Token> = Vec::with_capacity(args.len() + 1);
            tokens.push(Token::from_test(cmd.as_str()));
            for a in &args {
                tokens.push(Token::from_test(a));
            }
            proptest::prop_assert_eq!(
                dispatch(cmd.as_str(), &tokens),
                None,
                "non-find cmd `{}` must return None, got Some",
                cmd,
            );
        }

        // Invariant (allowlist fail-closed). The walk's terminal branch is the explicit
        // `return Verdict::Denied` for any primary the registry doesn't list. Promising
        // that branch is reachable is the *point* of the allowlist — the old denylist
        // falsely admitted primaries it hadn't enumerated. For any expression made of
        // a benign worktree base plus a deny-by-omission primary (anywhere — leading,
        // middle, trailing), `is_safe_find` must deny. A regression that accidentally
        // swallowed the primary, treated it as a value, or added it to the allowlist
        // would surface here.
        #[test]
        fn is_safe_find_denies_any_unknown_primary_regardless_of_position(
            unknown in arb_unknown_primary(),
            position in 0usize..=2,
            suffix in proptest::collection::vec(arb_read_only_primary(), 0..4),
        ) {
            // Build the expression: `find . [read-only prefix] <unknown> [read-only suffix]`.
            // `position` selects whether the unknown primary sits first, second, or last
            // (position 0/1 within the test primaries), so a position-sensitive bug in the
            // walk (off-by-one after a valued primary, e.g.) fails the property.
            let mut tokens: Vec<Token> = vec![Token::from_test("find"), Token::from_test(".")];
            match position {
                0 => {
                    tokens.push(Token::from_test(&unknown));
                    tokens.extend(suffix.iter().map(|s| Token::from_test(s)));
                }
                _ => {
                    tokens.push(Token::from_test("-print"));
                    if position == 1 {
                        tokens.push(Token::from_test(&unknown));
                    }
                    tokens.extend(suffix.iter().map(|s| Token::from_test(s)));
                    if position == 2 {
                        tokens.push(Token::from_test(&unknown));
                    }
                }
            }
            proptest::prop_assert_eq!(
                is_safe_find(&tokens),
                Verdict::Denied,
                "deny-by-omission primary `{}` at position {} of {:?} was not denied",
                unknown,
                position,
                &tokens[..],
            );
        }

        // Metamorphic. Once `is_safe_find` has classified an expression as Allowed, appending
        // a read-only standalone primary (one whose allowlist presence advances the cursor
        // by exactly one token and adds no destructive capacity) must keep the verdict
        // unchanged. The test composes a benign worktree expression with an arbitrary
        // read-only primary tail and asserts the tail-appended expression classifies
        // identically — Allowed iff Allowed, exactly the property the walk's
        // `safe_standalone.contains_flag(s)` branch implements. A regression that, say,
        // re-classified the expression from scratch on every primary (or made the tail's
        // safety decision depend on what came before) would flip the verdict here.
        #[test]
        fn is_safe_find_verdict_is_invariant_under_appending_a_read_only_standalone_primary(
            prefix_primaries in proptest::collection::vec(arb_read_only_primary(), 0..4),
            tail_primary in arb_read_only_primary(),
            operand in arb_benign_operand(),
        ) {
            let base = {
                let mut tokens: Vec<Token> = vec![Token::from_test("find"), Token::from_test(".")];
                for p in &prefix_primaries {
                    tokens.push(Token::from_test(p));
                }
                tokens.push(Token::from_test(&operand));
                tokens
            };
            let tail = {
                let mut tokens = base.clone();
                tokens.push(Token::from_test(&tail_primary));
                tokens
            };
            proptest::prop_assert_eq!(
                is_safe_find(&base),
                is_safe_find(&tail),
                "appending `{}` to {:?} flipped the verdict: base -> {:?}, tail -> {:?}",
                tail_primary,
                &base[..],
                is_safe_find(&base),
                is_safe_find(&tail),
            );
        }
    }
}