Skip to content

Commit bae0d45

Browse files
committed
refactor: decode_cpe23
Signed-off-by: Meet Soni <[email protected]>
1 parent 1d15256 commit bae0d45

File tree

3 files changed

+26
-36
lines changed

3 files changed

+26
-36
lines changed

cve_bin_tool/parsers/__init__.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
from __future__ import annotations
55

6-
import re
6+
# import re
77
import sqlite3
88

99
from packageurl import PackageURL
1010

1111
from cve_bin_tool.cvedb import DBNAME, DISK_LOCATION_DEFAULT
1212
from cve_bin_tool.error_handler import CVEDBError
13-
from cve_bin_tool.util import ProductInfo, ScanInfo
13+
from cve_bin_tool.util import ProductInfo, ScanInfo, decode_cpe23
1414

1515
__all__ = [
1616
"parse",
@@ -128,7 +128,7 @@ def find_vendor_from_purl(self, purl, ver) -> tuple[list[ScanInfo], bool]:
128128

129129
if cpeList != []:
130130
for item in cpeList:
131-
vendor, _, _ = self.decode_cpe23(str(item))
131+
vendor, _, _ = decode_cpe23(str(item))
132132
vendors.add((vendor, purl["name"]))
133133
else:
134134
return vendorlist, False
@@ -141,7 +141,7 @@ def find_vendor_from_purl(self, purl, ver) -> tuple[list[ScanInfo], bool]:
141141
vendor,
142142
product,
143143
ver,
144-
"/usr/local/bin/product",
144+
self.filename,
145145
purl_with_ver,
146146
),
147147
self.filename,
@@ -212,15 +212,3 @@ def db_open_and_get_cursor(self) -> sqlite3.Cursor:
212212
self.logger.error("Database cursor does not exist")
213213
raise CVEDBError
214214
return cursor
215-
216-
def decode_cpe23(self, cpe23) -> tuple[str, str, str]:
217-
"""
218-
Decodes a CPE 2.3 formatted string to extract vendor, product, and version information.
219-
220-
"""
221-
222-
# split on `:` only if it's not escaped
223-
cpe = re.split(r"(?<!\\):", cpe23)
224-
vendor, product, version = cpe[3], cpe[4], cpe[5]
225-
# Return available data, convert empty fields to None
226-
return (vendor, product, version)

cve_bin_tool/sbom_manager/parse.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from cve_bin_tool.util import (
2020
ProductInfo,
2121
Remarks,
22+
decode_cpe23,
2223
find_product_location,
2324
validate_location,
2425
)
@@ -349,7 +350,7 @@ def parse_ext_ref(self, ext_ref) -> (str | None, str | None, str | None):
349350
ref_type = ref[1]
350351
ref_string = ref[2]
351352
if ref_type == "cpe23Type" and self.is_valid_string("cpe23", ref_string):
352-
decoded["cpe23Type"] = self.decode_cpe23(ref_string)
353+
decoded["cpe23Type"] = decode_cpe23(ref_string)
353354

354355
elif ref_type == "cpe22Type" and self.is_valid_string("cpe22", ref_string):
355356
decoded["cpe22Type"] = self.decode_cpe22(ref_string)
@@ -382,25 +383,6 @@ def decode_cpe22(self, cpe22) -> (str | None, str | None, str | None):
382383
# Return available data, convert empty fields to None
383384
return [vendor or None, product or None, version or None]
384385

385-
def decode_cpe23(self, cpe23) -> (str | None, str | None, str | None):
386-
"""
387-
Decode a CPE 2.3 formatted string to extract vendor, product, and version information.
388-
389-
Args:
390-
- cpe23 (str): CPE 2.3 formatted string.
391-
392-
Returns:
393-
- Tuple[str | None, str | None, str | None]: A tuple containing the vendor, product, and version
394-
information extracted from the CPE 2.3 string, or None if the information is incomplete.
395-
396-
"""
397-
398-
# split on `:` only if it's not escaped
399-
cpe = re.split(r"(?<!\\):", cpe23)
400-
vendor, product, version = cpe[3], cpe[4], cpe[5]
401-
# Return available data, convert empty fields to None
402-
return [vendor or None, product or None, version or None]
403-
404386
def decode_purl(self, purl) -> (str | None, str | None, str | None):
405387
"""
406388
Decode a Package URL (purl) to extract version information.

cve_bin_tool/util.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,23 @@ def pattern_match(text: str, patterns: str) -> bool:
524524
if fnmatch.fnmatch(text, pattern):
525525
return True
526526
return False
527+
528+
529+
def decode_cpe23(cpe23) -> list:
530+
"""
531+
Decode a CPE 2.3 formatted string to extract vendor, product, and version information.
532+
533+
Args:
534+
- cpe23 (str): CPE 2.3 formatted string.
535+
536+
Returns:
537+
- list[str | None, str | None, str | None]: A tuple containing the vendor, product, and version
538+
information extracted from the CPE 2.3 string, or None if the information is incomplete.
539+
540+
"""
541+
542+
# split on `:` only if it's not escaped
543+
cpe = re.split(r"(?<!\\):", cpe23)
544+
vendor, product, version = cpe[3], cpe[4], cpe[5]
545+
# Return available data, convert empty fields to None
546+
return [vendor or None, product or None, version or None]

0 commit comments

Comments
 (0)