openvet-policy 0.6.0

Requirement language and Kleene evaluator for OpenVet audit policies.
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Subject-level evaluation.
//!
//! Picks the effective requirement set for a subject (defaults
//! plus overrides), runs each requirement's expression against
//! every audit with alias-aware claim lookup, and collapses the
//! per-(requirement, audit) tri-state grid into a single
//! pass-or-fail verdict per subject.
//!
//! Per-requirement collapse:
//!
//! - `Pass` iff at least one audit evaluates to `True` and no
//!   audit evaluates to `False`.
//! - `Fail` otherwise. Distinguished as `NotAsserted` (every audit
//!   was `Unknown`) or `Contradicted` (at least one audit evaluated
//!   to `False`).
//!
//! Per-subject collapse: every effective requirement must pass.

use crate::config::{Alias, OverrideOp, Policy, Requirement, SubjectMatcher};
use crate::expr::{self, Expr, Tri};
use openvet_crypto::TaggedHash;
use openvet_proto::{Subject, audit::Audit};
use std::fmt;

/// Outcome of evaluating a [`Policy`] for one subject.
///
/// `Pass` means every effective requirement passed; `Fail` carries
/// one [`FailureReason`] per requirement that didn't; `Unaudited`
/// means no audit applies to this subject, so the per-requirement
/// breakdown would be uniformly uninformative ("not asserted" for
/// every requirement) and is suppressed.
#[derive(Debug, Clone)]
pub enum Verdict {
    /// Every effective requirement passed.
    Pass,
    /// At least one requirement failed; one entry per failing
    /// requirement.
    Fail(Vec<FailureReason>),
    /// No audit applies to this subject — the audit set passed to
    /// [`evaluate`] was empty. Distinct from `Fail` because the
    /// remediation is different: an unaudited subject needs an
    /// audit, not a policy adjustment.
    Unaudited,
}

/// Why a single requirement failed for the subject under evaluation.
#[derive(Debug, Clone)]
pub struct FailureReason {
    /// The name of the requirement that failed.
    pub requirement: String,
    /// Whether the requirement was undecided or actively contradicted.
    pub kind: FailureKind,
}

/// Kind of failure recorded by a [`FailureReason`].
#[derive(Debug, Clone)]
pub enum FailureKind {
    /// No audit was able to assert this requirement to true (and
    /// none explicitly failed it). The requirement remains
    /// undecided.
    NotAsserted,
    /// At least one audit explicitly failed the requirement; the
    /// per-audit claim values that drove the failure are included
    /// for diagnostics.
    Contradicted(Vec<AuditContradiction>),
}

/// One audit's contribution to a [`FailureKind::Contradicted`]
/// outcome.
#[derive(Debug, Clone)]
pub struct AuditContradiction {
    /// Name of the log this audit came from, as configured in
    /// `openvet.toml`.
    pub log: String,
    /// Each claim referenced in the requirement expression and its
    /// resolved tri-state for this audit (post-alias).
    pub relevant_claims: Vec<(String, Tri)>,
}

/// Evaluate `policy` for `subject` against `audits`.
///
/// Each audit is paired with the name of the log it came from, as
/// configured in `openvet.toml`. The log name drives alias
/// resolution: a claim listed under `[alias]` is looked up by its
/// alternate name for the named log and by the canonical name
/// otherwise.
pub fn evaluate(policy: &Policy, subject: &Subject, audits: &[(&str, &Audit)]) -> Verdict {
    // Subject-level short-circuit: with no audits applying, every
    // requirement would degenerate to NotAsserted with no per-audit
    // detail to show. Surface this as its own outcome so callers can
    // render "no matching audit" once instead of repeating it per
    // requirement.
    //
    // The empty-effective-requirement case (an override stripped all
    // requirements) is *not* unaudited — there's nothing to check, so
    // it still passes trivially below.
    let reqs = effective_requirements(policy, subject);
    if audits.is_empty() && !reqs.is_empty() {
        return Verdict::Unaudited;
    }
    let mut failures = Vec::new();
    for name in reqs {
        let Some(req) = policy.requirement(&name) else {
            // Validated at parse time; should be unreachable.
            continue;
        };
        if let Some(kind) = evaluate_requirement(policy, req, audits) {
            failures.push(FailureReason {
                requirement: name,
                kind,
            });
        }
    }
    if failures.is_empty() {
        Verdict::Pass
    } else {
        Verdict::Fail(failures)
    }
}

/// Compute the effective requirement set for `subject`.
///
/// Starts from the policy's default-on requirements, then walks
/// `[[override]]` blocks in declaration order, replacing or
/// patching the working set when a matcher matches the subject.
pub fn effective_requirements(policy: &Policy, subject: &Subject) -> Vec<String> {
    let mut current: Vec<String> = policy
        .requirements
        .iter()
        .filter(|r| r.default)
        .map(|r| r.name.clone())
        .collect();
    for ov in &policy.overrides {
        if !matches_subject(&ov.matcher, subject) {
            continue;
        }
        match &ov.op {
            OverrideOp::Replace(names) => current = names.clone(),
            OverrideOp::Patch { add, remove } => {
                current.retain(|n| !remove.contains(n));
                for a in add {
                    if !current.contains(a) {
                        current.push(a.clone());
                    }
                }
            }
        }
    }
    current
}

fn evaluate_requirement(
    policy: &Policy,
    req: &Requirement,
    audits: &[(&str, &Audit)],
) -> Option<FailureKind> {
    let mut some_true = false;
    let mut contradictions: Vec<AuditContradiction> = Vec::new();
    for (log, audit) in audits {
        let lookup = |name: &str| resolve_claim(audit, log, name, &policy.aliases);
        match expr::evaluate(&req.expr, &lookup) {
            Tri::True => some_true = true,
            Tri::False => {
                contradictions.push(AuditContradiction {
                    log: (*log).to_string(),
                    relevant_claims: claim_snapshot(&req.expr, &lookup),
                });
            }
            Tri::Unknown => {}
        }
    }
    if !contradictions.is_empty() {
        Some(FailureKind::Contradicted(contradictions))
    } else if some_true {
        None
    } else {
        Some(FailureKind::NotAsserted)
    }
}

/// Build a claim-lookup closure for one audit, applying the
/// policy's `[alias]` mappings.
///
/// Claims listed under `[alias]` are translated to the per-log
/// alternative name; unaliased claims look up by canonical name.
pub fn claim_lookup<'a>(
    log: &'a str,
    audit: &'a Audit,
    aliases: &'a [Alias],
) -> impl Fn(&str) -> Tri + 'a {
    move |name: &str| resolve_claim(audit, log, name, aliases)
}

fn resolve_claim(audit: &Audit, log: &str, canonical: &str, aliases: &[Alias]) -> Tri {
    // If `canonical` is aliased and a per-log mapping exists, use
    // that name; otherwise fall through to the canonical name.
    let actual = aliases
        .iter()
        .find(|a| a.canonical == canonical)
        .and_then(|a| a.mappings.iter().find(|(l, _)| l == log))
        .map(|(_, n)| n.as_str())
        .unwrap_or(canonical);
    match audit.claims.get(actual) {
        Some(true) => Tri::True,
        Some(false) => Tri::False,
        None => Tri::Unknown,
    }
}

/// Collect all claim names referenced by the expression (deduped,
/// in first-encounter order) and their resolved tri-state under the
/// given lookup. Used for failure diagnostics.
fn claim_snapshot<F>(expr: &Expr, lookup: &F) -> Vec<(String, Tri)>
where
    F: Fn(&str) -> Tri,
{
    let mut names = Vec::new();
    collect_claims(expr, &mut names);
    names
        .into_iter()
        .map(|n| {
            let v = lookup(&n);
            (n, v)
        })
        .collect()
}

fn collect_claims(expr: &Expr, out: &mut Vec<String>) {
    match expr {
        Expr::Claim(name) => {
            if !out.iter().any(|n| n == name) {
                out.push(name.clone());
            }
        }
        Expr::Not(inner) => collect_claims(inner, out),
        Expr::And(children) | Expr::Or(children) => {
            for c in children {
                collect_claims(c, out);
            }
        }
    }
}

// ──────────────────────────────────────────────────────────────────
// Subject matching
// ──────────────────────────────────────────────────────────────────

fn matches_subject(m: &SubjectMatcher, s: &Subject) -> bool {
    matches_str(&m.registry, &s.registry)
        && matches_str(&m.package, &s.package)
        && matches_str(&m.version, &s.version)
        && matches_str(&m.variant, s.variant.as_deref().unwrap_or(""))
        && matches_hash(&m.hash, &s.hash)
}

fn matches_str(matcher: &Option<String>, value: &str) -> bool {
    match matcher.as_deref() {
        None | Some("*") => true,
        Some(s) => s == value,
    }
}

fn matches_hash(matcher: &Option<String>, hash: &TaggedHash) -> bool {
    match matcher.as_deref() {
        None | Some("*") => true,
        Some(s) => s == hash.to_string(),
    }
}

impl fmt::Display for Tri {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Tri::True => "true",
            Tri::False => "false",
            Tri::Unknown => "?",
        })
    }
}

impl fmt::Display for Verdict {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Verdict::Pass => f.write_str("pass"),
            Verdict::Unaudited => f.write_str("unaudited (no matching audit)"),
            Verdict::Fail(reasons) => {
                writeln!(f, "fail")?;
                for r in reasons {
                    writeln!(f, "  - {r}")?;
                }
                Ok(())
            }
        }
    }
}

impl fmt::Display for FailureReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            FailureKind::NotAsserted => {
                write!(f, "no audit asserted requirement {:?}", self.requirement)
            }
            FailureKind::Contradicted(c) => {
                writeln!(f, "requirement {:?} contradicted by:", self.requirement)?;
                for ac in c {
                    write!(f, "      log {:?}:", ac.log)?;
                    for (name, tri) in &ac.relevant_claims {
                        write!(f, " {name}={tri}")?;
                    }
                    writeln!(f)?;
                }
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::parse_str;
    use openvet_crypto::TaggedHash;
    use openvet_proto::Subject;
    use std::collections::BTreeMap;

    fn subj(reg: &str, pkg: &str, ver: &str) -> Subject {
        Subject {
            registry: reg.into(),
            package: pkg.into(),
            version: ver.into(),
            variant: None,
            hash: TaggedHash::tagged("sha256", [0; 32]),
        }
    }

    fn audit_with(claims: &[(&str, bool)]) -> Audit {
        Audit::builder()
            .subject(subj("cargo", "anything", "0.0.0"))
            .claims(
                claims
                    .iter()
                    .map(|(k, v)| ((*k).to_string(), *v))
                    .collect::<BTreeMap<_, _>>(),
            )
            .build()
    }

    #[test]
    fn passes_when_default_requirement_satisfied() {
        let p = parse_str(
            r#"
            [requirement]
            std-deploy = "safe-to-deploy"
        "#,
        )
        .unwrap();
        let a = audit_with(&[("safe-to-deploy", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn unaudited_when_audit_set_is_empty() {
        // No audits at all → subject-level Unaudited, not a
        // per-requirement NotAsserted.
        let p = parse_str(
            r#"
            [requirement]
            std-deploy = "safe-to-deploy"
        "#,
        )
        .unwrap();
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[]);
        assert!(matches!(v, Verdict::Unaudited));
    }

    #[test]
    fn empty_requirement_set_passes_trivially_even_when_unaudited() {
        // An override stripped every requirement. No requirements to
        // satisfy → trivially Pass, regardless of audit presence.
        let p = parse_str(
            r#"
            [requirement]
            r1 = "safe-to-deploy"
            [[override]]
            package = "x"
            requirements = []
        "#,
        )
        .unwrap();
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn fails_not_asserted_when_no_audit_speaks() {
        let p = parse_str(
            r#"
            [requirement]
            std-deploy = "safe-to-deploy"
        "#,
        )
        .unwrap();
        let a = audit_with(&[]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        match v {
            Verdict::Fail(rs) => {
                assert_eq!(rs.len(), 1);
                assert!(matches!(rs[0].kind, FailureKind::NotAsserted));
            }
            _ => panic!("expected Fail"),
        }
    }

    #[test]
    fn fails_contradicted_when_audit_says_false() {
        let p = parse_str(
            r#"
            [requirement]
            std-deploy = "safe-to-deploy"
        "#,
        )
        .unwrap();
        let asserted = audit_with(&[("safe-to-deploy", true)]);
        let denied = audit_with(&[("safe-to-deploy", false)]);
        let v = evaluate(
            &p,
            &subj("cargo", "x", "1.0"),
            &[("alice", &asserted), ("bob", &denied)],
        );
        match v {
            Verdict::Fail(rs) => {
                assert!(matches!(rs[0].kind, FailureKind::Contradicted(_)));
            }
            _ => panic!("expected Fail (one audit asserts; another contradicts)"),
        }
    }

    #[test]
    fn at_least_one_true_passes_when_others_unknown() {
        let p = parse_str(
            r#"
            [requirement]
            std-deploy = "safe-to-deploy"
        "#,
        )
        .unwrap();
        let asserted = audit_with(&[("safe-to-deploy", true)]);
        let silent = audit_with(&[]);
        let v = evaluate(
            &p,
            &subj("cargo", "x", "1.0"),
            &[("alice", &asserted), ("bob", &silent)],
        );
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn override_replace_swaps_requirement_set() {
        let p = parse_str(
            r#"
            [requirement]
            r1 = "safe-to-deploy"
            r2 = { condition = "safe-to-run", default = false }
            [[override]]
            package = "x"
            requirements = ["r2"]
        "#,
        )
        .unwrap();
        // Without the override, only r1 (default) applies. With the
        // override, only r2 applies — and r2 is satisfied here.
        let a = audit_with(&[("safe-to-run", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn override_patch_adds_and_removes() {
        let p = parse_str(
            r#"
            [requirement]
            r1 = "safe-to-deploy"
            r2 = { condition = "safe-to-run", default = false }
            [[override]]
            package = "x"
            requirements = { add = ["r2"], remove = ["r1"] }
        "#,
        )
        .unwrap();
        // After patch: r1 removed, r2 required. Audit asserts r2 only.
        let a = audit_with(&[("safe-to-run", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn override_only_applies_to_matching_subject() {
        let p = parse_str(
            r#"
            [requirement]
            r1 = "safe-to-deploy"
            r2 = { condition = "safe-to-run", default = false }
            [[override]]
            package = "x"
            requirements = ["r2"]
        "#,
        )
        .unwrap();
        // Subject `y` doesn't match the override, so the default
        // requirement r1 still applies, and the audit doesn't
        // satisfy it.
        let a = audit_with(&[("safe-to-run", true)]);
        let v = evaluate(&p, &subj("cargo", "y", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Fail(_)));
    }

    #[test]
    fn alias_translates_claim_name_per_log() {
        let p = parse_str(
            r#"
            [requirement]
            r = "safe-to-run"
            [alias]
            safe-to-run = ["mozilla:runtime-safe"]
        "#,
        )
        .unwrap();
        // mozilla audit uses `runtime-safe` (its native name).
        let m = audit_with(&[("runtime-safe", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("mozilla", &m)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn alias_falls_back_to_canonical_for_unlisted_log() {
        let p = parse_str(
            r#"
            [requirement]
            r = "safe-to-run"
            [alias]
            safe-to-run = ["mozilla:runtime-safe"]
        "#,
        )
        .unwrap();
        // alice isn't in the alias mapping; the canonical name is
        // looked up directly. alice's audit asserts it.
        let a = audit_with(&[("safe-to-run", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn all_requirements_must_pass() {
        let p = parse_str(
            r#"
            [requirement]
            r1 = "safe-to-deploy"
            r2 = "safe-to-run"
        "#,
        )
        .unwrap();
        // r1 satisfied; r2 silent → overall Fail (NotAsserted on r2).
        let a = audit_with(&[("safe-to-deploy", true)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Fail(_)));
    }

    #[test]
    fn version_matcher_requires_exact() {
        let p = parse_str(
            r#"
            [requirement]
            r = "safe-to-deploy"
            [[override]]
            package = "x"
            version = "1.0.0"
            requirements = []
        "#,
        )
        .unwrap();
        // Override matches 1.0.0 → empty requirements → trivially pass.
        let a = audit_with(&[]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
        // Doesn't match 1.0.1 → default requirement still applies.
        let v = evaluate(&p, &subj("cargo", "x", "1.0.1"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Fail(_)));
    }

    #[test]
    fn star_is_explicit_wildcard() {
        let p = parse_str(
            r#"
            [requirement]
            r = "safe-to-deploy"
            [[override]]
            registry = "*"
            package = "x"
            requirements = []
        "#,
        )
        .unwrap();
        let a = audit_with(&[]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        assert!(matches!(v, Verdict::Pass));
    }

    #[test]
    fn display_prose_for_fail_includes_diagnostic() {
        let p = parse_str(
            r#"
            [requirement]
            r = "safe-to-deploy and safe-to-run"
        "#,
        )
        .unwrap();
        let a = audit_with(&[("safe-to-deploy", true), ("safe-to-run", false)]);
        let v = evaluate(&p, &subj("cargo", "x", "1.0"), &[("alice", &a)]);
        let s = format!("{v}");
        assert!(s.contains("fail"));
        assert!(s.contains("safe-to-run"));
        assert!(s.contains("alice"));
    }
}