Skip to content

Bug Fix: improve Kiuwan SCA parser to support multi component findings #12753

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 6 commits into
base: bugfix
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
100 changes: 55 additions & 45 deletions dojo/tools/kiuwan_sca/parser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import hashlib
import json
import logging

from dojo.models import Finding

logger = logging.getLogger(__name__)

__author__ = "mwager"


Expand Down Expand Up @@ -37,50 +40,57 @@ def get_findings(self, filename, test):
if row["muted"] is True:
continue

finding = Finding(test=test)
finding.unique_id_from_tool = row["id"]
finding.cve = row["cve"]
finding.description = row["description"]
finding.severity = self.SEVERITY[row["securityRisk"]]

if "components" in row and len(row["components"]) > 0:
finding.component_name = row["components"][0]["artifact"]
finding.component_version = row["components"][0]["version"]
finding.title = finding.component_name + " v" + str(finding.component_version)

if not finding.title:
finding.title = row["cve"]

if "cwe" in row and "CWE-" in row["cwe"]:
finding.cwe = int(row["cwe"].replace("CWE-", ""))

if "epss_score" in row:
finding.epss_score = row["epss_score"]
if "epss_percentile" in row:
finding.epss_percentile = row["epss_percentile"]

if "cVSSv3BaseScore" in row:
finding.cvssv3_score = float(row["cVSSv3BaseScore"])

finding.references = "See Kiuwan Web UI"
finding.mitigation = "See Kiuwan Web UI"
finding.static_finding = True

key = hashlib.sha256(
(
finding.description
+ "|"
+ finding.severity
+ "|"
+ finding.component_name
+ "|"
+ finding.component_version
+ "|"
+ str(finding.cwe)
).encode("utf-8"),
).hexdigest()

if key not in dupes:
dupes[key] = finding
components = row.get("components", [])
if not components:
logger.warning("WARNING: Insights Finding from kiuwan does not have a related component - Skipping.")
continue

# We want one unique finding in DD for each component affected:
for component in components:
finding = Finding(test=test)
finding.unique_id_from_tool = f"{row['cve']}|{component.get('artifact')}|{component.get('version')}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constructing the unique_id_from_tool is not something that is generally done. The intention is that the field is created from the tool.

This parser in particular uses the DEDUPE_ALGO_HASH_CODE, so I think the unique_id_from_tool should be omitted. This could be an issue for folks that updated the default hash code fields to use unique_id_from_tool, so we should also include something in the release notes to indicate the change

Here is the current dedupe settings:

"Kiuwan SCA Scan": ["description", "severity", "component_name", "component_version", "cwe"],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Kiuwan provides a unique id, this was just a test on our side. I will run one more test later this week and maybe change this back. This PR should be about the components issue only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed changes back to old logic

finding.cve = row["cve"]
finding.description = row["description"]
finding.severity = self.SEVERITY[row["securityRisk"]]

finding.component_name = component.get("artifact", "Unknown")
finding.component_version = component.get("version", "Unknown")
finding.title = f"{finding.component_name} v{finding.component_version}"

if "cwe" in row and "CWE-" in row["cwe"]:
finding.cwe = int(row["cwe"].replace("CWE-", ""))

if "epss_score" in row:
finding.epss_score = row["epss_score"]
if "epss_percentile" in row:
finding.epss_percentile = row["epss_percentile"]

if "cVSSv3BaseScore" in row:
finding.cvssv3_score = float(row["cVSSv3BaseScore"])

finding.references = "See Kiuwan Web UI"
finding.mitigation = "See Kiuwan Web UI"
finding.static_finding = True

key = hashlib.sha256(
(
str(finding.unique_id_from_tool)
+ "|"
+ finding.cve
+ "|"
+ finding.description
+ "|"
+ finding.severity
+ "|"
+ finding.component_name
+ "|"
+ finding.component_version
+ "|"
+ str(finding.cwe or "")
).encode("utf-8"),
).hexdigest()

if key not in dupes:
dupes[key] = finding

return list(dupes.values())
10 changes: 5 additions & 5 deletions unittests/tools/test_kiuwan_sca_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def test_parse_file_with_two_vuln_has_two_findings(self):
with (get_unit_tests_scans_path("kiuwan_sca") / "kiuwan_sca_two_vuln.json").open(encoding="utf-8") as testfile:
parser = KiuwanSCAParser()
findings = parser.get_findings(testfile, Test())
# file contains 3, but we only get 2 as "muted" ones are ignored:
self.assertEqual(2, len(findings))
# file contains 3 cves, one of them is muted. so we have 2 cves with a total of 5 components
self.assertEqual(5, len(findings))

def test_parse_file_with_multiple_vuln_has_multiple_finding(self):
with (get_unit_tests_scans_path("kiuwan_sca") / "kiuwan_sca_many_vuln.json").open(encoding="utf-8") as testfile:
parser = KiuwanSCAParser()
findings = parser.get_findings(testfile, Test())
# also tests deduplication as there are 28 findings in the file:
self.assertEqual(27, len(findings))
# also tests deduplication as there are 28 cves in the file (but some including >1 components!):
self.assertEqual(45, len(findings))

def test_correct_mapping(self):
with (get_unit_tests_scans_path("kiuwan_sca") / "kiuwan_sca_two_vuln.json").open(encoding="utf-8") as testfile:
Expand All @@ -37,7 +37,7 @@ def test_correct_mapping(self):
self.assertEqual(finding1.component_name, "org.apache.cxf:cxf-rt-ws-policy")
self.assertEqual(finding1.component_version, "3.3.5")
self.assertEqual(finding1.cwe, 835)
self.assertEqual(finding1.unique_id_from_tool, 158713)
self.assertEqual(finding1.unique_id_from_tool, "CVE-2021-30468|org.apache.cxf:cxf-rt-ws-policy|3.3.5")
self.assertEqual(finding1.cvssv3_score, 7.5)
self.assertEqual(finding1.epss_score, 0.1)
self.assertEqual(finding1.epss_percentile, 0.2)