rsigma-parser 0.28.0

Parser for Sigma detection rules, correlations, and filters
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
//! Metadata overlays: loading, application, and validation.
//!
//! Overlays change metadata on already-selected rules without editing the
//! source files. They target a kept detection rule or correlation by id and
//! apply after dependency closure, before any pipeline.

use std::collections::{HashMap, HashSet};
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use yaml_serde::Value;

use crate::ads::{ADS_PREFIX, AdsContent, EXEMPT_KEY};
use crate::ast::{Level, Status};
use crate::exemplar::{EXEMPLARS_KEY, ExemplarRuleKind, exemplars_from_attrs};

use super::closure;
use super::error::RulesetError;
use super::select::Decision;
use super::source::{
    Candidate, CandidateDoc, DocumentKind, ResolveOptions, SourceFile, confine, relative_to,
};
use super::spec::{
    APPEND_KEYS, OverlayEntry, OverlayPatch, RulesetSpec, SET_KEYS, StaleOverlayPolicy, suggestion,
    validate_patch,
};

/// Where an applied overlay came from.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum OverlayOrigin {
    /// An inline entry at this index in the spec.
    Inline {
        /// Index into the spec's `overlays`.
        index: usize,
    },
    /// An entry at this index in an overlay file.
    File {
        /// The overlay file.
        path: PathBuf,
        /// Index of the entry in that file.
        index: usize,
    },
}

impl fmt::Display for OverlayOrigin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Inline { index } => write!(f, "inline:{index}"),
            Self::File { path, index } => write!(f, "{}:{index}", path.display()),
        }
    }
}

/// An overlay that was applied to a kept document.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppliedOverlay {
    /// Where the overlay entry was declared.
    pub origin: OverlayOrigin,
    /// Key paths that were set, such as `level` or
    /// `custom_attributes.rsigma.exemplars`.
    pub set: Vec<String>,
    /// Key paths that were appended.
    pub appended: Vec<String>,
}

/// Expands `file` and `dir` overlay entries. Inline entries are handled by the
/// resolver and are never passed to a loader.
pub trait OverlayLoader {
    /// Expand one `file` or `dir` entry into its inline entries, in order.
    fn load(
        &self,
        entry: &OverlayEntry,
    ) -> Result<Vec<(OverlayOrigin, OverlayEntry)>, RulesetError>;

    /// Overlay files read by this loader, for the lockfile.
    fn files(&self) -> Vec<SourceFile> {
        Vec::new()
    }
}

/// Reads overlay files and directories from disk, recording what it read.
pub struct FsOverlayLoader {
    /// Reject overlay paths outside this root.
    pub confine_to: Option<PathBuf>,
    files: Mutex<Vec<SourceFile>>,
}

impl FsOverlayLoader {
    /// A loader confined to `confine_to`, when set.
    pub fn new(confine_to: Option<PathBuf>) -> Self {
        Self {
            confine_to,
            files: Mutex::new(Vec::new()),
        }
    }

    fn load_file(
        &self,
        path: &Path,
        opts: &ResolveOptions,
        out: &mut Vec<(OverlayOrigin, OverlayEntry)>,
    ) -> Result<(), RulesetError> {
        let canonical = path.canonicalize().map_err(|e| RulesetError::Io {
            ruleset: None,
            path: path.to_path_buf(),
            source: e,
        })?;
        confine(&canonical, opts)?;
        let bytes = std::fs::read(path).map_err(|e| RulesetError::Io {
            ruleset: None,
            path: path.to_path_buf(),
            source: e,
        })?;
        let text = String::from_utf8_lossy(&bytes);
        let entries = parse_overlay_file(&text, path)?;
        self.files
            .lock()
            .expect("overlay file list lock")
            .push(SourceFile {
                source: 0,
                path: path.to_path_buf(),
                relative: relative_to(path, Path::new(".")),
                sha256_input: bytes,
            });
        for (index, entry) in entries.into_iter().enumerate() {
            out.push((
                OverlayOrigin::File {
                    path: path.to_path_buf(),
                    index,
                },
                entry,
            ));
        }
        Ok(())
    }

    fn load_dir(
        &self,
        dir: &Path,
        opts: &ResolveOptions,
        out: &mut Vec<(OverlayOrigin, OverlayEntry)>,
    ) -> Result<(), RulesetError> {
        if !dir.exists() {
            return Err(RulesetError::Io {
                ruleset: None,
                path: dir.to_path_buf(),
                source: std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "overlay directory does not exist",
                ),
            });
        }
        let canonical = dir.canonicalize().map_err(|e| RulesetError::Io {
            ruleset: None,
            path: dir.to_path_buf(),
            source: e,
        })?;
        confine(&canonical, opts)?;
        let mut paths = Vec::new();
        let mut visited = HashSet::new();
        crate::walk::collect_yaml_files(dir, dir, None, &mut paths, &mut visited).map_err(|e| {
            RulesetError::Io {
                ruleset: None,
                path: dir.to_path_buf(),
                source: std::io::Error::other(e.to_string()),
            }
        })?;
        for path in paths {
            self.load_file(&path, opts, out)?;
        }
        Ok(())
    }
}

impl OverlayLoader for FsOverlayLoader {
    fn load(
        &self,
        entry: &OverlayEntry,
    ) -> Result<Vec<(OverlayOrigin, OverlayEntry)>, RulesetError> {
        if entry.id.is_some() {
            return Err(RulesetError::Overlay(
                "inline overlay entry passed to a filesystem loader".into(),
            ));
        }
        let opts = ResolveOptions {
            confine_to: self.confine_to.clone(),
        };
        let mut out = Vec::new();
        if let Some(file) = &entry.file {
            self.load_file(file, &opts, &mut out)?;
        } else if let Some(dir) = &entry.dir {
            self.load_dir(dir, &opts, &mut out)?;
        } else {
            return Err(RulesetError::Overlay(
                "overlay entry must set one of id, file, or dir".into(),
            ));
        }
        Ok(out)
    }

    fn files(&self) -> Vec<SourceFile> {
        self.files.lock().expect("overlay file list lock").clone()
    }
}

/// A loader that accepts only inline overlays, for fuzzing and benchmarks.
pub struct InlineOnlyOverlayLoader;

impl OverlayLoader for InlineOnlyOverlayLoader {
    fn load(
        &self,
        _entry: &OverlayEntry,
    ) -> Result<Vec<(OverlayOrigin, OverlayEntry)>, RulesetError> {
        Err(RulesetError::Overlay(
            "file and dir overlays need a filesystem loader".into(),
        ))
    }
}

fn parse_overlay_file(text: &str, path: &Path) -> Result<Vec<OverlayEntry>, RulesetError> {
    let entries: Option<Vec<OverlayEntry>> = yaml_serde::from_str(text).map_err(|e| {
        RulesetError::Overlay(format!("cannot parse overlay file {}: {e}", path.display()))
    })?;
    Ok(entries.unwrap_or_default())
}

/// Expand every overlay entry in declaration order, replacing file and dir
/// entries with their contents at that position.
pub(crate) fn expand_overlays(
    name: &str,
    spec: &RulesetSpec,
    loader: &dyn OverlayLoader,
) -> Result<Vec<(OverlayOrigin, OverlayEntry)>, RulesetError> {
    let mut expanded = Vec::new();
    for (index, entry) in spec.overlays.iter().enumerate() {
        if entry.id.is_some() {
            expanded.push((OverlayOrigin::Inline { index }, entry.clone()));
        } else {
            expanded.extend(loader.load(entry).map_err(|e| e.named(name))?);
        }
    }
    for (origin, entry) in &expanded {
        validate_expanded(name, origin, entry)?;
    }
    Ok(expanded)
}

/// Apply overlays to kept candidates and return per-candidate provenance.
pub(crate) fn apply_overlays(
    name: &str,
    spec: &RulesetSpec,
    candidates: &mut [Candidate],
    decisions: &[Decision],
    expanded: Vec<(OverlayOrigin, OverlayEntry)>,
) -> Result<(Vec<Vec<AppliedOverlay>>, Vec<String>), RulesetError> {
    let policy = match &spec.stale_overlays {
        Some(value) => StaleOverlayPolicy::parse(value)
            .map_err(|e| RulesetError::InvalidSpec(vec![format!("ruleset '{name}': {e}")]))?,
        None => StaleOverlayPolicy::Error,
    };

    let mut kept_by_id: HashMap<String, usize> = HashMap::new();
    let mut filter_ids: HashSet<String> = HashSet::new();
    let mut any_by_id: HashMap<String, usize> = HashMap::new();
    for (index, candidate) in candidates.iter().enumerate() {
        let Some(id) = candidate.id() else {
            continue;
        };
        let key = id.to_ascii_lowercase();
        any_by_id.entry(key.clone()).or_insert(index);
        if candidate.kind() == DocumentKind::Filter {
            filter_ids.insert(key);
        } else if closure::is_kept(&decisions[index]) {
            kept_by_id.insert(key, index);
        }
    }

    let mut applied: Vec<Vec<AppliedOverlay>> = vec![Vec::new(); candidates.len()];
    let mut warnings = Vec::new();
    let mut stale = Vec::new();

    for (origin, entry) in expanded {
        let Some(id) = entry.id.as_deref() else {
            // `validate_expanded` guarantees an id.
            continue;
        };
        let key = id.to_ascii_lowercase();
        if let Some(index) = kept_by_id.get(&key).copied() {
            let (set, appended) =
                apply_to_candidate(name, &origin, &mut candidates[index], &entry)?;
            applied[index].push(AppliedOverlay {
                origin,
                set,
                appended,
            });
        } else if filter_ids.contains(&key) {
            return Err(RulesetError::Overlay(format!(
                "ruleset '{name}': overlay {origin} targets id '{id}', which is a filter; filters cannot be overlaid"
            )));
        } else {
            let reason = stale_reason(name, &origin, id, any_by_id.get(&key).copied(), decisions);
            match policy {
                StaleOverlayPolicy::Error => stale.push(reason),
                StaleOverlayPolicy::Warn => warnings.push(reason),
            }
        }
    }

    if !stale.is_empty() {
        return Err(RulesetError::StaleOverlay(stale));
    }
    Ok((applied, warnings))
}

fn stale_reason(
    name: &str,
    origin: &OverlayOrigin,
    id: &str,
    candidate_index: Option<usize>,
    decisions: &[Decision],
) -> String {
    let because = match candidate_index {
        None => "exists in no source".to_string(),
        Some(index) => match &decisions[index] {
            Decision::Excluded { selector } => {
                format!("is excluded by exclude selector {selector}")
            }
            Decision::NotSelected => "is not matched by include".to_string(),
            Decision::Dropped { reason } => format!("was dropped: {reason}"),
            other => format!("is {}", other.as_str()),
        },
    };
    format!("ruleset '{name}': overlay {origin} targets id '{id}', which {because}")
}

struct Metadata<'a> {
    level: &'a mut Option<Level>,
    status: &'a mut Option<Status>,
    description: &'a mut Option<String>,
    author: &'a mut Option<String>,
    tags: &'a mut Vec<String>,
    references: &'a mut Vec<String>,
    falsepositives: &'a mut Vec<String>,
    fields: &'a mut Vec<String>,
    custom_attributes: &'a mut HashMap<String, Value>,
}

fn metadata_mut(doc: &mut CandidateDoc) -> Metadata<'_> {
    match doc {
        CandidateDoc::Detection(rule) => Metadata {
            level: &mut rule.level,
            status: &mut rule.status,
            description: &mut rule.description,
            author: &mut rule.author,
            tags: &mut rule.tags,
            references: &mut rule.references,
            falsepositives: &mut rule.falsepositives,
            fields: &mut rule.fields,
            custom_attributes: &mut rule.custom_attributes,
        },
        CandidateDoc::Correlation(rule) => Metadata {
            level: &mut rule.level,
            status: &mut rule.status,
            description: &mut rule.description,
            author: &mut rule.author,
            tags: &mut rule.tags,
            references: &mut rule.references,
            falsepositives: &mut rule.falsepositives,
            fields: &mut rule.fields,
            custom_attributes: &mut rule.custom_attributes,
        },
        CandidateDoc::Filter(rule) => Metadata {
            level: &mut rule.level,
            status: &mut rule.status,
            description: &mut rule.description,
            author: &mut rule.author,
            tags: &mut rule.tags,
            references: &mut rule.references,
            falsepositives: &mut rule.falsepositives,
            fields: &mut rule.fields,
            custom_attributes: &mut rule.custom_attributes,
        },
    }
}

fn apply_to_candidate(
    name: &str,
    origin: &OverlayOrigin,
    candidate: &mut Candidate,
    entry: &OverlayEntry,
) -> Result<(Vec<String>, Vec<String>), RulesetError> {
    let mut set_keys = Vec::new();
    let mut appended_keys = Vec::new();
    {
        let mut meta = metadata_mut(&mut candidate.doc);
        if let Some(patch) = &entry.set {
            apply_set(name, origin, &mut meta, patch, &mut set_keys)?;
        }
        if let Some(patch) = &entry.append {
            apply_append(name, origin, &mut meta, patch, &mut appended_keys)?;
        }
    }
    validate_touched(name, origin, &candidate.doc, &set_keys, &appended_keys)?;
    Ok((set_keys, appended_keys))
}

fn apply_set(
    name: &str,
    origin: &OverlayOrigin,
    meta: &mut Metadata<'_>,
    patch: &OverlayPatch,
    keys: &mut Vec<String>,
) -> Result<(), RulesetError> {
    if let Some(value) = &patch.level {
        let level = value.parse::<Level>().map_err(|_| {
            overlay_error(
                name,
                origin,
                format!("set level '{value}' is not a valid level"),
            )
        })?;
        *meta.level = Some(level);
        keys.push("level".into());
    }
    if let Some(value) = &patch.status {
        let status = value.parse::<Status>().map_err(|_| {
            overlay_error(
                name,
                origin,
                format!("set status '{value}' is not a valid status"),
            )
        })?;
        *meta.status = Some(status);
        keys.push("status".into());
    }
    if let Some(value) = &patch.description {
        *meta.description = Some(value.clone());
        keys.push("description".into());
    }
    if let Some(value) = &patch.author {
        *meta.author = Some(value.clone());
        keys.push("author".into());
    }
    if let Some(value) = &patch.tags {
        *meta.tags = value.clone();
        keys.push("tags".into());
    }
    if let Some(value) = &patch.references {
        *meta.references = value.clone();
        keys.push("references".into());
    }
    if let Some(value) = &patch.falsepositives {
        *meta.falsepositives = value.clone();
        keys.push("falsepositives".into());
    }
    if let Some(value) = &patch.fields {
        *meta.fields = value.clone();
        keys.push("fields".into());
    }
    if let Some(attributes) = &patch.custom_attributes {
        for (key, value) in attributes {
            if value.is_null() {
                meta.custom_attributes.remove(key);
            } else {
                let converted = canonical_yaml_value(yaml_serde::to_value(value).map_err(|e| {
                    overlay_error(
                        name,
                        origin,
                        format!("cannot convert custom attribute '{key}': {e}"),
                    )
                })?);
                meta.custom_attributes.insert(key.clone(), converted);
            }
            keys.push(format!("custom_attributes.{key}"));
        }
    }
    Ok(())
}

fn apply_append(
    name: &str,
    origin: &OverlayOrigin,
    meta: &mut Metadata<'_>,
    patch: &OverlayPatch,
    keys: &mut Vec<String>,
) -> Result<(), RulesetError> {
    if let Some(values) = &patch.tags {
        for value in values {
            if !meta.tags.contains(value) {
                meta.tags.push(value.clone());
            }
        }
        keys.push("tags".into());
    }
    if let Some(values) = &patch.references {
        meta.references.extend(values.iter().cloned());
        keys.push("references".into());
    }
    if let Some(values) = &patch.falsepositives {
        meta.falsepositives.extend(values.iter().cloned());
        keys.push("falsepositives".into());
    }
    if let Some(values) = &patch.fields {
        meta.fields.extend(values.iter().cloned());
        keys.push("fields".into());
    }
    if let Some(attributes) = &patch.custom_attributes {
        for (key, value) in attributes {
            let converted = canonical_yaml_value(yaml_serde::to_value(value).map_err(|e| {
                overlay_error(
                    name,
                    origin,
                    format!("cannot convert custom attribute '{key}': {e}"),
                )
            })?);
            let Value::Sequence(items) = converted else {
                return Err(overlay_error(
                    name,
                    origin,
                    format!("append custom_attributes.{key} must be a sequence"),
                ));
            };
            match meta.custom_attributes.entry(key.clone()) {
                std::collections::hash_map::Entry::Vacant(slot) => {
                    slot.insert(Value::Sequence(items));
                }
                std::collections::hash_map::Entry::Occupied(mut slot) => match slot.get_mut() {
                    Value::Sequence(existing) => existing.extend(items),
                    _ => {
                        return Err(overlay_error(
                            name,
                            origin,
                            format!(
                                "append custom_attributes.{key} must target an existing sequence"
                            ),
                        ));
                    }
                },
            }
            keys.push(format!("custom_attributes.{key}"));
        }
    }
    Ok(())
}

fn validate_touched(
    name: &str,
    origin: &OverlayOrigin,
    doc: &CandidateDoc,
    set: &[String],
    appended: &[String],
) -> Result<(), RulesetError> {
    let exemplar_path = format!("custom_attributes.{EXEMPLARS_KEY}");
    let touched_exemplars = set.iter().chain(appended).any(|key| key == &exemplar_path);

    let attributes = match doc {
        CandidateDoc::Detection(rule) => &rule.custom_attributes,
        CandidateDoc::Correlation(rule) => &rule.custom_attributes,
        CandidateDoc::Filter(rule) => &rule.custom_attributes,
    };

    if touched_exemplars {
        let kind = match doc {
            CandidateDoc::Detection(_) => ExemplarRuleKind::Detection,
            CandidateDoc::Correlation(_) => ExemplarRuleKind::Correlation,
            CandidateDoc::Filter(_) => ExemplarRuleKind::Filter,
        };
        if let Err(errors) = exemplars_from_attrs(attributes, kind) {
            let detail = errors
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join("; ");
            return Err(overlay_error(
                name,
                origin,
                format!("{EXEMPLARS_KEY} is invalid: {detail}"),
            ));
        }
    }

    for key in set.iter().chain(appended) {
        let Some(attribute) = key.strip_prefix("custom_attributes.") else {
            continue;
        };
        let Some(value) = attributes.get(attribute) else {
            continue;
        };
        if attribute == EXEMPT_KEY {
            if value.as_bool().is_none() {
                return Err(overlay_error(
                    name,
                    origin,
                    format!("{EXEMPT_KEY} must be a boolean"),
                ));
            }
        } else if attribute.starts_with(ADS_PREFIX) && AdsContent::from_yaml(value).is_none() {
            return Err(overlay_error(
                name,
                origin,
                format!("{attribute} must be text or a list of text"),
            ));
        }
    }
    Ok(())
}

fn overlay_error(name: &str, origin: &OverlayOrigin, detail: impl fmt::Display) -> RulesetError {
    RulesetError::Overlay(format!("ruleset '{name}': overlay {origin}: {detail}"))
}

/// Sort mapping keys recursively so overlay-derived YAML does not depend on
/// whether the build enabled `serde_json`'s `preserve_order` feature.
fn canonical_yaml_value(value: Value) -> Value {
    match value {
        Value::Mapping(map) => {
            let mut entries: Vec<(String, Value)> = map
                .into_iter()
                .map(|(key, value)| {
                    let key = match key {
                        Value::String(text) => text,
                        other => format!("{other:?}"),
                    };
                    (key, canonical_yaml_value(value))
                })
                .collect();
            entries.sort_by(|a, b| a.0.cmp(&b.0));
            let mut sorted = yaml_serde::Mapping::new();
            for (key, value) in entries {
                sorted.insert(Value::String(key), value);
            }
            Value::Mapping(sorted)
        }
        Value::Sequence(items) => {
            Value::Sequence(items.into_iter().map(canonical_yaml_value).collect())
        }
        other => other,
    }
}

fn validate_expanded(
    name: &str,
    origin: &OverlayOrigin,
    entry: &OverlayEntry,
) -> Result<(), RulesetError> {
    let mut problems = Vec::new();
    let file_entry = matches!(origin, OverlayOrigin::File { .. });
    let at = format!("overlay {origin}");

    for key in entry.unknown.keys() {
        problems.push(format!(
            "ruleset '{name}': {at} has unknown key '{key}'{}",
            suggestion(key, &["id", "file", "dir", "set", "append"])
        ));
    }
    if file_entry {
        if entry.file.is_some() || entry.dir.is_some() {
            problems.push(format!(
                "ruleset '{name}': {at} must not use file or dir inside an overlay file"
            ));
        }
    } else {
        let forms = [
            entry.id.is_some(),
            entry.file.is_some(),
            entry.dir.is_some(),
        ]
        .into_iter()
        .filter(|set| *set)
        .count();
        if forms != 1 {
            problems.push(format!(
                "ruleset '{name}': {at} must set exactly one of id, file, or dir"
            ));
        }
    }
    if entry.id.is_none() {
        problems.push(format!("ruleset '{name}': {at} needs an id"));
    }
    if entry.set.is_none() && entry.append.is_none() {
        problems.push(format!(
            "ruleset '{name}': {at} needs at least one of set or append"
        ));
    }
    if let Some(patch) = &entry.set {
        validate_patch(name, patch, &at, "set", SET_KEYS, &mut problems);
        for value in patch.level.iter() {
            if value.parse::<Level>().is_err() {
                problems.push(format!(
                    "ruleset '{name}': {at}.set has unknown level '{value}'"
                ));
            }
        }
        for value in patch.status.iter() {
            if value.parse::<Status>().is_err() {
                problems.push(format!(
                    "ruleset '{name}': {at}.set has unknown status '{value}'"
                ));
            }
        }
    }
    if let Some(patch) = &entry.append {
        validate_patch(name, patch, &at, "append", APPEND_KEYS, &mut problems);
        for (key, value) in [
            ("level", patch.level.is_some()),
            ("status", patch.status.is_some()),
            ("description", patch.description.is_some()),
            ("author", patch.author.is_some()),
        ] {
            if value {
                problems.push(format!(
                    "ruleset '{name}': {at}.append cannot set '{key}'; use set"
                ));
            }
        }
    }

    if problems.is_empty() {
        Ok(())
    } else {
        Err(RulesetError::InvalidSpec(problems))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ruleset::ResolvedRuleset;
    use crate::ruleset::source::RuleOrigin;
    use crate::ruleset::spec::RuleSelector;

    fn origin(id: &str) -> RuleOrigin {
        RuleOrigin {
            source: 0,
            root: PathBuf::from("."),
            file: PathBuf::from(format!("{id}.yml")),
            ordinal: 0,
        }
    }

    fn detection(id: &str) -> Candidate {
        let collection = crate::parse_sigma_yaml(&format!(
            "title: Rule {id}\nid: {id}\nlogsource:\n    product: linux\ndetection:\n    selection:\n        Field: value\n    condition: selection\n"
        ))
        .unwrap();
        Candidate {
            doc: CandidateDoc::Detection(collection.rules.into_iter().next().unwrap()),
            origin: origin(id),
        }
    }

    fn correlation(id: &str) -> Candidate {
        let collection = crate::parse_sigma_yaml(&format!(
            "title: Correlation {id}\nid: {id}\ncorrelation:\n    type: event_count\n    rules:\n        - base\n    timespan: 5m\n    condition:\n        gte: 2\n"
        ))
        .unwrap();
        Candidate {
            doc: CandidateDoc::Correlation(collection.correlations.into_iter().next().unwrap()),
            origin: origin(id),
        }
    }

    fn with_attr(candidate: &mut Candidate, key: &str, value: Value) {
        match &mut candidate.doc {
            CandidateDoc::Detection(rule) => {
                rule.custom_attributes.insert(key.to_string(), value);
            }
            CandidateDoc::Correlation(rule) => {
                rule.custom_attributes.insert(key.to_string(), value);
            }
            CandidateDoc::Filter(rule) => {
                rule.custom_attributes.insert(key.to_string(), value);
            }
        }
    }

    fn spec_with_overlays(overlays: Vec<OverlayEntry>) -> RulesetSpec {
        RulesetSpec {
            sources: vec![PathBuf::from("rules")],
            overlays,
            ..Default::default()
        }
    }

    fn resolve_with(
        name: &str,
        spec: &RulesetSpec,
        candidates: Vec<Candidate>,
    ) -> Result<ResolvedRuleset, RulesetError> {
        crate::ruleset::resolve_loaded(
            name,
            spec,
            crate::ruleset::LoadedSources {
                candidates,
                ..Default::default()
            },
            &InlineOnlyOverlayLoader,
        )
    }

    fn exemplar_value() -> Value {
        yaml_serde::to_value(serde_json::json!([{
            "name": "one",
            "expect": "match",
            "event": {"Field": "value"}
        }]))
        .unwrap()
    }

    fn inline(id: &str, set: Option<OverlayPatch>, append: Option<OverlayPatch>) -> OverlayEntry {
        OverlayEntry {
            id: Some(id.into()),
            set,
            append,
            ..Default::default()
        }
    }

    fn level_patch(value: &str) -> OverlayPatch {
        OverlayPatch {
            level: Some(value.into()),
            ..Default::default()
        }
    }

    #[test]
    fn set_replaces_level() {
        let spec = spec_with_overlays(vec![inline("d", Some(level_patch("critical")), None)]);
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        assert_eq!(resolved.collection.rules[0].level, Some(Level::Critical));
        assert_eq!(resolved.entries[0].overlays.len(), 1);
        assert_eq!(resolved.entries[0].overlays[0].set, ["level"]);
    }

    #[test]
    fn invalid_level_errors() {
        let spec = spec_with_overlays(vec![inline("d", Some(level_patch("severe")), None)]);
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert_eq!(
            error.to_string(),
            "ruleset 'test': overlays[0].set has unknown level 'severe'"
        );
    }

    #[test]
    fn custom_attributes_merge_per_key_and_keep_exemplars() {
        let mut rule = detection("d");
        with_attr(&mut rule, "rsigma.exemplars", exemplar_value());
        with_attr(
            &mut rule,
            "rsigma.foo",
            yaml_serde::to_value("old").unwrap(),
        );
        let patch = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.foo".to_string(),
                    serde_json::Value::String("new".into()),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", Some(patch), None)]);
        let resolved = resolve_with("test", &spec, vec![rule]).unwrap();
        let attrs = &resolved.collection.rules[0].custom_attributes;
        assert_eq!(
            attrs.get("rsigma.foo"),
            Some(&yaml_serde::to_value("new").unwrap())
        );
        assert!(attrs.contains_key("rsigma.exemplars"));
    }

    #[test]
    fn null_removes_a_key() {
        let mut rule = detection("d");
        with_attr(&mut rule, "rsigma.foo", yaml_serde::to_value("x").unwrap());
        let patch = OverlayPatch {
            custom_attributes: Some(
                [("rsigma.foo".to_string(), serde_json::Value::Null)]
                    .into_iter()
                    .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", Some(patch), None)]);
        let resolved = resolve_with("test", &spec, vec![rule]).unwrap();
        assert!(
            !resolved.collection.rules[0]
                .custom_attributes
                .contains_key("rsigma.foo")
        );
    }

    #[test]
    fn append_creates_absent_exemplars() {
        let patch = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.exemplars".to_string(),
                    serde_json::json!([{
                        "name": "one",
                        "expect": "match",
                        "event": {"Field": "value"}
                    }]),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", None, Some(patch))]);
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        assert!(
            resolved.collection.rules[0]
                .custom_attributes
                .contains_key("rsigma.exemplars")
        );
        assert_eq!(
            resolved.entries[0].overlays[0].appended,
            ["custom_attributes.rsigma.exemplars"]
        );
    }

    #[test]
    fn append_onto_a_mapping_errors() {
        let mut rule = detection("d");
        with_attr(
            &mut rule,
            "rsigma.foo",
            yaml_serde::from_str("a: b").unwrap(),
        );
        let patch = OverlayPatch {
            custom_attributes: Some(
                [("rsigma.foo".to_string(), serde_json::json!(["x"]))]
                    .into_iter()
                    .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", None, Some(patch))]);
        let error = resolve_with("test", &spec, vec![rule]).unwrap_err();
        assert!(error.to_string().contains("existing sequence"), "{error}");
    }

    #[test]
    fn tags_are_deduplicated() {
        let mut rule = detection("d");
        if let CandidateDoc::Detection(rule) = &mut rule.doc {
            rule.tags = vec!["attack.t1059".into()];
        }
        let patch = OverlayPatch {
            tags: Some(vec![
                "attack.t1059".into(),
                "attack.t1071".into(),
                "attack.t1071".into(),
            ]),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", None, Some(patch))]);
        let resolved = resolve_with("test", &spec, vec![rule]).unwrap();
        assert_eq!(
            resolved.collection.rules[0].tags,
            ["attack.t1059", "attack.t1071"]
        );
    }

    #[test]
    fn set_then_append_within_one_entry() {
        let entry = inline(
            "d",
            Some(OverlayPatch {
                tags: Some(vec!["a".into()]),
                ..Default::default()
            }),
            Some(OverlayPatch {
                tags: Some(vec!["b".into()]),
                ..Default::default()
            }),
        );
        let spec = spec_with_overlays(vec![entry]);
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        assert_eq!(resolved.collection.rules[0].tags, ["a", "b"]);
        assert_eq!(resolved.entries[0].overlays[0].set, ["tags"]);
        assert_eq!(resolved.entries[0].overlays[0].appended, ["tags"]);
    }

    #[test]
    fn two_entries_apply_in_order() {
        let spec = spec_with_overlays(vec![
            inline("d", Some(level_patch("low")), None),
            inline("d", Some(level_patch("high")), None),
        ]);
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        assert_eq!(resolved.collection.rules[0].level, Some(Level::High));
        let origins: Vec<String> = resolved.entries[0]
            .overlays
            .iter()
            .map(|o| o.origin.to_string())
            .collect();
        assert_eq!(origins, ["inline:0", "inline:1"]);
    }

    #[test]
    fn directory_entries_expand_in_sorted_order_at_their_position() {
        let dir = tempfile::tempdir().unwrap();
        let overlays = dir.path().join("overlays");
        std::fs::create_dir_all(&overlays).unwrap();
        std::fs::write(overlays.join("b.yml"), "- id: d\n  set:\n    level: high\n").unwrap();
        std::fs::write(
            overlays.join("a.yml"),
            "- id: d\n  set:\n    level: medium\n",
        )
        .unwrap();

        let spec = RulesetSpec {
            sources: vec![PathBuf::from("rules")],
            overlays: vec![
                inline("d", Some(level_patch("low")), None),
                OverlayEntry {
                    dir: Some(overlays.clone()),
                    ..Default::default()
                },
                inline("d", Some(level_patch("critical")), None),
            ],
            ..Default::default()
        };
        let loader = FsOverlayLoader::new(None);
        let resolved = crate::ruleset::resolve_loaded(
            "test",
            &spec,
            crate::ruleset::LoadedSources {
                candidates: vec![detection("d")],
                ..Default::default()
            },
            &loader,
        )
        .unwrap();
        assert_eq!(resolved.collection.rules[0].level, Some(Level::Critical));
        let origins: Vec<String> = resolved.entries[0]
            .overlays
            .iter()
            .map(|o| o.origin.to_string())
            .collect();
        assert_eq!(
            origins,
            [
                "inline:0".to_string(),
                format!("{}:0", overlays.join("a.yml").display()),
                format!("{}:0", overlays.join("b.yml").display()),
                "inline:2".to_string(),
            ]
        );
        assert_eq!(resolved.overlay_files.len(), 2);
        let files: Vec<String> = resolved
            .overlay_files
            .iter()
            .map(|f| f.path.file_name().unwrap().to_string_lossy().into_owned())
            .collect();
        assert_eq!(files, ["a.yml", "b.yml"]);
    }

    #[test]
    fn empty_and_null_overlay_files_contribute_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let empty = dir.path().join("empty.yml");
        std::fs::write(&empty, "").unwrap();
        let null = dir.path().join("null.yml");
        std::fs::write(&null, "null\n").unwrap();
        let spec = spec_with_overlays(vec![
            OverlayEntry {
                file: Some(empty),
                ..Default::default()
            },
            OverlayEntry {
                file: Some(null),
                ..Default::default()
            },
        ]);
        let loader = FsOverlayLoader::new(None);
        let resolved = crate::ruleset::resolve_loaded(
            "test",
            &spec,
            crate::ruleset::LoadedSources {
                candidates: vec![detection("d")],
                ..Default::default()
            },
            &loader,
        )
        .unwrap();
        assert!(resolved.entries[0].overlays.is_empty());
        assert_eq!(resolved.overlay_files.len(), 2);
    }

    #[test]
    fn unknown_keys_in_a_file_error() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("overlay.yml");
        std::fs::write(&path, "- id: d\n  set:\n    level: low\n    tagz: [x]\n").unwrap();
        let spec = RulesetSpec {
            sources: vec![PathBuf::from("rules")],
            overlays: vec![OverlayEntry {
                file: Some(path),
                ..Default::default()
            }],
            ..Default::default()
        };
        let loader = FsOverlayLoader::new(None);
        let error = crate::ruleset::resolve_loaded(
            "test",
            &spec,
            crate::ruleset::LoadedSources {
                candidates: vec![detection("d")],
                ..Default::default()
            },
            &loader,
        )
        .unwrap_err();
        let message = error.to_string();
        assert!(message.contains("unknown key 'tagz'"), "{message}");
        assert!(message.contains("did you mean 'tags'"), "{message}");
    }

    #[test]
    fn a_file_overlay_cannot_shadow_a_standard_key_or_be_empty() {
        for (body, expected) in [
            (
                "- id: d\n  set:\n    custom_attributes:\n      title: Renamed\n",
                "overlay.yml:0.set.custom_attributes cannot set 'title'",
            ),
            ("- id: d\n  append: {}\n", "overlay.yml:0.append is empty"),
        ] {
            let dir = tempfile::tempdir().unwrap();
            let path = dir.path().join("overlay.yml");
            std::fs::write(&path, body).unwrap();
            let spec = RulesetSpec {
                sources: vec![PathBuf::from("rules")],
                overlays: vec![OverlayEntry {
                    file: Some(path),
                    ..Default::default()
                }],
                ..Default::default()
            };
            let error = crate::ruleset::resolve_loaded(
                "test",
                &spec,
                crate::ruleset::LoadedSources {
                    candidates: vec![detection("d")],
                    ..Default::default()
                },
                &FsOverlayLoader::new(None),
            )
            .unwrap_err();
            let message = error.to_string();
            assert!(message.contains(expected), "{message}");
        }
    }

    #[test]
    fn filter_target_errors() {
        let filter = crate::parse_sigma_yaml(
            "title: Filter\nid: f\nlogsource:\n    product: linux\nfilter:\n    rules: any\n    selection:\n        Field: value\n    condition: selection\n",
        )
        .unwrap()
        .filters
        .remove(0);
        let candidate = Candidate {
            doc: CandidateDoc::Filter(filter),
            origin: origin("f"),
        };
        let spec = spec_with_overlays(vec![inline("f", Some(level_patch("low")), None)]);
        let error = resolve_with("test", &spec, vec![candidate]).unwrap_err();
        assert!(error.to_string().contains("which is a filter"), "{error}");
    }

    #[test]
    fn stale_absent_and_excluded_are_errors() {
        let spec = spec_with_overlays(vec![inline("missing", Some(level_patch("low")), None)]);
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert!(error.to_string().contains("exists in no source"), "{error}");

        let mut spec = spec_with_overlays(vec![inline("d", Some(level_patch("low")), None)]);
        spec.exclude = vec![RuleSelector {
            id: vec!["d".into()],
            ..Default::default()
        }];
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert!(
            error.to_string().contains("excluded by exclude selector 0"),
            "{error}"
        );
    }

    #[test]
    fn stale_warn_records_a_warning() {
        let mut spec = spec_with_overlays(vec![inline("missing", Some(level_patch("low")), None)]);
        spec.stale_overlays = Some("warn".into());
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        assert_eq!(resolved.warnings.len(), 1);
        assert!(
            resolved.warnings[0].contains("exists in no source"),
            "{:?}",
            resolved.warnings
        );
    }

    #[test]
    fn appended_exemplar_with_duplicate_name_errors() {
        let mut rule = detection("d");
        with_attr(&mut rule, "rsigma.exemplars", exemplar_value());
        let patch = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.exemplars".to_string(),
                    serde_json::json!([{
                        "name": "one",
                        "expect": "no-match",
                        "event": {"Field": "other"}
                    }]),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", None, Some(patch))]);
        let error = resolve_with("test", &spec, vec![rule]).unwrap_err();
        assert!(
            error.to_string().contains("duplicate exemplar name"),
            "{error}"
        );
    }

    #[test]
    fn correlation_exemplars_are_validated_as_correlations() {
        let patch = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.exemplars".to_string(),
                    serde_json::json!([{
                        "name": "burst",
                        "expect": "match",
                        "events": [{"offset": "0s", "event": {"Field": "value"}}]
                    }]),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("c", Some(patch.clone()), None)]);
        let resolved =
            resolve_with("test", &spec, vec![detection("base"), correlation("c")]).unwrap();
        assert!(
            resolved.collection.correlations[0]
                .custom_attributes
                .contains_key("rsigma.exemplars")
        );

        let detection_patch = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.exemplars".to_string(),
                    serde_json::json!([{
                        "name": "burst",
                        "expect": "match",
                        "event": {"Field": "value"}
                    }]),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("c", Some(detection_patch), None)]);
        let error =
            resolve_with("test", &spec, vec![detection("base"), correlation("c")]).unwrap_err();
        assert!(error.to_string().contains("must use 'events'"), "{error}");
    }

    #[test]
    fn ads_values_are_validated() {
        let list = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.ads.blind_spots".to_string(),
                    serde_json::json!(["one", "two"]),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", Some(list), None)]);
        resolve_with("test", &spec, vec![detection("d")]).unwrap();

        let mapping = OverlayPatch {
            custom_attributes: Some(
                [(
                    "rsigma.ads.blind_spots".to_string(),
                    serde_json::json!({"one": "two"}),
                )]
                .into_iter()
                .collect(),
            ),
            ..Default::default()
        };
        let spec = spec_with_overlays(vec![inline("d", Some(mapping), None)]);
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert!(
            error.to_string().contains("must be text or a list"),
            "{error}"
        );
    }

    #[test]
    fn provenance_records_set_and_appended_keys() {
        let entry = inline(
            "d",
            Some(OverlayPatch {
                level: Some("high".into()),
                ..Default::default()
            }),
            Some(OverlayPatch {
                tags: Some(vec!["attack.t1059".into()]),
                ..Default::default()
            }),
        );
        let spec = spec_with_overlays(vec![entry]);
        let resolved = resolve_with("test", &spec, vec![detection("d")]).unwrap();
        let overlay = &resolved.entries[0].overlays[0];
        assert_eq!(overlay.origin, OverlayOrigin::Inline { index: 0 });
        assert_eq!(overlay.set, ["level"]);
        assert_eq!(overlay.appended, ["tags"]);
    }

    fn attr_patch(key: &str, value: serde_json::Value) -> OverlayPatch {
        OverlayPatch {
            custom_attributes: Some([(key.to_string(), value)].into_iter().collect()),
            ..Default::default()
        }
    }

    /// Resolve `detection("d")` with one file or dir overlay entry through the
    /// filesystem loader, optionally confined to `root`.
    fn resolve_fs(
        entry: OverlayEntry,
        root: Option<&Path>,
    ) -> Result<ResolvedRuleset, RulesetError> {
        let spec = spec_with_overlays(vec![entry]);
        crate::ruleset::resolve_loaded(
            "test",
            &spec,
            crate::ruleset::LoadedSources {
                candidates: vec![detection("d")],
                ..Default::default()
            },
            &FsOverlayLoader::new(root.map(Path::to_path_buf)),
        )
    }

    fn file_entry(path: PathBuf) -> OverlayEntry {
        OverlayEntry {
            file: Some(path),
            ..Default::default()
        }
    }

    fn dir_entry(path: PathBuf) -> OverlayEntry {
        OverlayEntry {
            dir: Some(path),
            ..Default::default()
        }
    }

    #[test]
    fn ads_exempt_must_be_a_boolean() {
        let patch = attr_patch("rsigma.ads.exempt", serde_json::json!("yes"));
        let spec = spec_with_overlays(vec![inline("d", Some(patch), None)]);
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("rsigma.ads.exempt must be a boolean"),
            "{error}"
        );
    }

    #[test]
    fn appending_a_non_sequence_errors() {
        let patch = attr_patch("team", serde_json::json!("blue"));
        let spec = spec_with_overlays(vec![inline("d", None, Some(patch))]);
        let error = resolve_with("test", &spec, vec![detection("d")]).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("append custom_attributes.team must be a sequence"),
            "{error}"
        );
    }

    #[test]
    fn file_or_dir_inside_an_overlay_file_errors() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("overlay.yml");
        std::fs::write(
            &path,
            "- id: d\n  file: other.yml\n  set:\n    level: low\n",
        )
        .unwrap();
        let error = resolve_fs(file_entry(path), None).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("must not use file or dir inside an overlay file"),
            "{error}"
        );
    }

    #[test]
    fn a_missing_overlay_directory_errors() {
        let dir = tempfile::tempdir().unwrap();
        let missing = dir.path().join("missing");
        let error = resolve_fs(dir_entry(missing.clone()), None).unwrap_err();
        assert!(
            matches!(&error, RulesetError::Io { path, .. } if *path == missing),
            "{error}"
        );
        assert!(
            error
                .to_string()
                .contains("overlay directory does not exist"),
            "{error}"
        );
    }

    #[test]
    fn an_unparseable_overlay_file_errors() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("overlay.yml");
        std::fs::write(&path, "- id: [unclosed\n").unwrap();
        let error = resolve_fs(file_entry(path), None).unwrap_err();
        assert!(
            error.to_string().contains("cannot parse overlay file"),
            "{error}"
        );
    }

    #[test]
    fn the_filesystem_loader_confines_overlay_files_and_dirs() {
        let root = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        let outside_file = outside.path().join("overlay.yml");
        std::fs::write(&outside_file, "- id: d\n  set:\n    level: low\n").unwrap();

        let error = resolve_fs(file_entry(outside_file.clone()), Some(root.path())).unwrap_err();
        assert!(matches!(error, RulesetError::Confinement { .. }), "{error}");

        #[cfg(unix)]
        {
            let overlays = root.path().join("overlays");
            std::fs::create_dir(&overlays).unwrap();
            std::os::unix::fs::symlink(&outside_file, overlays.join("link.yml")).unwrap();
            let error = resolve_fs(dir_entry(overlays), Some(root.path())).unwrap_err();
            assert!(matches!(error, RulesetError::Confinement { .. }), "{error}");
        }
    }

    #[test]
    fn stale_overlays_name_unselected_and_dropped_targets() {
        let mut spec = spec_with_overlays(vec![inline("d", Some(level_patch("low")), None)]);
        spec.include = vec![RuleSelector {
            id: vec!["other".into()],
            ..Default::default()
        }];
        let error =
            resolve_with("test", &spec, vec![detection("d"), detection("other")]).unwrap_err();
        assert!(
            error.to_string().contains("is not matched by include"),
            "{error}"
        );

        let mut spec = spec_with_overlays(vec![inline("c", Some(level_patch("low")), None)]);
        spec.dependencies = Some("drop".into());
        spec.exclude = vec![RuleSelector {
            id: vec!["base".into()],
            ..Default::default()
        }];
        let error =
            resolve_with("test", &spec, vec![detection("base"), correlation("c")]).unwrap_err();
        assert!(error.to_string().contains("was dropped"), "{error}");
    }
}