Skip to content

Commit

Permalink
Merge pull request #1567 from aboutcode-org/fix-severity-range
Browse files Browse the repository at this point in the history
Add severity range score in API
  • Loading branch information
TG1999 authored Aug 23, 2024
2 parents d62f377 + e0c073d commit 76428da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion vulnerabilities/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

from urllib.parse import unquote

from cvss.exceptions import CVSS2MalformedError
from cvss.exceptions import CVSS3MalformedError
from cvss.exceptions import CVSS4MalformedError
from django.db.models import Prefetch
from django_filters import rest_framework as filters
from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import inline_serializer
from packageurl import PackageURL
from packageurl import normalize_qualifiers
from rest_framework import serializers
Expand All @@ -32,7 +34,10 @@
from vulnerabilities.models import VulnerabilitySeverity
from vulnerabilities.models import Weakness
from vulnerabilities.models import get_purl_query_lookups
from vulnerabilities.severity_systems import EPSS
from vulnerabilities.severity_systems import SCORING_SYSTEMS
from vulnerabilities.throttling import StaffUserRateThrottle
from vulnerabilities.utils import get_severity_range


class VulnerabilitySeveritySerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -193,6 +198,7 @@ class VulnerabilitySerializer(BaseResourceSerializer):
aliases = AliasSerializer(many=True, source="alias")
kev = KEVSerializer(read_only=True)
weaknesses = WeaknessSerializer(many=True)
severity_range_score = serializers.SerializerMethodField()

def to_representation(self, instance):
data = super().to_representation(instance)
Expand All @@ -206,6 +212,30 @@ def to_representation(self, instance):

return data

def get_severity_range_score(self, instance):
severity_vectors = []
severity_values = set()
for s in instance.severities:
if s.scoring_system == EPSS.identifier:
continue

if s.scoring_elements and s.scoring_system in SCORING_SYSTEMS:
try:
vector_values = SCORING_SYSTEMS[s.scoring_system].get(s.scoring_elements)
severity_vectors.append(vector_values)
except (
CVSS2MalformedError,
CVSS3MalformedError,
CVSS4MalformedError,
NotImplementedError,
):
pass

if s.value:
severity_values.add(s.value)
severity_range = get_severity_range(severity_values)
return severity_range

class Meta:
model = Vulnerability
fields = [
Expand All @@ -218,6 +248,7 @@ class Meta:
"references",
"weaknesses",
"kev",
"severity_range_score",
]


Expand Down
2 changes: 2 additions & 0 deletions vulnerabilities/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def test_api_with_single_vulnerability(self):
"url": f"http://testserver/api/vulnerabilities/{self.vulnerability.id}",
"vulnerability_id": self.vulnerability.vulnerability_id,
"summary": "test",
"severity_range_score": None,
"aliases": [],
"resource_url": f"http://testserver/vulnerabilities/{self.vulnerability.vulnerability_id}",
"fixed_packages": [
Expand Down Expand Up @@ -307,6 +308,7 @@ def test_api_with_single_vulnerability_with_filters(self):
"url": f"http://testserver/api/vulnerabilities/{self.vulnerability.id}",
"vulnerability_id": self.vulnerability.vulnerability_id,
"summary": "test",
"severity_range_score": None,
"aliases": [],
"resource_url": f"http://testserver/vulnerabilities/{self.vulnerability.vulnerability_id}",
"fixed_packages": [
Expand Down

0 comments on commit 76428da

Please sign in to comment.