rsigma-parser 0.27.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
//! Semantic guard and fingerprint for formatted output.

use std::collections::BTreeMap;
use std::fmt::Write as _;

use crate::fmt::context::DocKind;
use crate::fmt::cst::{Model, NodeId, NodeKind, PathSeg, PreludeLine, Prop, key_string};
use crate::fmt::order::Plan;
use crate::fmt::{FormatError, comments, cst, deserialize_values, order, parse_tree};
use crate::parser::parse_sigma_yaml;

const FINGERPRINT_REASON: &str = "comments, anchors, aliases, tags, or directives changed";

/// Everything the guard needs from the source file.
pub(crate) struct Before<'a> {
    pub values: &'a [yaml_serde::Value],
    pub collection: &'a crate::SigmaCollection,
    pub fingerprint: Vec<String>,
    pub kinds: &'a [DocKind],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum CommentRole {
    Leading,
    Trailing,
    Dangling,
}

impl std::fmt::Display for CommentRole {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(match self {
            CommentRole::Leading => "leading",
            CommentRole::Trailing => "trailing",
            CommentRole::Dangling => "dangling",
        })
    }
}

/// Build the per-document fingerprint used to compare the source and output.
///
/// The fingerprint normalises the leading comments of the root mapping's first
/// entry in canonical order into the document header, because output text
/// cannot distinguish them once blank lines are gone.
pub(crate) fn fingerprint(model: &Model, _kinds: &[DocKind], plan: &Plan) -> Vec<String> {
    let mut normalized = model.clone();
    for doc in &mut normalized.docs {
        let root = doc.root;
        let NodeKind::Map { entries, .. } = &mut normalized.nodes[root as usize].kind else {
            continue;
        };
        if entries.is_empty() {
            continue;
        }
        let first = plan.order(root, entries.len())[0];
        let leading = std::mem::take(&mut entries[first].leading);
        doc.header.extend(leading);
    }
    let mut documents = Vec::with_capacity(normalized.docs.len());
    for doc in &normalized.docs {
        let mut lines = Vec::new();
        for line in &doc.prelude {
            match line {
                PreludeLine::Directive(text) => lines.push(format!("directive: {text}")),
                PreludeLine::Comment(comment) => {
                    lines.push(format!("prelude-comment: {}", comment.text));
                }
            }
        }
        for comment in &doc.header {
            lines.push(format!("header: {}", comment.text));
        }
        let mut comments: BTreeMap<(Vec<PathSeg>, CommentRole), Vec<String>> = BTreeMap::new();
        let mut tags: BTreeMap<Vec<PathSeg>, String> = BTreeMap::new();
        let mut anchors: BTreeMap<Vec<PathSeg>, String> = BTreeMap::new();
        let mut aliases: BTreeMap<Vec<PathSeg>, String> = BTreeMap::new();
        collect_fingerprint(
            &normalized,
            doc.root,
            Vec::new(),
            &mut comments,
            &mut tags,
            &mut anchors,
            &mut aliases,
        );
        for ((path, role), texts) in comments {
            lines.push(format!(
                "{role} {}: {}",
                path_text(&path),
                texts.join(" | ")
            ));
        }
        for (path, tag) in tags {
            lines.push(format!("tag {}: {tag}", path_text(&path)));
        }
        for (path, anchor) in anchors {
            lines.push(format!("anchor {}: {anchor}", path_text(&path)));
        }
        for (path, alias) in aliases {
            lines.push(format!("alias {}: {alias}", path_text(&path)));
        }
        if let Some(bindings) = order::bindings(&normalized, None, doc.root) {
            for (path, anchor_path) in bindings {
                lines.push(format!(
                    "binding {}: {}",
                    path_text(&path),
                    path_text(&anchor_path)
                ));
            }
        }
        if let Some(action) = action_value(&normalized, doc.root) {
            lines.push(format!("action: {action}"));
        }
        documents.push(lines.join("\n"));
    }
    documents
}

#[allow(clippy::too_many_arguments)]
fn collect_fingerprint(
    model: &Model,
    id: NodeId,
    path: Vec<PathSeg>,
    comments: &mut BTreeMap<(Vec<PathSeg>, CommentRole), Vec<String>>,
    tags: &mut BTreeMap<Vec<PathSeg>, String>,
    anchors: &mut BTreeMap<Vec<PathSeg>, String>,
    aliases: &mut BTreeMap<Vec<PathSeg>, String>,
) {
    let node = &model.nodes[id as usize];
    for prop in &node.props {
        match prop {
            Prop::Tag(tag) => {
                tags.insert(path.clone(), tag.clone());
            }
            Prop::Anchor(name) => {
                anchors.insert(path.clone(), name.clone());
            }
        }
    }
    match &node.kind {
        NodeKind::Map {
            entries, dangling, ..
        } => {
            for comment in dangling {
                comments
                    .entry((path.clone(), CommentRole::Dangling))
                    .or_default()
                    .push(comment.text.clone());
            }
            for entry in entries {
                let mut entry_path = path.clone();
                entry_path.push(PathSeg::Key(key_string(model, entry.key)));
                let mut key_path = entry_path.clone();
                key_path.push(PathSeg::KeyNode);
                collect_key_props(model, entry.key, &key_path, tags, anchors, aliases);
                for comment in &entry.leading {
                    comments
                        .entry((entry_path.clone(), CommentRole::Leading))
                        .or_default()
                        .push(comment.text.clone());
                }
                if let Some(comment) = &entry.trailing {
                    comments
                        .entry((entry_path.clone(), CommentRole::Trailing))
                        .or_default()
                        .push(comment.text.clone());
                }
                for comment in &entry.extra_trailing {
                    comments
                        .entry((entry_path.clone(), CommentRole::Trailing))
                        .or_default()
                        .push(comment.text.clone());
                }
                collect_fingerprint(
                    model,
                    entry.value,
                    entry_path,
                    comments,
                    tags,
                    anchors,
                    aliases,
                );
            }
        }
        NodeKind::Seq {
            items, dangling, ..
        } => {
            for comment in dangling {
                comments
                    .entry((path.clone(), CommentRole::Dangling))
                    .or_default()
                    .push(comment.text.clone());
            }
            for (index, item) in items.iter().enumerate() {
                let mut item_path = path.clone();
                item_path.push(PathSeg::Index(index));
                for comment in &item.leading {
                    comments
                        .entry((item_path.clone(), CommentRole::Leading))
                        .or_default()
                        .push(comment.text.clone());
                }
                if let Some(comment) = &item.trailing {
                    comments
                        .entry((item_path.clone(), CommentRole::Trailing))
                        .or_default()
                        .push(comment.text.clone());
                }
                for comment in &item.extra_trailing {
                    comments
                        .entry((item_path.clone(), CommentRole::Trailing))
                        .or_default()
                        .push(comment.text.clone());
                }
                collect_fingerprint(
                    model, item.value, item_path, comments, tags, anchors, aliases,
                );
            }
        }
        NodeKind::Alias(name) => {
            aliases.insert(path, name.clone());
        }
        _ => {}
    }
}

fn collect_key_props(
    model: &Model,
    id: NodeId,
    path: &[PathSeg],
    tags: &mut BTreeMap<Vec<PathSeg>, String>,
    anchors: &mut BTreeMap<Vec<PathSeg>, String>,
    aliases: &mut BTreeMap<Vec<PathSeg>, String>,
) {
    let node = &model.nodes[id as usize];
    for prop in &node.props {
        match prop {
            Prop::Tag(tag) => {
                tags.insert(path.to_vec(), tag.clone());
            }
            Prop::Anchor(name) => {
                anchors.insert(path.to_vec(), name.clone());
            }
        }
    }
    if let NodeKind::Alias(name) = &node.kind {
        aliases.insert(path.to_vec(), name.clone());
    }
}

fn action_value(model: &Model, root: NodeId) -> Option<String> {
    let NodeKind::Map { entries, .. } = &model.nodes[root as usize].kind else {
        return None;
    };
    let entry = entries
        .iter()
        .find(|entry| key_string(model, entry.key) == "action")?;
    Some(node_text(model, entry.value))
}

fn node_text(model: &Model, id: NodeId) -> String {
    match &model.nodes[id as usize].kind {
        NodeKind::Scalar { text, .. } => text.clone(),
        NodeKind::Alias(name) => format!("*{name}"),
        _ => String::new(),
    }
}

fn path_text(path: &[PathSeg]) -> String {
    if path.is_empty() {
        return "<root>".to_string();
    }
    let mut out = String::new();
    for (index, segment) in path.iter().enumerate() {
        if index > 0 {
            out.push('/');
        }
        match segment {
            PathSeg::Key(key) => {
                out.push_str(&key.replace('\\', "\\\\").replace('/', "\\/"));
            }
            PathSeg::Index(value) => {
                let _ = write!(out, "#{value}");
            }
            PathSeg::KeyNode => out.push_str("<key>"),
        }
    }
    out
}

/// Check that `output` is semantically identical to the source described by
/// `before`.
pub(crate) fn check(before: &Before<'_>, output: &str) -> Result<(), FormatError> {
    let tree = parse_tree(output).map_err(|_| guard(FINGERPRINT_REASON))?;
    if tree.root_node().has_error() {
        return Err(FormatError::Guard(
            "the output is not valid YAML".to_string(),
        ));
    }
    let values = deserialize_values(output).map_err(|_| guard(FINGERPRINT_REASON))?;
    if values.len() != before.values.len() {
        return Err(FormatError::Guard(format!(
            "document count changed from {} to {}",
            before.values.len(),
            values.len()
        )));
    }
    for (index, (source, formatted)) in before.values.iter().zip(&values).enumerate() {
        if source != formatted {
            return Err(FormatError::Guard(format!(
                "document {} changed value",
                index + 1
            )));
        }
    }
    let collection = parse_sigma_yaml(output).map_err(|_| guard("parser diagnostics changed"))?;
    if collection
        .diagnostics
        .iter()
        .any(|diagnostic| diagnostic.is_error())
    {
        return Err(guard("parser diagnostics changed"));
    }
    if collection.rules != before.collection.rules {
        return Err(guard("parsed rules changed"));
    }
    if collection.correlations != before.collection.correlations {
        return Err(guard("parsed correlations changed"));
    }
    if collection.filters != before.collection.filters {
        return Err(guard("parsed filters changed"));
    }
    let mut formatted_diagnostics = collection.diagnostics.clone();
    let mut source_diagnostics = before.collection.diagnostics.clone();
    for diagnostic in &mut formatted_diagnostics {
        diagnostic.location = None;
        diagnostic.path = None;
    }
    for diagnostic in &mut source_diagnostics {
        diagnostic.location = None;
        diagnostic.path = None;
    }
    if formatted_diagnostics != source_diagnostics {
        return Err(guard("parser diagnostics changed"));
    }
    let mut model = cst::build(output, &tree, &values).map_err(|_| guard(FINGERPRINT_REASON))?;
    comments::attach(&mut model, output).map_err(|_| guard(FINGERPRINT_REASON))?;
    let plan = order::plan(&model, before.kinds);
    let after = fingerprint(&model, before.kinds, &plan);
    if after != before.fingerprint {
        let item = first_difference(&before.fingerprint, &after);
        return Err(FormatError::Guard(match item {
            Some(item) => format!("{FINGERPRINT_REASON}: {item}"),
            None => FINGERPRINT_REASON.to_string(),
        }));
    }
    Ok(())
}

fn first_difference(before: &[String], after: &[String]) -> Option<String> {
    for (index, (source, formatted)) in before.iter().zip(after).enumerate() {
        if source == formatted {
            continue;
        }
        let source_lines: Vec<&str> = source.lines().collect();
        let formatted_lines: Vec<&str> = formatted.lines().collect();
        for (line_index, (source_line, formatted_line)) in
            source_lines.iter().zip(&formatted_lines).enumerate()
        {
            if source_line != formatted_line {
                return Some(format!(
                    "document {} line {}: {} -> {}",
                    index + 1,
                    line_index + 1,
                    source_line,
                    formatted_line
                ));
            }
        }
        if source_lines.len() != formatted_lines.len() {
            let extra = if source_lines.len() > formatted_lines.len() {
                source_lines.get(formatted_lines.len())
            } else {
                formatted_lines.get(source_lines.len())
            };
            return Some(format!(
                "document {} line {}: {}",
                index + 1,
                source_lines.len().min(formatted_lines.len()) + 1,
                extra.copied().unwrap_or("")
            ));
        }
        return Some(format!("document {} fingerprint differs", index + 1));
    }
    if before.len() != after.len() {
        return Some(format!(
            "document count changed from {} to {}",
            before.len(),
            after.len()
        ));
    }
    None
}

fn guard(reason: &str) -> FormatError {
    FormatError::Guard(reason.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fmt::classify;
    use crate::fmt::context::DocKind;
    use crate::fmt::order;

    struct Prepared {
        values: Vec<yaml_serde::Value>,
        collection: crate::SigmaCollection,
        fingerprint: Vec<String>,
        kinds: Vec<DocKind>,
    }

    fn prepare(source: &str) -> Prepared {
        let tree = parse_tree(source).unwrap();
        let values = deserialize_values(source).unwrap();
        let kinds = classify::classify(&values).unwrap();
        let collection = parse_sigma_yaml(source).unwrap();
        let mut model = cst::build(source, &tree, &values).unwrap();
        comments::attach(&mut model, source).unwrap();
        let plan = order::plan(&model, &kinds);
        let fingerprint = fingerprint(&model, &kinds, &plan);
        Prepared {
            values,
            collection,
            fingerprint,
            kinds,
        }
    }

    fn check_pair(source: &str, output: &str) -> Result<(), FormatError> {
        let prepared = prepare(source);
        let before = Before {
            values: &prepared.values,
            collection: &prepared.collection,
            fingerprint: prepared.fingerprint.clone(),
            kinds: &prepared.kinds,
        };
        check(&before, output)
    }

    const RULE: &str = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";

    #[test]
    fn accepts_identical_output() {
        assert!(check_pair(RULE, RULE).is_ok());
    }

    #[test]
    fn refuses_changed_value() {
        let output = RULE.replace("Field: value", "Field: other");
        let error = check_pair(RULE, &output).unwrap_err();
        assert!(matches!(
            error,
            FormatError::Guard(ref reason) if reason.contains("changed value")
        ));
    }

    #[test]
    fn refuses_reordered_sequence() {
        let source = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field:\n            - one\n            - two\n    condition: selection\n";
        let output = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field:\n            - two\n            - one\n    condition: selection\n";
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(
            error,
            FormatError::Guard(ref reason) if reason.contains("changed value")
        ));
    }

    #[test]
    fn refuses_dropped_comment() {
        let source = "title: Test # note\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let output = RULE;
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(
            error,
            FormatError::Guard(ref reason) if reason.contains(FINGERPRINT_REASON)
        ));
    }

    #[test]
    fn refuses_comment_moved_to_another_key() {
        let source = "title: Test\nlogsource: # note\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let output = "title: Test # note\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(
            error,
            FormatError::Guard(ref reason) if reason.contains(FINGERPRINT_REASON)
        ));
    }

    #[test]
    fn refuses_rebound_alias() {
        let source = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: &a one\n        Other: *a\n    condition: selection\n";
        let output = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Other: *a\n        Field: &a one\n    condition: selection\n";
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(error, FormatError::Guard(_)));
    }

    #[test]
    fn refuses_dropped_directive() {
        let source = "%YAML 1.2\n---\ntitle: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let output = RULE;
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(
            error,
            FormatError::Guard(ref reason) if reason.contains(FINGERPRINT_REASON)
        ));
    }

    #[test]
    fn refuses_changed_tag() {
        let source = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: !!str 123\n    condition: selection\n";
        let output = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: !!int 123\n    condition: selection\n";
        let error = check_pair(source, output).unwrap_err();
        assert!(matches!(error, FormatError::Guard(_)));
    }

    #[test]
    fn fingerprint_is_stable_across_key_order() {
        let source = "title: Test\nlogsource:\n    category: test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let reordered = "logsource:\n    category: test\ntitle: Test\ndetection:\n    selection:\n        Field: value\n    condition: selection\n";
        let prepared = prepare(source);
        let before = Before {
            values: &prepared.values,
            collection: &prepared.collection,
            fingerprint: prepared.fingerprint.clone(),
            kinds: &prepared.kinds,
        };
        assert!(check(&before, reordered).is_ok());
    }
}