Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions dojo/tools/sonarqube/sonarqube_restapi_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import re
from datetime import datetime

Check failure on line 2 in dojo/tools/sonarqube/sonarqube_restapi_json.py

View workflow job for this annotation

GitHub Actions / ruff-linting

Ruff (F401)

dojo/tools/sonarqube/sonarqube_restapi_json.py:2:22: F401 `datetime.datetime` imported but unused
Comment thread
Maffooch marked this conversation as resolved.
Outdated

import dateutil.parser
Comment thread
Maffooch marked this conversation as resolved.
from django.utils import timezone

from dojo.models import Finding

Expand All @@ -23,6 +27,10 @@
scope = issue.get("scope")
quickFixAvailable = str(issue.get("quickFixAvailable"))
codeVariants = str(issue.get("codeVariants"))
try:
date = str(dateutil.parser.parse(issue.get("creationDate")).date())
except (ValueError, TypeError, dateutil.parser.ParserError):
date = timezone.now()
Comment on lines +29 to +32
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

not a blocker, but could save some repeated code by making this a helper function for all parsers?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I like this idea! I've got some ideas for parser improvements to discuss with you at some point too. Let's hold on this helper for now

description = ""
description += "**key:** " + key + "\n"
description += "**rule:** " + rule + "\n"
Expand Down Expand Up @@ -50,6 +58,7 @@
dynamic_finding=False,
tags=["bug"],
line=line,
date=date,
)
elif issue.get("type") == "VULNERABILITY":
key = issue.get("key")
Expand All @@ -61,6 +70,10 @@
message = issue.get("message")
line = issue.get("line")
cwe = None
try:
date = str(dateutil.parser.parse(issue.get("creationDate")).date())
except (ValueError, TypeError, dateutil.parser.ParserError):
date = timezone.now()
if "Category: CWE-" in message:
cwe_pattern = r"Category: CWE-\d{1,5}"
cwes = re.findall(cwe_pattern, message)
Expand Down Expand Up @@ -119,6 +132,7 @@
file_path=component,
tags=["vulnerability"],
line=line,
date=date,
)
vulnids = []
if "Reference: CVE" in message:
Expand Down Expand Up @@ -154,6 +168,10 @@
scope = issue.get("scope")
quickFixAvailable = str(issue.get("quickFixAvailable"))
codeVariants = issue.get("codeVariants", [])
try:
date = str(dateutil.parser.parse(issue.get("creationDate")).date())
except (ValueError, TypeError, dateutil.parser.ParserError):
date = timezone.now()
description = ""
description += "**rule:** " + rule + "\n"
description += "**component:** " + component + "\n"
Expand Down Expand Up @@ -185,6 +203,7 @@
file_path=component,
tags=["code_smell"],
line=line,
date=date,
)
items.append(item)
if json_content.get("hotspots"):
Expand All @@ -200,6 +219,10 @@
flows = hotspot.get("flows", [])
ruleKey = hotspot.get("ruleKey")
messageFormattings = hotspot.get("messageFormattings", [])
try:
date = str(dateutil.parser.parse(hotspot.get("creationDate")).date())
except (ValueError, TypeError, dateutil.parser.ParserError):
date = timezone.now()
description = ""
description += "**key:** " + key + "\n"
description += "**component:** " + component + "\n"
Expand Down Expand Up @@ -229,6 +252,7 @@
file_path=component,
tags=["hotspot"],
line=line,
date=date,
)
items.append(item)
return items
Expand Down
8 changes: 8 additions & 0 deletions unittests/tools/test_sonarqube_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,20 +579,25 @@ def test_parse_json_file_from_api_with_multiple_findings_json(self):
self.assertEqual("6.4", item.cvssv3_score)
self.assertEqual("package", item.component_name)
self.assertEqual("1.1.2", item.component_version)
self.assertEqual("2023-10-16", item.date)
item = findings[1]
self.assertEqual("Web:TableWithoutCaptionCheck_asdfwfewfwefewf", item.title)
self.assertEqual("Low", item.severity)
self.assertEqual(0, item.cwe)
self.assertIsNone(item.cvssv3_score)
self.assertEqual("2023-07-25", item.date)
item = findings[2]
self.assertEqual("typescript:S1533_fjoiewfjoweifjoihugu-", item.title)
self.assertEqual("Low", item.severity)
self.assertEqual("2024-01-29", item.date)
item = findings[3]
self.assertEqual("GHSA-frr2-c345-p7c2", item.unsaved_vulnerability_ids[0])
self.assertEqual("2023-10-16", item.date)
item = findings[4]
self.assertEqual("CVE-2023-52428", item.unsaved_vulnerability_ids[0])
self.assertEqual("nimbus-jose-jwt-9.24.4.jar", item.component_name)
self.assertIsNone(item.component_version)
self.assertEqual("2023-10-16", item.date)
my_file_handle.close()

def test_parse_json_file_from_api_with_multiple_findings_hotspots_json(self):
Expand All @@ -606,12 +611,15 @@ def test_parse_json_file_from_api_with_multiple_findings_hotspots_json(self):
self.assertEqual(str, type(item.description))
self.assertEqual("typescript:7777_fwafewef", item.title)
self.assertEqual("High", item.severity)
self.assertEqual("2024-02-13", item.date)
item = findings[1]
self.assertEqual("Web:1222_cyxcvyxcvyxv", item.title)
self.assertEqual("Low", item.severity)
self.assertEqual("2023-07-27", item.date)
item = findings[2]
self.assertEqual("Web:9876_werrwerwerwer", item.title)
self.assertEqual("Low", item.severity)
self.assertEqual("2023-07-27", item.date)
my_file_handle.close()

def test_parse_json_file_from_api_with_empty_json(self):
Expand Down
Loading