stixflayer 0.1.1

Fast, spec-correct STIX 2.1 parsing, validation, and serialization in Rust.
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
//! Data structures used by the STIX Patterning parser

use crate::error::CheckContext;
use std::collections::HashSet;

use crate::{
    base::Stix,
    error::{add_error, return_multiple_errors, StixError as Error},
    types::{ScoTypes, Timestamp},
};
use ordered_float::OrderedFloat;
use strum::IntoEnumIterator;

/// A STIX Pattern Constant
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Constant {
    /// A boolean literal.
    Boolean(bool),
    /// A binary literal, such as `b'0123'`.
    Binary(String),
    /// A hexadecimal literal, such as `h'ff'`.
    Hex(String),
    /// An integer literal.
    Integer(i64),
    /// A floating-point literal.
    Float(OrderedFloat<f64>),
    /// A string literal.
    String(String),
    /// A timestamp literal.
    Timestamp(Timestamp),
    /// A set of constants, as used with the `IN` operator.
    Set(Vec<Constant>),
}

/// A STIX Pattern Observation Expression
///
/// For more information, see <https://docs.oasis-open.org/cti/stix/v2.1/os/stix-v2.1-os.html#_e8slinrhxcc9> for details of the STIX Patterning grammar
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObservationExpression {
    /// One or more comparison expressions, optionally joined by boolean operators.
    Comparison(ComparisonComponent),
    /// Two observation expressions joined by an observation operator.
    ObservationOp(
        Box<ObservationExpression>,
        ObservationOperator,
        Box<ObservationExpression>,
    ),
    /// An observation expression qualified by a `REPEATS`, `WITHIN`, or `START`/`STOP` qualifier.
    Qualification(Box<ObservationExpression>, Qualifier),
}
impl ObservationExpression {
    fn validate(
        &self,
        errors: &mut Vec<Error>,
        qualifiers: &mut HashSet<String>,
    ) -> Result<(), Error> {
        match self {
            // Validate an observation expression that is only one or more connected comparision expressions
            Self::Comparison(comparison) => add_error(errors, comparison.validate()),
            // Validate two observation expressions joined by an observation operator
            Self::ObservationOp(lhs, _, rhs) => {
                add_error(errors, lhs.validate(&mut errors.clone(), qualifiers));
                add_error(errors, rhs.validate(&mut errors.clone(), qualifiers));
            }
            // Validate an observation expression followed by a qualifier
            Self::Qualification(observation, qualifier) => {
                add_error(
                    errors,
                    observation.validate(&mut errors.clone(), qualifiers),
                );
                add_error(errors, qualifier.check_qualifier(qualifiers));
            }
        };

        return_multiple_errors(errors.clone())
    }
}

impl Stix for ObservationExpression {
    fn check(&self, _ctx: &mut CheckContext) -> Result<(), Error> {
        let mut errors = Vec::new();
        let mut qualifiers = HashSet::new();

        self.validate(&mut errors, &mut qualifiers)
    }
}
/// Left-associative Boolean comparison of one or more Comparison Expressions
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComparisonComponent {
    /// A single comparison expression.
    ComparisonExpression(ComparisonExpression),
    /// Two comparison components joined by a boolean operator.
    BooleanOp(
        Box<ComparisonComponent>,
        BooleanOperator,
        Box<ComparisonComponent>,
    ),
}

impl ComparisonComponent {
    /// Get the object type from one or more comparison expressions, **if** those object types are all identical
    fn get_matching_object(&self) -> Option<&str> {
        match self {
            Self::ComparisonExpression(expression) => Some(expression.get_object()),
            Self::BooleanOp(lhs, _, rhs) => {
                let lhs_object = lhs.get_matching_object();
                let rhs_object = rhs.get_matching_object();
                match (lhs_object, rhs_object) {
                    (Some(lhs_object), Some(rhs_object)) if lhs_object == rhs_object => {
                        Some(lhs_object)
                    }
                    _ => None,
                }
            }
        }
    }

    fn validate(&self) -> Result<(), Error> {
        let mut errors = Vec::new();

        match self {
            Self::ComparisonExpression(comparison) => add_error(&mut errors, comparison.validate()),
            Self::BooleanOp(lhs, operator, rhs) => {
                add_error(&mut errors, lhs.validate());
                add_error(&mut errors, rhs.validate());
                // Check that if comparison expressions are connect by an AND, both of them refer to the same object type
                if let BooleanOperator::And = operator {
                    if self.get_matching_object().is_none() {
                        errors.push(Error::ValidationError("A pair of Comparison Expressions in the pattern has an AND Boolean Operator between different objects".to_string()));
                    }
                }
            }
        }

        return_multiple_errors(errors)
    }
}

/// A STIX Pattern Comparison Expression
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ComparisonExpression {
    /// The path to the observable property being compared.
    pub object_path: ObjectPath,
    /// The operator applied between the object path and the constant.
    pub operator: ComparisonOperator,
    /// The constant compared against the object path; `None` for `EXISTS` comparisons.
    pub constant: Option<Constant>,
    /// Whether the comparison is negated with `NOT`.
    ///
    /// Negation is preserved rather than rewritten to the opposite operator, so
    /// `NOT IN`, `NOT LIKE`, and `NOT EXISTS` keep the operator the user wrote.
    pub negated: bool,
}

impl ComparisonExpression {
    fn get_object(&self) -> &str {
        &self.object_path.sco
    }

    fn validate(&self) -> Result<(), Error> {
        self.object_path.validate()?;

        if let ComparisonOperator::Exists = self.operator {
            if self.constant.is_some() {
                return Err(Error::ValidationError("A comparison expression in the pattern has an 'EXISTS' Comparison Operator but contains a constant".to_string()));
            }
        } else {
            let Some(ref constant) = self.constant else {
                return Err(Error::ValidationError("A comparison expression in the pattern has a Comparison Operator other than `EXISTS` but is missing a constant".to_string()));
            };

            match self.operator {
                ComparisonOperator::In => {
                    let Constant::Set(_) = constant else {
                        return Err(Error::ValidationError("A comparison expression in the pattern has an 'IN' Comparison Operator that is not applied to a set of constants".to_string()));
                    };
                }
                ComparisonOperator::Like
                | ComparisonOperator::IsSubset
                | ComparisonOperator::IsSuperset => {
                    let Constant::String(_) = constant else {
                        return Err(Error::ValidationError("A comparison expression in the pattern has a Comparison Operator that expects a string but is not applied to a string".to_string()));
                    };
                }
                ComparisonOperator::Matches => {
                    let Constant::String(_) = constant else {
                        return Err(Error::ValidationError("A comparison expression in the pattern has a `MATCHES` Comparison Operator that expects a regular expression string but is not applied to a string".to_string()));
                    };
                }
                _ => (),
            }
        }

        Ok(())
    }
}

/// A STIX Pattern Object Path
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectPath {
    /// The observable type name at the start of the path, e.g. `ipv4-addr`.
    pub sco: String,
    /// The property path after the `:`, which may index into lists and nested objects.
    pub property: String,
}

impl ObjectPath {
    fn validate(&self) -> Result<(), Error> {
        // 1. Validate SCO type
        let is_known_sco = ScoTypes::iter().any(|x| x.as_ref() == self.sco);
        let is_valid_custom = self.sco.len() >= 3
            && self.sco.len() <= 250
            && self
                .sco
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-');

        if !is_known_sco && !is_valid_custom {
            return Err(Error::ValidationError(format!(
                "'{}' is not a valid observable type name",
                self.sco
            )));
        }

        // 2. Reject double quotes anywhere in the property path
        if self.property.contains('"') {
            return Err(Error::ValidationError(format!(
                "Property path '{}' contains invalid double quotes",
                self.property
            )));
        }

        // 3. Validate first property component length
        let first_prop = self
            .property
            .split(['.', '['])
            .next()
            .unwrap_or("")
            .trim_matches('\'');

        if !first_prop.is_empty() && first_prop != "id" && first_prop.len() < 3 {
            return Err(Error::ValidationError(format!(
                "'{}' is not a valid observable property name",
                first_prop
            )));
        }

        Ok(())
    }
}

/// A STIX Pattern Comparison Operator
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComparisonOperator {
    /// An equality comparison (`=`).
    Eq,
    /// An inequality comparison (`!=`).
    Neq,
    /// A greater-than comparison (`>`).
    Gt,
    /// A less-than comparison (`<`).
    Lt,
    /// A greater-than-or-equal comparison (`>=`).
    Gte,
    /// A less-than-or-equal comparison (`<=`).
    Lte,
    /// A test that the path's value is in a set of constants (`IN`).
    In,
    /// A wild-card string comparison (`LIKE`).
    Like,
    /// A regular-expression string comparison (`MATCHES`).
    Matches,
    /// A test that the path's value is a subset of the string constant (`ISSUBSET`).
    IsSubset,
    /// A test that the path's value is a superset of the string constant (`ISSUPERSET`).
    IsSuperset,
    /// A test of whether the property exists (`EXISTS`); takes no constant.
    Exists,
}

/// A STIX Pattern Boolean Operator
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BooleanOperator {
    /// A boolean OR (`OR`) between comparison expressions.
    Or,
    /// A boolean AND (`AND`) between comparison expressions.
    And,
}

/// A STIX Pattern Observation Operator
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ObservationOperator {
    /// An observation OR (`OR`) between observation expressions.
    Or,
    /// An observation AND (`AND`) between observation expressions.
    And,
    /// A temporal ordering operator (`FOLLOWEDBY`) between observation expressions.
    FollowedBy,
}

/// A STIX Pattern Observation Expression Qualifier
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Qualifier {
    /// A `REPEATS` qualifier matching an observation repeated a given number of times.
    Repeats(i64),
    /// A `WITHIN` qualifier bounding an observation to a time window, in seconds.
    Within(OrderedFloat<f64>),
    /// A `START`/`STOP` qualifier bounding an observation to a timestamp range.
    StartStop(Timestamp, Timestamp),
}

impl Qualifier {
    fn check_qualifier(&self, qualifiers: &mut HashSet<String>) -> Result<(), Error> {
        match self {
            Qualifier::Repeats(count) => {
                if *count <= 0 {
                    return Err(Error::ValidationError(
                        "An Observation Expression in the pattern has a REPEATS Qualifier that is not a positive integer."
                            .to_string(),
                    ));
                }
                check_insert_qualifier("Repeats", qualifiers)
            }
            Qualifier::Within(seconds) => {
                if *seconds <= OrderedFloat(0.0) {
                    return Err(Error::ValidationError(
                        "An Observation Expression in the pattern has a WITHIN Qualifier that is not a positive number of seconds."
                            .to_string(),
                    ));
                }
                check_insert_qualifier("Within", qualifiers)
            }
            // The specification defines the START/STOP range as x <= observation < y
            // but does not prohibit x >= y, so a reversed or empty range is valid.
            Qualifier::StartStop(..) => check_insert_qualifier("Start Stop", qualifiers),
        }
    }
}

/// Add a qualifier of a given type to a set of qualifiers, erroring if that type is already in the set
fn check_insert_qualifier(qualifier: &str, qualifiers: &mut HashSet<String>) -> Result<(), Error> {
    if qualifiers.contains(qualifier) {
        Err(Error::ValidationError("An Observation Expression in the pattern has more than one Qualifier of the same type.".to_string()))
    } else {
        qualifiers.insert(qualifier.to_string());
        Ok(())
    }
}

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

    fn comparison(sco: &str, property: &str, operator: ComparisonOperator) -> ComparisonExpression {
        ComparisonExpression {
            object_path: ObjectPath {
                sco: sco.to_string(),
                property: property.to_string(),
            },
            operator,
            constant: Some(Constant::String("x".to_string())),
            negated: false,
        }
    }

    fn observation(component: ComparisonComponent) -> ObservationExpression {
        ObservationExpression::Comparison(component)
    }

    #[test]
    fn negation_is_preserved_with_the_underlying_operator() {
        let expression = ComparisonExpression {
            object_path: ObjectPath {
                sco: "file".to_string(),
                property: "name".to_string(),
            },
            operator: ComparisonOperator::Neq,
            constant: Some(Constant::String("x".to_string())),
            negated: true,
        };

        assert!(expression.negated);
        assert_eq!(expression.operator, ComparisonOperator::Neq);
    }

    #[test]
    fn groups_are_preserved_on_both_sides() {
        let a = comparison("file", "name", ComparisonOperator::Eq);
        let b = comparison("file", "name", ComparisonOperator::Neq);
        let c = comparison("file", "size", ComparisonOperator::Gt);

        let left_group = ComparisonComponent::BooleanOp(
            Box::new(ComparisonComponent::ComparisonExpression(a)),
            BooleanOperator::Or,
            Box::new(ComparisonComponent::ComparisonExpression(b)),
        );
        let right_group = ComparisonComponent::BooleanOp(
            Box::new(ComparisonComponent::ComparisonExpression(comparison(
                "file",
                "size",
                ComparisonOperator::Eq,
            ))),
            BooleanOperator::Or,
            Box::new(ComparisonComponent::ComparisonExpression(c)),
        );

        let component = ComparisonComponent::BooleanOp(
            Box::new(left_group),
            BooleanOperator::And,
            Box::new(right_group),
        );

        assert_eq!(component.get_matching_object(), Some("file"));
        assert!(observation(component).stix_check().is_ok());
    }

    #[test]
    fn boolean_op_validates_both_sides() {
        let invalid = ComparisonExpression {
            object_path: ObjectPath {
                sco: "file".to_string(),
                property: "name".to_string(),
            },
            operator: ComparisonOperator::In,
            constant: Some(Constant::String("not a set".to_string())),
            negated: false,
        };
        let valid = comparison("file", "size", ComparisonOperator::Gt);

        let component = ComparisonComponent::BooleanOp(
            Box::new(ComparisonComponent::ComparisonExpression(invalid)),
            BooleanOperator::Or,
            Box::new(ComparisonComponent::ComparisonExpression(valid)),
        );

        assert!(observation(component).stix_check().is_err());
    }

    #[test]
    fn and_across_different_objects_is_rejected() {
        let component = ComparisonComponent::BooleanOp(
            Box::new(ComparisonComponent::ComparisonExpression(comparison(
                "ipv4-addr",
                "value",
                ComparisonOperator::Eq,
            ))),
            BooleanOperator::And,
            Box::new(ComparisonComponent::ComparisonExpression(comparison(
                "ipv6-addr",
                "value",
                ComparisonOperator::Eq,
            ))),
        );

        let error = observation(component).stix_check().unwrap_err();
        assert!(error.to_string().contains("different objects"));
    }

    #[test]
    fn or_across_different_objects_is_accepted() {
        let component = ComparisonComponent::BooleanOp(
            Box::new(ComparisonComponent::ComparisonExpression(comparison(
                "ipv4-addr",
                "value",
                ComparisonOperator::Eq,
            ))),
            BooleanOperator::Or,
            Box::new(ComparisonComponent::ComparisonExpression(comparison(
                "ipv6-addr",
                "value",
                ComparisonOperator::Eq,
            ))),
        );

        assert!(observation(component).stix_check().is_ok());
    }

    #[test]
    fn repeats_must_be_positive() {
        for count in [0, -1] {
            let observation = ObservationExpression::Qualification(
                Box::new(observation(ComparisonComponent::ComparisonExpression(
                    comparison("file", "name", ComparisonOperator::Eq),
                ))),
                Qualifier::Repeats(count),
            );
            let error = observation.stix_check().unwrap_err();
            assert!(error.to_string().contains("positive integer"));
        }
    }

    #[test]
    fn within_must_be_positive() {
        for seconds in [0.0, -1.0] {
            let observation = ObservationExpression::Qualification(
                Box::new(observation(ComparisonComponent::ComparisonExpression(
                    comparison("file", "name", ComparisonOperator::Eq),
                ))),
                Qualifier::Within(OrderedFloat(seconds)),
            );
            let error = observation.stix_check().unwrap_err();
            assert!(error.to_string().contains("positive number"));
        }
    }

    #[test]
    fn empty_start_stop_ranges_are_accepted() {
        let start = Timestamp::new("2025-01-01T00:00:00Z").unwrap();
        let stop = Timestamp::new("2025-02-01T00:00:00Z").unwrap();

        for (start, stop) in [
            (stop.clone(), start.clone()),
            (start.clone(), start.clone()),
            (start, stop),
        ] {
            let observation = ObservationExpression::Qualification(
                Box::new(observation(ComparisonComponent::ComparisonExpression(
                    comparison("file", "name", ComparisonOperator::Eq),
                ))),
                Qualifier::StartStop(start, stop),
            );
            assert!(observation.stix_check().is_ok());
        }
    }

    #[test]
    fn duplicate_qualifiers_are_rejected() {
        let observation = ObservationExpression::Qualification(
            Box::new(ObservationExpression::Qualification(
                Box::new(observation(ComparisonComponent::ComparisonExpression(
                    comparison("file", "name", ComparisonOperator::Eq),
                ))),
                Qualifier::Within(OrderedFloat(600.0)),
            )),
            Qualifier::Within(OrderedFloat(300.0)),
        );

        let error = observation.stix_check().unwrap_err();
        assert!(error.to_string().contains("more than one Qualifier"));
    }

    #[test]
    fn operator_and_literal_kinds_must_agree() {
        for (operator, constant) in [
            (
                ComparisonOperator::In,
                Constant::String("not a set".to_string()),
            ),
            (ComparisonOperator::Like, Constant::Integer(5)),
            (ComparisonOperator::Matches, Constant::Integer(5)),
            (ComparisonOperator::IsSubset, Constant::Integer(5)),
            (ComparisonOperator::IsSuperset, Constant::Integer(5)),
        ] {
            let expression = ComparisonExpression {
                object_path: ObjectPath {
                    sco: "file".to_string(),
                    property: "name".to_string(),
                },
                operator,
                constant: Some(constant),
                negated: false,
            };
            assert!(
                observation(ComparisonComponent::ComparisonExpression(expression))
                    .stix_check()
                    .is_err()
            );
        }
    }

    #[test]
    fn exists_must_not_carry_a_constant() {
        let expression = ComparisonExpression {
            object_path: ObjectPath {
                sco: "file".to_string(),
                property: "name".to_string(),
            },
            operator: ComparisonOperator::Exists,
            constant: Some(Constant::String("x".to_string())),
            negated: true,
        };
        assert!(
            observation(ComparisonComponent::ComparisonExpression(expression))
                .stix_check()
                .is_err()
        );
    }
}