don't allow leading '_' on custom properties, whenever allow_custom is true
parent
2c4e47de56
commit
9e5e998c3d
|
@ -169,10 +169,16 @@ class _STIXBase(Mapping):
|
||||||
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:
|
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
|
self._allow_custom = True
|
||||||
if self.get_class_version() == "v21":
|
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):
|
if not re.match(PREFIX_21_REGEX, prop_name):
|
||||||
raise InvalidValueError(
|
raise InvalidValueError(
|
||||||
self.__class__, prop_name,
|
self.__class__, prop_name,
|
||||||
|
|
|
@ -20,6 +20,18 @@ IDENTITY_CUSTOM_PROP = stix2.v21.Identity(
|
||||||
|
|
||||||
|
|
||||||
def test_identity_custom_property():
|
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:
|
with pytest.raises(ValueError) as excinfo:
|
||||||
stix2.v21.Identity(
|
stix2.v21.Identity(
|
||||||
id=IDENTITY_ID,
|
id=IDENTITY_ID,
|
||||||
|
@ -45,6 +57,8 @@ def test_identity_custom_property():
|
||||||
)
|
)
|
||||||
assert "Unexpected properties for Identity" in str(excinfo.value)
|
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:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.v21.Identity(
|
stix2.v21.Identity(
|
||||||
id=IDENTITY_ID,
|
id=IDENTITY_ID,
|
||||||
|
@ -58,6 +72,8 @@ def test_identity_custom_property():
|
||||||
)
|
)
|
||||||
assert "must begin with an alpha character." in str(excinfo.value)
|
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:
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
stix2.v21.Identity(
|
stix2.v21.Identity(
|
||||||
id=IDENTITY_ID,
|
id=IDENTITY_ID,
|
||||||
|
@ -71,17 +87,17 @@ def test_identity_custom_property():
|
||||||
)
|
)
|
||||||
assert "must begin with an alpha character." in str(excinfo.value)
|
assert "must begin with an alpha character." in str(excinfo.value)
|
||||||
|
|
||||||
identity = stix2.v21.Identity(
|
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
|
||||||
id=IDENTITY_ID,
|
identity = stix2.v21.Identity(
|
||||||
created="2015-12-21T19:59:11Z",
|
id=IDENTITY_ID,
|
||||||
modified="2015-12-21T19:59:11Z",
|
created="2015-12-21T19:59:11Z",
|
||||||
name="John Smith",
|
modified="2015-12-21T19:59:11Z",
|
||||||
identity_class="individual",
|
name="John Smith",
|
||||||
custom_properties={
|
identity_class="individual",
|
||||||
"foo": "bar",
|
_x_foo="bar",
|
||||||
},
|
allow_custom=True,
|
||||||
)
|
)
|
||||||
assert identity.foo == "bar"
|
assert "must begin with an alpha character." in str(excinfo.value)
|
||||||
|
|
||||||
|
|
||||||
def test_identity_custom_property_invalid():
|
def test_identity_custom_property_invalid():
|
||||||
|
|
Loading…
Reference in New Issue