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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! `ptuf plugin test <path>` runner.
//!
//! For each rule defined in the plugin file, evaluates every entry
//! under `tests.deny:` and `tests.allow:` against the rule alone and
//! reports pass/fail counts. The runner does **not** evaluate built-in
//! rules — a plugin author should only assert their own rule's
//! behaviour without binding to the engine's whole pipeline.
//!
//! Exit semantics for callers (`cli::run_plugin_test`):
//! * 0 — every case passed,
//! * 1 — at least one case failed or the plugin file failed to load.
//!
//! See `docs/design/cli-and-hooks.md:17,28` for the user-facing
//! contract.

use std::io::{self, Write};
use std::path::{Path, PathBuf};

use crate::Decision;
use crate::HookInput;
use crate::facts;
use crate::rules::ConfigRule;

use super::PluginError;
use super::dsl::{WhenNode, compile};
use super::loader::load_path;
use super::rule::PluginRule;
use super::schema::{RawPlugin, RawRule, RawTestCase};

/// Result of executing one `tests.deny` or `tests.allow` entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CaseOutcome {
    pub rule_id: String,
    pub expectation: Expectation,
    pub passed: bool,
    pub command: String,
    pub got: Option<Decision>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Expectation {
    /// The rule should fire (return `Some(_)`).
    ShouldTrigger,
    /// The rule should not fire (return `None`).
    ShouldSkip,
}

/// Aggregated result of running every test case in a plugin file.
#[derive(Debug)]
pub struct RunReport {
    pub source: PathBuf,
    pub plugin_name: String,
    pub cases: Vec<CaseOutcome>,
}

impl RunReport {
    /// `true` iff every case passed.
    pub fn passed(&self) -> bool {
        self.cases.iter().all(|c| c.passed)
    }

    pub fn passed_count(&self) -> usize {
        self.cases.iter().filter(|c| c.passed).count()
    }

    pub fn failed_count(&self) -> usize {
        self.cases.iter().filter(|c| !c.passed).count()
    }

    /// Render a human-readable summary. Returns the underlying I/O
    /// error if one of the writes fails.
    pub fn render<W: Write>(&self, out: &mut W) -> io::Result<()> {
        writeln!(
            out,
            "plugin {} ({}): {} passed, {} failed",
            self.plugin_name,
            self.source.display(),
            self.passed_count(),
            self.failed_count()
        )?;
        for case in &self.cases {
            let mark = if case.passed { "ok " } else { "FAIL" };
            let label = match case.expectation {
                Expectation::ShouldTrigger => "deny",
                Expectation::ShouldSkip => "allow",
            };
            writeln!(out, "  {mark} {} [{label}] {}", case.rule_id, case.command)?;
            if !case.passed {
                match &case.got {
                    Some(d) => writeln!(out, "       got {}", decision_label(d))?,
                    None => writeln!(out, "       got <no decision>")?,
                }
            }
        }
        Ok(())
    }
}

/// Run every plugin test case from `path`.
pub fn run(path: &Path) -> Result<RunReport, PluginError> {
    let loaded = load_path(path)?;
    let cases = build_cases(path, &loaded.raw_rules)?;
    let outcomes = cases.into_iter().map(execute_case).collect();
    Ok(RunReport {
        source: path.to_path_buf(),
        plugin_name: loaded.name,
        cases: outcomes,
    })
}

/// Run from an in-memory YAML string. Mostly handy for tests of the
/// runner itself; the public CLI path always reads from disk.
pub fn run_str(path: &Path, source: &str) -> Result<RunReport, PluginError> {
    let raw: RawPlugin = serde_yaml_ng::from_str(source).map_err(|e| PluginError::Yaml {
        path: path.to_path_buf(),
        message: e.to_string(),
    })?;
    let cases = build_cases(path, &raw.rules)?;
    let outcomes = cases.into_iter().map(execute_case).collect();
    Ok(RunReport {
        source: path.to_path_buf(),
        plugin_name: raw.metadata.name,
        cases: outcomes,
    })
}

struct PreparedCase {
    rule: PluginRule,
    raw_case: RawTestCase,
    expectation: Expectation,
    rule_id: String,
}

fn build_cases(path: &Path, raw_rules: &[RawRule]) -> Result<Vec<PreparedCase>, PluginError> {
    let mut out = Vec::new();
    for raw_rule in raw_rules {
        for case in &raw_rule.tests.deny {
            out.push(prepare_case(
                path,
                raw_rule,
                case.clone(),
                Expectation::ShouldTrigger,
            )?);
        }
        for case in &raw_rule.tests.allow {
            out.push(prepare_case(
                path,
                raw_rule,
                case.clone(),
                Expectation::ShouldSkip,
            )?);
        }
    }
    Ok(out)
}

fn prepare_case(
    path: &Path,
    raw_rule: &RawRule,
    raw_case: RawTestCase,
    expectation: Expectation,
) -> Result<PreparedCase, PluginError> {
    let when: WhenNode = compile(&raw_rule.when).map_err(|e| PluginError::Compile {
        path: path.to_path_buf(),
        rule_id: raw_rule.id.clone(),
        message: e.to_string(),
    })?;
    let rule = PluginRule::from_raw(raw_rule, when);
    Ok(PreparedCase {
        rule_id: raw_rule.id.clone(),
        rule,
        raw_case,
        expectation,
    })
}

fn execute_case(prepared: PreparedCase) -> CaseOutcome {
    let input = HookInput {
        tool_name: prepared.raw_case.input.tool_name,
        tool_input: prepared.raw_case.input.tool_input,
    };
    let command = input
        .bash_command()
        .map_or_else(|| format!("(tool={})", input.tool_name), str::to_owned);
    let facts = facts::extract(&input);
    let got = prepared.rule.evaluate(&facts, &input);
    let passed = match prepared.expectation {
        Expectation::ShouldTrigger => got.is_some(),
        Expectation::ShouldSkip => got.is_none(),
    };
    CaseOutcome {
        rule_id: prepared.rule_id,
        expectation: prepared.expectation,
        passed,
        command,
        got,
    }
}

fn decision_label(d: &Decision) -> &'static str {
    match d {
        Decision::Allow => "allow",
        Decision::Monitor { .. } => "monitor",
        Decision::Ask { .. } => "ask",
        Decision::Deny { .. } => "deny",
    }
}

impl Clone for RawTestCase {
    fn clone(&self) -> Self {
        Self {
            input: super::schema::RawTestInput {
                tool_name: self.input.tool_name.clone(),
                tool_input: self.input.tool_input.clone(),
            },
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    fn p() -> PathBuf {
        PathBuf::from("test.yaml")
    }

    #[test]
    fn passes_when_all_cases_meet_expectations() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      shell.argv:
        headAny: [curl]
    reason: blocked
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "curl https://example.com"
      allow:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        assert_eq!(report.cases.len(), 2);
        assert!(report.passed());
        assert_eq!(report.passed_count(), 2);
        assert_eq!(report.failed_count(), 0);
    }

    #[test]
    fn fails_when_deny_case_does_not_trigger() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: x
rules:
  - id: x.bad
    severity: low
    defaultDecision: deny
    when:
      tool: Read
    reason: r
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        assert!(!report.passed());
        assert_eq!(report.failed_count(), 1);
    }

    #[test]
    fn fails_when_allow_case_triggers() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: x
rules:
  - id: x.bad
    severity: low
    defaultDecision: deny
    when:
      tool: Bash
    reason: r
    tests:
      allow:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        assert!(!report.passed());
        assert_eq!(report.failed_count(), 1);
    }

    #[test]
    fn invalid_yaml_propagates_as_plugin_error() {
        let err = run_str(&p(), "::nope::").expect_err("should fail");
        assert!(matches!(err, PluginError::Yaml { .. }));
    }

    #[test]
    fn no_test_section_yields_zero_cases_and_passes() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: empty
rules:
  - id: empty.x
    severity: low
    defaultDecision: deny
    when:
      tool: Bash
    reason: r
"#;
        let report = run_str(&p(), yaml).expect("run");
        assert!(report.passed());
        assert!(report.cases.is_empty());
    }

    #[test]
    fn render_labels_monitor_when_allow_expectation_fails() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: r
rules:
  - id: r.x
    severity: low
    defaultDecision: monitor
    when:
      tool: Bash
    reason: r
    tests:
      allow:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        let mut buf = Vec::new();
        report.render(&mut buf).expect("render");
        let s = String::from_utf8_lossy(&buf);
        assert!(s.contains("got monitor"), "render output: {s}");
    }

    #[test]
    fn render_includes_pass_and_fail_summary() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: r
rules:
  - id: r.x
    severity: low
    defaultDecision: deny
    when:
      tool: Bash
    reason: r
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
      allow:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        let mut buf = Vec::new();
        report.render(&mut buf).expect("render");
        let s = String::from_utf8_lossy(&buf);
        assert!(s.contains("plugin r"));
        assert!(s.contains("FAIL"));
        assert!(s.contains("ok"));
    }

    /// `Write` impl that succeeds for the first `fail_after` bytes and
    /// then returns `Err` on every subsequent call. Used to exercise the
    /// `?`-propagation paths inside [`RunReport::render`] without
    /// touching the filesystem.
    struct FailingWriter {
        budget: usize,
    }

    impl FailingWriter {
        fn new(budget: usize) -> Self {
            Self { budget }
        }
    }

    impl Write for FailingWriter {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            if self.budget == 0 {
                return Err(io::Error::other("disk full"));
            }
            let n = buf.len().min(self.budget);
            self.budget -= n;
            Ok(n)
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn render_propagates_io_error_when_header_write_fails() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: r
rules:
  - id: r.x
    severity: low
    defaultDecision: deny
    when:
      tool: Bash
    reason: r
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        let mut writer = FailingWriter::new(0);
        let err = report.render(&mut writer).expect_err("header must fail");
        assert!(format!("{err}").contains("disk full"));
    }

    #[test]
    fn render_propagates_io_error_when_per_case_write_fails() {
        // Configure a deny case whose rule does NOT fire: that forces
        // render down the `case.passed == false` branch with `got: None`,
        // which adds a third writeln per case. With a budget that just
        // covers the header, the per-case line triggers the second `?`.
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: r
rules:
  - id: r.x
    severity: low
    defaultDecision: deny
    when:
      tool: Read
    reason: r
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let report = run_str(&p(), yaml).expect("run");
        // Header "plugin r (test.yaml): 0 passed, 1 failed\n" is 41 bytes.
        let mut writer = FailingWriter::new(41);
        let err = report
            .render(&mut writer)
            .expect_err("per-case write must fail");
        assert!(format!("{err}").contains("disk full"));
    }

    #[test]
    fn prepare_case_propagates_compile_error_for_unknown_when_key() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: bad
rules:
  - id: bad.r
    severity: low
    defaultDecision: deny
    when:
      noSuchFact: yes
    reason: r
    tests:
      deny:
        - input:
            tool_name: Bash
            tool_input:
              command: "ls"
"#;
        let err = run_str(&p(), yaml).expect_err("compile must fail");
        match err {
            PluginError::Compile {
                path,
                rule_id,
                message,
            } => {
                assert_eq!(path, p());
                assert_eq!(rule_id, "bad.r");
                assert!(
                    message.contains("noSuchFact") || message.contains("unknown"),
                    "unexpected message: {message}"
                );
            },
            other => panic!("expected Compile, got {other:?}"),
        }
    }

    #[test]
    fn decision_label_maps_each_variant_for_render_diagnostics() {
        // `render` only calls `decision_label` for failing cases whose `got`
        // is `Some(_)`, which in practice is Deny / Monitor / Ask. The Allow
        // arm is unreachable from plugin evaluation but is covered here so
        // the match stays exhaustive under mutation testing.
        assert_eq!(decision_label(&Decision::Allow), "allow");
        assert_eq!(
            decision_label(&Decision::Monitor {
                rule_id: "x".into()
            }),
            "monitor"
        );
        assert_eq!(
            decision_label(&Decision::Ask {
                rule_id: "x".into(),
                reason: "r".into(),
            }),
            "ask"
        );
        assert_eq!(
            decision_label(&Decision::Deny {
                rule_id: "x".into(),
                reason: "r".into(),
            }),
            "deny"
        );
    }
}