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 #706

Closed
wants to merge 4 commits into from
Closed
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
12 changes: 12 additions & 0 deletions cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
from .license import License, LicenseRepository
from .release_note import ReleaseNotes

CPE_REGEX = re.compile(
r'([c][pP][eE]:/[AHOaho]?(:[A-Za-z0-9._\-~%]*){0,6})|'
r'(cpe:2\.3:[aho*-](:(((\?*|\*?)([a-zA-Z0-9\-._]|'
r'(\\[\\\*\?!\"#\$%&\'\(\)\+,/:;<=>@\[\]\^`\{\|\}~]))+(\?*|\*?))|'
r'[\*\-])){5}(:(([a-zA-Z]{2,3}(-([a-zA-Z]{2}|[0-9]{3}))?)|'
r'[\*\-]))(:(((\?*|\*?)([a-zA-Z0-9\-._]|'
r'(\\[\\\*\?!\"#\$%&\'\(\)\+,/:;<=>@\[\]\^`\{\|\}~]))+(\?*|'
r'\*?))|[\*\-])){4})'
)


@serializable.serializable_class
class Commit:
Expand Down Expand Up @@ -1457,6 +1467,8 @@ def cpe(self) -> Optional[str]:

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

@jkowalleck jkowalleck Oct 14, 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 a breaking change.

This is a remark, not a blocker. :-)

self._cpe = cpe

@property
Expand Down
11 changes: 11 additions & 0 deletions tests/test_model_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def test_empty_basic_component(self) -> None:
self.assertSetEqual(c.external_references, set())
self.assertFalse(c.properties)
self.assertIsNone(c.release_notes)
self.assertIsNone(c.cpe)
self.assertEqual(len(c.components), 0)
self.assertEqual(len(c.get_all_nested_components(include_self=True)), 1)

Expand Down Expand Up @@ -283,6 +284,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(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