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
//! A collection of STIX-wide properties and features that could be used by any STIX object or type.
use crate::{
common::{time::check_timestamp_ordering, validation::validate_vocab_value},
custom_property::{validate_custom_property_name, validate_custom_property_suffix_value},
error::{add_error, return_multiple_errors, StixError as Error},
extensions::{
check_extension, FileExtensions, NetworkTrafficExtensions, ProcessExtensions,
UserAccountExtensions,
},
types::{
check_observable_dictionary_key, stix_case, DictionaryValue, ExtensionType,
ExternalReference, GranularMarking, Identifier, StixDictionary, Timestamp,
},
};
use language_tags::LanguageTag;
use log::warn;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_with::skip_serializing_none;
use std::collections::BTreeMap;
use std::str::FromStr;
use stixflayer_derive::StixProperties;
use strum::IntoEnumIterator;
/// A trait for all STIX 2.1 compliant objects and properties.
pub trait Stix {
/// Method that ensures that all fields in the object or property conform to the STIX 2.1 standard.
fn stix_check(&self) -> Result<(), Error>;
}
/// Properties that are common across multiple STIX Objects.
///
/// This struct is intended to be nested and flattened inside of a specific STIX Object,
/// with the validator ensuring that properties that cannot exist for that object are not included.
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize, StixProperties)]
pub struct CommonProperties {
/// The version of the STIX specification used to represent this object (**MUST** be 2.1 in STIX 2.1).
///
/// Required property for SDOs, SROs, and Meta Objects.
pub spec_version: Option<String>,
/// Uniquely identifies this object.
///
/// For objects that support versioning, all objects with the same `id` are considered different versions of the same object and the version of the object is identified by
/// its `modified` property.
pub id: Identifier,
/// Specifies the identity that describes the entity that created this object.
///
/// Can be omitted for objects with anonymous creators, except for Extenion Objects, for which it is a required property.
pub created_by_ref: Option<Identifier>,
/// Represents the time at which the object was originally created.
/// The object creator can use the time it deems most appropriate as the time the object was created.
///
/// The minimum precision **MUST** be milliseconds but **MAY** be more precise.
/// This **MUST** remain constant even if a new version of the same object is created.
///
/// Required property for SDOs, SROs, and Meta Objects.
pub created: Option<Timestamp>,
/// Represents the time that this particular version of the object was last modified.
/// The object creator can use the time it deems most appropriate as the time this version of the object was modified.
///
/// The minimum precision **MUST** be milliseconds but **MAY** be more precise.
/// This **MUST** be later than or equal to `created`. This is set each time a new version of an object is created.
///
/// Required property for SDOs, SROs, Extension Objects, and Language Marking Objects.
pub modified: Option<Timestamp>,
/// Indicates whether the object has been revoked.
///
/// Revoked objects are no longer considered valid by the object creator. Revoking an object is permanent; future versions of the object with this `id` **MUST NOT** be created.
pub revoked: Option<bool>,
/// Specifies an optional set of terms used to describe this object.
/// The terms are user-defined or trust-group defined and their meaning is outside the scope of this specification.
pub labels: Option<Vec<String>>,
/// Identifies the confidence that the creator has in the correctness of their data.
///
/// **MUST** be a number in the range of 0-100.
/// Omitted if the confidence is unspecified.
pub confidence: Option<u8>,
/// Identifies the language of the text content in this object.
///
/// **MUST** be a language code conformant to [RFC5646](https://www.rfc-editor.org/info/rfc5646).
/// If omitted, then the language of the content is `en` (English).
pub lang: Option<String>,
/// Specifies a list of external references which refers to non-STIX information.
/// Provides descriptions, URLs, or IDs to other system's records.
pub external_references: Option<Vec<ExternalReference>>,
/// Specifies a list of identities of marking-definition objects that apply to this object.
pub object_marking_refs: Option<Vec<Identifier>>,
pub granular_markings: Option<Vec<GranularMarking>>,
/// This property defines whether or not the data contained within the object has been defanged.
///
/// `None` is the same as `Some(false)`
/// This property **MUST NOT** be used for any STIX Objects other than SCOs.
pub defanged: Option<bool>,
/// Specifies any extensions of the object, as a dictionary.
///
/// Dictionary keys SHOULD be the id of a STIX Extension object or the name of a predefined object extension found in this specification,
/// depending on the type of extension being used.
///
/// The corresponding dictionary values **MUST** contain the contents of the extension instance.
pub extensions: Option<StixDictionary<StixDictionary<DictionaryValue>>>,
/// Custom properties that are not part of the STIX 2.1 specification.
///
/// This bag is populated after deserialization by stripping unknown top-level keys from the
/// incoming JSON. It is flattened back out on serialization so custom keys remain as siblings
/// of the standard properties in the JSON output.
///
/// `skip_deserializing` is required: a flattened map would otherwise absorb every unknown key and
/// duplicate the fields that serde has already assigned to object_type/common_properties. Unknown
/// keys are instead captured explicitly in [`crate::validation::validate_value`].
#[serde(default, skip_deserializing, flatten)]
pub custom_properties: Option<BTreeMap<String, Value>>,
}
impl Stix for CommonProperties {
fn stix_check(&self) -> Result<(), Error> {
let mut errors = Vec::new();
if let Some(spec_version) = &self.spec_version {
if spec_version != "2.1" {
errors.push(Error::ValidationError(format!(
"The spec version of Object {} is {}. It must be 2.1.",
self.id, spec_version
)));
}
}
if let Some(creator) = &self.created_by_ref {
add_error(&mut errors, creator.stix_check());
if creator.get_type() != "identity" {
errors.push(Error::ValidationError(format!("Object {} has a 'created by' reference to an object of type '{}'. Only an Identity SDO can be the creator of another STIX object.",
self.id,
creator.get_type()
)));
}
}
if let (Some(created), Some(modified)) = (&self.created, &self.modified) {
add_error(
&mut errors,
check_timestamp_ordering(
created,
modified,
"created",
"modified",
&self.id.to_string(),
),
);
}
if let Some(confidence_number) = self.confidence {
if confidence_number > 100 {
errors.push(Error::ValidationError(format!("The confidence value of Object {} is {}. An object's confidence value cannot be greater than 100.",
self.id,
confidence_number
)));
}
}
if let Some(external_references) = &self.external_references {
add_error(&mut errors, external_references.stix_check());
}
if let Some(language) = &self.lang {
match LanguageTag::parse(language) {
Ok(tag) => if let Err(e) = LanguageTag::validate(&tag) {
errors.push(Error::ValidationError(format!("Object {}'s `language` is {}. A `language` must conform to RFC5646. Details: {}",
self.id,
language,
e
)));
}
Err(e) => errors.push(Error::ValidationError(format!("Object {}'s `language` is {}. A `language` must conform to RFC5646. Details: {}",
self.id,
language,
e
))),
}
}
if let Some(granular_markings) = &self.granular_markings {
add_error(&mut errors, granular_markings.stix_check());
}
if let Some(extensions) = &self.extensions {
add_error(&mut errors, extensions.stix_check());
for (key, value) in extensions.iter() {
// Check constraints for general extensions, idetified by using a STIX identifer as the extension key
if let Ok(id) = Identifier::from_str(key) {
if id.get_type() != "extension-definition" {
warn!("If `extensions` keys are STIX Object id's, they should be the id of an Extension Definition SMO. Object {} has an extension id key {}.",
self.id,
key
)
}
if !value.keys().any(|k| k == "extension_type") {
errors.push(Error::ValidationError(format!("If an `extensions` dictionary entry is not predefined object extension, the 'extension_type' property must be present in that dictionary entry. Object {} has an extension with key {} that is missing that property",
self.id,
key,
)));
}
for (inner_key, inner_value) in value.iter() {
if let Err(e) = check_observable_dictionary_key(inner_key) {
errors.push(e);
}
if inner_key == "extension_type" {
if let DictionaryValue::String(inner_value_string) = inner_value {
add_error(
&mut errors,
validate_vocab_value::<ExtensionType, _>(
inner_value_string,
"extension-type-enum",
),
);
} else {
errors.push(Error::ValidationError(format!("The value of an 'extension_type' property in an `extensions` dictionary entry must come from the `extension-type-enum` enumeration. Object {} has an extension with key {} whose `extension_type` value is {}.",
self.id,
key,
inner_value
)));
}
}
}
// Check known SCO-specific predefined extensions
} else {
match self.id.get_type() {
"file" => {
if key.starts_with("extension-definition--") {
// Custom extension - allow
} else if FileExtensions::iter().any(|x| x.as_ref() == stix_case(key)) {
add_error(&mut errors, check_extension(key, value));
} else {
errors.push(Error::WrongExtension)
}
}
"network-traffic" => {
if key.starts_with("extension-definition--") {
// Custom extension - allow
} else if NetworkTrafficExtensions::iter()
.any(|x| x.as_ref() == stix_case(key))
{
add_error(&mut errors, check_extension(key, value));
} else {
errors.push(Error::WrongExtension)
}
}
"process" => {
if key.starts_with("extension-definition--") {
// Custom extension - allow
} else if ProcessExtensions::iter()
.any(|x| x.as_ref() == stix_case(key))
{
add_error(&mut errors, check_extension(key, value));
} else {
errors.push(Error::WrongExtension)
}
}
"user-account" => {
if key.starts_with("extension-definition--") {
// Custom extension - allow
} else if UserAccountExtensions::iter()
.any(|x| x.as_ref() == stix_case(key))
{
add_error(&mut errors, check_extension(key, value));
} else {
errors.push(Error::WrongExtension)
}
}
// For other object types, allow any extension key (warn if not extension-definition)
_ => {
if !key.starts_with("extension-definition--") {
warn!("`extensions` keys should be the id of an Extension Definition SMO, unless you are using a predefined object extension. Confirm that object {}'s extension key {} is a predefined object extension.",
self.id,
key
)
}
}
}
add_error(&mut errors, value.stix_check());
}
}
}
if let Some(custom_properties) = &self.custom_properties {
if custom_properties.is_empty() {
errors.push(Error::ValidationError(
"custom_properties cannot be empty".to_string(),
));
}
for (key, value) in custom_properties.iter() {
add_error(&mut errors, validate_custom_property_name(key));
add_error(
&mut errors,
validate_custom_property_suffix_value(key, value),
);
add_error(&mut errors, value.stix_check());
}
}
return_multiple_errors(errors)
}
}
/// Trait implemented by top-level STIX object structs so that the central
/// deserialization gate ([`crate::validation::validate_value`]) can store the
/// custom-property bag separately from the serde deserialization of standard fields.
pub trait CustomPropertiesHolder {
/// Access the object's custom property bag.
fn custom_properties(&self) -> &Option<BTreeMap<String, Value>>;
/// Replace the object's custom property bag.
fn set_custom_properties(&mut self, custom_properties: Option<BTreeMap<String, Value>>);
}
/// Implement [`CustomPropertiesHolder`] for a standard STIX object struct that
/// carries common properties in a field named `common_properties`.
#[macro_export]
macro_rules! impl_custom_properties_holder {
($type:ty) => {
impl $crate::base::CustomPropertiesHolder for $type {
fn custom_properties(
&self,
) -> &Option<std::collections::BTreeMap<std::string::String, serde_json::Value>> {
&self.common_properties.custom_properties
}
fn set_custom_properties(
&mut self,
custom_properties: Option<
std::collections::BTreeMap<std::string::String, serde_json::Value>,
>,
) {
self.common_properties.custom_properties = custom_properties;
}
}
};
}