Skip to content

Commit 5fe61fb

Browse files
committed
Refactor code
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 5d24fb6 commit 5fe61fb

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

vulnerabilities/improve_runner.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,44 @@ def get_or_create_vulnerability_without_aliases(inference):
236236

237237
vuln_by_refs = {}
238238

239-
for ref in inference.references:
240-
try:
241-
reference = VulnerabilityReference.objects.get(url=ref.url)
242-
vuln_by_refs[ref.url] = set(reference.vulnerabilities.all())
243-
except VulnerabilityReference.DoesNotExist:
244-
refs_are_exact_match = False
245-
pass
239+
ref_urls = [ref.url for ref in inference.references]
240+
241+
references = VulnerabilityReference.objects.filter(url__in=ref_urls)
242+
243+
if references.count() != len(ref_urls):
244+
refs_are_exact_match = False
245+
else:
246+
for ref in references:
247+
# saving it as a set to use intersection later
248+
vuln_by_refs[ref.url] = set(ref.vulnerabilities.all())
246249

247250
if refs_are_exact_match:
248-
common_vulns = set.intersection(*vuln_by_refs.values())
249251

252+
list_of_vulns_by_each_url = vuln_by_refs.values()
253+
254+
# Find all common vulnerabilities for all references
255+
common_vulns = set.intersection(*list_of_vulns_by_each_url)
256+
257+
# For a single common vulnerability that have same exact reference
258+
# return it
250259
if len(common_vulns) == 1:
251260
return common_vulns.pop()
261+
262+
# If there are multiple common vulnerabilities, try to find
263+
# one that matches the summary and packages
252264
elif len(common_vulns) > 1:
253265
for vuln in common_vulns:
254266
if vuln.summary == inference.summary:
255267
if match_packages(inference, vuln):
256268
return vuln
257269

270+
else:
271+
# If there are no common vulnerabilities, fall back to creating
272+
# a new vulnerability
273+
pass
274+
275+
# This is fallback for cases when we cannot find vulnerability
276+
# by references, summary and packages
258277
vulnerability = Vulnerability(summary=inference.summary)
259278
vulnerability.save()
260279

0 commit comments

Comments
 (0)