Merge pull request #392 from khdesai/fix_issue_389

Fix issue 389 - Add property names to canonicalization for deterministic id gen
master^2
Chris Lenk 2020-05-14 09:39:26 -04:00 committed by GitHub
commit 658e70bf04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 16 deletions

View File

@ -401,25 +401,25 @@ class _Observable(_STIXBase):
properties_to_use = self._id_contributing_properties properties_to_use = self._id_contributing_properties
if properties_to_use: if properties_to_use:
streamlined_obj_vals = [] streamlined_object = {}
if "hashes" in kwargs and "hashes" in properties_to_use: if "hashes" in kwargs and "hashes" in properties_to_use:
possible_hash = _choose_one_hash(kwargs["hashes"]) possible_hash = _choose_one_hash(kwargs["hashes"])
if possible_hash: if possible_hash:
streamlined_obj_vals.append(possible_hash) streamlined_object["hashes"] = possible_hash
for key in properties_to_use: for key in properties_to_use:
if key != "hashes" and key in kwargs: if key != "hashes" and key in kwargs:
if isinstance(kwargs[key], dict) or isinstance(kwargs[key], _STIXBase): if isinstance(kwargs[key], dict) or isinstance(kwargs[key], _STIXBase):
temp_deep_copy = copy.deepcopy(dict(kwargs[key])) temp_deep_copy = copy.deepcopy(dict(kwargs[key]))
_recursive_stix_to_dict(temp_deep_copy) _recursive_stix_to_dict(temp_deep_copy)
streamlined_obj_vals.append(temp_deep_copy) streamlined_object[key] = temp_deep_copy
elif isinstance(kwargs[key], list): elif isinstance(kwargs[key], list):
temp_deep_copy = copy.deepcopy(kwargs[key]) temp_deep_copy = copy.deepcopy(kwargs[key])
_recursive_stix_list_to_dict(temp_deep_copy) _recursive_stix_list_to_dict(temp_deep_copy)
streamlined_obj_vals.append(temp_deep_copy) streamlined_object[key] = temp_deep_copy
else: else:
streamlined_obj_vals.append(kwargs[key]) streamlined_object[key] = kwargs[key]
if streamlined_obj_vals: if streamlined_object:
data = canonicalize(streamlined_obj_vals, utf8=False) data = canonicalize(streamlined_object, utf8=False)
# The situation is complicated w.r.t. python 2/3 behavior, so # The situation is complicated w.r.t. python 2/3 behavior, so
# I'd rather not rely on particular exceptions being raised to # I'd rather not rely on particular exceptions being raised to

View File

@ -503,7 +503,7 @@ class ReferenceProperty(Property):
if possible_prefix not in ref_invalid_types: if possible_prefix not in ref_invalid_types:
required_prefix = possible_prefix required_prefix = possible_prefix
else: else:
raise ValueError("An invalid type-specifying prefix '%s' was specified for this property" % (possible_prefix, value)) raise ValueError("An invalid type-specifying prefix '%s' was specified for this property" % (possible_prefix))
_validate_id(value, self.spec_version, required_prefix) _validate_id(value, self.spec_version, required_prefix)

View File

@ -46,7 +46,7 @@ def test_observed_data_example():
objects={ objects={
"0": { "0": {
"type": "file", "type": "file",
"id": "file--5956efbb-a7b0-566d-a7f9-a202eb05c70f", "id": "file--7af1312c-4402-5d2f-b169-b118d73b85c4",
"name": "foo.exe", "name": "foo.exe",
}, },
}, },
@ -102,12 +102,12 @@ def test_observed_data_example_with_refs():
objects={ objects={
"0": { "0": {
"type": "file", "type": "file",
"id": "file--5956efbb-a7b0-566d-a7f9-a202eb05c70f", "id": "file--7af1312c-4402-5d2f-b169-b118d73b85c4",
"name": "foo.exe", "name": "foo.exe",
}, },
"1": { "1": {
"type": "directory", "type": "directory",
"id": "directory--536a61a4-0934-516b-9aad-fcbb75e0583a", "id": "directory--ee97f78e-7e2b-5b3d-bcbd-5692968cacea",
"path": "/usr/home", "path": "/usr/home",
"contains_refs": ["file--5956efbb-a7b0-566d-a7f9-a202eb05c70f"], "contains_refs": ["file--5956efbb-a7b0-566d-a7f9-a202eb05c70f"],
}, },
@ -719,7 +719,7 @@ def test_directory_example():
assert dir1.ctime == dt.datetime(2015, 12, 21, 19, 0, 0, tzinfo=pytz.utc) assert dir1.ctime == dt.datetime(2015, 12, 21, 19, 0, 0, tzinfo=pytz.utc)
assert dir1.mtime == dt.datetime(2015, 12, 24, 19, 0, 0, tzinfo=pytz.utc) assert dir1.mtime == dt.datetime(2015, 12, 24, 19, 0, 0, tzinfo=pytz.utc)
assert dir1.atime == dt.datetime(2015, 12, 21, 20, 0, 0, tzinfo=pytz.utc) assert dir1.atime == dt.datetime(2015, 12, 21, 20, 0, 0, tzinfo=pytz.utc)
assert dir1.contains_refs == ["file--9d050a3b-72cd-5b57-bf18-024e74e1e5eb"] assert dir1.contains_refs == ["file--c6ae2cf8-92d3-56d0-a25f-713efad643a7"]
def test_directory_example_ref_error(): def test_directory_example_ref_error():
@ -747,7 +747,7 @@ def test_domain_name_example():
) )
assert dn2.value == "example.com" assert dn2.value == "example.com"
assert dn2.resolves_to_refs == ["domain-name--02af94ea-7e38-5718-87c3-5cc023e3d49d"] assert dn2.resolves_to_refs == ["domain-name--5b5803bf-a7eb-5076-b799-96aa574c44eb"]
def test_domain_name_example_invalid_ref_type(): def test_domain_name_example_invalid_ref_type():
@ -1032,7 +1032,7 @@ def test_ipv4_address_valid_refs():
) )
assert ip4.value == "177.60.40.7" assert ip4.value == "177.60.40.7"
assert ip4.resolves_to_refs == ["mac-addr--a85820f7-d9b7-567a-a3a6-dedc34139342", "mac-addr--9a59b496-fdeb-510f-97b5-7137210bc699"] assert ip4.resolves_to_refs == ["mac-addr--f72d7d00-86bd-5cd2-8c86-52f7a83bef62", "mac-addr--875ad625-177b-5c2a-9101-d44b0ad55938"]
def test_ipv4_address_example_cidr(): def test_ipv4_address_example_cidr():
@ -1565,7 +1565,7 @@ def test_id_gen_recursive_dict_conversion_1():
}, },
) )
assert file_observable.id == "file--5219d93d-13c1-5f1f-896b-039f10ec67ea" assert file_observable.id == "file--ced31cd4-bdcb-537d-aefa-92d291bfc11d"
def test_id_gen_recursive_dict_conversion_2(): def test_id_gen_recursive_dict_conversion_2():
@ -1582,4 +1582,4 @@ def test_id_gen_recursive_dict_conversion_2():
], ],
) )
assert wrko.id == "windows-registry-key--c087d9fe-a03e-5922-a1cd-da116e5b8a7b" assert wrko.id == "windows-registry-key--36594eba-bcc7-5014-9835-0e154264e588"