ptuf 0.5.0

PreToolUseFilter: a generic guardrail layer for coding agents
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use crate::decision::{Decision, Severity};
use crate::facts::Facts;
use crate::facts::shell::{Argv, unwrap_prefix_wrapper};
use crate::hook_input::HookInput;
use crate::reason;

use super::ConfigRule;

pub struct DestructiveRm;

const RULE_ID: &str = "core.filesystem.destructive-rm";

// Heads are compared by basename, so absolute and relative invocation
// paths (`/bin/rm`, `/usr/local/bin/rm`, `./rm`) match without enumeration.
const RM_HEADS: &[&str] = &["rm"];

const SYSTEM_ROOTS: &[&str] = &[
    "/etc", "/usr", "/var", "/bin", "/boot", "/lib", "/lib32", "/lib64", "/sbin", "/opt", "/root",
    "/sys", "/proc",
];

const HOME_TARGETS: &[&str] = &["~", "~/", "$HOME", "$HOME/", "${HOME}", "${HOME}/"];

impl ConfigRule for DestructiveRm {
    fn id(&self) -> &str {
        RULE_ID
    }

    fn severity(&self) -> Severity {
        Severity::Critical
    }

    fn hard_deny(&self) -> bool {
        true
    }

    fn evaluate(&self, facts: &Facts, _input: &HookInput) -> Option<Decision> {
        let bash = facts.bash.as_ref()?;
        let triggered = bash
            .commands()
            .into_iter()
            .any(is_destructive_rm_invocation);
        if !triggered {
            return None;
        }

        let reason = reason::build(
            RULE_ID,
            "The command recursively force-deletes a system, home, or root path. \
             This operation is unrecoverable and would destroy critical data.",
            &[
                "Target only specific subdirectories you intend to remove.",
                "Use a relative path scoped to the project tree.",
                "Ask the user to confirm before any recursive deletion.",
            ],
        );

        Some(Decision::Deny {
            rule_id: RULE_ID.into(),
            reason,
        })
    }
}

fn is_destructive_rm_invocation(argv: &Argv) -> bool {
    if let Some(inner) = unwrap_prefix_wrapper(argv) {
        return is_destructive_rm_invocation(&inner);
    }
    is_rm_head(argv.head_basename())
        && has_recursive_force_flag(argv)
        && argv.positional().any(is_destructive_target)
}

fn is_rm_head(head: &str) -> bool {
    RM_HEADS.contains(&head)
}

fn has_recursive_force_flag(argv: &Argv) -> bool {
    let flags: Vec<&str> = argv.flags().collect();
    has_recursive(&flags) && has_force(&flags)
}

fn has_recursive(flags: &[&str]) -> bool {
    flags.iter().any(|flag| {
        if *flag == "--recursive" {
            return true;
        }
        if flag.starts_with("--") {
            return false;
        }
        flag[1..].chars().any(|c| c == 'r' || c == 'R')
    })
}

fn has_force(flags: &[&str]) -> bool {
    flags.iter().any(|flag| {
        if *flag == "--force" {
            return true;
        }
        if flag.starts_with("--") {
            return false;
        }
        flag[1..].contains('f')
    })
}

fn is_destructive_target(arg: &str) -> bool {
    if arg.contains("..") {
        return true;
    }
    let normalized = normalize_rm_target(arg);
    normalized == "/"
        || normalized == "/*"
        || HOME_TARGETS.contains(&normalized.as_str())
        || SYSTEM_ROOTS
            .iter()
            .any(|root| normalized == *root || normalized.starts_with(&format!("{root}/")))
}

/// Collapse POSIX duplicate slashes and trim a trailing `/` before
/// comparing rm targets. Parent-segment tokens are handled pessimistically
/// by the caller because the resolved path cannot be known pre-shell.
fn normalize_rm_target(arg: &str) -> String {
    let mut out = String::with_capacity(arg.len());
    let mut prev_slash = false;
    for ch in arg.chars() {
        if ch == '/' {
            if !prev_slash {
                out.push(ch);
            }
            prev_slash = true;
        } else {
            out.push(ch);
            prev_slash = false;
        }
    }
    if out.len() > 1 && out.ends_with('/') {
        out.pop();
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hook_input::HookInput;

    fn bash(cmd: &str) -> HookInput {
        HookInput {
            tool_name: "Bash".into(),
            tool_input: serde_json::json!({ "command": cmd }),
        }
    }

    fn assert_deny(cmd: &str) {
        let input = bash(cmd);
        let facts = crate::facts::extract(&input);
        let result = DestructiveRm.evaluate(&facts, &input);
        assert!(
            matches!(&result, Some(Decision::Deny { rule_id, .. }) if rule_id == RULE_ID),
            "expected deny for {cmd:?}, got {result:?}",
        );
    }

    fn assert_allow(cmd: &str) {
        let input = bash(cmd);
        let facts = crate::facts::extract(&input);
        let result = DestructiveRm.evaluate(&facts, &input);
        assert!(
            result.is_none(),
            "expected allow for {cmd:?}, got {result:?}"
        );
    }

    #[test]
    fn trait_metadata_is_stable() {
        let r = DestructiveRm;
        assert_eq!(r.id(), RULE_ID);
        assert_eq!(r.severity(), Severity::Critical);
        assert!(r.hard_deny());
    }

    #[test]
    fn denies_rm_rf_root() {
        assert_deny("rm -rf /");
    }

    #[test]
    fn denies_absolute_and_relative_rm_heads() {
        // rm heads must match on basename, including install locations
        // outside the hand-enumerated /bin//usr/bin pair.
        assert_deny("/bin/rm -rf /");
        assert_deny("/usr/local/bin/rm -rf /etc");
        assert_deny("./rm -rf /");
    }

    #[test]
    fn denies_bash_dash_c_wrapped_rm() {
        assert_deny("bash -c 'rm -rf /'");
    }

    #[test]
    fn denies_find_exec_wrapped_rm() {
        assert_deny(r"find . -name tmp -exec rm -rf / \;");
    }

    #[test]
    fn denies_rm_rf_home_tilde() {
        assert_deny("rm -rf ~");
        assert_deny("rm -rf ~/");
    }

    #[test]
    fn denies_rm_rf_home_envvar() {
        assert_deny("rm -rf $HOME");
        assert_deny("rm -rf ${HOME}");
        assert_deny(r#"rm -rf "${HOME}""#);
        assert_deny("rm -rf '$HOME'");
    }

    #[test]
    fn denies_rm_rf_root_glob() {
        assert_deny("rm -rf /*");
    }

    #[test]
    fn destructive_rm_normalizes_double_slash() {
        assert_deny("rm -rf //");
        assert_deny("rm -rf //etc");
        assert_deny("rm -rf /etc//");
        assert_deny("rm -rf ///usr/local");
    }

    #[test]
    fn destructive_rm_pessimistic_on_parent_segments() {
        assert_deny("rm -rf ./safe/../etc");
        assert_deny("rm -rf /tmp/foo/../etc/passwd");
    }

    #[test]
    fn denies_rm_rf_system_paths() {
        for path in [
            "/etc", "/usr", "/var", "/bin", "/boot", "/lib", "/lib32", "/lib64", "/sbin", "/opt",
            "/root", "/sys", "/proc",
        ] {
            assert_deny(&format!("rm -rf {path}"));
            assert_deny(&format!("rm -rf {path}/something"));
        }
    }

    #[test]
    fn allows_lookalike_system_paths() {
        // Each target shares a prefix with a SYSTEM_ROOTS entry but is
        // not itself a member or a `{root}/` subpath. The rule uses
        // `arg.starts_with(format!("{root}/"))` for the prefix branch,
        // so dropping the trailing `/` would falsely match these.
        assert_allow("rm -rf /etcd");
        assert_allow("rm -rf /var2");
        assert_allow("rm -rf /usr-local");
        assert_allow("rm -rf /root2");
        assert_allow("rm -rf /procfs");
        assert_allow("rm -rf /tmp");
        assert_allow("rm -rf /home/user/projects");
    }

    #[test]
    fn denies_when_one_of_multiple_targets_is_destructive() {
        assert_deny("rm -rf ./safe /etc");
        assert_deny("rm -rf /etc ./safe");
        assert_deny("rm -rf ./a ./b /usr ./c");
    }

    #[test]
    fn denies_home_targets_with_trailing_slash() {
        assert_deny("rm -rf $HOME/");
        assert_deny("rm -rf ${HOME}/");
    }

    #[test]
    fn denies_alternate_flag_orderings() {
        assert_deny("rm -fr /");
        assert_deny("rm -rfv /");
        assert_deny("rm -vrf /");
        assert_deny("rm --recursive --force /");
        assert_deny("rm --force --recursive /");
    }

    #[test]
    fn denies_separated_lowercase_short_flags() {
        assert_deny("rm -r -f /");
        assert_deny("rm -f -r /");
    }

    #[test]
    fn denies_uppercase_recursive_flag() {
        assert_deny("rm -Rf /");
        assert_deny("rm -fR /");
        assert_deny("rm -R -f /");
        assert_deny("rm -f -R /");
        assert_deny("rm -Rfv /etc");
        assert_deny("rm -vRf /etc");
        assert_deny("rm -fRv /");
        assert_deny("rm -vfR /");
    }

    #[test]
    fn denies_mixed_long_and_short_flags() {
        assert_deny("rm --recursive -f /");
        assert_deny("rm -r --force /");
        assert_deny("rm --force -r /");
        assert_deny("rm -R --force /usr");
        assert_deny("rm -f --recursive /");
        assert_deny("rm --force -R /");
    }

    #[test]
    fn denies_in_pipeline_or_compound() {
        assert_deny("echo go && rm -rf /");
        assert_deny("ls; rm -rf /etc");
        assert_deny("true || rm -rf /");
        assert_deny("cat foo | rm -rf /");
        assert_deny("rm -rf /etc | tee log");
    }

    #[test]
    fn denies_full_path_to_rm() {
        assert_deny("/bin/rm -rf /");
        assert_deny("/usr/bin/rm -rf /etc");
        assert_deny("/bin/rm -Rf /");
        assert_deny("/usr/bin/rm --recursive --force /");
    }

    #[test]
    fn denies_sudo_wrapped_rm() {
        assert_deny("sudo rm -rf /");
        // `sudo -u root` interposes a value-taking flag before `rm`;
        // unwrapping must skip the flag value, not stop at `root`.
        assert_deny("sudo -u root rm -rf /etc");
    }

    #[test]
    fn allows_safe_sudo_rm_invocations() {
        assert_allow("sudo rm file.txt");
        assert_allow("sudo");
    }

    #[test]
    fn allows_safe_rm_invocations() {
        assert_allow("rm file.txt");
        assert_allow("rm -r ./build");
        assert_allow("rm -rf ./build");
        assert_allow("rm -rf $HOME/scratch/foo");
        assert_allow("rm -rf ~/projects/myrepo/target");
        assert_allow("rm -Rf ./build");
        assert_allow("rm --recursive --force ./build");
    }

    #[test]
    fn allows_when_only_one_of_recursive_or_force_is_set() {
        assert_allow("rm -R /etc");
        assert_allow("rm -r /etc");
        assert_allow("rm --recursive /etc");
        assert_allow("rm -f /etc/passwd");
        assert_allow("rm --force /etc/passwd");
    }

    #[test]
    fn allows_non_bash_tools() {
        let input = HookInput {
            tool_name: "Read".into(),
            tool_input: serde_json::json!({ "command": "rm -rf /" }),
        };
        let facts = crate::facts::extract(&input);
        assert!(DestructiveRm.evaluate(&facts, &input).is_none());
    }

    #[test]
    fn allows_other_commands_with_rm_substring() {
        assert_allow("echo rm -rf / # not actually deleting");
    }

    use crate::testing::proptest::{arbitrary_command, bash_command, non_bash_hook_input};
    use proptest::prelude::*;

    fn evaluate_for(input: &HookInput) -> Option<Decision> {
        let facts = crate::facts::extract(input);
        DestructiveRm.evaluate(&facts, input)
    }

    proptest! {
        // Non-Bash tool ⇒ always None, regardless of payload contents.
        #[test]
        fn pbt_non_bash_yields_none(input in non_bash_hook_input()) {
            prop_assert!(evaluate_for(&input).is_none());
        }

        // Adversarial: never panics on any printable ASCII bash string.
        #[test]
        fn pbt_evaluate_never_panics(cmd in arbitrary_command()) {
            let input = bash(&cmd);
            let _ = evaluate_for(&input);
        }

        // When the rule fires, the result is always Deny with this rule's id.
        #[test]
        fn pbt_only_emits_deny_with_correct_id(cmd in bash_command()) {
            let input = bash(&cmd);
            if let Some(d) = evaluate_for(&input) {
                match d {
                    Decision::Deny { rule_id, .. } => prop_assert_eq!(rule_id, RULE_ID),
                    other => prop_assert!(
                        false,
                        "expected Deny, got {other:?}",
                    ),
                }
            }
        }

        // Negative space: commands whose argv head never matches an rm
        // binary cannot trigger this rule.
        #[test]
        fn pbt_no_rm_head_means_no_fire(
            head in "[a-z][a-z0-9]{0,5}",
            args in proptest::collection::vec("[a-zA-Z0-9_./-]{1,8}", 0..4),
        ) {
            prop_assume!(!RM_HEADS.contains(&head.as_str()));
            let cmd = if args.is_empty() {
                head
            } else {
                format!("{} {}", head, args.join(" "))
            };
            let input = bash(&cmd);
            prop_assert!(evaluate_for(&input).is_none());
        }

        // Positive space: any cartesian product of (rm head × recursive
        // form × force form × destructive target × flag order) must
        // produce a Deny. Guards against future helper refactors that
        // silently lose coverage on a corner of the matrix.
        #[test]
        fn pbt_all_destructive_combinations_deny(
            head_idx in 0usize..3,
            rec_idx in 0usize..3,
            force_idx in 0usize..2,
            target in prop_oneof![
                Just("/"),
                Just("/*"),
                Just("~"),
                Just("~/"),
                Just("$HOME"),
                Just("${HOME}"),
                Just("\"${HOME}\""),
                Just("'$HOME'"),
                Just("/etc"),
                Just("/usr"),
                Just("/var"),
                Just("/bin"),
                Just("/boot"),
                Just("/lib"),
                Just("/lib32"),
                Just("/lib64"),
                Just("/opt"),
                Just("/root"),
                Just("/sys"),
                Just("/proc"),
                Just("/usr/local-fake/junk"),
                Just("/proc/sys"),
                Just("/sbin"),
            ],
            rec_first in any::<bool>(),
        ) {
            let head = ["rm", "/bin/rm", "/usr/bin/rm"][head_idx];
            let rec = ["-r", "-R", "--recursive"][rec_idx];
            let force = ["-f", "--force"][force_idx];
            let cmd = if rec_first {
                format!("{head} {rec} {force} {target}")
            } else {
                format!("{head} {force} {rec} {target}")
            };
            let input = bash(&cmd);
            let result = evaluate_for(&input);
            prop_assert!(
                matches!(&result, Some(Decision::Deny { rule_id, .. }) if rule_id == RULE_ID),
                "expected deny for {cmd:?}, got {result:?}",
            );
        }

        // Bundled short flags (`-Rf`, `-fR`, verbose permutations) must
        // not slip past a refactor that only recognises separated tokens.
        #[test]
        fn pbt_bundled_destructive_flags_deny(
            head_idx in 0usize..3,
            bundle_idx in 0usize..8,
            target in prop_oneof![
                Just("/"),
                Just("/etc"),
                Just("~"),
                Just("$HOME"),
            ],
        ) {
            let head = ["rm", "/bin/rm", "/usr/bin/rm"][head_idx];
            let bundle = ["-Rf", "-fR", "-rfv", "-vRf", "-Rfv", "-fRv", "-vfR", "-fr"][bundle_idx];
            let cmd = format!("{head} {bundle} {target}");
            let input = bash(&cmd);
            let result = evaluate_for(&input);
            prop_assert!(
                matches!(&result, Some(Decision::Deny { rule_id, .. }) if rule_id == RULE_ID),
                "expected deny for {cmd:?}, got {result:?}",
            );
        }
    }
}