diff --git a/stix2/custom.py b/stix2/custom.py index e73a16a..736d459 100644 --- a/stix2/custom.py +++ b/stix2/custom.py @@ -17,7 +17,7 @@ def _custom_object_builder(cls, type, properties, version): if version == "2.0": if not re.match(TYPE_REGEX, type): raise ValueError( - "Invalid type type name '%s': must only contain the " + "Invalid type name '%s': must only contain the " "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % type, ) @@ -39,7 +39,7 @@ def _custom_object_builder(cls, type, properties, version): if version == "2.1": for prop_name, prop in properties: - if not re.match(r'^[a-z]', prop_name): + if not re.match(PREFIX_21_REGEX, prop_name): raise ValueError("Property name %s must begin with an alpha character" % prop_name) _type = type @@ -54,33 +54,46 @@ def _custom_object_builder(cls, type, properties, version): def _custom_marking_builder(cls, type, properties, version): + + try: + prop_dict = OrderedDict(properties) + except TypeError as e: + six.raise_from( + ValueError( + "Marking properties must be dict-like, e.g. a list " + "containing tuples. For example, " + "[('property1', IntegerProperty())]", + ), + e, + ) + class _CustomMarking(cls, _STIXBase): if version == "2.0": if not re.match(TYPE_REGEX, type): raise ValueError( - "Invalid type type name '%s': must only contain the " + "Invalid marking type name '%s': must only contain the " "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % type, ) else: # 2.1+ if not re.match(SCO21_TYPE_REGEX, type): raise ValueError( - "Invalid type name '%s': must only contain the " + "Invalid marking type name '%s': must only contain the " "characters a-z (lowercase ASCII), 0-9, and hyphen (-) " "and must begin with an a-z character" % type, ) if len(type) < 3 or len(type) > 250: raise ValueError( - "Invalid type name '%s': must be between 3 and 250 characters." % type, + "Invalid marking type name '%s': must be between 3 and 250 characters." % type, ) if not properties or not isinstance(properties, list): raise ValueError("Must supply a list, containing tuples. For example, [('property1', IntegerProperty())]") - if version == "2.1": - for prop_name, prop in properties: + if version == "2.1 ": + for prop_name, prop_value in prop_dict.items(): if not re.match(PREFIX_21_REGEX, prop_name): raise ValueError("Property name %s must begin with an alpha character." % prop_name) @@ -104,7 +117,7 @@ def _custom_observable_builder(cls, type, properties, version, id_contrib_props= if version == "2.0": if not re.match(TYPE_REGEX, type): raise ValueError( - "Invalid type type name '%s': must only contain the " + "Invalid observable type name '%s': must only contain the " "characters a-z (lowercase ASCII), 0-9, and hyphen (-)." % type, ) diff --git a/stix2/test/v21/test_custom.py b/stix2/test/v21/test_custom.py index 33cffd5..8b1224e 100644 --- a/stix2/test/v21/test_custom.py +++ b/stix2/test/v21/test_custom.py @@ -292,6 +292,19 @@ def test_identity_custom_property_edit_markings(): identity2.clear_markings('x_foo') +def test_invalid_custom_property_in_marking(): + with pytest.raises(ValueError) as excinfo: + @stix2.v21.CustomMarking( + 'x-new-obj', [ + ('9property1', stix2.properties.StringProperty(required=True)), + ], + ) + class NewObj(): + pass + + assert "Property names must begin with an alpha character." in str(excinfo.value) + + def test_custom_marking_no_init_1(): @stix2.v21.CustomMarking( 'x-new-obj', [ @@ -327,7 +340,7 @@ def test_custom_marking_invalid_type_name(): ) class NewObj(object): pass # pragma: no cover - assert "Invalid type name 'x': " in str(excinfo.value) + assert "Invalid marking type name 'x': " in str(excinfo.value) with pytest.raises(ValueError) as excinfo: @stix2.v21.CustomMarking( @@ -337,7 +350,7 @@ def test_custom_marking_invalid_type_name(): ) class NewObj2(object): pass # pragma: no cover - assert "Invalid type name 'x_new_marking':" in str(excinfo.value) + assert "Invalid marking type name 'x_new_marking':" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: @stix2.v21.CustomMarking( @@ -347,7 +360,7 @@ def test_custom_marking_invalid_type_name(): ) class NewObj3(object): pass # pragma: no cover - assert "Invalid type name '7x-new-marking':" in str(excinfo.value) + assert "Invalid marking type name '7x-new-marking':" in str(excinfo.value) @stix2.v21.CustomObject( @@ -442,7 +455,6 @@ def test_custom_object_invalid_type_name(): assert "Invalid type name '7x-new-object':" in str(excinfo.value) - def test_parse_custom_object_type(): nt_string = """{ "type": "x-new-type", @@ -579,7 +591,6 @@ def test_custom_observable_object_invalid_type_name(): assert "Invalid observable type name '7x-new-obs':" in str(excinfo.value) - def test_custom_observable_object_invalid_ref_property(): with pytest.raises(ValueError) as excinfo: @stix2.v21.CustomObservable(