Skip to content

Commit 7bc0e23

Browse files
fix(rules): reject a rule with no patterns
An empty pattern list builds a GlobSet that matches nothing, so it fails exactly the way an invalid glob does — silently, and only for the rule whose author cared about it. validate() exists to stop a supplied file from reporting success for a rule that can never fire, and this was the other way to write one.
1 parent fca6890 commit 7bc0e23

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

‎crates/diskern-core/src/rules.rs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ impl RulesDb {
8181
/// for a rule that can never match.
8282
pub fn validate(&self) -> Result<()> {
8383
for rule in &self.rules {
84+
// An empty pattern list compiles cleanly into a GlobSet that
85+
// matches nothing, so it fails the same way a bad glob does —
86+
// silently, and only for the rule the author cared about.
87+
if rule.patterns.is_empty() {
88+
return Err(GenomeError::Rules(format!(
89+
"rule '{}' has no patterns, so it can never match",
90+
rule.id
91+
)));
92+
}
8493
for pattern in &rule.patterns {
8594
build_glob(pattern).map_err(|error| {
8695
GenomeError::Rules(format!(
@@ -274,6 +283,26 @@ mod tests {
274283
assert!(error.to_string().contains("invalid glob pattern"));
275284
}
276285

286+
#[test]
287+
fn rules_without_patterns_are_rejected() {
288+
let db = RulesDb::new(
289+
1,
290+
vec![Rule {
291+
id: "empty".into(),
292+
patterns: vec![],
293+
category: Category::Unknown,
294+
verdict: Verdict::Review,
295+
description: "rule with no patterns".into(),
296+
}],
297+
);
298+
299+
let error = db
300+
.validate()
301+
.expect_err("a rule with no patterns must fail validation");
302+
assert!(error.to_string().contains("empty"));
303+
assert!(error.to_string().contains("can never match"));
304+
}
305+
277306
#[test]
278307
fn embedded_protected_rules_precede_external_rules() {
279308
let external = RulesDb::new(

0 commit comments

Comments
 (0)