Merge pull request #287 from zrush-mitre/master

Fixing precision not holding on a marking-definition during parsing and serialization
master
Chris Lenk 2019-09-17 09:10:51 -04:00 committed by GitHub
commit 91f2e50321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 16 deletions

View File

@ -25,7 +25,7 @@ EXPECTED_STATEMENT_MARKING_DEFINITION = """{
"type": "marking-definition", "type": "marking-definition",
"spec_version": "2.1", "spec_version": "2.1",
"id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9", "id": "marking-definition--613f2e26-407d-48c7-9eca-b8e91df99dc9",
"created": "2017-01-20T00:00:00Z", "created": "2017-01-20T00:00:00.000Z",
"definition_type": "statement", "definition_type": "statement",
"definition": { "definition": {
"statement": "Copyright 2016, Example Corp" "statement": "Copyright 2016, Example Corp"

View File

@ -3,6 +3,8 @@
from collections import OrderedDict from collections import OrderedDict
import copy import copy
import six
from ..base import _STIXBase from ..base import _STIXBase
from ..custom import _custom_marking_builder from ..custom import _custom_marking_builder
from ..markings import _MarkingsMixin from ..markings import _MarkingsMixin
@ -14,6 +16,21 @@ from ..properties import (
from ..utils import NOW, _get_dict from ..utils import NOW, _get_dict
def _should_set_millisecond(cr, marking_type):
# TLP instances in the 2.0 spec have millisecond precision unlike other markings
if marking_type == TLPMarking:
return True
# otherwise, precision is kept from how it was given
if isinstance(cr, six.string_types):
if '.' in cr:
return True
else:
return False
if cr.precision == 'millisecond':
return True
return False
class ExternalReference(_STIXBase): class ExternalReference(_STIXBase):
"""For more detailed information on this object's properties, see """For more detailed information on this object's properties, see
`the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part1-stix-core/stix-v2.0-cs01-part1-stix-core.html#_Toc496709261>`__. `the STIX 2.0 specification <http://docs.oasis-open.org/cti/stix/v2.0/cs01/part1-stix-core/stix-v2.0-cs01-part1-stix-core.html#_Toc496709261>`__.
@ -122,12 +139,12 @@ class MarkingDefinition(_STIXBase, _MarkingsMixin):
except KeyError: except KeyError:
raise ValueError("definition_type must be a valid marking type") raise ValueError("definition_type must be a valid marking type")
if marking_type == TLPMarking: if 'created' in kwargs:
# TLP instances in the spec have millisecond precision unlike other markings if _should_set_millisecond(kwargs['created'], marking_type):
self._properties = copy.deepcopy(self._properties) self._properties = copy.deepcopy(self._properties)
self._properties.update([ self._properties.update([
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')), ('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
]) ])
if not isinstance(kwargs['definition'], marking_type): if not isinstance(kwargs['definition'], marking_type):
defn = _get_dict(kwargs['definition']) defn = _get_dict(kwargs['definition'])

View File

@ -1,7 +1,6 @@
"""STIX 2.1 Common Data Types and Properties.""" """STIX 2.1 Common Data Types and Properties."""
from collections import OrderedDict from collections import OrderedDict
import copy
from ..base import _STIXBase from ..base import _STIXBase
from ..custom import _custom_marking_builder from ..custom import _custom_marking_builder
@ -146,7 +145,7 @@ class MarkingDefinition(_STIXBase, _MarkingsMixin):
('spec_version', StringProperty(fixed='2.1')), ('spec_version', StringProperty(fixed='2.1')),
('id', IDProperty(_type)), ('id', IDProperty(_type)),
('created_by_ref', ReferenceProperty(type='identity')), ('created_by_ref', ReferenceProperty(type='identity')),
('created', TimestampProperty(default=lambda: NOW)), ('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
('external_references', ListProperty(ExternalReference)), ('external_references', ListProperty(ExternalReference)),
('object_marking_refs', ListProperty(ReferenceProperty(type='marking-definition'))), ('object_marking_refs', ListProperty(ReferenceProperty(type='marking-definition'))),
('granular_markings', ListProperty(GranularMarking)), ('granular_markings', ListProperty(GranularMarking)),
@ -162,13 +161,6 @@ class MarkingDefinition(_STIXBase, _MarkingsMixin):
except KeyError: except KeyError:
raise ValueError("definition_type must be a valid marking type") raise ValueError("definition_type must be a valid marking type")
if marking_type == TLPMarking:
# TLP instances in the spec have millisecond precision unlike other markings
self._properties = copy.deepcopy(self._properties)
self._properties.update([
('created', TimestampProperty(default=lambda: NOW, precision='millisecond')),
])
if not isinstance(kwargs['definition'], marking_type): if not isinstance(kwargs['definition'], marking_type):
defn = _get_dict(kwargs['definition']) defn = _get_dict(kwargs['definition'])
kwargs['definition'] = marking_type(**defn) kwargs['definition'] = marking_type(**defn)