Add back usage of spec_version where HashesProperty is used for

various STIX objects.  Also pre-commit stylistic fixes...
pull/1/head
Michael Chisholm 2020-07-10 16:57:22 -04:00
parent a8a65599bf
commit c8c4e89415
4 changed files with 57 additions and 52 deletions

View File

@ -1,8 +1,10 @@
from stix2.hashes import Hash, infer_hash_algorithm, check_hash
import pytest
from stix2.hashes import Hash, check_hash, infer_hash_algorithm
@pytest.mark.parametrize("hash_name, expected_alg", [
@pytest.mark.parametrize(
"hash_name, expected_alg", [
("md5", Hash.MD5),
("md6", Hash.MD6),
("ripemd160", Hash.RIPEMD160),
@ -19,7 +21,8 @@ import pytest
("whirlpool", Hash.WHIRLPOOL),
("tlsh", Hash.TLSH),
("xxxx", None),
])
],
)
def test_hash_inference(hash_name, expected_alg):
alg = infer_hash_algorithm(hash_name)
assert alg == expected_alg
@ -32,7 +35,8 @@ def test_hash_inference(hash_name, expected_alg):
assert alg == expected_alg
@pytest.mark.parametrize("hash_alg, hash_value", [
@pytest.mark.parametrize(
"hash_alg, hash_value", [
(Hash.MD5, "f9e40b9aa5464f3dae711ca524fceb63"),
(Hash.MD6, "f9e40b9aa5464f3dae711ca524fceb63"),
(Hash.RIPEMD160, "8ae5d2e6b1f3a514257f2469b637454931844aeb"),
@ -49,7 +53,8 @@ def test_hash_inference(hash_name, expected_alg):
(Hash.WHIRLPOOL, "b752b6eeb497a8bebfc1be1649ca41d57fd1973bffc2261ca196b5474e0f353762f354c1d743581f61c51f4d86921360bc2e8ad35e830578b68b12e884a50894"),
(Hash.TLSH, "6FF02BEF718027B0160B4391212923ED7F1A463D563B1549B86CF62973B197AD2731F8"),
("foo", "bar"), # unrecognized hash type is accepted as-is
])
],
)
def test_hash_check(hash_alg, hash_value):
assert check_hash(hash_alg, hash_value)
assert check_hash(hash_alg, hash_value.upper()) # check case sensitivity

View File

@ -31,7 +31,7 @@ class Artifact(_Observable):
('mime_type', StringProperty()),
('payload_bin', BinaryProperty()),
('url', StringProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
('extensions', ExtensionsProperty(spec_version="2.0", enclosing_type=_type)),
])
@ -174,7 +174,7 @@ class AlternateDataStream(_STIXBase20):
_properties = OrderedDict([
('name', StringProperty(required=True)),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
('size', IntegerProperty()),
])
@ -257,7 +257,7 @@ class WindowsPEOptionalHeaderType(_STIXBase20):
('size_of_heap_commit', IntegerProperty()),
('loader_flags_hex', HexProperty()),
('number_of_rva_and_sizes', IntegerProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
])
def _check_object_constraints(self):
@ -274,7 +274,7 @@ class WindowsPESection(_STIXBase20):
('name', StringProperty(required=True)),
('size', IntegerProperty()),
('entropy', FloatProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
])
@ -294,7 +294,7 @@ class WindowsPEBinaryExt(_Extension):
('number_of_symbols', IntegerProperty()),
('size_of_optional_header', IntegerProperty()),
('characteristics_hex', HexProperty()),
('file_header_hashes', HashesProperty(HASHING_ALGORITHM)),
('file_header_hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
('optional_header', EmbeddedObjectProperty(type=WindowsPEOptionalHeaderType)),
('sections', ListProperty(EmbeddedObjectProperty(type=WindowsPESection))),
])
@ -308,7 +308,7 @@ class File(_Observable):
_type = 'file'
_properties = OrderedDict([
('type', TypeProperty(_type, spec_version='2.0')),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
('size', IntegerProperty()),
('name', StringProperty()),
('name_enc', StringProperty()),
@ -772,7 +772,7 @@ class X509Certificate(_Observable):
_properties = OrderedDict([
('type', TypeProperty(_type, spec_version='2.0')),
('is_self_signed', BooleanProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.0")),
('version', StringProperty()),
('serial_number', StringProperty()),
('signature_algorithm', StringProperty()),

View File

@ -25,7 +25,7 @@ class ExternalReference(_STIXBase21):
('source_name', StringProperty(required=True)),
('description', StringProperty()),
('url', StringProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('external_id', StringProperty()),
])

View File

@ -40,7 +40,7 @@ class Artifact(_Observable):
('mime_type', StringProperty()),
('payload_bin', BinaryProperty()),
('url', StringProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('encryption_algorithm', EnumProperty(ENCRYPTION_ALGORITHM)),
('decryption_key', StringProperty()),
('object_marking_refs', ListProperty(ReferenceProperty(valid_types='marking-definition', spec_version='2.1'))),
@ -219,7 +219,7 @@ class AlternateDataStream(_STIXBase21):
_properties = OrderedDict([
('name', StringProperty(required=True)),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('size', IntegerProperty()),
])
@ -301,7 +301,7 @@ class WindowsPEOptionalHeaderType(_STIXBase21):
('size_of_heap_commit', IntegerProperty()),
('loader_flags_hex', HexProperty()),
('number_of_rva_and_sizes', IntegerProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
])
def _check_object_constraints(self):
@ -318,7 +318,7 @@ class WindowsPESection(_STIXBase21):
('name', StringProperty(required=True)),
('size', IntegerProperty(min=0)),
('entropy', FloatProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
])
@ -338,7 +338,7 @@ class WindowsPEBinaryExt(_Extension):
('number_of_symbols', IntegerProperty(min=0)),
('size_of_optional_header', IntegerProperty(min=0)),
('characteristics_hex', HexProperty()),
('file_header_hashes', HashesProperty(HASHING_ALGORITHM)),
('file_header_hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('optional_header', EmbeddedObjectProperty(type=WindowsPEOptionalHeaderType)),
('sections', ListProperty(EmbeddedObjectProperty(type=WindowsPESection))),
])
@ -354,7 +354,7 @@ class File(_Observable):
('type', TypeProperty(_type, spec_version='2.1')),
('spec_version', StringProperty(fixed='2.1')),
('id', IDProperty(_type, spec_version='2.1')),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('size', IntegerProperty(min=0)),
('name', StringProperty()),
('name_enc', StringProperty()),
@ -826,7 +826,7 @@ class X509Certificate(_Observable):
('spec_version', StringProperty(fixed='2.1')),
('id', IDProperty(_type, spec_version='2.1')),
('is_self_signed', BooleanProperty()),
('hashes', HashesProperty(HASHING_ALGORITHM)),
('hashes', HashesProperty(HASHING_ALGORITHM, spec_version="2.1")),
('version', StringProperty()),
('serial_number', StringProperty()),
('signature_algorithm', StringProperty()),