Skip to main content

Crate stixflayer

Crate stixflayer 

Source
Expand description

Rust crate documentation is the crate README so docs.rs and crates.io render the same page, and the README’s Rust example is compiled and run as a doctest by cargo test.

§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.

§License

BSD-3-Clause

Re-exports§

pub use bundles::Bundle;
pub use cyber_observable_objects::sco::CyberObjectBuilder;
pub use domain_objects::sdo::DomainObjectBuilder;
pub use domain_objects::sdo::DomainObjectType;
pub use error::StixError;
pub use meta_objects::extension_definition::ExtensionDefinitionBuilder;
pub use meta_objects::language_content::LanguageContentBuilder;
pub use meta_objects::marking_definition::MarkingDefinitionBuilder;
pub use object::FromJson;
pub use relationship_objects::RelationshipObjectBuilder;
pub use types::ExtensionType;
pub use types::Hashes;
pub use types::Identified;
pub use types::Identifier;
pub use types::KillChainPhase;
pub use types::Timestamp;
pub use validation::ValidationOptions;

Modules§

base
Re-export facade for STIX-wide properties, traits, and helpers.
builder
Builder for common STIX object properties.
bundles
Contains the implementation logic for STIX Bundles
common
Shared helper modules: validation utilities, string normalization, and timestamp ordering checks.
custom_objects
Contains the implementation logic for unrecognized custom STIX Objects.
custom_property
Custom property name and value validation.
cyber_observable_objects
Data structures and functions for implementing STIX Cyber-observable Objects (SCOs).
dictionary
STIX 2.1 dictionaries and primitive dictionary values.
domain_objects
Data structures and functions for implementing STIX Domain Objects (SDOs).
error
A custom STIX Error type, with its associated functions.
extensions
Contains the data structures and implementation for predefined Stix Cyber-observable object extensions.
external_reference
External references and reference URLs for STIX objects.
granular_marking
Granular markings for STIX objects.
hashes
STIX 2.1 compliant hash lists.
identifier
STIX 2.1 compliant identifiers.
json
Functions for deserializing STIX objects from JSON strings.
kill_chain
Kill-chain phases for STIX objects.
meta_objects
Data structures and functions for implementing STIX Meta Objects (SMOs).
object
Top level STIX object structures and implementation
pattern
A Rust implementation of the STIX 2.1 Patterning grammar.
properties
Property registry for STIX types.
relationship_objects
Data structures and functions for implementing STIX Relationship Objects (SROs).
stix
A collection of STIX-wide properties and features that could be used by any STIX object or type.
taxonomy
STIX object type taxonomies and classification helpers.
timestamp
A STIX 2.1 compliant timestamp type.
types
Re-export facade for STIX core types.
validation
Value-based collect-all validation for STIX objects.

Macros§

impl_custom_properties_holder
Implement CustomPropertiesHolder for a standard STIX object struct that carries common properties in a field named common_properties.