Fix deterministic ID behavior when an empty "hashes" property is

given.  Now, an exception is raised.  Added a unit test for this.
pull/1/head
Michael Chisholm 2020-06-03 15:02:48 -04:00
parent c94feae623
commit 2179028c91
2 changed files with 30 additions and 5 deletions

View File

@ -410,9 +410,12 @@ class _Observable(_STIXBase):
obj_value = self[key] obj_value = self[key]
if key == "hashes": if key == "hashes":
possible_hash = _choose_one_hash(obj_value) serializable_value = _choose_one_hash(obj_value)
if possible_hash:
serializable_value = possible_hash if serializable_value is None:
raise InvalidValueError(
self, key, "No hashes given",
)
else: else:
serializable_value = _make_json_serializable(obj_value) serializable_value = _make_json_serializable(obj_value)

View File

@ -2,14 +2,17 @@ from collections import OrderedDict
import datetime import datetime
import uuid import uuid
import pytest
import six import six
import stix2.base import stix2.base
import stix2.canonicalization.Canonicalize import stix2.canonicalization.Canonicalize
import stix2.exceptions
from stix2.properties import ( from stix2.properties import (
BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, BooleanProperty, DictionaryProperty, EmbeddedObjectProperty,
ExtensionsProperty, FloatProperty, IDProperty, IntegerProperty, ExtensionsProperty, FloatProperty, HashesProperty, IDProperty,
ListProperty, StringProperty, TimestampProperty, TypeProperty, IntegerProperty, ListProperty, StringProperty, TimestampProperty,
TypeProperty,
) )
import stix2.v21.base import stix2.v21.base
@ -188,3 +191,22 @@ def test_embedded_object():
actual_uuid5 = _uuid_from_id(sco["id"]) actual_uuid5 = _uuid_from_id(sco["id"])
assert actual_uuid5 == expected_uuid5 assert actual_uuid5 == expected_uuid5
def test_empty_hash():
class SomeSCO(stix2.v21.base._Observable):
_type = "some-sco"
_properties = OrderedDict((
('type', TypeProperty(_type, spec_version='2.1')),
('id', IDProperty(_type, spec_version='2.1')),
(
'extensions', ExtensionsProperty(
spec_version='2.1', enclosing_type=_type,
),
),
('hashes', HashesProperty()),
))
_id_contributing_properties = ['hashes']
with pytest.raises(stix2.exceptions.InvalidValueError):
SomeSCO(hashes={})