Skip to content

Commit 813c224

Browse files
committed
add flag gating to checkmarx, risk_recon, acunetix + tests
1 parent 412bada commit 813c224

6 files changed

Lines changed: 102 additions & 16 deletions

File tree

dojo/tools/acunetix/parse_acunetix360_json.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ def get_findings(self, filename, test):
142142
else:
143143
# TODO: Delete this after the move to Locations
144144
finding.unsaved_endpoints = [Endpoint.from_uri(url)]
145-
if item.get("FirstSeenDate"):
146-
parseddate = parser.parse(item["FirstSeenDate"], dayfirst=True)
147-
finding.date = parseddate
145+
if settings.USE_FIRST_SEEN and item.get("FirstSeenDate"):
146+
finding.date = parser.parse(item["FirstSeenDate"], dayfirst=True)
148147
if dupe_key in dupes:
149148
find = dupes[dupe_key]
150149
find.unsaved_req_resp.extend(finding.unsaved_req_resp)

dojo/tools/checkmarx/parser.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from dateutil import parser
66
from defusedxml import ElementTree
7+
from django.conf import settings
78

89
from dojo.models import Finding
910
from dojo.utils import add_language
@@ -447,12 +448,14 @@ def _get_findings_json(self, file, test):
447448
if query.get("groupName"):
448449
query.get("groupName").replace("_", " ")
449450
for vulnerability in query.get("vulnerabilities", []):
451+
if settings.USE_FIRST_SEEN:
452+
date = self._parse_date(vulnerability.get("firstFoundDate"))
453+
else:
454+
date = self._parse_date(vulnerability.get("foundDate"))
450455
finding = Finding(
451456
description=descriptiondetails,
452457
title=title,
453-
date=self._parse_date(
454-
vulnerability.get("firstFoundDate"),
455-
),
458+
date=date,
456459
severity=vulnerability.get("severity").title(),
457460
active=(
458461
vulnerability.get("status")
@@ -486,12 +489,14 @@ def _get_findings_json(self, file, test):
486489
component_version = package.get("name").split("-")[-1]
487490
for vulnerability in package.get("vulnerabilities", []):
488491
cve = vulnerability.get("cveId")
492+
if settings.USE_FIRST_SEEN:
493+
date = self._parse_date(vulnerability.get("firstFoundDate"))
494+
else:
495+
date = self._parse_date(vulnerability.get("foundDate"))
489496
finding = Finding(
490497
title=f"{component_name}:{component_version} | {cve}",
491498
description=vulnerability.get("description"),
492-
date=self._parse_date(
493-
vulnerability.get("firstFoundDate"),
494-
),
499+
date=date,
495500
severity=vulnerability.get("severity").title(),
496501
active=(
497502
vulnerability.get("status")
@@ -524,12 +529,14 @@ def _get_findings_json(self, file, test):
524529
for kics_type in results[result_type].get("results", []):
525530
name = kics_type.get("name")
526531
for vulnerability in kics_type.get("vulnerabilities", []):
532+
if settings.USE_FIRST_SEEN:
533+
date = self._parse_date(vulnerability.get("firstFoundDate"))
534+
else:
535+
date = self._parse_date(vulnerability.get("foundDate"))
527536
finding = Finding(
528537
title=f'{name} | {vulnerability.get("issueType")}',
529538
description=vulnerability.get("description"),
530-
date=self._parse_date(
531-
vulnerability.get("firstFoundDate"),
532-
),
539+
date=date,
533540
severity=vulnerability.get("severity").title(),
534541
active=(
535542
vulnerability.get("status")

dojo/tools/risk_recon/parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
import dateutil
4+
from django.conf import settings
45

56
from dojo.models import Finding
67
from dojo.tools.risk_recon.api import RiskReconAPI
@@ -80,7 +81,10 @@ def _get_findings_internal(self, findings, test):
8081
findingdetail += "**Priority:** " + item.get("priority") + "\n"
8182
findingdetail += "**First Seen:** " + item.get("first_seen") + "\n"
8283

83-
date = dateutil.parser.parse(item.get("first_seen"))
84+
if settings.USE_FIRST_SEEN:
85+
date = dateutil.parser.parse(item.get("first_seen"))
86+
else:
87+
date = dateutil.parser.parse(item.get("last_seen"))
8488

8589
sev = item.get("severity", "").capitalize()
8690
sev = sev or "Info"

unittests/tools/test_acunetix_parser.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import datetime
22
from datetime import datetime as date
33

4+
from django.test import override_settings
5+
46
from dojo.models import Test
57
from dojo.tools.acunetix.parser import AcunetixParser
68
from unittests.dojo_test_case import DojoTestCase, get_unit_tests_scans_path
@@ -198,6 +200,7 @@ def test_parse_file_with_example_com(self):
198200
self.assertIsInstance(req_resp["resp"], str)
199201

200202
def test_parse_file_with_one_finding_acunetix360(self):
203+
"""With USE_FIRST_SEEN=False (default), date should come from Generated (scan date)."""
201204
with (get_unit_tests_scans_path("acunetix") / "acunetix360_one_finding.json").open(encoding="utf-8") as testfile:
202205
parser = AcunetixParser()
203206
findings = parser.get_findings(testfile, Test())
@@ -213,9 +216,22 @@ def test_parse_file_with_one_finding_acunetix360(self):
213216
self.assertEqual(1, len(self.get_unsaved_locations(finding)))
214217
location = self.get_unsaved_locations(finding)[0]
215218
self.assertEqual(str(location), "http://php.testsparker.com/auth/login.php")
216-
self.assertEqual(finding.date, date(2021, 6, 16, 12, 30))
219+
# Generated date is "25/06/2021 09:59 AM" — with USE_FIRST_SEEN=False, FirstSeenDate is ignored
220+
self.assertEqual(finding.date, date(2021, 6, 25, 9, 59))
217221
self.assertIn("https://online.acunetix360.com/issues/detail/735f4503-e9eb-4b4c-4306-ad49020a4c4b", finding.references)
218222

223+
@override_settings(USE_FIRST_SEEN=True)
224+
def test_parse_file_with_one_finding_acunetix360_first_seen(self):
225+
"""With USE_FIRST_SEEN=True, date should come from FirstSeenDate."""
226+
with (get_unit_tests_scans_path("acunetix") / "acunetix360_one_finding.json").open(encoding="utf-8") as testfile:
227+
parser = AcunetixParser()
228+
findings = parser.get_findings(testfile, Test())
229+
self.assertEqual(1, len(findings))
230+
with self.subTest(i=0):
231+
finding = findings[0]
232+
# FirstSeenDate is "16/06/2021 12:30 PM"
233+
self.assertEqual(finding.date, date(2021, 6, 16, 12, 30))
234+
219235
def test_parse_file_with_one_finding_false_positive(self):
220236
with (get_unit_tests_scans_path("acunetix") / "acunetix360_one_finding_false_positive.json").open(encoding="utf-8") as testfile:
221237
parser = AcunetixParser()
@@ -323,11 +339,25 @@ def test_parse_file_issue_10435(self):
323339
self.assertEqual(1, len(findings))
324340

325341
def test_parse_file_issue_11206(self):
342+
"""With USE_FIRST_SEEN=False (default), date should come from Generated (scan date)."""
326343
with (get_unit_tests_scans_path("acunetix") / "issue_11206.json").open(encoding="utf-8") as testfile:
327344
parser = AcunetixParser()
328345
findings = parser.get_findings(testfile, Test())
329346
self.validate_locations(findings)
330347
self.assertEqual(1, len(findings))
331348
with self.subTest(i=0):
332349
finding = findings[0]
350+
# Generated date is "25/06/2021 09:59 AM" — FirstSeenDate ignored when USE_FIRST_SEEN=False
351+
self.assertEqual(finding.date, date(2021, 6, 25, 9, 59))
352+
353+
@override_settings(USE_FIRST_SEEN=True)
354+
def test_parse_file_issue_11206_first_seen(self):
355+
"""With USE_FIRST_SEEN=True, date should come from FirstSeenDate."""
356+
with (get_unit_tests_scans_path("acunetix") / "issue_11206.json").open(encoding="utf-8") as testfile:
357+
parser = AcunetixParser()
358+
findings = parser.get_findings(testfile, Test())
359+
self.assertEqual(1, len(findings))
360+
with self.subTest(i=0):
361+
finding = findings[0]
362+
# FirstSeenDate is "12/06/2021 12:30 PM"
333363
self.assertEqual(finding.date, date(2021, 6, 12, 12, 30))

unittests/tools/test_checkmarx_parser.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import datetime
33
from unittest.mock import patch
44

5+
from django.test import override_settings
6+
57
from dojo.models import Engagement, Product, Test
68
from dojo.tools.checkmarx.parser import CheckmarxParser
79
from unittests.dojo_test_case import DojoTestCase, get_unit_tests_scans_path
@@ -837,6 +839,7 @@ def test_finding_date_should_be_date_xml(self, mock):
837839

838840
@patch("dojo.tools.checkmarx.parser.add_language")
839841
def test_finding_date_should_be_date_json(self, mock):
842+
"""With USE_FIRST_SEEN=False (default), date should come from foundDate."""
840843
my_file_handle, _product, _engagement, test = self.init(
841844
get_unit_tests_scans_path("checkmarx") / "multiple_findings.json",
842845
)
@@ -845,3 +848,28 @@ def test_finding_date_should_be_date_json(self, mock):
845848
findings = parser.get_findings(my_file_handle, test)
846849
self.teardown(my_file_handle)
847850
self.assertEqual(findings[0].date, datetime.date(2022, 2, 25))
851+
852+
@override_settings(USE_FIRST_SEEN=True)
853+
@patch("dojo.tools.checkmarx.parser.add_language")
854+
def test_finding_date_should_use_first_found_date_json(self, mock):
855+
"""With USE_FIRST_SEEN=True, date should come from firstFoundDate."""
856+
my_file_handle, _product, _engagement, test = self.init(
857+
get_unit_tests_scans_path("checkmarx") / "sample_report.json",
858+
)
859+
parser = CheckmarxParser()
860+
findings = parser.get_findings(my_file_handle, test)
861+
self.teardown(my_file_handle)
862+
# firstFoundDate seconds=1651835169 -> 2022-05-06
863+
self.assertEqual(findings[0].date, datetime.date(2022, 5, 6))
864+
865+
@patch("dojo.tools.checkmarx.parser.add_language")
866+
def test_finding_date_should_use_found_date_json(self, mock):
867+
"""With USE_FIRST_SEEN=False (default), date should come from foundDate."""
868+
my_file_handle, _product, _engagement, test = self.init(
869+
get_unit_tests_scans_path("checkmarx") / "sample_report.json",
870+
)
871+
parser = CheckmarxParser()
872+
findings = parser.get_findings(my_file_handle, test)
873+
self.teardown(my_file_handle)
874+
# foundDate seconds=1663153613 -> 2022-09-14
875+
self.assertEqual(findings[0].date, datetime.date(2022, 9, 14))

unittests/tools/test_risk_recon_parser.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22

33
import requests
4+
from django.test import override_settings
45

56
from dojo.models import Test
67
from dojo.tools.risk_recon.parser import RiskReconParser
@@ -22,15 +23,32 @@ def test_api_with_bad_key(self):
2223
parser.get_findings(testfile, Test())
2324

2425
def test_parser_without_api(self):
26+
"""With USE_FIRST_SEEN=False (default), date should come from last_seen."""
2527
with (get_unit_tests_scans_path("risk_recon") / "findings.json").open(encoding="utf-8") as testfile:
2628
parser = RiskReconParser()
2729
findings = parser.get_findings(testfile, Test())
2830
self.assertEqual(2, len(findings))
2931
with self.subTest(i=0):
3032
finding = findings[0]
31-
self.assertEqual(datetime.date(2017, 3, 17), finding.date.date())
33+
# last_seen is "2020-08-13T12:18:45+00:00"
34+
self.assertEqual(datetime.date(2020, 8, 13), finding.date.date())
3235
self.assertEqual("ff2bbdbfc2b6fddc061ed96b1fasfwefb", finding.unique_id_from_tool)
3336
with self.subTest(i=1):
3437
finding = findings[1]
35-
self.assertEqual(datetime.date(2017, 3, 17), finding.date.date())
38+
self.assertEqual(datetime.date(2020, 8, 13), finding.date.date())
3639
self.assertEqual("ff2bbdbfc2b6gsrgwergwe6b1fasfwefb", finding.unique_id_from_tool)
40+
41+
@override_settings(USE_FIRST_SEEN=True)
42+
def test_parser_without_api_first_seen(self):
43+
"""With USE_FIRST_SEEN=True, date should come from first_seen."""
44+
with (get_unit_tests_scans_path("risk_recon") / "findings.json").open(encoding="utf-8") as testfile:
45+
parser = RiskReconParser()
46+
findings = parser.get_findings(testfile, Test())
47+
self.assertEqual(2, len(findings))
48+
with self.subTest(i=0):
49+
finding = findings[0]
50+
# first_seen is "2017-03-17T00:34:24+00:00"
51+
self.assertEqual(datetime.date(2017, 3, 17), finding.date.date())
52+
with self.subTest(i=1):
53+
finding = findings[1]
54+
self.assertEqual(datetime.date(2017, 3, 17), finding.date.date())

0 commit comments

Comments
 (0)