1717from univers .version_constraint import VersionConstraint
1818from univers .version_range import EbuildVersionRange
1919from univers .versions import GentooVersion
20+ from univers .versions import InvalidVersion
2021
2122from vulnerabilities .importer import AdvisoryData
2223from vulnerabilities .importer import AffectedPackageV2
2324from vulnerabilities .importer import ReferenceV2
25+ from vulnerabilities .importer import VulnerabilitySeverity
26+ from vulnerabilities .management .commands .commit_export import logger
2427from vulnerabilities .pipelines import VulnerableCodeBaseImporterPipelineV2
28+ from vulnerabilities .severity_systems import GENERIC
2529
2630
2731class GentooImporterPipeline (VulnerableCodeBaseImporterPipelineV2 ):
@@ -57,18 +61,19 @@ def collect_advisories(self) -> Iterable[AdvisoryData]:
5761 def process_file (self , file ):
5862 cves = []
5963 summary = ""
60- vuln_references = []
6164 xml_root = ET .parse (file ).getroot ()
6265 id = xml_root .attrib .get ("id" )
63- if id :
64- glsa = "GLSA-" + id
65- vuln_references = [
66- ReferenceV2 (
67- reference_id = glsa ,
68- url = f"https://security.gentoo.org/glsa/{ id } " ,
69- )
70- ]
66+ glsa = "GLSA-" + id
67+
68+ vuln_references = [
69+ ReferenceV2 (
70+ reference_id = glsa ,
71+ url = f"https://security.gentoo.org/glsa/{ id } " ,
72+ )
73+ ]
7174
75+ severities = []
76+ affected_packages = []
7277 for child in xml_root :
7378 if child .tag == "references" :
7479 cves = self .cves_from_reference (child )
@@ -79,19 +84,23 @@ def process_file(self, file):
7984 if child .tag == "affected" :
8085 affected_packages = list (affected_and_safe_purls (child ))
8186
82- # It is very inefficient, to create new Advisory for each CVE
83- # this way, but there seems no alternative.
84- for cve in cves :
85- yield AdvisoryData (
86- advisory_id = cve ,
87- aliases = [cve ],
88- summary = summary ,
89- references = vuln_references ,
90- affected_packages = affected_packages ,
91- url = f"https://security.gentoo.org/glsa/{ id } "
92- if id
93- else "https://security.gentoo.org/glsa" ,
94- )
87+ if child .tag == "impact" :
88+ severity_value = child .attrib .get ("type" )
89+ if severity_value :
90+ severities .append (VulnerabilitySeverity (system = GENERIC , value = severity_value ))
91+
92+ yield AdvisoryData (
93+ advisory_id = glsa ,
94+ aliases = cves ,
95+ summary = summary ,
96+ references_v2 = vuln_references ,
97+ severities = severities ,
98+ affected_packages = affected_packages ,
99+ url = f"https://security.gentoo.org/glsa/{ id } "
100+ if id
101+ else "https://security.gentoo.org/glsa" ,
102+ original_advisory_text = file ,
103+ )
95104
96105 def clean_downloads (self ):
97106 if self .vcs_response :
@@ -123,12 +132,20 @@ def affected_and_safe_purls(affected_elem):
123132 safe_versions , affected_versions = get_safe_and_affected_versions (pkg )
124133
125134 for version in safe_versions :
126- constraints .append (
127- VersionConstraint (version = GentooVersion (version ), comparator = "=" ).invert ()
128- )
135+ try :
136+ constraints .append (
137+ VersionConstraint (version = GentooVersion (version ), comparator = "=" ).invert ()
138+ )
139+ except InvalidVersion as e :
140+ logger .error (f"InvalidVersion - version: { version } - error:{ e } " )
129141
130142 for version in affected_versions :
131- constraints .append (VersionConstraint (version = GentooVersion (version ), comparator = "=" ))
143+ try :
144+ constraints .append (
145+ VersionConstraint (version = GentooVersion (version ), comparator = "=" )
146+ )
147+ except InvalidVersion as e :
148+ logger .error (f"InvalidVersion - version: { version } - error:{ e } " )
132149
133150 if not constraints :
134151 continue
0 commit comments