Skip to content

Commit d3e1e5b

Browse files
committed
Add CWE support in the API
issue: #1094 Signed-off-by: ziadhany <ziadhany2016@gmail.com>
1 parent d0414eb commit d3e1e5b

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

@@ -89,15 +90,25 @@ class Meta:
8990
fields = ["alias"]
9091

9192

92-
class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
93+
class WeaknessSerializer(serializers.HyperlinkedModelSerializer):
94+
"""
95+
Used for nesting inside weakness focused APIs.
96+
"""
97+
98+
class Meta:
99+
model = Weakness
100+
fields = ["cwe_id", "name", "description"]
101+
93102

103+
class VulnerabilitySerializer(serializers.HyperlinkedModelSerializer):
94104
fixed_packages = MinimalPackageSerializer(
95105
many=True, source="filtered_fixed_packages", read_only=True
96106
)
97107
affected_packages = MinimalPackageSerializer(many=True, read_only=True)
98108

99109
references = VulnerabilityReferenceSerializer(many=True, source="vulnerabilityreference_set")
100110
aliases = AliasSerializer(many=True, source="alias")
111+
weaknesses = WeaknessSerializer(many=True)
101112

102113
class Meta:
103114
model = Vulnerability
@@ -109,6 +120,7 @@ class Meta:
109120
"fixed_packages",
110121
"affected_packages",
111122
"references",
123+
"weaknesses",
112124
]
113125

114126

@@ -329,11 +341,12 @@ def get_queryset(self):
329341
to a custom attribute `filtered_fixed_packages`
330342
"""
331343
return Vulnerability.objects.prefetch_related(
344+
"weaknesses",
332345
Prefetch(
333346
"packages",
334347
queryset=self.get_fixed_packages_qs(),
335348
to_attr="filtered_fixed_packages",
336-
)
349+
),
337350
)
338351

339352
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)