Skip to content

Commit

Permalink
Merge branch 'main' into 1627-migrate-pysec
Browse files Browse the repository at this point in the history
  • Loading branch information
TG1999 authored Oct 28, 2024
2 parents cd9afea + 45070e8 commit a02e211
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
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="[email protected]")
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/[email protected]"
)
self.fixed_package, _ = Package.objects.get_or_create_from_purl(purl="pkg:nginx/[email protected]")
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="[email protected]")
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",
}

0 comments on commit a02e211

Please sign in to comment.