Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include BomRef within Component hash calculation #678

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ def __hash__(self) -> int:
self.mime_type, self.supplier, self.author, self.publisher,
self.description, self.scope, tuple(self.hashes),
tuple(self.licenses), self.copyright, self.cpe,
self.purl,
self.purl, self.bom_ref.value,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why you go with self.bom_ref.value instead of self.bom_ref.
BomRef.__hash__ exists and is implemented to account for the __id__ of the "empty" object entity, so that None values are unequal.
Your implementation does not do so, and may cause unexpected behaviour here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed I had originally only added self.bom_ref, but that would then not deduplicate the Nones. Is there a reason for the BomRef hash to include the id? It seems superfluous when there is a unique identifier. Note also that the code claims a random UUIDv4 should have been assigned when there is no ref, but this is not actually the case

self.swid, self.pedigree,
tuple(self.external_references), tuple(self.properties),
tuple(self.components), self.evidence, self.release_notes, self.modified,
Expand Down
48 changes: 48 additions & 0 deletions tests/_data/own/json/1.5/duplicate_components.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/test_model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ def test_component_equal_3(self) -> None:

self.assertNotEqual(c, c2)

def test_component_equal_4(self) -> None:
c = Component(
name='test-component', version='1.2.3', bom_ref='ref1'
)
c2 = Component(
name='test-component', version='1.2.3', bom_ref='ref2'
)

self.assertNotEqual(c, c2)

def test_same_1(self) -> None:
c1 = get_component_setuptools_simple()
c2 = get_component_setuptools_simple()
Expand Down
9 changes: 9 additions & 0 deletions tests/test_real_world_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import unittest
from datetime import datetime
from json import loads as json_loads
from os.path import join
from typing import Any
from unittest.mock import patch
Expand All @@ -36,3 +37,11 @@ def test_webgoat_6_1(self, *_: Any, **__: Any) -> None:
def test_regression_issue_630(self, *_: Any, **__: Any) -> None:
with open(join(OWN_DATA_DIRECTORY, 'xml', '1.6', 'regression_issue630.xml')) as input_xml:
Bom.from_xml(input_xml)

def test_merged_bom_duplicate_component(self, *_: Any, **__: Any) -> None:
with open(join(OWN_DATA_DIRECTORY, 'json', '1.5', 'duplicate_components.json')) as input_json:
json = json_loads(input_json.read())

bom = Bom.from_json(json)
self.assertEqual(4, len(bom.components)) # tests https://github.com/CycloneDX/cyclonedx-python-lib/issues/540
bom.validate() # tests https://github.com/CycloneDX/cyclonedx-python-lib/issues/677