Skip to content

feat(sbom): add purl #4910

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

Open
wants to merge 1 commit 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
28 changes: 28 additions & 0 deletions cve_bin_tool/cvedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,34 @@ def update_vendors(self, cve_data):

return updated_severity, updated_affected

def guess_purl(self, vendor, product, version):
query = """
SELECT
distinct purl
FROM purl2cpe
WHERE cpe like ?
"""
cursor = self.db_open_and_get_cursor()
cursor.execute(query, ["%:" + vendor + ":" + product + ":%"])

# Prefer upstream purl (e.g., github, gitlab, sourceforge)
# over distribution specific purl (e.g. debian, ubuntu, fedora)
purl_entries = [x[0] for x in cursor.fetchall()]
preferred_upstream = [
vendor,
f"github/{vendor}",
f"gitlab/{vendor}",
f"sourceforge/{vendor}",
"github",
"gitlab",
"sourceforge",
"",
]
for subs in preferred_upstream:
res = [i for i in purl_entries if f"pkg:{subs}" in i]
if res:
return res[0] + "@" + version

def db_open_and_get_cursor(self) -> sqlite3.Cursor:
"""Opens connection to sqlite database, returns cursor object."""

Expand Down
2 changes: 2 additions & 0 deletions cve_bin_tool/sbom_manager/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def generate_sbom(self) -> None:
else:
evidence = path
my_package.set_evidence(evidence)
if product_data.purl:
my_package.set_purl(product_data.purl)
self.sbom_packages[
(
my_package.get_name(),
Expand Down
3 changes: 2 additions & 1 deletion cve_bin_tool/version_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ def run_checkers(self, filename: str, lines: str) -> Iterator[ScanInfo]:
f"{file_path} matched {dummy_checker_name} {version} ({version_results.matched_filename=}, {version_results.matched_contains=})"
)
for vendor, product in checker.VENDOR_PRODUCT:
purl = self.cve_db.guess_purl(vendor, product, version)
yield ScanInfo(
ProductInfo(vendor, product, version),
ProductInfo(vendor, product, version, purl),
file_path,
)

Expand Down
Loading