|
11 | 11 | //! anchoring is the point: a plain substring test made `/tmp/` fire on |
12 | 12 | //! `/home/user/tmp/tax-return.pdf`, which is user data, not scratch space. |
13 | 13 |
|
14 | | -use crate::{Category, Verdict}; |
| 14 | +use crate::{Category, GenomeError, Result, Verdict}; |
15 | 15 | use globset::{Glob, GlobBuilder, GlobSet, GlobSetBuilder}; |
16 | 16 | use serde::{Deserialize, Serialize}; |
17 | 17 | use std::sync::OnceLock; |
@@ -75,6 +75,38 @@ impl RulesDb { |
75 | 75 | } |
76 | 76 | } |
77 | 77 |
|
| 78 | + /// Reject malformed patterns before an externally supplied database is |
| 79 | + /// used. `compile` remains tolerant for the embedded matcher path, but a |
| 80 | + /// caller loading rules from outside the binary must not report success |
| 81 | + /// for a rule that can never match. |
| 82 | + pub fn validate(&self) -> Result<()> { |
| 83 | + for rule in &self.rules { |
| 84 | + for pattern in &rule.patterns { |
| 85 | + build_glob(pattern).map_err(|error| { |
| 86 | + GenomeError::Rules(format!( |
| 87 | + "rule '{}' has invalid glob pattern '{}': {error}", |
| 88 | + rule.id, pattern |
| 89 | + )) |
| 90 | + })?; |
| 91 | + } |
| 92 | + } |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | + |
| 96 | + /// Put the embedded protected rules ahead of an externally supplied |
| 97 | + /// database. First-match-wins makes this ordering a safety boundary: |
| 98 | + /// custom rules may add coverage, but cannot shadow the system-critical |
| 99 | + /// rules shipped with the application. |
| 100 | + pub fn with_embedded_protected_rules(self) -> Self { |
| 101 | + let mut rules = RulesDb::embedded() |
| 102 | + .rules |
| 103 | + .into_iter() |
| 104 | + .filter(|rule| rule.verdict == Verdict::Protected) |
| 105 | + .collect::<Vec<_>>(); |
| 106 | + rules.extend(self.rules); |
| 107 | + Self::new(self.version, rules) |
| 108 | + } |
| 109 | + |
78 | 110 | /// First matching rule wins; order in the db is priority order. |
79 | 111 | /// Protected rules are listed first for exactly that reason. |
80 | 112 | pub fn classify(&self, path: &std::path::Path) -> (Category, Verdict, Option<&Rule>) { |
@@ -222,6 +254,50 @@ mod tests { |
222 | 254 | } |
223 | 255 | } |
224 | 256 |
|
| 257 | + #[test] |
| 258 | + fn invalid_patterns_are_rejected_before_external_use() { |
| 259 | + let db = RulesDb::new( |
| 260 | + 1, |
| 261 | + vec![Rule { |
| 262 | + id: "broken".into(), |
| 263 | + patterns: vec!["[".into()], |
| 264 | + category: Category::Unknown, |
| 265 | + verdict: Verdict::Review, |
| 266 | + description: "invalid test rule".into(), |
| 267 | + }], |
| 268 | + ); |
| 269 | + |
| 270 | + let error = db |
| 271 | + .validate() |
| 272 | + .expect_err("invalid glob must fail validation"); |
| 273 | + assert!(error.to_string().contains("broken")); |
| 274 | + assert!(error.to_string().contains("invalid glob pattern")); |
| 275 | + } |
| 276 | + |
| 277 | + #[test] |
| 278 | + fn embedded_protected_rules_precede_external_rules() { |
| 279 | + let external = RulesDb::new( |
| 280 | + 1, |
| 281 | + vec![Rule { |
| 282 | + id: "unsafe-override".into(), |
| 283 | + patterns: vec!["**/*.msi".into()], |
| 284 | + category: Category::Installer, |
| 285 | + verdict: Verdict::Safe, |
| 286 | + description: "must not shadow protected rules".into(), |
| 287 | + }], |
| 288 | + ) |
| 289 | + .with_embedded_protected_rules(); |
| 290 | + |
| 291 | + let (category, verdict, rule) = |
| 292 | + external.classify(std::path::Path::new("/tmp/windows/installer/setup.msi")); |
| 293 | + assert_eq!(category, Category::SystemCritical); |
| 294 | + assert_eq!(verdict, Verdict::Protected); |
| 295 | + assert_eq!( |
| 296 | + rule.map(|rule| rule.id.as_str()), |
| 297 | + Some("windows-installer-cache") |
| 298 | + ); |
| 299 | + } |
| 300 | + |
225 | 301 | /// Issue #41. Under substring matching every one of these matched a |
226 | 302 | /// rule written for somewhere else on the disk, and `review` is an |
227 | 303 | /// actionable verdict — the app offered to move them. |
|
0 commit comments