From 387ce7e7cbfff8ea4190a71e6fbf90efff5fb32d Mon Sep 17 00:00:00 2001 From: Michael Chisholm Date: Fri, 19 Jun 2020 11:40:36 -0400 Subject: [PATCH] Fix Report SDO: I'd fixed ReferenceProperty to work the way I thought it should, but forgot to fix Report to use ReferenceProperty in the way I thought it should! Oops. Added some tests to ensure Report is working property with custom ID types in object_refs. --- stix2/test/v20/test_properties.py | 5 +++++ stix2/test/v20/test_report.py | 28 ++++++++++++++++++++++++++++ stix2/test/v21/test_properties.py | 5 +++++ stix2/test/v21/test_report.py | 26 +++++++++++++++++++++++++- stix2/v20/sdo.py | 2 +- stix2/v21/sdo.py | 2 +- 6 files changed, 65 insertions(+), 3 deletions(-) diff --git a/stix2/test/v20/test_properties.py b/stix2/test/v20/test_properties.py index 3ff207a..9e4b6e1 100644 --- a/stix2/test/v20/test_properties.py +++ b/stix2/test/v20/test_properties.py @@ -138,6 +138,11 @@ def test_reference_property_blacklist_type(): "identity--8a8e8758-f92c-4058-ba38-f061cd42a0cf", True, ) + with pytest.raises(CustomContentError): + ref_prop.clean( + "some-type--8a8e8758-f92c-4058-ba38-f061cd42a0cf", False, + ) + @pytest.mark.parametrize( "d", [ diff --git a/stix2/test/v20/test_report.py b/stix2/test/v20/test_report.py index 53707ce..2de9363 100644 --- a/stix2/test/v20/test_report.py +++ b/stix2/test/v20/test_report.py @@ -4,6 +4,7 @@ import pytest import pytz import stix2 +from stix2.exceptions import InvalidValueError from .constants import ( CAMPAIGN_ID, IDENTITY_ID, INDICATOR_ID, INDICATOR_KWARGS, RELATIONSHIP_ID, @@ -133,3 +134,30 @@ def test_parse_report(data): assert rept.name == "The Black Vine Cyberespionage Group" # TODO: Add other examples + + +def test_report_on_custom(): + with pytest.raises(InvalidValueError): + stix2.v20.Report( + name="my report", + labels=["a label"], + published="2016-01-20T17:00:00Z", + object_refs=[ + "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7", + "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" + ] + ) + + report = stix2.v20.Report( + name="my report", + labels=["a label"], + published="2016-01-20T17:00:00Z", + object_refs=[ + "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7", + "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" + ], + allow_custom=True, + ) + + assert "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" \ + in report.object_refs diff --git a/stix2/test/v21/test_properties.py b/stix2/test/v21/test_properties.py index 9914bd1..d2e2694 100644 --- a/stix2/test/v21/test_properties.py +++ b/stix2/test/v21/test_properties.py @@ -159,6 +159,11 @@ def test_reference_property_blacklist_type(): "identity--8a8e8758-f92c-4058-ba38-f061cd42a0cf", True, ) + with pytest.raises(CustomContentError): + ref_prop.clean( + "some-type--8a8e8758-f92c-4058-ba38-f061cd42a0cf", False, + ) + @pytest.mark.parametrize( "d", [ diff --git a/stix2/test/v21/test_report.py b/stix2/test/v21/test_report.py index e54e11d..69af096 100644 --- a/stix2/test/v21/test_report.py +++ b/stix2/test/v21/test_report.py @@ -9,6 +9,7 @@ from .constants import ( CAMPAIGN_ID, IDENTITY_ID, INDICATOR_ID, INDICATOR_KWARGS, RELATIONSHIP_ID, REPORT_ID, ) +from stix2.exceptions import InvalidValueError EXPECTED = """{ "type": "report", @@ -135,4 +136,27 @@ def test_parse_report(data): assert rept.report_types == ["campaign"] assert rept.name == "The Black Vine Cyberespionage Group" -# TODO: Add other examples + +def test_report_on_custom(): + with pytest.raises(InvalidValueError): + stix2.v21.Report( + name="my report", + published="2016-01-20T17:00:00Z", + object_refs=[ + "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7", + "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" + ] + ) + + report = stix2.v21.Report( + name="my report", + published="2016-01-20T17:00:00Z", + object_refs=[ + "indicator--a740531e-63ff-4e49-a9e1-a0a3eed0e3e7", + "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" + ], + allow_custom=True, + ) + + assert "some-type--2672975a-ce1e-4473-a1c6-0d79868930c7" \ + in report.object_refs diff --git a/stix2/v20/sdo.py b/stix2/v20/sdo.py index 538558a..3c854e0 100644 --- a/stix2/v20/sdo.py +++ b/stix2/v20/sdo.py @@ -235,7 +235,7 @@ class Report(_DomainObject): ('name', StringProperty(required=True)), ('description', StringProperty()), ('published', TimestampProperty(required=True)), - ('object_refs', ListProperty(ReferenceProperty(valid_types=["SCO", "SDO", "SRO"], spec_version='2.0'), required=True)), + ('object_refs', ListProperty(ReferenceProperty(invalid_types=[], spec_version='2.0'), required=True)), ('revoked', BooleanProperty(default=lambda: False)), ('labels', ListProperty(StringProperty, required=True)), ('external_references', ListProperty(ExternalReference)), diff --git a/stix2/v21/sdo.py b/stix2/v21/sdo.py index d0b6077..3b81e43 100644 --- a/stix2/v21/sdo.py +++ b/stix2/v21/sdo.py @@ -646,7 +646,7 @@ class Report(_DomainObject): ('description', StringProperty()), ('report_types', ListProperty(StringProperty)), ('published', TimestampProperty(required=True)), - ('object_refs', ListProperty(ReferenceProperty(valid_types=["SCO", "SDO", "SRO"], spec_version='2.1'), required=True)), + ('object_refs', ListProperty(ReferenceProperty(invalid_types=[], spec_version='2.1'), required=True)), ('revoked', BooleanProperty(default=lambda: False)), ('labels', ListProperty(StringProperty)), ('confidence', IntegerProperty()),