File tree Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change 2323
2424# See https://github.com/package-url/packageurl-python/issues/65
2525import serializable
26+ from cpe import CPE # type:ignore
2627from packageurl import PackageURL
2728from sortedcontainers import SortedSet
2829
@@ -1457,7 +1458,11 @@ def cpe(self) -> Optional[str]:
14571458
14581459 @cpe .setter
14591460 def cpe (self , cpe : Optional [str ]) -> None :
1460- self ._cpe = cpe
1461+ if cpe :
1462+ try :
1463+ CPE (cpe )
1464+ except NotImplementedError :
1465+ raise ValueError (f'Invalid CPE format: { cpe } ' )
14611466
14621467 @property
14631468 @serializable .type_mapping (PackageUrlSH )
Original file line number Diff line number Diff line change @@ -74,6 +74,7 @@ sortedcontainers = "^2.4.0"
7474license-expression = " ^30"
7575jsonschema = { version = " ^4.18" , extras =[' format' ], optional =true }
7676lxml = { version =" >=4,<6" , optional =true }
77+ cpe = " ^1.3.1"
7778
7879[tool .poetry .extras ]
7980validation = [" jsonschema" , " lxml" ]
Original file line number Diff line number Diff line change @@ -283,6 +283,16 @@ def test_nested_components_2(self) -> None:
283283 self .assertEqual (3 , len (comp_b .get_all_nested_components (include_self = True )))
284284 self .assertEqual (2 , len (comp_b .get_all_nested_components (include_self = False )))
285285
286+ def test_cpe_validation_valid_format (self ) -> None :
287+ cpe = 'cpe:2.3:a:python:setuptools:50.3.2:*:*:*:*:*:*:*'
288+ c = Component (name = 'test-component' , cpe = cpe )
289+ self .assertEqual (c .cpe , cpe )
290+
291+ def test_cpe_validation_invalid_format (self ) -> None :
292+ invalid_cpe = 'invalid-cpe-string'
293+ with self .assertRaises (ValueError ):
294+ Component (name = 'test-component' , cpe = invalid_cpe )
295+
286296
287297class TestModelComponentEvidence (TestCase ):
288298
You can’t perform that action at this time.
0 commit comments