Skip to content

Commit 3997872

Browse files
Merge pull request #97 from Coding-Moves/fix/rules-file-followup
fix(rules): finish the external rules file follow-ups
2 parents fc0378d + 7bc0e23 commit 3997872

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎crates/diskern-cli/src/main.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ enum Command {
2929
/// Only show findings with this verdict
3030
#[arg(long, value_enum)]
3131
verdict: Option<VerdictFilter>,
32-
/// Load rules from a JSON file instead of the embedded database
32+
/// Load rules from a JSON file; embedded protected rules still apply
3333
#[arg(long, value_name = "FILE")]
3434
rules: Option<PathBuf>,
3535
},

‎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)