Skip to content

Commit c7d1c45

Browse files
authored
refactor: decode_cpe22, decode_cpe23 (#4268)
Signed-off-by: Meet Soni <[email protected]>
1 parent b5263f9 commit c7d1c45

File tree

3 files changed

+47
-56
lines changed

3 files changed

+47
-56
lines changed

cve_bin_tool/parsers/__init__.py

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

44
from __future__ import annotations
55

6-
import re
76
import sqlite3
87

98
from packageurl import PackageURL
109

1110
from cve_bin_tool.cvedb import DBNAME, DISK_LOCATION_DEFAULT
1211
from cve_bin_tool.error_handler import CVEDBError
13-
from cve_bin_tool.util import ProductInfo, ScanInfo
12+
from cve_bin_tool.util import ProductInfo, ScanInfo, decode_cpe23
1413

1514
__all__ = [
1615
"parse",
@@ -128,7 +127,7 @@ def find_vendor_from_purl(self, purl, ver) -> tuple[list[ScanInfo], bool]:
128127

129128
if cpeList != []:
130129
for item in cpeList:
131-
vendor, _, _ = self.decode_cpe23(str(item))
130+
vendor, _, _ = decode_cpe23(str(item))
132131
vendors.add((vendor, purl["name"]))
133132
else:
134133
return vendorlist, False
@@ -141,7 +140,7 @@ def find_vendor_from_purl(self, purl, ver) -> tuple[list[ScanInfo], bool]:
141140
vendor,
142141
product,
143142
ver,
144-
"/usr/local/bin/product",
143+
self.filename,
145144
purl_with_ver,
146145
),
147146
self.filename,
@@ -212,15 +211,3 @@ def db_open_and_get_cursor(self) -> sqlite3.Cursor:
212211
self.logger.error("Database cursor does not exist")
213212
raise CVEDBError
214213
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: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from cve_bin_tool.util import (
2020
ProductInfo,
2121
Remarks,
22+
decode_cpe22,
23+
decode_cpe23,
2224
find_product_location,
2325
validate_location,
2426
)
@@ -349,10 +351,10 @@ def parse_ext_ref(self, ext_ref) -> (str | None, str | None, str | None):
349351
ref_type = ref[1]
350352
ref_string = ref[2]
351353
if ref_type == "cpe23Type" and self.is_valid_string("cpe23", ref_string):
352-
decoded["cpe23Type"] = self.decode_cpe23(ref_string)
354+
decoded["cpe23Type"] = decode_cpe23(ref_string)
353355

354356
elif ref_type == "cpe22Type" and self.is_valid_string("cpe22", ref_string):
355-
decoded["cpe22Type"] = self.decode_cpe22(ref_string)
357+
decoded["cpe22Type"] = decode_cpe22(ref_string)
356358

357359
elif ref_type == "purl" and self.is_valid_string("purl", ref_string):
358360
decoded["purl"] = self.decode_purl(ref_string)
@@ -363,44 +365,6 @@ def parse_ext_ref(self, ext_ref) -> (str | None, str | None, str | None):
363365
decoded.get("cpe22Type", decoded.get("purl", (None, None, None))),
364366
)
365367

366-
def decode_cpe22(self, cpe22) -> (str | None, str | None, str | None):
367-
"""
368-
Decode a CPE 2.2 formatted string to extract vendor, product, and version information.
369-
370-
Args:
371-
- cpe22 (str): CPE 2.2 formatted string.
372-
373-
Returns:
374-
- Tuple[str | None, str | None, str | None]: A tuple containing the vendor, product, and version
375-
information extracted from the CPE 2.2 string, or None if the information is incomplete.
376-
377-
"""
378-
379-
# split on `:` only if it's not escaped
380-
cpe = re.split(r"(?<!\\):", cpe22)
381-
vendor, product, version = cpe[2], cpe[3], cpe[4]
382-
# Return available data, convert empty fields to None
383-
return [vendor or None, product or None, version or None]
384-
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-
404368
def decode_purl(self, purl) -> (str | None, str | None, str | None):
405369
"""
406370
Decode a Package URL (purl) to extract version information.

cve_bin_tool/util.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,43 @@ def pattern_match(text: str, patterns: str) -> bool:
543543
if fnmatch.fnmatch(text, pattern):
544544
return True
545545
return False
546+
547+
548+
def decode_cpe23(cpe23) -> list:
549+
"""
550+
Decode a CPE 2.3 formatted string to extract vendor, product, and version information.
551+
552+
Args:
553+
- cpe23 (str): CPE 2.3 formatted string.
554+
555+
Returns:
556+
- list[str | None, str | None, str | None]: A tuple containing the vendor, product, and version
557+
information extracted from the CPE 2.3 string, or None if the information is incomplete.
558+
559+
"""
560+
561+
# split on `:` only if it's not escaped
562+
cpe = re.split(r"(?<!\\):", cpe23)
563+
vendor, product, version = cpe[3], cpe[4], cpe[5]
564+
# Return available data, convert empty fields to None
565+
return [vendor or None, product or None, version or None]
566+
567+
568+
def decode_cpe22(self, cpe22) -> list:
569+
"""
570+
Decode a CPE 2.2 formatted string to extract vendor, product, and version information.
571+
572+
Args:
573+
- cpe22 (str): CPE 2.2 formatted string.
574+
575+
Returns:
576+
- Tuple[str | None, str | None, str | None]: A tuple containing the vendor, product, and version
577+
information extracted from the CPE 2.2 string, or None if the information is incomplete.
578+
579+
"""
580+
581+
# split on `:` only if it's not escaped
582+
cpe = re.split(r"(?<!\\):", cpe22)
583+
vendor, product, version = cpe[2], cpe[3], cpe[4]
584+
# Return available data, convert empty fields to None
585+
return [vendor or None, product or None, version or None]

0 commit comments

Comments
 (0)