Skip to content

Commit

Permalink
feat: added PURL generation to ruby parser (intel#3939)
Browse files Browse the repository at this point in the history
Signed-off-by: Meet Soni <[email protected]>
  • Loading branch information
inosmeet authored Mar 21, 2024
1 parent 8087b86 commit d81ca11
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions cve_bin_tool/parsers/ruby.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,54 @@


class RubyParser(Parser):
"""
Parser implementation for Ruby gem files (Gemfile.lock).
This parser is designed to parse Ruby gem files and generate Package URL (PURL) strings
based on the modules and their dependencies listed in the file.
Attributes:
cve_db (CVEDB): The CVE database instance used for vulnerability information.
logger (Logger): The logger instance for logging messages and debugging information.
Methods:
generate_purl(product, version, vendor):
Generates PURL after normalizing all components.
run_checker(filename):
Parse the Ruby gem file and yield valid PURLs for the modules listed in the file.
"""

def __init__(self, cve_db, logger):
super().__init__(cve_db, logger)
self.purl_pkg_type = "gem"

def generate_purl(self, product, version, vendor, qualifier={}, subpath=None):
"""Generates PURL after normalizing all components."""

product = re.sub(r"^[^a-z]|[^a-z0-9_-]", "", product)
version = re.sub(r"^[^0-9]|[^a-zA-Z0-9.+-]", "", version)
vendor = re.sub(r"^[^a-z]|[^a-z0-9_-]", "", vendor)

if not re.match(r"^[a-z]|[a-z0-9_-]", product):
return
if vendor == "":
vendor = "UNKNOWN"
if version == "":
version = "UNKNOWN"

purl = super().generate_purl(
product,
version,
vendor,
qualifier,
subpath,
)

return purl

def run_checker(self, filename):
"""Parse the file and yield valid PURLs."""
self.filename = filename
with open(filename) as fh:
lines = fh.readlines()
Expand All @@ -29,7 +73,7 @@ def run_checker(self, filename):
):
product = line.strip().split()[0]
version = line.strip().split("(")[1][:-1]
vendor = self.find_vendor(product, version)
if vendor is not None:
yield from vendor
vendors = self.find_vendor(product, version)
if vendors is not None:
yield from vendors
self.logger.debug(f"Done scanning file: {self.filename}")

0 comments on commit d81ca11

Please sign in to comment.