Fixes #308
parent
8719a7206f
commit
6df7da65b8
|
@ -143,7 +143,7 @@ class _STIXBase(collections.Mapping):
|
||||||
|
|
||||||
def __init__(self, allow_custom=False, **kwargs):
|
def __init__(self, allow_custom=False, **kwargs):
|
||||||
cls = self.__class__
|
cls = self.__class__
|
||||||
self.__allow_custom = allow_custom
|
self._allow_custom = allow_custom
|
||||||
|
|
||||||
# Use the same timestamp for any auto-generated datetimes
|
# Use the same timestamp for any auto-generated datetimes
|
||||||
self.__now = get_timestamp()
|
self.__now = get_timestamp()
|
||||||
|
@ -152,12 +152,12 @@ class _STIXBase(collections.Mapping):
|
||||||
custom_props = kwargs.pop('custom_properties', {})
|
custom_props = kwargs.pop('custom_properties', {})
|
||||||
if custom_props and not isinstance(custom_props, dict):
|
if custom_props and not isinstance(custom_props, dict):
|
||||||
raise ValueError("'custom_properties' must be a dictionary")
|
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))
|
extra_kwargs = list(set(kwargs) - set(self._properties))
|
||||||
if extra_kwargs:
|
if extra_kwargs:
|
||||||
raise ExtraPropertiesError(cls, extra_kwargs)
|
raise ExtraPropertiesError(cls, extra_kwargs)
|
||||||
if custom_props:
|
if custom_props:
|
||||||
self.__allow_custom = True
|
self._allow_custom = True
|
||||||
|
|
||||||
# Remove any keyword arguments whose value is None or [] (i.e. empty list)
|
# Remove any keyword arguments whose value is None or [] (i.e. empty list)
|
||||||
setting_kwargs = {}
|
setting_kwargs = {}
|
||||||
|
@ -235,7 +235,7 @@ class _STIXBase(collections.Mapping):
|
||||||
if isinstance(self, _Observable):
|
if isinstance(self, _Observable):
|
||||||
# Assume: valid references in the original object are still valid in the new version
|
# Assume: valid references in the original object are still valid in the new version
|
||||||
new_inner['_valid_refs'] = {'*': '*'}
|
new_inner['_valid_refs'] = {'*': '*'}
|
||||||
new_inner['allow_custom'] = self.__allow_custom
|
new_inner['allow_custom'] = self._allow_custom
|
||||||
return cls(**new_inner)
|
return cls(**new_inner)
|
||||||
|
|
||||||
def properties_populated(self):
|
def properties_populated(self):
|
||||||
|
@ -306,7 +306,7 @@ class _Observable(_STIXBase):
|
||||||
# the constructor might be called independently of an observed data object
|
# the constructor might be called independently of an observed data object
|
||||||
self._STIXBase__valid_refs = kwargs.pop('_valid_refs', [])
|
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)
|
self._properties['extensions'].allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -16,8 +16,8 @@ import stix2
|
||||||
from .base import _Observable, _STIXBase
|
from .base import _Observable, _STIXBase
|
||||||
from .core import STIX2_OBJ_MAPS, parse, parse_observable
|
from .core import STIX2_OBJ_MAPS, parse, parse_observable
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
CustomContentError, DictionaryKeyError, ExtraPropertiesError,
|
CustomContentError, DictionaryKeyError, MissingPropertiesError,
|
||||||
MissingPropertiesError, MutuallyExclusivePropertiesError,
|
MutuallyExclusivePropertiesError,
|
||||||
)
|
)
|
||||||
from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime
|
from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime
|
||||||
|
|
||||||
|
@ -201,8 +201,10 @@ class ListProperty(Property):
|
||||||
|
|
||||||
if isinstance(valid, collections.Mapping):
|
if isinstance(valid, collections.Mapping):
|
||||||
try:
|
try:
|
||||||
|
valid._allow_custom
|
||||||
|
except AttributeError:
|
||||||
result.append(obj_type(**valid))
|
result.append(obj_type(**valid))
|
||||||
except ExtraPropertiesError:
|
else:
|
||||||
result.append(obj_type(allow_custom=True, **valid))
|
result.append(obj_type(allow_custom=True, **valid))
|
||||||
else:
|
else:
|
||||||
result.append(obj_type(valid))
|
result.append(obj_type(valid))
|
||||||
|
|
|
@ -233,3 +233,19 @@ def test_indicator_with_custom_embedded_objs():
|
||||||
assert ind.indicator_types == ['malicious-activity']
|
assert ind.indicator_types == ['malicious-activity']
|
||||||
assert len(ind.external_references) == 1
|
assert len(ind.external_references) == 1
|
||||||
assert ind.external_references[0] == ext_ref
|
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)."
|
||||||
|
|
|
@ -31,7 +31,7 @@ class Bundle(_STIXBase):
|
||||||
else:
|
else:
|
||||||
kwargs['objects'] = list(args) + kwargs.get('objects', [])
|
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)
|
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
super(Bundle, self).__init__(**kwargs)
|
super(Bundle, self).__init__(**kwargs)
|
||||||
|
|
|
@ -212,7 +212,7 @@ class ObservedData(STIXDomainObject):
|
||||||
])
|
])
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
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)
|
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
super(ObservedData, self).__init__(*args, **kwargs)
|
super(ObservedData, self).__init__(*args, **kwargs)
|
||||||
|
|
|
@ -29,7 +29,7 @@ class Bundle(_STIXBase):
|
||||||
else:
|
else:
|
||||||
kwargs['objects'] = list(args) + kwargs.get('objects', [])
|
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)
|
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
super(Bundle, self).__init__(**kwargs)
|
super(Bundle, self).__init__(**kwargs)
|
||||||
|
|
|
@ -571,7 +571,7 @@ class ObservedData(STIXDomainObject):
|
||||||
])
|
])
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
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)
|
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
if "objects" in kwargs:
|
if "objects" in kwargs:
|
||||||
|
|
Loading…
Reference in New Issue