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

feat: add CPE format validation in property setter #711

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

# See https://github.com/package-url/packageurl-python/issues/65
import serializable
from cpe import CPE # type:ignore
from packageurl import PackageURL
from sortedcontainers import SortedSet

Expand Down Expand Up @@ -1455,6 +1456,11 @@ def cpe(self) -> Optional[str]:

@cpe.setter
def cpe(self, cpe: Optional[str]) -> None:
if cpe:
try:
CPE(cpe)
except NotImplementedError:
raise ValueError(f'Invalid CPE format: {cpe}')
Copy link
Member

@jkowalleck jkowalleck Oct 15, 2024

Choose a reason for hiding this comment

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

This behavioral change is considered a breaking change.
Not a blocker, just a remark.

self._cpe = cpe

@property
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ sortedcontainers = "^2.4.0"
license-expression = "^30"
jsonschema = { version = "^4.18", extras=['format'], optional=true }
lxml = { version=">=4,<6", optional=true }
cpe = "^1.3.1"

[tool.poetry.extras]
validation = ["jsonschema", "lxml"]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ def test_nested_components_2(self) -> None:
self.assertEqual(3, len(comp_b.get_all_nested_components(include_self=True)))
self.assertEqual(2, len(comp_b.get_all_nested_components(include_self=False)))

def test_cpe_validation_valid_format(self) -> None:
cpe = 'cpe:2.3:a:python:setuptools:50.3.2:*:*:*:*:*:*:*'
c = Component(name='test-component', cpe=cpe)
self.assertEqual(c.cpe, cpe)

def test_cpe_validation_invalid_format(self) -> None:
invalid_cpe = 'invalid-cpe-string'
with self.assertRaises(ValueError):
Component(name='test-component', cpe=invalid_cpe)


class TestModelComponentEvidence(TestCase):

Expand Down