From 8719a7206fc73a1599edd29e45b053cb59b6b3f3 Mon Sep 17 00:00:00 2001 From: "Desai, Kartikey H" Date: Mon, 16 Dec 2019 16:32:55 -0500 Subject: [PATCH] Fixes #308 --- stix2/properties.py | 9 ++++++--- stix2/test/v21/test_indicator.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/stix2/properties.py b/stix2/properties.py index c956a08..8eb7303 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -16,8 +16,8 @@ import stix2 from .base import _Observable, _STIXBase from .core import STIX2_OBJ_MAPS, parse, parse_observable from .exceptions import ( - CustomContentError, DictionaryKeyError, MissingPropertiesError, - MutuallyExclusivePropertiesError, + CustomContentError, DictionaryKeyError, ExtraPropertiesError, + MissingPropertiesError, MutuallyExclusivePropertiesError, ) from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime @@ -200,7 +200,10 @@ class ListProperty(Property): obj_type = self.contained if isinstance(valid, collections.Mapping): - result.append(obj_type(**valid)) + try: + result.append(obj_type(**valid)) + except ExtraPropertiesError: + result.append(obj_type(allow_custom=True, **valid)) else: result.append(obj_type(valid)) diff --git a/stix2/test/v21/test_indicator.py b/stix2/test/v21/test_indicator.py index ea46d6d..bc4cd82 100644 --- a/stix2/test/v21/test_indicator.py +++ b/stix2/test/v21/test_indicator.py @@ -205,3 +205,31 @@ def test_invalid_indicator_pattern(): assert excinfo.value.cls == stix2.v21.Indicator assert excinfo.value.prop_name == 'pattern' assert 'mismatched input' in excinfo.value.reason + + +def test_indicator_with_custom_embedded_objs(): + now = dt.datetime(2017, 1, 1, 0, 0, 1, tzinfo=pytz.utc) + epoch = dt.datetime(1970, 1, 1, 0, 0, 1, tzinfo=pytz.utc) + + ext_ref = stix2.v21.ExternalReference( + source_name="Test", + description="Example Custom Ext Ref", + random_custom_prop="This is a custom property", + allow_custom=True, + ) + + ind = stix2.v21.Indicator( + type="indicator", + id=INDICATOR_ID, + created=now, + modified=now, + pattern="[file:hashes.MD5 = 'd41d8cd98f00b204e9800998ecf8427e']", + pattern_type="stix", + valid_from=epoch, + indicator_types=['malicious-activity'], + external_references=[ext_ref], + ) + + assert ind.indicator_types == ['malicious-activity'] + assert len(ind.external_references) == 1 + assert ind.external_references[0] == ext_ref