From 03c265c3a36b46d22f7728365df7d7bc3d2a646b Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Mon, 22 Jun 2020 16:58:28 -0400 Subject: [PATCH] Add a check in ReferenceProperty constructor for an impossible to satisfy type constraint: empty whitelist. It would be silly for anyone to do that, but I should check just in case I guess. --- stix2/properties.py | 3 +++ stix2/test/v20/test_properties.py | 5 +++++ stix2/test/v21/test_properties.py | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/stix2/properties.py b/stix2/properties.py index 31c1d51..80c9e0a 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -511,6 +511,9 @@ class ReferenceProperty(Property): elif invalid_types and not isinstance(invalid_types, list): invalid_types = [invalid_types] + if valid_types is not None and len(valid_types) == 0: + raise ValueError("Impossible type constraint: empty whitelist") + self.types = set(valid_types or invalid_types) self.auth_type = self._WHITELIST if valid_types else self._BLACKLIST diff --git a/stix2/test/v20/test_properties.py b/stix2/test/v20/test_properties.py index e899f81..5c57ed0 100644 --- a/stix2/test/v20/test_properties.py +++ b/stix2/test/v20/test_properties.py @@ -229,6 +229,11 @@ def test_reference_property_hybrid_constraint_type(): ReferenceProperty(invalid_types=["a", "SCO"], spec_version="2.0") +def test_reference_property_impossible_constraint(): + with pytest.raises(ValueError): + ReferenceProperty(valid_types=[], spec_version="2.0") + + @pytest.mark.parametrize( "d", [ {'description': 'something'}, diff --git a/stix2/test/v21/test_properties.py b/stix2/test/v21/test_properties.py index 1015789..f963944 100644 --- a/stix2/test/v21/test_properties.py +++ b/stix2/test/v21/test_properties.py @@ -250,6 +250,11 @@ def test_reference_property_hybrid_constraint_type(): ReferenceProperty(invalid_types=["a", "SCO"], spec_version="2.1") +def test_reference_property_impossible_constraint(): + with pytest.raises(ValueError): + ReferenceProperty(valid_types=[], spec_version="2.1") + + @pytest.mark.parametrize( "d", [ {'description': 'something'},