stixflayer 0.1.1

Fast, spec-correct STIX 2.1 parsing, validation, and serialization in Rust.
Documentation

stixflayer

Fast, strict STIX 2.1 parsing, validation, and serialization in Rust.

stixflayer validates STIX 2.1 JSON against the OASIS specification, including its MUST/SHOULD distinction. The same engine also powers the Python package and the stixflayer CLI, so every interface returns the same verdict.

Install

cargo add stixflayer

Parse and validate

Parsing validates. Bundle::from_json returns a parsed bundle or a structured StixError; there is no separate validate call to forget.

use stixflayer::Bundle;

fn main() -> Result<(), stixflayer::StixError> {
    let json = r#"{
        "type": "bundle",
        "id": "bundle--0c7b5d88-7c1c-4a8b-8f0c-6e9d4f2b1a55",
        "objects": [{
            "type": "attack-pattern",
            "spec_version": "2.1",
            "id": "attack-pattern--d3046a90-580c-4004-8208-66915bc29830",
            "created": "2019-04-24T15:12:36.609Z",
            "modified": "2023-03-24T15:14:15.637Z",
            "name": "Clear Command History"
        }]
    }"#;

    let bundle = Bundle::from_json(json)?;
    assert_eq!(bundle.get_objects().len(), 1);

    // Warnings are SHOULD findings; they do not make the document invalid.
    let outcome = Bundle::from_json_outcome(json);
    assert!(outcome.valid);
    assert!(outcome.errors.is_empty());

    Ok(())
}

Validation model

  • Structural checks always run: required properties, JSON types, identifiers, timestamps, and closed enumerations.
  • MUST violations are errors. SHOULD findings are warnings and never reject a document. from_json_outcome returns both lists as structured entries with kind, message, and optional property, value, and object_id fields.
  • ValidationOptions::new(allow_custom, strict) controls unknown properties and semantic checks. The default is (false, true): unknown properties are rejected and SHOULD checks run.
  • Validation collects independent findings instead of stopping at the first error. Object-scoped findings carry the object's id.

See the StixError API documentation for the error and warning kinds.

Command-line validation

The stixflayer binary validates files, directories, and standard input without a Python or Rust project:

cargo install stixflayer-cli
stixflayer path/to/bundle.json

It exits 0 when every document is valid, 1 when any document is invalid, and 2 on a usage or I/O failure.

Related packages

License

BSD-3-Clause