Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 37 additions & 7 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@


logger = logging.getLogger(__name__)
deduplicationLogger = logging.getLogger("dojo.specific-loggers.deduplication")


class TagList(list):
Expand Down Expand Up @@ -970,8 +971,6 @@ class Meta:
fields = '__all__'


# class ImportScanSerializer(TaggitSerializer, serializers.ModelSerializer):
# class ImportScanSerializer(TaggitSerializer, serializers.Serializer):
class ImportScanSerializer(serializers.Serializer):
scan_date = serializers.DateField(default=datetime.date.today)

Expand Down Expand Up @@ -1066,7 +1065,10 @@ def save(self, push_to_jira=False):
new_findings = []
skipped_hashcodes = []
try:
for item in parser.items:
items = parser.items
logger.debug('starting import of %i items.', len(items))
i = 0
for item in items:
sev = item.severity
if sev == 'Information' or sev == 'Informational':
sev = 'Info'
Expand Down Expand Up @@ -1177,7 +1179,7 @@ def save(self, push_to_jira=False):
status.save()

old_finding.tags.add('stale')
old_finding.save()
old_finding.save(dedupe_option=False)

logger.debug('done importing findings')

Expand Down Expand Up @@ -1269,6 +1271,8 @@ def save(self, push_to_jira=False):
unchanged_count = 0
unchanged_items = []

logger.debug('starting reimport of %i items.', len(items))
i = 0
for item in items:
sev = item.severity

Expand Down Expand Up @@ -1299,10 +1303,29 @@ def save(self, push_to_jira=False):
severity=sev,
numerical_severity=Finding.get_numerical_severity(sev)).all()

# some parsers generate 1 finding for each vulnerable file for each vulnerability
# i.e
# #: title : sev : file_path
# 1: CVE-2020-1234 jquery : 1 : /file1.jar
# 2: CVE-2020-1234 jquery : 1 : /file2.jar
#
# if we don't filter on file_path, we would find 2 existing findings
# and the logic below will get confused and map all incoming findings
# from the reimport on the first finding
#
# for Anchore we fix this here, we may need a broader fix (and testcases)
# or we may need to change the matching logic here to use the same logic
# as the deduplication logic (hashcode fields)

if scan_type == 'Anchore Engine Scan':
if item.file_path:
findings = findings.filter(file_path=item.file_path)

if findings:
# existing finding found
finding = findings[0]
if finding.mitigated or finding.is_Mitigated:
logger.debug('%i: reactivating: %i:%s:%s:%s', i, finding.id, finding, finding.component_name, finding.component_version)
finding.mitigated = None
finding.is_Mitigated = False
finding.mitigated_by = None
Expand All @@ -1313,7 +1336,8 @@ def save(self, push_to_jira=False):
finding.component_name = finding.component_name if finding.component_name else component_name
finding.component_version = finding.component_version if finding.component_version else component_version

finding.save()
# don't dedupe before endpoints are added
finding.save(dedupe_option=False)
note = Notes(
entry="Re-activated by %s re-upload." % scan_type,
author=self.context['request'].user)
Expand All @@ -1330,10 +1354,11 @@ def save(self, push_to_jira=False):
reactivated_count += 1
else:
# existing findings may be from before we had component_name/version fields
logger.debug('%i: updating existing finding: %i:%s:%s:%s', i, finding.id, finding, finding.component_name, finding.component_version)
if not finding.component_name or not finding.component_version:
finding.component_name = finding.component_name if finding.component_name else component_name
finding.component_version = finding.component_version if finding.component_version else component_version
finding.save(dedupe_option=False, push_to_jira=False)
finding.save(dedupe_option=False)

unchanged_items.append(finding)
unchanged_count += 1
Expand All @@ -1345,7 +1370,10 @@ def save(self, push_to_jira=False):
item.last_reviewed_by = self.context['request'].user
item.verified = verified
item.active = active
# Save it. Don't dedupe before endpoints are added.
item.save(dedupe_option=False)
logger.debug('%i: creating new finding: %i:%s:%s:%s', i, item.id, item, item.component_name, item.component_version)
deduplicationLogger.debug('reimport found multiple identical existing findings for %i, a non-exact match. these are ignored and a new finding has been created', item.id)
finding_added_count += 1
new_items.append(item)
finding = item
Expand Down Expand Up @@ -1405,6 +1433,7 @@ def save(self, push_to_jira=False):
if close_old_findings:
for finding in to_mitigate:
if not finding.mitigated or not finding.is_Mitigated:
logger.debug('mitigating finding: %i:%s', finding.id, finding)
finding.mitigated = scan_date_time
finding.is_Mitigated = True
finding.mitigated_by = self.context['request'].user
Expand All @@ -1418,7 +1447,8 @@ def save(self, push_to_jira=False):
status.last_modified = timezone.now()
status.save()

finding.save(push_to_jira=push_to_jira)
# don't try to dedupe findings that we are closing
finding.save(push_to_jira=push_to_jira, dedupe_option=False)
note = Notes(entry="Mitigated by %s re-upload." % scan_type,
author=self.context['request'].user)
note.save()
Expand Down
9 changes: 6 additions & 3 deletions dojo/engagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,10 @@ def import_scan_results(request, eid=None, pid=None):
# push_to_jira = jira_helper.is_push_to_jira(new_finding, jform.cleaned_data.get('push_to_jira'))
push_to_jira = push_all_jira_issues or (jform and jform.cleaned_data.get('push_to_jira'))

for item in parser.items:
# print("item blowup")
# print(item)
items = parser.items
logger.debug('starting reimport of %i items.', len(items))
i = 0
for item in items:
sev = item.severity
if sev == 'Information' or sev == 'Informational':
sev = 'Info'
Expand All @@ -627,6 +628,7 @@ def import_scan_results(request, eid=None, pid=None):
item.verified = verified

item.save(dedupe_option=False, false_history=True)
logger.debug('%i: creating new finding: %i:%s:%s:%s', i, item.id, item, item.component_name, item.component_version)

if hasattr(item, 'unsaved_req_resp') and len(
item.unsaved_req_resp) > 0:
Expand Down Expand Up @@ -692,6 +694,7 @@ def import_scan_results(request, eid=None, pid=None):
item.tags = item.unsaved_tags

finding_count += 1
i += 1

messages.add_message(
request,
Expand Down
3 changes: 3 additions & 0 deletions dojo/settings/settings.dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
HASHCODE_FIELDS_PER_SCANNER = {
# In checkmarx, same CWE may appear with different severities: example "sql injection" (high) and "blind sql injection" (low).
# Including the severity in the hash_code keeps those findings not duplicate
'Anchore Engine Scan': ['title', 'severity', 'component_name', 'component_version', 'file_path'],
'Checkmarx Scan': ['cwe', 'severity', 'file_path'],
'SonarQube Scan': ['cwe', 'severity', 'file_path'],
'Dependency Check Scan': ['cve', 'file_path'],
Expand All @@ -779,6 +780,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
# If False and cwe = 0, then the hash_code computation will fallback to legacy algorithm for the concerned finding
# Default is True (if scanner is not configured here but is configured in HASHCODE_FIELDS_PER_SCANNER, it allows null cwe)
HASHCODE_ALLOWS_NULL_CWE = {
'Anchore Engine Scan': True,
'Checkmarx Scan': False,
'SonarQube Scan': False,
'Dependency Check Scan': True,
Expand Down Expand Up @@ -815,6 +817,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
# Key = the scan_type from factory.py (= the test_type)
# Default is DEDUPE_ALGO_LEGACY
DEDUPLICATION_ALGORITHM_PER_PARSER = {
'Anchore Engine Scan': DEDUPE_ALGO_HASH_CODE,
'Checkmarx Scan detailed': DEDUPE_ALGO_UNIQUE_ID_FROM_TOOL,
'Checkmarx Scan': DEDUPE_ALGO_HASH_CODE,
'SonarQube Scan detailed': DEDUPE_ALGO_UNIQUE_ID_FROM_TOOL,
Expand Down
67 changes: 51 additions & 16 deletions dojo/test/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

logger = logging.getLogger(__name__)
parse_logger = logging.getLogger('dojo')
deduplicationLogger = logging.getLogger("dojo.specific-loggers.deduplication")


@user_must_be_authorized(Test, 'view', 'tid')
Expand Down Expand Up @@ -674,7 +675,7 @@ def re_import_scan_results(request, tid):

try:
items = parser.items
original_items = test.finding_set.all().values_list("id", flat=True)
original_items = list(test.finding_set.all())
new_items = []
mitigated_count = 0
finding_count = 0
Expand All @@ -688,8 +689,9 @@ def re_import_scan_results(request, tid):
# push_to_jira = jira_helper.is_push_to_jira(new_finding, jform.cleaned_data.get('push_to_jira'))
push_to_jira = push_all_jira_issues or (jform and jform.cleaned_data.get('push_to_jira'))

logger.debug('starting reimport of %i items.', len(items))
i = 0
for item in items:

sev = item.severity
if sev == 'Information' or sev == 'Informational':
sev = 'Info'
Expand All @@ -709,21 +711,40 @@ def re_import_scan_results(request, tid):
from titlecase import titlecase
item.title = titlecase(item.title)
if scan_type == 'Veracode Scan' or scan_type == 'Arachni Scan':
finding = Finding.objects.filter(title=item.title,
findings = Finding.objects.filter(title=item.title,
test__id=test.id,
severity=sev,
numerical_severity=Finding.get_numerical_severity(sev),
description=item.description)

else:
finding = Finding.objects.filter(title=item.title,
findings = Finding.objects.filter(title=item.title,
test__id=test.id,
severity=sev,
numerical_severity=Finding.get_numerical_severity(sev))

if len(finding) == 1:
finding = finding[0]
# some parsers generate 1 finding for each vulnerable file for each vulnerability
# i.e
# #: title : sev : file_path
# 1: CVE-2020-1234 jquery : 1 : /file1.jar
# 2: CVE-2020-1234 jquery : 1 : /file2.jar
#
# if we don't filter on file_path, we would find 2 existing findings
# and the logic below will get confused and map all incoming findings
# from the reimport on the first finding
#
# for Anchore we fix this here, we may need a broader fix (and testcases)
# or we may need to change the matching logic here to use the same logic
# as the deduplication logic (hashcode fields)

if scan_type == 'Anchore Engine Scan':
if item.file_path:
findings = findings.filter(file_path=item.file_path)

if findings:
finding = findings[0]
if finding.mitigated or finding.is_Mitigated:
logger.debug('%i: reactivating: %i:%s:%s:%s', i, finding.id, finding, finding.component_name, finding.component_version)
# it was once fixed, but now back
finding.mitigated = None
finding.is_Mitigated = False
Expand All @@ -735,7 +756,8 @@ def re_import_scan_results(request, tid):
finding.component_name = finding.component_name if finding.component_name else component_name
finding.component_version = finding.component_version if finding.component_version else component_version

finding.save()
# don't run dedupe when reactivating
finding.save(dedupe_option=False)
note = Notes(
entry="Re-activated by %s re-upload." % scan_type,
author=request.user)
Expand All @@ -748,17 +770,18 @@ def re_import_scan_results(request, tid):
status.mitigated_time = None
status.mitigated = False
status.last_modified = timezone.now()
status.save(push_to_jira=push_to_jira)
status.save()

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.

Always false anyways, so good to remove 👍


reactivated_items.append(finding.id)
reactivated_items.append(finding)
reactivated_count += 1
else:
# existing findings may be from before we had component_name/version fields
logger.debug('%i: updating existing finding: %i:%s:%s:%s', i, finding.id, finding, finding.component_name, finding.component_version)
if not finding.component_name or not finding.component_version:
finding.component_name = finding.component_name if finding.component_name else component_name
finding.component_version = finding.component_version if finding.component_version else component_version
finding.save(dedupe_option=False, push_to_jira=False)
unchanged_items.append(finding.id)
finding.save(dedupe_option=False)
unchanged_items.append(finding)
unchanged_count += 1

else:
Expand All @@ -769,11 +792,13 @@ def re_import_scan_results(request, tid):
item.verified = verified
item.active = active

# Save it
# Save it. Don't dedupe before endpoints are added.
item.save(dedupe_option=False)
logger.debug('%i: creating new finding: %i:%s:%s:%s', i, item.id, item, item.component_name, item.component_version)
deduplicationLogger.debug('reimport found multiple identical existing findings for %i, a non-exact match. these are ignored and a new finding has been created', item.id)
finding_added_count += 1
# Add it to the new items
new_items.append(item.id)
new_items.append(item)
finding = item

if hasattr(item, 'unsaved_req_resp') and len(item.unsaved_req_resp) > 0:
Expand All @@ -800,6 +825,7 @@ def re_import_scan_results(request, tid):
)
burp_rr.clean()
burp_rr.save()

if finding:
finding_count += 1
for endpoint in item.unsaved_endpoints:
Expand Down Expand Up @@ -839,19 +865,23 @@ def re_import_scan_results(request, tid):

# Save it. This may be the second time we save it in this function.
finding.save(push_to_jira=push_to_jira)
i += 1
# calculate the difference
to_mitigate = set(original_items) - set(reactivated_items) - set(unchanged_items)
mitigated_findings = []
if close_old_findings:
for finding_id in to_mitigate:
finding = Finding.objects.get(id=finding_id)
for finding in to_mitigate:
# finding = Finding.objects.get(id=finding_id)
if not finding.mitigated or not finding.is_Mitigated:
logger.debug('mitigating finding: %i:%s', finding.id, finding)
finding.mitigated = scan_date_time
finding.is_Mitigated = True
finding.mitigated_by = request.user
finding.active = False

finding.save(push_to_jira=push_to_jira)
# if we're mitigating a finding, we don't want to run dedupe
# finding.save(push_to_jira=push_to_jira)
finding.save(push_to_jira=push_to_jira, dedupe_option=False)
note = Notes(entry="Mitigated by %s re-upload." % scan_type,
author=request.user)
note.save()
Expand Down Expand Up @@ -908,6 +938,11 @@ def re_import_scan_results(request, tid):
create_notification(event='scan_added', title=title, findings_new=new_items, findings_mitigated=mitigated_findings, findings_reactivated=reactivated_items,
finding_count=updated_count, test=test, engagement=test.engagement, product=test.engagement.product, findings_untouched=untouched,
url=reverse('view_test', args=(test.id,)))
else:
messages.add_message(request,
messages.SUCCESS,
'No findings were added/updated/closed/reactivated as the findings in Defect Dojo are identical to those in the uploaded report.',
extra_tags='alert-success')

return HttpResponseRedirect(reverse('view_test', args=(test.id,)))
except SyntaxError:
Expand Down
Loading