7
7
8
8
9
9
class RubyParser (Parser ):
10
+ """
11
+ Parser implementation for Ruby gem files (Gemfile.lock).
12
+
13
+ This parser is designed to parse Ruby gem files and generate Package URL (PURL) strings
14
+ based on the modules and their dependencies listed in the file.
15
+
16
+ Attributes:
17
+ cve_db (CVEDB): The CVE database instance used for vulnerability information.
18
+ logger (Logger): The logger instance for logging messages and debugging information.
19
+
20
+ Methods:
21
+ generate_purl(product, version, vendor):
22
+ Generates PURL after normalizing all components.
23
+ run_checker(filename):
24
+ Parse the Ruby gem file and yield valid PURLs for the modules listed in the file.
25
+
26
+ """
27
+
10
28
def __init__ (self , cve_db , logger ):
11
29
super ().__init__ (cve_db , logger )
30
+ self .purl_pkg_type = "gem"
31
+
32
+ def generate_purl (self , product , version , vendor , qualifier = {}, subpath = None ):
33
+ """Generates PURL after normalizing all components."""
34
+
35
+ product = re .sub (r"^[^a-z]|[^a-z0-9_-]" , "" , product )
36
+ version = re .sub (r"^[^0-9]|[^a-zA-Z0-9.+-]" , "" , version )
37
+ vendor = re .sub (r"^[^a-z]|[^a-z0-9_-]" , "" , vendor )
38
+
39
+ if not re .match (r"^[a-z]|[a-z0-9_-]" , product ):
40
+ return
41
+ if vendor == "" :
42
+ vendor = "UNKNOWN"
43
+ if version == "" :
44
+ version = "UNKNOWN"
45
+
46
+ purl = super ().generate_purl (
47
+ product ,
48
+ version ,
49
+ vendor ,
50
+ qualifier ,
51
+ subpath ,
52
+ )
53
+
54
+ return purl
12
55
13
56
def run_checker (self , filename ):
57
+ """Parse the file and yield valid PURLs."""
14
58
self .filename = filename
15
59
with open (filename ) as fh :
16
60
lines = fh .readlines ()
@@ -29,7 +73,7 @@ def run_checker(self, filename):
29
73
):
30
74
product = line .strip ().split ()[0 ]
31
75
version = line .strip ().split ("(" )[1 ][:- 1 ]
32
- vendor = self .find_vendor (product , version )
33
- if vendor is not None :
34
- yield from vendor
76
+ vendors = self .find_vendor (product , version )
77
+ if vendors is not None :
78
+ yield from vendors
35
79
self .logger .debug (f"Done scanning file: { self .filename } " )
0 commit comments