diff --git a/stix2/properties.py b/stix2/properties.py index 9f8849b..4f1ebd2 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -9,7 +9,6 @@ import re import uuid from six import string_types, text_type -from stix2patterns.validator import run_validator import stix2 @@ -558,16 +557,8 @@ class EnumProperty(StringProperty): class PatternProperty(StringProperty): - def __init__(self, spec_version=stix2.DEFAULT_VERSION, **kwargs): - self.spec_version = spec_version - super(PatternProperty, self).__init__(**kwargs) - def clean(self, value): cleaned_value = super(PatternProperty, self).clean(value) - errors = run_validator(cleaned_value, self.spec_version) - if errors: - raise ValueError(str(errors[0])) - return cleaned_value diff --git a/stix2/v20/sdo.py b/stix2/v20/sdo.py index c1f480d..430bfc0 100644 --- a/stix2/v20/sdo.py +++ b/stix2/v20/sdo.py @@ -3,8 +3,11 @@ from collections import OrderedDict import itertools +from stix2patterns.validator import run_validator + from ..core import STIXDomainObject from ..custom import _custom_object_builder +from ..exceptions import InvalidValueError from ..properties import ( BooleanProperty, IDProperty, IntegerProperty, ListProperty, ObservableProperty, PatternProperty, ReferenceProperty, StringProperty, @@ -124,7 +127,7 @@ class Indicator(STIXDomainObject): ('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')), ('name', StringProperty()), ('description', StringProperty()), - ('pattern', PatternProperty(required=True, spec_version='2.0')), + ('pattern', PatternProperty(required=True)), ('valid_from', TimestampProperty(default=lambda: NOW)), ('valid_until', TimestampProperty()), ('kill_chain_phases', ListProperty(KillChainPhase)), @@ -135,6 +138,11 @@ class Indicator(STIXDomainObject): ('granular_markings', ListProperty(GranularMarking)), ]) + def _check_object_constraints(self): + errors = run_validator(self.get('pattern'), '2.0') + if errors: + raise InvalidValueError(self.__class__, 'pattern', str(errors[0])) + class IntrusionSet(STIXDomainObject): """For more detailed information on this object's properties, see diff --git a/stix2/v21/sdo.py b/stix2/v21/sdo.py index 250d4a1..1d97261 100644 --- a/stix2/v21/sdo.py +++ b/stix2/v21/sdo.py @@ -5,10 +5,13 @@ import itertools import warnings from six.moves.urllib.parse import quote_plus +from stix2patterns.validator import run_validator from ..core import STIXDomainObject from ..custom import _custom_object_builder -from ..exceptions import PropertyPresenceError, STIXDeprecationWarning +from ..exceptions import ( + InvalidValueError, PropertyPresenceError, STIXDeprecationWarning, +) from ..properties import ( BinaryProperty, BooleanProperty, EmbeddedObjectProperty, EnumProperty, FloatProperty, IDProperty, IntegerProperty, ListProperty, @@ -200,7 +203,7 @@ class Indicator(STIXDomainObject): ('name', StringProperty()), ('description', StringProperty()), ('indicator_types', ListProperty(StringProperty, required=True)), - ('pattern', PatternProperty(required=True, spec_version='2.1')), + ('pattern', PatternProperty(required=True)), ('pattern_type', StringProperty(required=True)), ('pattern_version', StringProperty()), ('valid_from', TimestampProperty(default=lambda: NOW, required=True)), @@ -232,6 +235,16 @@ class Indicator(STIXDomainObject): msg = "{0.id} 'valid_until' must be greater than 'valid_from'" raise ValueError(msg.format(self)) + if self.get('pattern_type') == "stix": + try: + pat_ver = self.get('pattern_version') + except AttributeError: + pat_ver = '2.1' + + errors = run_validator(self.get('pattern'), pat_ver) + if errors: + raise InvalidValueError(self.__class__, 'pattern', str(errors[0])) + class Infrastructure(STIXDomainObject): # TODO: Add link