Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 500 error in /api/cpes endpoint #1629

Merged
merged 3 commits into from
Oct 28, 2024
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
10 changes: 3 additions & 7 deletions vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,17 +642,13 @@ def filter_cpe(self, queryset, name, value):
return self.queryset.filter(vulnerabilityreference__reference_id__startswith=cpe).distinct()


class CPEViewSet(viewsets.ReadOnlyModelViewSet):
"""
Lookup for vulnerabilities by CPE (https://nvd.nist.gov/products/cpe)
"""
class CPEViewSet(VulnerabilityViewSet):
"""Lookup for vulnerabilities by CPE (https://nvd.nist.gov/products/cpe)"""

queryset = Vulnerability.objects.filter(
vulnerabilityreference__reference_id__startswith="cpe"
).distinct()
serializer_class = VulnerabilitySerializer
filter_backends = (filters.DjangoFilterBackend,)
throttle_classes = [StaffUserRateThrottle, AnonRateThrottle]

filterset_class = CPEFilterSet

@action(detail=False, methods=["post"])
Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def __str__(self):
@property
def is_cpe(self):
"""
Return Trueis this is a CPE reference.
Return True if this is a CPE reference.
"""
return self.reference_id.startswith("cpe")

Expand Down Expand Up @@ -557,7 +557,7 @@ def for_cve(self, cve):

def with_is_vulnerable(self):
"""
Annotate Package with ``with_is_vulnerable`` boolean attribute.
Annotate Package with ``is_vulnerable`` boolean attribute.
"""
return self.annotate(
is_vulnerable=Exists(
Expand Down
40 changes: 40 additions & 0 deletions vulnerabilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,46 @@ def test_api_response(self):
self.assertEqual(response["count"], 1)


class TestCPEApiWithPackageVulnerabilityRelation(TestCase):
def setUp(self):
self.user = ApiUser.objects.create_api_user(username="e@mail.com")
self.auth = f"Token {self.user.auth_token.key}"
self.csrf_client = APIClient(enforce_csrf_checks=True)
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth)
self.vulnerability = Vulnerability.objects.create(summary="test")
self.affected_package, _ = Package.objects.get_or_create_from_purl(
purl="pkg:nginx/nginx@v3.4"
)
self.fixed_package, _ = Package.objects.get_or_create_from_purl(purl="pkg:nginx/nginx@v4.0")
AffectedByPackageRelatedVulnerability.objects.create(
vulnerability=self.vulnerability,
created_by="test",
package=self.affected_package,
confidence=100,
)
FixingPackageRelatedVulnerability.objects.create(
vulnerability=self.vulnerability,
created_by="test",
package=self.fixed_package,
confidence=100,
)
for i in range(0, 10):
ref, _ = VulnerabilityReference.objects.get_or_create(
reference_id=f"cpe:/a:nginx:{i}",
url=f"https://nvd.nist.gov/vuln/search/results?adv_search=true&isCpeNameSearch=true&query=cpe:/a:nginx:{i}",
)
VulnerabilityRelatedReference.objects.create(
reference=ref, vulnerability=self.vulnerability
)

def test_cpe_api(self):
response = self.csrf_client.get("/api/cpes/", format="json")
self.assertEqual(status.HTTP_200_OK, response.status_code)

response_data = response.json()
self.assertEqual(1, response_data["count"])


class AliasApi(TestCase):
def setUp(self):
self.user = ApiUser.objects.create_api_user(username="e@mail.com")
Expand Down
6 changes: 6 additions & 0 deletions vulnerablecode/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,9 @@
},
},
}

if DEBUG:
LOGGING["django"] = {
"handlers": ["console"],
"level": "ERROR",
}
Loading