Introduce and relocate version-based pattern checking. Fixes #307

master
Desai, Kartikey H 2020-01-03 12:26:38 -05:00 committed by Chris Lenk
parent a18612bdfb
commit 4350680e79
3 changed files with 24 additions and 12 deletions

View File

@ -9,7 +9,6 @@ import re
import uuid import uuid
from six import string_types, text_type from six import string_types, text_type
from stix2patterns.validator import run_validator
import stix2 import stix2
@ -558,16 +557,8 @@ class EnumProperty(StringProperty):
class PatternProperty(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): def clean(self, value):
cleaned_value = super(PatternProperty, self).clean(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 return cleaned_value

View File

@ -3,8 +3,11 @@
from collections import OrderedDict from collections import OrderedDict
import itertools import itertools
from stix2patterns.validator import run_validator
from ..core import STIXDomainObject from ..core import STIXDomainObject
from ..custom import _custom_object_builder from ..custom import _custom_object_builder
from ..exceptions import InvalidValueError
from ..properties import ( from ..properties import (
BooleanProperty, IDProperty, IntegerProperty, ListProperty, BooleanProperty, IDProperty, IntegerProperty, ListProperty,
ObservableProperty, PatternProperty, ReferenceProperty, StringProperty, ObservableProperty, PatternProperty, ReferenceProperty, StringProperty,
@ -124,7 +127,7 @@ class Indicator(STIXDomainObject):
('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')), ('modified', TimestampProperty(default=lambda: NOW, precision='millisecond')),
('name', StringProperty()), ('name', StringProperty()),
('description', StringProperty()), ('description', StringProperty()),
('pattern', PatternProperty(required=True, spec_version='2.0')), ('pattern', PatternProperty(required=True)),
('valid_from', TimestampProperty(default=lambda: NOW)), ('valid_from', TimestampProperty(default=lambda: NOW)),
('valid_until', TimestampProperty()), ('valid_until', TimestampProperty()),
('kill_chain_phases', ListProperty(KillChainPhase)), ('kill_chain_phases', ListProperty(KillChainPhase)),
@ -135,6 +138,11 @@ class Indicator(STIXDomainObject):
('granular_markings', ListProperty(GranularMarking)), ('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): class IntrusionSet(STIXDomainObject):
"""For more detailed information on this object's properties, see """For more detailed information on this object's properties, see

View File

@ -5,10 +5,13 @@ import itertools
import warnings import warnings
from six.moves.urllib.parse import quote_plus from six.moves.urllib.parse import quote_plus
from stix2patterns.validator import run_validator
from ..core import STIXDomainObject from ..core import STIXDomainObject
from ..custom import _custom_object_builder from ..custom import _custom_object_builder
from ..exceptions import PropertyPresenceError, STIXDeprecationWarning from ..exceptions import (
InvalidValueError, PropertyPresenceError, STIXDeprecationWarning,
)
from ..properties import ( from ..properties import (
BinaryProperty, BooleanProperty, EmbeddedObjectProperty, EnumProperty, BinaryProperty, BooleanProperty, EmbeddedObjectProperty, EnumProperty,
FloatProperty, IDProperty, IntegerProperty, ListProperty, FloatProperty, IDProperty, IntegerProperty, ListProperty,
@ -200,7 +203,7 @@ class Indicator(STIXDomainObject):
('name', StringProperty()), ('name', StringProperty()),
('description', StringProperty()), ('description', StringProperty()),
('indicator_types', ListProperty(StringProperty, required=True)), ('indicator_types', ListProperty(StringProperty, required=True)),
('pattern', PatternProperty(required=True, spec_version='2.1')), ('pattern', PatternProperty(required=True)),
('pattern_type', StringProperty(required=True)), ('pattern_type', StringProperty(required=True)),
('pattern_version', StringProperty()), ('pattern_version', StringProperty()),
('valid_from', TimestampProperty(default=lambda: NOW, required=True)), ('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'" msg = "{0.id} 'valid_until' must be greater than 'valid_from'"
raise ValueError(msg.format(self)) 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): class Infrastructure(STIXDomainObject):
# TODO: Add link # TODO: Add link