Skip to content

Commit 52da958

Browse files
committed
Add CWE support in the API
Resolve merge conflict issue: #1094 Signed-off-by: ziadhany <ziadhany2016@gmail.com>
1 parent 2646d7e commit 52da958

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

vulnerabilities/api.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from vulnerabilities.models import Vulnerability
2323
from vulnerabilities.models import VulnerabilityReference
2424
from vulnerabilities.models import VulnerabilitySeverity
25+
from vulnerabilities.models import Weakness
2526
from vulnerabilities.models import get_purl_query_lookups
2627
from vulnerabilities.throttling import StaffUserRateThrottle
2728

@@ -96,15 +97,25 @@ class Meta:
9697
fields = ["url", "vulnerability_id", "summary", "references", "fixed_packages", "aliases"]
9798

9899

99-
class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
100+
class WeaknessSerializer(serializers.HyperlinkedModelSerializer):
101+
"""
102+
Used for nesting inside weakness focused APIs.
103+
"""
104+
105+
class Meta:
106+
model = Weakness
107+
fields = ["cwe_id", "name", "description"]
108+
100109

110+
class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
101111
fixed_packages = MinimalPackageSerializer(
102112
many=True, source="filtered_fixed_packages", read_only=True
103113
)
104114
affected_packages = MinimalPackageSerializer(many=True, read_only=True)
105115

106116
references = VulnerabilityReferenceSerializer(many=True, source="vulnerabilityreference_set")
107117
aliases = AliasSerializer(many=True, source="alias")
118+
weaknesses = WeaknessSerializer(many=True)
108119

109120
class Meta:
110121
model = Vulnerability
@@ -116,6 +127,7 @@ class Meta:
116127
"fixed_packages",
117128
"affected_packages",
118129
"references",
130+
"weaknesses",
119131
]
120132

121133

@@ -336,11 +348,12 @@ def get_queryset(self):
336348
to a custom attribute `filtered_fixed_packages`
337349
"""
338350
return Vulnerability.objects.prefetch_related(
351+
"weaknesses",
339352
Prefetch(
340353
"packages",
341354
queryset=self.get_fixed_packages_qs(),
342355
to_attr="filtered_fixed_packages",
343-
)
356+
),
344357
)
345358

346359
serializer_class = VulnerabilitySerializer

vulnerabilities/tests/test_api.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from vulnerabilities.models import Vulnerability
2727
from vulnerabilities.models import VulnerabilityReference
2828
from vulnerabilities.models import VulnerabilityRelatedReference
29+
from vulnerabilities.models import Weakness
2930

3031
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
3132
TEST_DATA = os.path.join(BASE_DIR, "test_data")
@@ -197,6 +198,8 @@ def setUp(self):
197198
PackageRelatedVulnerability.objects.create(
198199
package=pkg, vulnerability=self.vulnerability, fix=True
199200
)
201+
self.weaknesses = Weakness.objects.create(cwe_id=119)
202+
self.weaknesses.vulnerabilities.add(self.vulnerability)
200203

201204
def test_api_status(self):
202205
response = self.csrf_client.get("/api/vulnerabilities/")
@@ -229,6 +232,13 @@ def test_api_with_single_vulnerability(self):
229232
],
230233
"affected_packages": [],
231234
"references": [],
235+
"weaknesses": [
236+
{
237+
"cwe_id": 119,
238+
"name": "Improper Restriction of Operations within the Bounds of a Memory Buffer",
239+
"description": "The software performs operations on a memory buffer, but it can read from or write to a memory location that is outside of the intended boundary of the buffer.",
240+
}
241+
],
232242
}
233243

234244
def test_api_with_single_vulnerability_with_filters(self):
@@ -249,6 +259,13 @@ def test_api_with_single_vulnerability_with_filters(self):
249259
],
250260
"affected_packages": [],
251261
"references": [],
262+
"weaknesses": [
263+
{
264+
"cwe_id": 119,
265+
"name": "Improper Restriction of Operations within the Bounds of a Memory Buffer",
266+
"description": "The software performs operations on a memory buffer, but it can read from or write to a memory location that is outside of the intended boundary of the buffer.",
267+
}
268+
],
252269
}
253270

254271

0 commit comments

Comments
 (0)