Skip to content

Commit 38700b5

Browse files
4b75726169736859ahubertMaffooch
authored
Enhance OSV Parser to Include Mitigation Information with Fixed Package Versions (#11681)
* Introducing a mechanism to extract and include mitigation information (fixed versions) * Removing empty lines at the end of the file * Update unittest test_osv_scanner_parser script * Update unittest test_osv_scanner_parser script * Add new empty line to parser * Add empty line at end file * Update parser.py * Update parser.py * Add missing comments and document new methods in OSVScannerParserh * update * Add missing comments and document new methods in OSVScannerParser * update Fixed Package Versions * update Fixed test_osv_scanner * Fixing ruff --------- Co-authored-by: ahubert <ahubert@nganalytics.eu> Co-authored-by: Cody Maffucci <46459665+Maffooch@users.noreply.github.com>
1 parent df81430 commit 38700b5

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

dojo/tools/osv_scanner/parser.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def get_findings(self, file, test):
7979
vulnerabilitydetails = vulnerability.get("details", "")
8080
vulnerabilitypackagepurl = ""
8181
cwe = None
82+
mitigations_by_type = {} # Dictionary to store corrected versions by type
83+
8284
# Make sure we have an affected section to work with
8385
if (affected := vulnerability.get("affected")) is not None:
8486
if len(affected) > 0:
@@ -88,19 +90,45 @@ def get_findings(self, file, test):
8890
# Extract the CWE
8991
if (cwe := affected[0].get("database_specific", {}).get("cwes", None)) is not None:
9092
cwe = cwe[0]["cweId"]
93+
# Extraction of corrected versions by type
94+
ranges = affected[0].get("ranges", [])
95+
for range_item in ranges:
96+
range_type = range_item.get("type", "")
97+
repo_url = range_item.get("repo", "")
98+
for event in range_item.get("events", []):
99+
if "fixed" in event:
100+
fixed_value = event["fixed"]
101+
# GIT URL format if applicable
102+
if range_type == "GIT" and repo_url:
103+
formatted_value = f"{repo_url}/commit/{fixed_value}"
104+
else:
105+
formatted_value = fixed_value
106+
# Add to the list by type
107+
if range_type not in mitigations_by_type:
108+
mitigations_by_type[range_type] = []
109+
mitigations_by_type[range_type].append(formatted_value)
110+
111+
# Creation of formatted mitigation text
112+
mitigation_text = None
113+
if mitigations_by_type:
114+
mitigation_text = "**Upgrade to versions**:\n"
115+
for typ, versions in mitigations_by_type.items():
116+
mitigation_text += f"\t{typ} :\n"
117+
for version in versions:
118+
mitigation_text += f"\t\t- {version}\n"
91119
# Create some references
92120
reference = ""
93-
for ref in vulnerability.get("references"):
121+
for ref in vulnerability.get("references", []):
94122
reference += ref.get("url") + "\n"
95123
# Define the description
96124
description = vulnerabilitysummary + "\n"
97-
description += "**source_type**: " + source_type + "\n"
98-
description += "**package_ecosystem**: " + package_ecosystem + "\n"
99-
description += "**vulnerabilitydetails**: " + vulnerabilitydetails + "\n"
100-
description += "**vulnerabilitypackagepurl**: " + vulnerabilitypackagepurl + "\n"
125+
description += f"**Source type**: {source_type}\n"
126+
description += f"**Package ecosystem**: {package_ecosystem}\n"
127+
description += f"**Vulnerability details**: {vulnerabilitydetails}\n"
128+
description += f"**Vulnerability package purl**: {vulnerabilitypackagepurl}\n"
101129
sev = vulnerability.get("database_specific", {}).get("severity", "")
102130
finding = Finding(
103-
title=vulnerabilityid + "_" + package_name,
131+
title=f"{vulnerabilityid}_{package_name}",
104132
test=test,
105133
description=description,
106134
severity=self.classify_severity(sev),
@@ -112,8 +140,11 @@ def get_findings(self, file, test):
112140
file_path=source_path,
113141
references=reference,
114142
)
115-
if vulnerabilityid != "":
116-
finding.unsaved_vulnerability_ids = []
117-
finding.unsaved_vulnerability_ids.append(vulnerabilityid)
143+
144+
if mitigation_text:
145+
finding.mitigation = mitigation_text
146+
147+
if vulnerabilityid:
148+
finding.unsaved_vulnerability_ids = [vulnerabilityid]
118149
findings.append(finding)
119150
return findings

unittests/tools/test_osv_scanner_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ def test_many_findings(self):
4141
finding = findings[17]
4242
self.assertEqual(finding.references, "https://nvd.nist.gov/vuln/detail/CVE-2021-45115\nhttps://docs.djangoproject.com/en/4.0/releases/security\nhttps://github.com/django/django\nhttps://groups.google.com/forum/#!forum/django-announce\nhttps://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/B4SQG2EAF4WCI2SLRL6XRDJ3RPK3ZRDV\nhttps://security.netapp.com/advisory/ntap-20220121-0005\nhttps://www.djangoproject.com/weblog/2022/jan/04/security-releases\n")
4343
self.assertEqual(finding.title, "GHSA-53qw-q765-4fww_django")
44+
self.assertEqual(finding.mitigation, "**Upgrade to versions**:\n\tECOSYSTEM :\n\t\t- 2.2.26\n")

0 commit comments

Comments
 (0)