Skip to content

Commit b1a5cba

Browse files
authored
Refactor vulnerability ID and endpoint retrieval in Finding model (#13324)
* refactor: streamline vulnerability ID and endpoint retrieval in Finding model * attempt to fetch saved objects first * fix ruff * Update dojo/models.py * Update dojo/models.py
1 parent 6e613d1 commit b1a5cba

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

dojo/models.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,53 +2945,56 @@ def compute_hash_code_legacy(self):
29452945

29462946
# Get vulnerability_ids to use for hash_code computation
29472947
def get_vulnerability_ids(self):
2948-
vulnerability_id_str = ""
2949-
if self.id is None:
2950-
if self.unsaved_vulnerability_ids:
2948+
2949+
def _get_unsaved_vulnerability_ids(finding) -> str:
2950+
if finding.unsaved_vulnerability_ids:
29512951
deduplicationLogger.debug("get_vulnerability_ids before the finding was saved")
29522952
# convert list of unsaved vulnerability_ids to the list of their canonical representation
2953-
vulnerability_id_str_list = [str(vulnerability_id) for vulnerability_id in self.unsaved_vulnerability_ids]
2953+
vulnerability_id_str_list = [str(vulnerability_id) for vulnerability_id in finding.unsaved_vulnerability_ids]
29542954
# deduplicate (usually done upon saving finding) and sort endpoints
2955-
vulnerability_id_str = "".join(sorted(dict.fromkeys(vulnerability_id_str_list)))
2956-
else:
2957-
deduplicationLogger.debug("finding has no unsaved vulnerability references")
2958-
else:
2959-
vulnerability_ids = Vulnerability_Id.objects.filter(finding=self)
2960-
deduplicationLogger.debug("get_vulnerability_ids after the finding was saved. Vulnerability references count: " + str(vulnerability_ids.count()))
2961-
# convert list of vulnerability_ids to the list of their canonical representation
2962-
vulnerability_id_str_list = [str(vulnerability_id) for vulnerability_id in vulnerability_ids.all()]
2963-
# sort vulnerability_ids strings
2964-
vulnerability_id_str = "".join(sorted(vulnerability_id_str_list))
2965-
return vulnerability_id_str
2955+
return "".join(sorted(dict.fromkeys(vulnerability_id_str_list)))
2956+
deduplicationLogger.debug("finding has no unsaved vulnerability references")
2957+
return ""
2958+
2959+
def _get_saved_vulnerability_ids(finding) -> str:
2960+
if finding.id is not None:
2961+
vulnerability_ids = Vulnerability_Id.objects.filter(finding=finding)
2962+
deduplicationLogger.debug("get_vulnerability_ids after the finding was saved. Vulnerability references count: " + str(vulnerability_ids.count()))
2963+
# convert list of vulnerability_ids to the list of their canonical representation
2964+
vulnerability_id_str_list = [str(vulnerability_id) for vulnerability_id in vulnerability_ids.all()]
2965+
# sort vulnerability_ids strings
2966+
return "".join(sorted(vulnerability_id_str_list))
2967+
return ""
2968+
2969+
return _get_saved_vulnerability_ids(self) or _get_unsaved_vulnerability_ids(self)
29662970

29672971
# Get endpoints to use for hash_code computation
29682972
# (This sometimes reports "None")
29692973
def get_endpoints(self):
2970-
endpoint_str = ""
2971-
if (self.id is None):
2972-
if len(self.unsaved_endpoints) > 0:
2974+
2975+
def _get_unsaved_endpoints(finding) -> str:
2976+
if len(finding.unsaved_endpoints) > 0:
29732977
deduplicationLogger.debug("get_endpoints before the finding was saved")
29742978
# convert list of unsaved endpoints to the list of their canonical representation
2975-
endpoint_str_list = [str(endpoint) for endpoint in self.unsaved_endpoints]
2979+
endpoint_str_list = [str(endpoint) for endpoint in finding.unsaved_endpoints]
29762980
# deduplicate (usually done upon saving finding) and sort endpoints
2977-
endpoint_str = "".join(
2978-
sorted(
2979-
dict.fromkeys(endpoint_str_list)))
2980-
else:
2981-
# we can get here when the parser defines static_finding=True but leaves dynamic_finding defaulted
2982-
# In this case, before saving the finding, both static_finding and dynamic_finding are True
2983-
# After saving dynamic_finding may be set to False probably during the saving process (observed on Bandit scan before forcing dynamic_finding=False at parser level)
2984-
deduplicationLogger.debug("trying to get endpoints on a finding before it was saved but no endpoints found (static parser wrongly identified as dynamic?")
2985-
else:
2986-
deduplicationLogger.debug("get_endpoints: after the finding was saved. Endpoints count: " + str(self.endpoints.count()))
2987-
# convert list of endpoints to the list of their canonical representation
2988-
endpoint_str_list = [str(endpoint) for endpoint in self.endpoints.all()]
2989-
# sort endpoints strings
2990-
endpoint_str = "".join(
2991-
sorted(
2992-
endpoint_str_list,
2993-
))
2994-
return endpoint_str
2981+
return "".join(dict.fromkeys(endpoint_str_list))
2982+
# we can get here when the parser defines static_finding=True but leaves dynamic_finding defaulted
2983+
# In this case, before saving the finding, both static_finding and dynamic_finding are True
2984+
# After saving dynamic_finding may be set to False probably during the saving process (observed on Bandit scan before forcing dynamic_finding=False at parser level)
2985+
deduplicationLogger.debug("trying to get endpoints on a finding before it was saved but no endpoints found (static parser wrongly identified as dynamic?")
2986+
return ""
2987+
2988+
def _get_saved_endpoints(finding) -> str:
2989+
if finding.id is not None:
2990+
deduplicationLogger.debug("get_endpoints: after the finding was saved. Endpoints count: " + str(finding.endpoints.count()))
2991+
# convert list of endpoints to the list of their canonical representation
2992+
endpoint_str_list = [str(endpoint) for endpoint in finding.endpoints.all()]
2993+
# sort endpoints strings
2994+
return "".join(sorted(endpoint_str_list))
2995+
return ""
2996+
2997+
return _get_saved_endpoints(self) or _get_unsaved_endpoints(self)
29952998

29962999
# Compute the hash_code from the fields to hash
29973000
def hash_fields(self, fields_to_hash):

0 commit comments

Comments
 (0)