From 2179028c91152a931921976278858a997bff2bf3 Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Wed, 3 Jun 2020 15:02:48 -0400 Subject: [PATCH] Fix deterministic ID behavior when an empty "hashes" property is given. Now, an exception is raised. Added a unit test for this. --- stix2/base.py | 9 +++++--- stix2/test/v21/test_deterministic_ids.py | 26 ++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/stix2/base.py b/stix2/base.py index 0da477d..b03a5ab 100644 --- a/stix2/base.py +++ b/stix2/base.py @@ -410,9 +410,12 @@ class _Observable(_STIXBase): obj_value = self[key] if key == "hashes": - possible_hash = _choose_one_hash(obj_value) - if possible_hash: - serializable_value = possible_hash + serializable_value = _choose_one_hash(obj_value) + + if serializable_value is None: + raise InvalidValueError( + self, key, "No hashes given", + ) else: serializable_value = _make_json_serializable(obj_value) diff --git a/stix2/test/v21/test_deterministic_ids.py b/stix2/test/v21/test_deterministic_ids.py index 854ddd0..8a7d4bd 100644 --- a/stix2/test/v21/test_deterministic_ids.py +++ b/stix2/test/v21/test_deterministic_ids.py @@ -2,14 +2,17 @@ from collections import OrderedDict import datetime import uuid +import pytest import six import stix2.base import stix2.canonicalization.Canonicalize +import stix2.exceptions from stix2.properties import ( BooleanProperty, DictionaryProperty, EmbeddedObjectProperty, - ExtensionsProperty, FloatProperty, IDProperty, IntegerProperty, - ListProperty, StringProperty, TimestampProperty, TypeProperty, + ExtensionsProperty, FloatProperty, HashesProperty, IDProperty, + IntegerProperty, ListProperty, StringProperty, TimestampProperty, + TypeProperty, ) import stix2.v21.base @@ -188,3 +191,22 @@ def test_embedded_object(): actual_uuid5 = _uuid_from_id(sco["id"]) 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={})