-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathviews.py
348 lines (296 loc) · 12.4 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# VulnerableCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import logging
from cvss.exceptions import CVSS2MalformedError
from cvss.exceptions import CVSS3MalformedError
from cvss.exceptions import CVSS4MalformedError
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.core.mail import send_mail
from django.db.models import Prefetch
from django.http.response import Http404
from django.shortcuts import redirect
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import View
from django.views import generic
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from univers.version_range import RANGE_CLASS_BY_SCHEMES
from univers.version_range import AlpineLinuxVersionRange
from vulnerabilities import models
from vulnerabilities.forms import ApiUserCreationForm
from vulnerabilities.forms import PackageSearchForm
from vulnerabilities.forms import VulnerabilitySearchForm
from vulnerabilities.severity_systems import EPSS
from vulnerabilities.severity_systems import SCORING_SYSTEMS
from vulnerablecode import __version__ as VULNERABLECODE_VERSION
from vulnerablecode.settings import env
PAGE_SIZE = 20
class PackageSearch(ListView):
model = models.Package
template_name = "packages.html"
ordering = ["type", "namespace", "name", "-version_rank", "version"]
paginate_by = PAGE_SIZE
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request_query = self.request.GET
context["package_search_form"] = PackageSearchForm(request_query)
context["search"] = request_query.get("search")
return context
def get_queryset(self, query=None):
"""
Return a Package queryset for the ``query``.
Make a best effort approach to find matching packages either based
on exact purl, partial purl or just name and namespace.
"""
query = query or self.request.GET.get("search") or ""
return (
self.model.objects.search(query)
.with_vulnerability_counts()
.prefetch_related()
.order_by("type", "namespace", "name", "-version_rank", "version")
)
class VulnerabilitySearch(ListView):
model = models.Vulnerability
template_name = "vulnerabilities.html"
ordering = ["aliases"]
paginate_by = PAGE_SIZE
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
request_query = self.request.GET
context["vulnerability_search_form"] = VulnerabilitySearchForm(request_query)
context["search"] = request_query.get("search")
return context
def get_queryset(self, query=None):
query = query or self.request.GET.get("search") or ""
return self.model.objects.search(query=query).with_package_counts().order_by("-aliases")
class PackageDetails(DetailView):
model = models.Package
template_name = "package_details.html"
slug_url_kwarg = "purl"
slug_field = "purl"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
package = self.object
context["package"] = package
context["affected_by_vulnerabilities"] = package.affected_by.order_by("-aliases")
# Ghost package should not fix any vulnerability.
context["fixing_vulnerabilities"] = (
None if package.is_ghost else package.fixing.order_by("-aliases")
)
context["package_search_form"] = PackageSearchForm(self.request.GET)
context["fixed_package_details"] = package.fixed_package_details
context["history"] = list(package.history)
return context
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
purl = self.kwargs.get(self.slug_url_kwarg)
if purl:
queryset = queryset.for_purl(purl)
else:
cls = self.__class__.__name__
raise AttributeError(
f"Package details view {cls} must be called with a purl, " f"but got: {purl!r}"
)
try:
package = queryset.get()
except queryset.model.DoesNotExist:
raise Http404(f"No Package found for purl: {purl}")
return package
class VulnerabilityDetails(DetailView):
model = models.Vulnerability
template_name = "vulnerability_details.html"
slug_url_kwarg = "vulnerability_id"
slug_field = "vulnerability_id"
def get_queryset(self):
return (
super()
.get_queryset()
.select_related()
.prefetch_related(
Prefetch(
"references",
queryset=models.VulnerabilityReference.objects.only(
"reference_id", "reference_type", "url"
),
),
Prefetch(
"aliases",
queryset=models.Alias.objects.only("alias"),
),
Prefetch(
"weaknesses",
queryset=models.Weakness.objects.only("cwe_id"),
),
Prefetch(
"severities",
queryset=models.VulnerabilitySeverity.objects.only(
"scoring_system", "value", "url", "scoring_elements", "published_at"
),
),
Prefetch(
"exploits",
queryset=models.Exploit.objects.only(
"data_source", "description", "required_action", "due_date", "notes"
),
),
)
)
def get_context_data(self, **kwargs):
"""
Build context with preloaded QuerySets and minimize redundant queries.
"""
context = super().get_context_data(**kwargs)
vulnerability = self.object
# Pre-fetch and process data in Python instead of the template
weaknesses_present_in_db = [
weakness_object
for weakness_object in vulnerability.weaknesses.all()
if weakness_object.weakness
]
valid_severities = self.object.severities.exclude(scoring_system=EPSS.identifier).filter(
scoring_elements__isnull=False, scoring_system__in=SCORING_SYSTEMS.keys()
)
severity_vectors = []
for severity in valid_severities:
try:
vector_values = SCORING_SYSTEMS[severity.scoring_system].get(
severity.scoring_elements
)
if vector_values:
severity_vectors.append({"vector": vector_values, "origin": severity.url})
except (
CVSS2MalformedError,
CVSS3MalformedError,
CVSS4MalformedError,
NotImplementedError,
):
logging.error(f"CVSSMalformedError for {severity.scoring_elements}")
epss_severity = vulnerability.severities.filter(scoring_system="epss").first()
epss_data = None
if epss_severity:
epss_data = {
"percentile": epss_severity.scoring_elements,
"score": epss_severity.value,
"published_at": epss_severity.published_at,
}
context.update(
{
"vulnerability": vulnerability,
"vulnerability_search_form": VulnerabilitySearchForm(self.request.GET),
"severities": list(vulnerability.severities.all()),
"severity_vectors": severity_vectors,
"references": list(vulnerability.references.all()),
"aliases": list(vulnerability.aliases.all()),
"weaknesses": weaknesses_present_in_db,
"status": vulnerability.get_status_label,
"history": vulnerability.history,
"epss_data": epss_data,
}
)
return context
class HomePage(View):
template_name = "index.html"
def get(self, request):
request_query = request.GET
context = {
"vulnerability_search_form": VulnerabilitySearchForm(request_query),
"package_search_form": PackageSearchForm(request_query),
"release_url": f"https://github.com/aboutcode-org/vulnerablecode/releases/tag/v{VULNERABLECODE_VERSION}",
}
return render(request=request, template_name=self.template_name, context=context)
email_template = """
Dear VulnerableCode.io user:
We have received a request to send a VulnerableCode.io API key to this email address.
Here is your API key:
Token {auth_token}
If you did NOT request this API key, you can either ignore this email or contact us at [email protected] and let us know in the forward that you did not request an API key.
The API root is at https://public.vulnerablecode.io/api
To learn more about using the VulnerableCode.io API, please refer to the live API documentation at https://public.vulnerablecode.io/api/docs
To learn about VulnerableCode, refer to the general documentation at https://vulnerablecode.readthedocs.io
--
Sincerely,
The nexB support Team.
VulnerableCode is a free and open database of software package vulnerabilities
and the tools to aggregate and correlate these vulnerabilities.
Chat at https://gitter.im/aboutcode-org/vulnerablecode
Docs at https://vulnerablecode.readthedocs.org/
Source code and issues at https://github.com/nexB/vulnerablecode
"""
class ApiUserCreateView(generic.CreateView):
model = models.ApiUser
form_class = ApiUserCreationForm
template_name = "api_user_creation_form.html"
def form_valid(self, form):
try:
response = super().form_valid(form)
except ValidationError:
messages.error(self.request, "Email is invalid or already taken")
return redirect(self.get_success_url())
send_mail(
subject="VulnerableCode.io API key request",
message=email_template.format(auth_token=self.object.auth_token),
from_email=env.str("FROM_EMAIL", default=""),
recipient_list=[self.object.email],
fail_silently=True,
)
messages.success(
self.request, f"Your API key token has been sent to your email: {self.object.email}."
)
return response
def get_success_url(self):
return reverse_lazy("api_user_request")
class VulnerabilityPackagesDetails(DetailView):
"""
View to display all packages affected by or fixing a specific vulnerability.
URL: /vulnerabilities/{vulnerability_id}/packages
"""
model = models.Vulnerability
template_name = "vulnerability_package_details.html"
slug_url_kwarg = "vulnerability_id"
slug_field = "vulnerability_id"
def get_queryset(self):
"""
Prefetch and optimize related data to minimize database hits.
"""
return (
super()
.get_queryset()
.prefetch_related(
Prefetch(
"affecting_packages",
queryset=models.Package.objects.only("type", "namespace", "name", "version"),
),
Prefetch(
"fixed_by_packages",
queryset=models.Package.objects.only("type", "namespace", "name", "version"),
),
)
)
def get_context_data(self, **kwargs):
"""
Build context with preloaded QuerySets and minimize redundant queries.
"""
context = super().get_context_data(**kwargs)
vulnerability = self.object
(
sorted_fixed_by_packages,
sorted_affected_packages,
all_affected_fixed_by_matches,
) = vulnerability.aggregate_fixed_and_affected_packages()
context.update(
{
"affected_packages": sorted_affected_packages,
"fixed_by_packages": sorted_fixed_by_packages,
"all_affected_fixed_by_matches": all_affected_fixed_by_matches,
}
)
return context