From 9e5e998c3d83c0706eb7f7b8097cbc78f4772e25 Mon Sep 17 00:00:00 2001 From: Rich Piazza Date: Fri, 20 Mar 2020 12:49:20 -0400 Subject: [PATCH] don't allow leading '_' on custom properties, whenever allow_custom is true --- stix2/base.py | 10 +++++++-- stix2/test/v21/test_custom.py | 38 +++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 8bffa3a..e018059 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -169,10 +169,16 @@ class _STIXBase(Mapping): extra_kwargs = list(set(kwargs) - set(self._properties)) if extra_kwargs: raise ExtraPropertiesError(cls, extra_kwargs) - if custom_props: + else: + # because allow_custom is true, any extra kwargs are custom + extra_kwargs = list(set(kwargs) - set(self._properties)) + + if custom_props or extra_kwargs: self._allow_custom = True if self.get_class_version() == "v21": - for prop_name, prop_value in custom_props.items(): + all_custom_prop_names = extra_kwargs + all_custom_prop_names.extend(list(custom_props.keys())) + for prop_name in all_custom_prop_names: if not re.match(PREFIX_21_REGEX, prop_name): raise InvalidValueError( self.__class__, prop_name, diff --git a/stix2/test/v21/test_custom.py b/stix2/test/v21/test_custom.py index 2bdca05..ef57a9b 100644 --- a/stix2/test/v21/test_custom.py +++ b/stix2/test/v21/test_custom.py @@ -20,6 +20,18 @@ IDENTITY_CUSTOM_PROP = stix2.v21.Identity( def test_identity_custom_property(): + identity = stix2.v21.Identity( + id=IDENTITY_ID, + created="2015-12-21T19:59:11Z", + modified="2015-12-21T19:59:11Z", + name="John Smith", + identity_class="individual", + custom_properties={ + "foo": "bar", + }, + ) + assert identity.foo == "bar" + with pytest.raises(ValueError) as excinfo: stix2.v21.Identity( id=IDENTITY_ID, @@ -45,6 +57,8 @@ def test_identity_custom_property(): ) assert "Unexpected properties for Identity" in str(excinfo.value) + # leading numeric character is illegal in 2.1 + with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo: stix2.v21.Identity( id=IDENTITY_ID, @@ -58,6 +72,8 @@ def test_identity_custom_property(): ) assert "must begin with an alpha character." in str(excinfo.value) + # leading "_" is illegal in 2.1 + with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo: stix2.v21.Identity( id=IDENTITY_ID, @@ -71,17 +87,17 @@ def test_identity_custom_property(): ) assert "must begin with an alpha character." in str(excinfo.value) - identity = stix2.v21.Identity( - id=IDENTITY_ID, - created="2015-12-21T19:59:11Z", - modified="2015-12-21T19:59:11Z", - name="John Smith", - identity_class="individual", - custom_properties={ - "foo": "bar", - }, - ) - assert identity.foo == "bar" + with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo: + identity = stix2.v21.Identity( + id=IDENTITY_ID, + created="2015-12-21T19:59:11Z", + modified="2015-12-21T19:59:11Z", + name="John Smith", + identity_class="individual", + _x_foo="bar", + allow_custom=True, + ) + assert "must begin with an alpha character." in str(excinfo.value) def test_identity_custom_property_invalid():