Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 308 #315

Merged
merged 2 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stix2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _check_object_constraints(self):

def __init__(self, allow_custom=False, **kwargs):
cls = self.__class__
self.__allow_custom = allow_custom
self._allow_custom = allow_custom

# Use the same timestamp for any auto-generated datetimes
self.__now = get_timestamp()
Expand All @@ -152,12 +152,12 @@ def __init__(self, allow_custom=False, **kwargs):
custom_props = kwargs.pop('custom_properties', {})
if custom_props and not isinstance(custom_props, dict):
raise ValueError("'custom_properties' must be a dictionary")
if not self.__allow_custom:
if not self._allow_custom:
extra_kwargs = list(set(kwargs) - set(self._properties))
if extra_kwargs:
raise ExtraPropertiesError(cls, extra_kwargs)
if custom_props:
self.__allow_custom = True
self._allow_custom = True

# Remove any keyword arguments whose value is None or [] (i.e. empty list)
setting_kwargs = {}
Expand Down Expand Up @@ -235,7 +235,7 @@ def __deepcopy__(self, memo):
if isinstance(self, _Observable):
# Assume: valid references in the original object are still valid in the new version
new_inner['_valid_refs'] = {'*': '*'}
new_inner['allow_custom'] = self.__allow_custom
new_inner['allow_custom'] = self._allow_custom
return cls(**new_inner)

def properties_populated(self):
Expand Down Expand Up @@ -306,7 +306,7 @@ def __init__(self, **kwargs):
# the constructor might be called independently of an observed data object
self._STIXBase__valid_refs = kwargs.pop('_valid_refs', [])

self.__allow_custom = kwargs.get('allow_custom', False)
self._allow_custom = kwargs.get('allow_custom', False)
self._properties['extensions'].allow_custom = kwargs.get('allow_custom', False)

try:
Expand Down
7 changes: 6 additions & 1 deletion stix2/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ def clean(self, value):
obj_type = self.contained

if isinstance(valid, collections.Mapping):
result.append(obj_type(**valid))
try:
valid._allow_custom
except AttributeError:
result.append(obj_type(**valid))
else:
result.append(obj_type(allow_custom=True, **valid))
else:
result.append(obj_type(valid))

Expand Down
44 changes: 44 additions & 0 deletions stix2/test/v21/test_indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,47 @@ 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


def test_indicator_with_custom_embed_objs_extra_props_error():
ext_ref = stix2.v21.ExternalReference(
source_name="Test",
description="Example Custom Ext Ref",
random_custom_prop="This is a custom property",
allow_custom=True,
)

with pytest.raises(stix2.exceptions.ExtraPropertiesError) as excinfo:
stix2.v21.Indicator(external_references=[ext_ref], bad_custom_prop="shouldn't be here", **INDICATOR_KWARGS)

assert excinfo.value.cls == stix2.v21.Indicator
assert excinfo.value.properties == ['bad_custom_prop']
assert str(excinfo.value) == "Unexpected properties for Indicator: (bad_custom_prop)."
2 changes: 1 addition & 1 deletion stix2/v20/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, *args, **kwargs):
else:
kwargs['objects'] = list(args) + kwargs.get('objects', [])

self.__allow_custom = kwargs.get('allow_custom', False)
self._allow_custom = kwargs.get('allow_custom', False)
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)

super(Bundle, self).__init__(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion stix2/v20/sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ObservedData(STIXDomainObject):
])

def __init__(self, *args, **kwargs):
self.__allow_custom = kwargs.get('allow_custom', False)
self._allow_custom = kwargs.get('allow_custom', False)
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)

super(ObservedData, self).__init__(*args, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion stix2/v21/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, *args, **kwargs):
else:
kwargs['objects'] = list(args) + kwargs.get('objects', [])

self.__allow_custom = kwargs.get('allow_custom', False)
self._allow_custom = kwargs.get('allow_custom', False)
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)

super(Bundle, self).__init__(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion stix2/v21/sdo.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class ObservedData(STIXDomainObject):
])

def __init__(self, *args, **kwargs):
self.__allow_custom = kwargs.get('allow_custom', False)
self._allow_custom = kwargs.get('allow_custom', False)
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)

if "objects" in kwargs:
Expand Down