sieve-kit 0.1.0

Rule-based mail filtering engine: conditions, actions, and bounded regex evaluation
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
//! Rule evaluation engine. Synchronous, bounded, no I/O.
//!
//! Regex evaluation is checked against a 100 ms budget after matching.
//! Invalid regex patterns are treated as non-matching (never panic).

use std::sync::Arc;

use regex::Regex;

use crate::types::{Condition, ConditionField, FilterRule, Filterable, LogicOp, Operator};

/// Maximum time allowed for a single regex match (milliseconds).
const REGEX_TIMEOUT_MS: u128 = 100;

/// Cache of compiled regex patterns keyed by the raw pattern string.
/// Prevents re-compilation on every evaluation pass.
#[derive(Clone, Default)]
pub struct RegexCache {
    inner: Arc<std::sync::RwLock<std::collections::HashMap<String, Option<Regex>>>>,
}

impl RegexCache {
    /// Returns a compiled regex, or `None` if the pattern is invalid.
    #[must_use]
    pub fn get_or_compile(&self, pattern: &str) -> Option<Regex> {
        // Fast path: already compiled.
        if let Ok(cache) = self.inner.read() {
            if let Some(entry) = cache.get(pattern) {
                return entry.clone();
            }
        }
        // Slow path: compile and insert.
        let compiled = Regex::new(pattern).ok();
        if let Ok(mut cache) = self.inner.write() {
            cache.insert(pattern.to_string(), compiled.clone());
        }
        compiled
    }
}

/// Resolve the string value of a condition field on a message.
///
/// `HasAttachment` resolves to `"true"`/`"false"`; missing fields resolve to
/// the empty string.
#[must_use]
pub fn field_value<'a, F: Filterable + ?Sized>(msg: &'a F, field: &ConditionField) -> &'a str {
    match field {
        ConditionField::From => msg.from(),
        ConditionField::To => msg.to(),
        ConditionField::Cc => msg.cc(),
        ConditionField::Subject => msg.subject(),
        ConditionField::Body => msg.body(),
        ConditionField::Header(name) => msg.header(name).unwrap_or_default(),
        ConditionField::HasAttachment => {
            if msg.has_attachment() {
                "true"
            } else {
                "false"
            }
        }
    }
}

/// Evaluate a filter rule against a message.
///
/// Returns `true` when the rule matches (all/any conditions satisfied).
/// Disabled rules and rules without conditions always return `false`.
///
/// # Panics
///
/// Never panics. Invalid regex patterns are treated as non-matching.
#[must_use]
pub fn evaluate_rule<F: Filterable + ?Sized>(
    rule: &FilterRule,
    msg: &F,
    regex_cache: &RegexCache,
) -> bool {
    if !rule.enabled {
        return false;
    }
    if rule.conditions.is_empty() {
        return false;
    }

    let results: Vec<bool> = rule
        .conditions
        .iter()
        .map(|c| evaluate_condition(c, msg, regex_cache))
        .collect();

    match rule.condition_logic {
        LogicOp::And => results.iter().all(|&r| r),
        LogicOp::Or => results.iter().any(|&r| r),
    }
}

/// Evaluate a single condition against a message.
///
/// # Panics
///
/// Never panics.
#[must_use]
pub fn evaluate_condition<F: Filterable + ?Sized>(
    condition: &Condition,
    msg: &F,
    regex_cache: &RegexCache,
) -> bool {
    let field_value = field_value(msg, &condition.field);
    let result = match &condition.operator {
        Operator::Contains => field_value
            .to_lowercase()
            .contains(&condition.value.to_lowercase()),
        Operator::Equals => field_value.eq_ignore_ascii_case(&condition.value),
        Operator::Matches => glob_match(&condition.value, field_value),
        Operator::Regex => evaluate_regex(&condition.value, field_value, regex_cache),
        Operator::Exists => !field_value.is_empty(),
    };
    if condition.negate { !result } else { result }
}

/// Evaluate a regex condition with a bounded budget.
///
/// Returns `false` on invalid patterns or when the match exceeds the time
/// budget (never panics).
fn evaluate_regex(pattern: &str, input: &str, cache: &RegexCache) -> bool {
    let Some(re) = cache.get_or_compile(pattern) else {
        return false;
    };

    // The regex crate is linear-time (no catastrophic backtracking) and has
    // no built-in timeout. Wall-clock measurement is a post-hoc safety check:
    // a match that ran over budget is reported as non-matching so callers can
    // treat pathological rules conservatively.
    let start = std::time::Instant::now();
    let matched = re.is_match(input);
    if start.elapsed().as_millis() > REGEX_TIMEOUT_MS {
        return false;
    }
    matched
}

/// Simple glob matching supporting `*` (any chars) and `?` (single char).
///
/// `*` and `?` are the only special characters; backslash escapes them.
/// Matching is case-insensitive.
#[must_use]
pub fn glob_match(pattern: &str, input: &str) -> bool {
    let pattern_lower = pattern.to_lowercase();
    let input_lower = input.to_lowercase();
    glob_match_inner(pattern_lower.as_bytes(), input_lower.as_bytes())
}

#[allow(clippy::similar_names)]
fn glob_match_inner(pattern: &[u8], input: &[u8]) -> bool {
    let mut pi = 0;
    let mut ii = 0;
    let mut star_pi = usize::MAX;
    let mut star_ii = 0;

    while ii < input.len() {
        if pi < pattern.len() && pattern[pi] == b'*' {
            star_pi = pi;
            star_ii = ii;
            pi += 1;
        } else if pi < pattern.len() && (pattern[pi] == b'?' || pattern[pi] == input[ii]) {
            pi += 1;
            ii += 1;
        } else if star_pi != usize::MAX {
            pi = star_pi + 1;
            star_ii += 1;
            ii = star_ii;
        } else {
            return false;
        }
    }

    while pi < pattern.len() && pattern[pi] == b'*' {
        pi += 1;
    }

    pi == pattern.len()
}

/// Sort rules by priority (ascending = highest priority first).
pub fn sort_rules_by_priority(rules: &mut [FilterRule]) {
    rules.sort_by_key(|r| r.priority);
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use super::*;
    use crate::types::MailEnvelope;

    fn make_rule(conditions: Vec<Condition>, logic: LogicOp) -> FilterRule {
        FilterRule {
            id: "test-rule-1".to_string(),
            name: "Test Rule".to_string(),
            enabled: true,
            priority: 0,
            conditions,
            condition_logic: logic,
            actions: vec![],
        }
    }

    fn make_envelope() -> MailEnvelope {
        MailEnvelope {
            from: "alice@example.com".to_string(),
            to: "bob@example.com".to_string(),
            cc: String::new(),
            subject: "Hello World".to_string(),
            body: String::new(),
            has_attachment: false,
            headers: vec![],
        }
    }

    #[test]
    fn contains_operator_matches_case_insensitive() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "hello".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn contains_operator_no_match() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "nomatch".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn negate_inverts_result() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "nomatch".to_string(),
                negate: true,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn and_logic_requires_all() {
        let rule = make_rule(
            vec![
                Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "hello".to_string(),
                    negate: false,
                },
                Condition {
                    field: ConditionField::From,
                    operator: Operator::Contains,
                    value: "nomatch".to_string(),
                    negate: false,
                },
            ],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn or_logic_requires_any() {
        let rule = make_rule(
            vec![
                Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "nomatch".to_string(),
                    negate: false,
                },
                Condition {
                    field: ConditionField::From,
                    operator: Operator::Contains,
                    value: "alice".to_string(),
                    negate: false,
                },
            ],
            LogicOp::Or,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_condition_matches() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: r"(?i)hello\s+world".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_invalid_pattern_returns_false() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: "[invalid".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn glob_match_basic() {
        assert!(glob_match("hello*", "hello world"));
        assert!(glob_match("*world", "hello world"));
        assert!(glob_match("hello*world", "hello beautiful world"));
        assert!(glob_match("h?llo", "hello"));
        assert!(!glob_match("h?llo", "hllo"));
        assert!(glob_match("*", "anything"));
    }

    #[test]
    fn disabled_rule_never_matches() {
        let mut rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Exists,
                value: String::new(),
                negate: false,
            }],
            LogicOp::And,
        );
        rule.enabled = false;
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn empty_conditions_never_match() {
        let rule = make_rule(vec![], LogicOp::And);
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn exists_operator() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Exists,
                value: String::new(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn body_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Body,
                operator: Operator::Contains,
                value: "test".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            body: "this is a test body".to_string(),
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn has_attachment_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::HasAttachment,
                operator: Operator::Equals,
                value: "true".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            has_attachment: true,
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn header_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Header("X-Priority".to_string()),
                operator: Operator::Equals,
                value: "high".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            headers: vec![("x-priority".to_string(), "high".to_string())],
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_cache_reuses_compiled_pattern() {
        let cache = RegexCache::default();
        let r1 = cache.get_or_compile(r"\d+");
        let r2 = cache.get_or_compile(r"\d+");
        // Both calls should succeed and return equivalent patterns.
        assert!(r1.is_some());
        assert!(r2.is_some());
        // Verify both patterns match the same input.
        assert!(r1.unwrap().is_match("123"));
        assert!(r2.unwrap().is_match("123"));
    }

    #[test]
    fn rule_validation_rejects_invalid_regex() {
        let mut rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: "[invalid".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::InvalidRegex { .. })
        ));
        rule.conditions[0].operator = Operator::Contains;
        rule.conditions[0].value = "ok".to_string();
        assert!(rule.validate().is_ok());
        rule.id = String::new();
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::EmptyRuleId)
        ));
        rule.id = "x".to_string();
        rule.name = String::new();
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::EmptyRuleName)
        ));
    }
}