rsigma-eval 0.22.0

Evaluator for Sigma detection and correlation rules — match rules against events
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//! Replay embedded `rsigma.exemplars` against their host rules.

use std::collections::HashMap;
use std::fmt;

use rsigma_parser::{
    Exemplar, ExemplarErrorKind, ExemplarPayload, ExemplarRuleKind, ExemplarShapeError, Expect,
    SigmaCollection, SigmaRule, correlation_exemplars, exemplars, filter_exemplars,
};
use serde::Serialize;

use crate::compiler::yaml_to_json;
use crate::correlation_engine::{CorrelationConfig, CorrelationEngine};
use crate::engine::Engine;
use crate::error::EvalError;
use crate::event::JsonEvent;
use crate::pipeline::Pipeline;
use crate::result::EvaluationResult;

/// Deterministic base timestamp for correlation exemplar offsets.
const BASE_TIMESTAMP: i64 = 1_700_000_000;

/// One asserted exemplar outcome.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ExemplarResult {
    /// Rule `id`, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rule_id: Option<String>,
    /// Rule title.
    pub rule_title: String,
    /// Detection or correlation.
    pub rule_kind: ExemplarRuleKind,
    /// 0-based index in the source list.
    pub index: usize,
    /// Display name.
    pub name: String,
    /// Expected outcome.
    pub expect: Expect,
    /// Observed outcome.
    pub actual: Expect,
    /// Whether `actual` matches `expect`.
    pub passed: bool,
    /// Why the assertion failed. Absent on passing exemplars.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub diagnostic: Option<String>,
}

/// A detection or correlation rule that carried no exemplars.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MissingExemplars {
    /// Rule `id`, when present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rule_id: Option<String>,
    /// Rule title.
    pub rule_title: String,
    /// Detection or correlation.
    pub rule_kind: ExemplarRuleKind,
}

/// Report of every asserted exemplar plus rules with none.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ExemplarReport {
    /// Caller-supplied source label (path or `inline`).
    #[serde(skip_serializing_if = "String::is_empty")]
    pub source: String,
    /// Per-exemplar outcomes, in collection then list order.
    pub results: Vec<ExemplarResult>,
    /// Detection and correlation rules that carried no exemplars.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub missing: Vec<MissingExemplars>,
}

impl ExemplarReport {
    /// True when every asserted exemplar passed.
    pub fn all_passed(&self) -> bool {
        self.results.iter().all(|r| r.passed)
    }

    /// Failed assertions.
    pub fn failures(&self) -> impl Iterator<Item = &ExemplarResult> {
        self.results.iter().filter(|r| !r.passed)
    }
}

/// Configuration or compilation failure that aborts the run.
#[derive(Debug)]
pub enum ExemplarRunError {
    /// Structurally invalid exemplars on a named rule.
    Shape {
        /// Rule title or id used in the diagnostic.
        rule: String,
        /// Parser shape errors.
        errors: Vec<ExemplarShapeError>,
    },
    /// Title fallback is not unique in the collection.
    AmbiguousTitle(String),
    /// A correlation referenced a rule that is not in the collection.
    Reference(String),
    /// A rule or pipeline failed to compile.
    Compile(EvalError),
}

impl fmt::Display for ExemplarRunError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Shape { rule, errors } => {
                write!(f, "invalid exemplars on '{rule}': ")?;
                let messages: Vec<String> = errors.iter().map(ToString::to_string).collect();
                f.write_str(&messages.join("; "))
            }
            Self::AmbiguousTitle(title) => {
                write!(
                    f,
                    "ambiguous rule title '{title}': add an id or make the title unique"
                )
            }
            Self::Reference(msg) => f.write_str(msg),
            Self::Compile(err) => write!(f, "{err}"),
        }
    }
}

impl std::error::Error for ExemplarRunError {}

impl From<EvalError> for ExemplarRunError {
    fn from(err: EvalError) -> Self {
        match err {
            EvalError::UnknownRuleRef(r) => Self::Reference(format!("unknown rule reference: {r}")),
            other => Self::Compile(other),
        }
    }
}

/// Replay every embedded exemplar in `collection` with fresh engine state.
pub fn run_exemplars(
    collection: &SigmaCollection,
    pipelines: &[Pipeline],
) -> Result<ExemplarReport, ExemplarRunError> {
    let titles = title_counts(collection);
    let mut results = Vec::new();
    let mut missing = Vec::new();

    for rule in &collection.rules {
        let identity = TargetIdentity {
            id: rule.id.clone(),
            title: rule.title.clone(),
            kind: ExemplarRuleKind::Detection,
        };
        let list = exemplars(rule).map_err(|errors| ExemplarRunError::Shape {
            rule: identity.label(),
            errors,
        })?;
        if list.is_empty() {
            missing.push(identity.to_missing());
            continue;
        }
        identity.require_unique(&titles)?;
        for exemplar in list {
            results.push(run_detection(
                collection, rule, &identity, &exemplar, pipelines,
            )?);
        }
    }

    for rule in &collection.correlations {
        let identity = TargetIdentity {
            id: rule.id.clone(),
            title: rule.title.clone(),
            kind: ExemplarRuleKind::Correlation,
        };
        let list = correlation_exemplars(rule).map_err(|errors| ExemplarRunError::Shape {
            rule: identity.label(),
            errors,
        })?;
        if list.is_empty() {
            missing.push(identity.to_missing());
            continue;
        }
        identity.require_unique(&titles)?;
        for exemplar in list {
            results.push(run_correlation(
                collection, &identity, &exemplar, pipelines,
            )?);
        }
    }

    for rule in &collection.filters {
        if let Err(errors) = filter_exemplars(rule) {
            return Err(ExemplarRunError::Shape {
                rule: rule.id.clone().unwrap_or_else(|| rule.title.clone()),
                errors,
            });
        }
    }

    Ok(ExemplarReport {
        source: String::new(),
        results,
        missing,
    })
}

struct TargetIdentity {
    id: Option<String>,
    title: String,
    kind: ExemplarRuleKind,
}

impl TargetIdentity {
    fn label(&self) -> String {
        self.id.clone().unwrap_or_else(|| self.title.clone())
    }

    fn require_unique(&self, titles: &HashMap<String, usize>) -> Result<(), ExemplarRunError> {
        if self.id.is_some() {
            return Ok(());
        }
        if titles.get(&self.title).copied().unwrap_or(0) > 1 {
            return Err(ExemplarRunError::AmbiguousTitle(self.title.clone()));
        }
        Ok(())
    }

    fn to_missing(&self) -> MissingExemplars {
        MissingExemplars {
            rule_id: self.id.clone(),
            rule_title: self.title.clone(),
            rule_kind: self.kind,
        }
    }

    fn matches(&self, result: &EvaluationResult) -> bool {
        let kind_ok = match self.kind {
            ExemplarRuleKind::Detection => result.is_detection(),
            ExemplarRuleKind::Correlation => result.is_correlation(),
            ExemplarRuleKind::Filter => false,
        };
        if !kind_ok {
            return false;
        }
        match &self.id {
            Some(id) => result.header.rule_id.as_deref() == Some(id.as_str()),
            None => result.header.rule_title == self.title,
        }
    }
}

fn title_counts(collection: &SigmaCollection) -> HashMap<String, usize> {
    let mut counts = HashMap::new();
    for title in collection
        .rules
        .iter()
        .map(|r| r.title.as_str())
        .chain(collection.correlations.iter().map(|r| r.title.as_str()))
    {
        *counts.entry(title.to_string()).or_insert(0) += 1;
    }
    counts
}

fn run_detection(
    collection: &SigmaCollection,
    rule: &SigmaRule,
    identity: &TargetIdentity,
    exemplar: &Exemplar,
    pipelines: &[Pipeline],
) -> Result<ExemplarResult, ExemplarRunError> {
    let ExemplarPayload::Event(event) = &exemplar.payload else {
        return Err(ExemplarRunError::Shape {
            rule: identity.label(),
            errors: vec![ExemplarShapeError {
                path: format!("/custom_attributes/rsigma.exemplars/{}", exemplar.index),
                message: "detection exemplars must use 'event'".to_string(),
                kind: ExemplarErrorKind::WrongRuleKind,
            }],
        });
    };
    let synthetic = SigmaCollection {
        rules: vec![rule.clone()],
        correlations: Vec::new(),
        filters: collection.filters.clone(),
        errors: Vec::new(),
    };
    let mut engine = Engine::new();
    if pipelines.is_empty() {
        engine.add_collection(&synthetic)?;
    } else {
        engine.add_collection_with_pipelines(&synthetic, pipelines)?;
    }
    let json = yaml_to_json(event);
    let je = JsonEvent::borrow(&json);
    let matches = engine.evaluate(&je);
    let fired = matches.iter().any(|r| identity.matches(r));
    let diagnostic = match (fired, exemplar.expect) {
        (true, Expect::NoMatch) => Some("the rule matched the event".to_string()),
        (false, Expect::Match) => Some("the rule did not match the event".to_string()),
        _ => None,
    };
    Ok(outcome(identity, exemplar, fired, diagnostic))
}

fn run_correlation(
    collection: &SigmaCollection,
    identity: &TargetIdentity,
    exemplar: &Exemplar,
    pipelines: &[Pipeline],
) -> Result<ExemplarResult, ExemplarRunError> {
    let ExemplarPayload::Sequence(events) = &exemplar.payload else {
        return Err(ExemplarRunError::Shape {
            rule: identity.label(),
            errors: vec![ExemplarShapeError {
                path: format!("/custom_attributes/rsigma.exemplars/{}", exemplar.index),
                message: "correlation exemplars must use 'events'".to_string(),
                kind: ExemplarErrorKind::WrongRuleKind,
            }],
        });
    };
    let mut engine = CorrelationEngine::new(CorrelationConfig::default());
    for pipeline in pipelines {
        engine.add_pipeline(pipeline.clone());
    }
    engine.add_collection(collection)?;
    let owned: Vec<serde_json::Value> = events.iter().map(|e| yaml_to_json(&e.event)).collect();
    let mut first_fire: Option<(usize, String)> = None;
    for (index, (timed, json)) in events.iter().zip(owned.iter()).enumerate() {
        let je = JsonEvent::borrow(json);
        let ts = BASE_TIMESTAMP.saturating_add(timed.offset.seconds as i64);
        let results = engine.process_event_at(&je, ts);
        if first_fire.is_none() && results.iter().any(|r| identity.matches(r)) {
            first_fire = Some((index, timed.offset.original.clone()));
        }
    }
    let fired = first_fire.is_some();
    let diagnostic = match (first_fire, exemplar.expect) {
        (Some((index, offset)), Expect::NoMatch) => Some(format!(
            "the correlation fired at event index {index} (offset {offset})"
        )),
        (None, Expect::Match) => Some(format!(
            "the correlation never fired across {} events",
            events.len()
        )),
        _ => None,
    };
    Ok(outcome(identity, exemplar, fired, diagnostic))
}

fn outcome(
    identity: &TargetIdentity,
    exemplar: &Exemplar,
    fired: bool,
    diagnostic: Option<String>,
) -> ExemplarResult {
    let actual = if fired {
        Expect::Match
    } else {
        Expect::NoMatch
    };
    ExemplarResult {
        rule_id: identity.id.clone(),
        rule_title: identity.title.clone(),
        rule_kind: identity.kind,
        index: exemplar.index,
        name: exemplar.name.clone(),
        expect: exemplar.expect,
        actual,
        passed: actual == exemplar.expect,
        diagnostic,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rsigma_parser::parse_sigma_yaml;

    fn collection(yaml: &str) -> SigmaCollection {
        parse_sigma_yaml(yaml).unwrap()
    }

    fn run(yaml: &str) -> ExemplarReport {
        run_exemplars(&collection(yaml), &[]).expect("run")
    }

    const DETECTION: &str = r#"
title: Whoami
id: 11111111-2222-3333-4444-555555555555
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        CommandLine|contains: whoami
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - name: whoami fires
          expect: match
          event:
              CommandLine: whoami /all
        - name: benign hostname
          expect: no-match
          event:
              CommandLine: hostname
"#;

    #[test]
    fn detection_match_and_no_match() {
        let report = run(DETECTION);
        assert!(report.all_passed());
        assert_eq!(report.results.len(), 2);
        assert_eq!(report.results[0].actual, Expect::Match);
        assert_eq!(report.results[1].actual, Expect::NoMatch);
        assert!(report.missing.is_empty());
    }

    #[test]
    fn failed_assertion_is_not_a_run_error() {
        let yaml = r#"
title: Whoami
id: 11111111-2222-3333-4444-555555555555
logsource:
    category: process_creation
detection:
    selection:
        CommandLine|contains: whoami
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - expect: match
          event:
              CommandLine: hostname
"#;
        let report = run(yaml);
        assert!(!report.all_passed());
        assert_eq!(report.results[0].actual, Expect::NoMatch);
        assert_eq!(
            report.results[0].diagnostic.as_deref(),
            Some("the rule did not match the event")
        );
    }

    #[test]
    fn passing_exemplars_carry_no_diagnostic() {
        let report = run(DETECTION);
        assert!(report.results.iter().all(|r| r.diagnostic.is_none()));
    }

    #[test]
    fn failed_correlation_no_match_names_the_firing_event() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 2
custom_attributes:
    rsigma.exemplars:
        - expect: no-match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 30s
                event: { EventType: login, User: alice }
"#;
        let report = run(yaml);
        assert!(!report.all_passed());
        assert_eq!(
            report.results[0].diagnostic.as_deref(),
            Some("the correlation fired at event index 1 (offset 30s)")
        );
    }

    #[test]
    fn suppression_and_reset_do_not_mask_the_first_fire() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 2
custom_attributes:
    rsigma.suppress: 5m
    rsigma.action: reset
    rsigma.exemplars:
        - name: fires despite suppression
          expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 1s
                event: { EventType: login, User: alice }
              - offset: 2s
                event: { EventType: login, User: alice }
              - offset: 3s
                event: { EventType: login, User: alice }
        - name: single event stays quiet
          expect: no-match
          events:
              - offset: 0s
                event: { EventType: login, User: bob }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
        assert_eq!(report.results.len(), 2);
    }

    #[test]
    fn filter_excludes_matching_event() {
        let yaml = r#"
title: Whoami
id: 11111111-2222-3333-4444-555555555555
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        CommandLine|contains: whoami
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - name: alice fires
          expect: match
          event:
              CommandLine: whoami /all
              User: alice
        - name: system filtered
          expect: no-match
          event:
              CommandLine: whoami /all
              User: SYSTEM
---
title: Exclude SYSTEM
logsource:
    category: process_creation
    product: windows
filter:
    rules:
        - 11111111-2222-3333-4444-555555555555
    selection:
        User: SYSTEM
    condition: not selection
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
    }

    #[test]
    fn pipeline_rewrites_rule_fields() {
        let yaml = r#"
title: Whoami
id: 11111111-2222-3333-4444-555555555555
logsource:
    category: process_creation
detection:
    selection:
        CommandLine|contains: whoami
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - expect: match
          event:
              process.command_line: whoami /all
"#;
        let pipeline = crate::parse_pipeline(
            r#"
name: map
priority: 10
transformations:
  - type: field_name_mapping
    mapping:
        CommandLine: process.command_line
"#,
        )
        .unwrap();
        let report = run_exemplars(&collection(yaml), &[pipeline]).unwrap();
        assert!(report.all_passed(), "{report:?}");
    }

    #[test]
    fn event_count_correlation() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
id: many-logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 3
custom_attributes:
    rsigma.exemplars:
        - name: burst
          expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 1s
                event: { EventType: login, User: alice }
              - offset: 2s
                event: { EventType: login, User: alice }
        - name: too few
          expect: no-match
          events:
              - offset: 0s
                event: { EventType: login, User: bob }
              - offset: 1s
                event: { EventType: login, User: bob }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
        assert_eq!(report.missing.len(), 1);
        assert_eq!(report.missing[0].rule_title, "Login");
    }

    #[test]
    fn value_count_correlation() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Distinct Hosts
correlation:
    type: value_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        field: Host
        gte: 2
custom_attributes:
    rsigma.exemplars:
        - expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice, Host: a }
              - offset: 1s
                event: { EventType: login, User: alice, Host: b }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
    }

    #[test]
    fn temporal_correlation() {
        let yaml = r#"
title: Failed
id: failed-login
logsource:
    category: auth
detection:
    selection:
        EventType: failed
    condition: selection
---
title: Success
id: success-login
logsource:
    category: auth
detection:
    selection:
        EventType: success
    condition: selection
---
title: Fail then success
correlation:
    type: temporal
    rules:
        - failed-login
        - success-login
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 2
custom_attributes:
    rsigma.exemplars:
        - expect: match
          events:
              - offset: 0s
                event: { EventType: failed, User: alice }
              - offset: 10s
                event: { EventType: success, User: alice }
        - expect: no-match
          events:
              - offset: 0s
                event: { EventType: failed, User: bob }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
    }

    #[test]
    fn exemplars_do_not_share_state() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 3
custom_attributes:
    rsigma.exemplars:
        - name: first burst
          expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 1s
                event: { EventType: login, User: alice }
              - offset: 2s
                event: { EventType: login, User: alice }
        - name: second burst
          expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 1s
                event: { EventType: login, User: alice }
              - offset: 2s
                event: { EventType: login, User: alice }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
        assert_eq!(report.results.len(), 2);
    }

    #[test]
    fn unrelated_detection_matches_do_not_count() {
        let yaml = r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Also login
id: also-login
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
id: many-logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 2
custom_attributes:
    rsigma.exemplars:
        - expect: match
          events:
              - offset: 0s
                event: { EventType: login, User: alice }
              - offset: 1s
                event: { EventType: login, User: alice }
"#;
        let report = run(yaml);
        assert!(report.all_passed(), "{report:?}");
        assert_eq!(report.results[0].rule_id.as_deref(), Some("many-logins"));
    }

    #[test]
    fn missing_correlation_ref_is_config_error() {
        let yaml = r#"
title: Many Logins
correlation:
    type: event_count
    rules:
        - missing-rule
    timespan: 60s
    condition:
        gte: 2
custom_attributes:
    rsigma.exemplars:
        - expect: match
          events:
              - offset: 0s
                event: { EventType: login }
              - offset: 1s
                event: { EventType: login }
"#;
        let err = run_exemplars(&collection(yaml), &[]).unwrap_err();
        assert!(matches!(err, ExemplarRunError::Reference(_)), "{err}");
    }

    #[test]
    fn duplicate_titles_without_id_are_rejected() {
        let yaml = r#"
title: Dup
logsource:
    category: test
detection:
    selection:
        field: a
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - expect: match
          event:
              field: a
---
title: Dup
logsource:
    category: test
detection:
    selection:
        field: b
    condition: selection
"#;
        let err = run_exemplars(&collection(yaml), &[]).unwrap_err();
        assert!(matches!(err, ExemplarRunError::AmbiguousTitle(_)), "{err}");
    }

    #[test]
    fn malformed_exemplars_are_rejected() {
        let yaml = r#"
title: Whoami
logsource:
    category: test
detection:
    selection:
        field: value
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - expect: banana
          event:
              field: value
"#;
        let err = run_exemplars(&collection(yaml), &[]).unwrap_err();
        assert!(matches!(err, ExemplarRunError::Shape { .. }), "{err}");
    }

    #[test]
    fn filter_exemplars_are_rejected() {
        let yaml = r#"
title: F
logsource:
    category: test
filter:
    selection:
        User: SYSTEM
    condition: selection
custom_attributes:
    rsigma.exemplars:
        - expect: match
          event:
              User: SYSTEM
"#;
        let err = run_exemplars(&collection(yaml), &[]).unwrap_err();
        assert!(matches!(err, ExemplarRunError::Shape { .. }), "{err}");
    }
}