Skip to content

Commit

Permalink
Get vulnerabilities from CPE
Browse files Browse the repository at this point in the history
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Apr 22, 2022
1 parent 6bebd6b commit 19ff442
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,38 @@ class VulnerabilityViewSet(viewsets.ReadOnlyModelViewSet):
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = VulnerabilityFilterSet


class VulnerabilityReferenceFilterSet(filters.FilterSet):
class Meta:
model = VulnerabilityReference
fields = ["reference_id"]


class CPEFilterSet(filters.FilterSet):
cpe = filters.CharFilter(method="filter_cpe")

class Meta:
model = VulnerabilityReference
fields = ["reference_id"]

def filter_cpe(self, queryset, name, value):
cpe = unquote(value)
return self.queryset.filter(reference_id=cpe)


class CPESerializer(serializers.HyperlinkedModelSerializer):

vulnerability = MinimalVulnerabilitySerializer(read_only=True)

class Meta:
model = VulnerabilityReference
fields = ["vulnerability"]


class CPEViewSet(viewsets.ReadOnlyModelViewSet):
queryset = VulnerabilityReference.objects.filter(reference_id__startswith="cpe")
serializer_class = CPESerializer
paginate_by = 50
filter_backends = (filters.DjangoFilterBackend,)
filterset_class = CPEFilterSet
2 changes: 2 additions & 0 deletions vulnerablecode/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from django.urls import path
from rest_framework.routers import DefaultRouter

from vulnerabilities.api import CPEViewSet
from vulnerabilities.api import PackageViewSet
from vulnerabilities.api import VulnerabilityViewSet
from vulnerabilities.views import HomePage
Expand All @@ -47,6 +48,7 @@ def __init__(self, *args, **kwargs):
api_router.register(r"packages", PackageViewSet)
# `DefaultRouter` requires `basename` when registering viewsets that don't define a queryset.
api_router.register(r"vulnerabilities", VulnerabilityViewSet, basename="vulnerability")
api_router.register(r"cpes", CPEViewSet, basename="cpe")

urlpatterns = [
path("admin/", admin.site.urls),
Expand Down

0 comments on commit 19ff442

Please sign in to comment.