|
12 | 12 | import logging |
13 | 13 | from contextlib import suppress |
14 | 14 |
|
| 15 | +from cwe2.database import Database |
15 | 16 | from django.contrib.auth import get_user_model |
16 | 17 | from django.contrib.auth.models import UserManager |
17 | 18 | from django.core import exceptions |
@@ -249,6 +250,28 @@ def get_related_purls(self): |
249 | 250 | return [p.package_url for p in self.packages.distinct().all()] |
250 | 251 |
|
251 | 252 |
|
| 253 | +class Weakness(models.Model): |
| 254 | + """ |
| 255 | + A Common Weakness Enumeration model |
| 256 | + """ |
| 257 | + |
| 258 | + cwe_id = models.IntegerField(help_text="CWE id") |
| 259 | + vulnerabilities = models.ManyToManyField(Vulnerability, related_name="weaknesses") |
| 260 | + db = Database() |
| 261 | + |
| 262 | + @property |
| 263 | + def name(self): |
| 264 | + """Return the weakness's name.""" |
| 265 | + weakness = self.db.get(self.cwe_id) |
| 266 | + return weakness.name |
| 267 | + |
| 268 | + @property |
| 269 | + def description(self): |
| 270 | + """Return the weakness's description.""" |
| 271 | + weakness = self.db.get(self.cwe_id) |
| 272 | + return weakness.description |
| 273 | + |
| 274 | + |
252 | 275 | class VulnerabilityReferenceQuerySet(BaseQuerySet): |
253 | 276 | def for_cpe(self): |
254 | 277 | """ |
@@ -661,7 +684,6 @@ def update_or_create(self): |
661 | 684 |
|
662 | 685 |
|
663 | 686 | class VulnerabilitySeverity(models.Model): |
664 | | - |
665 | 687 | reference = models.ForeignKey(VulnerabilityReference, on_delete=models.CASCADE) |
666 | 688 |
|
667 | 689 | scoring_system_choices = tuple( |
@@ -773,6 +795,7 @@ class Advisory(models.Model): |
773 | 795 | date_published = models.DateTimeField( |
774 | 796 | blank=True, null=True, help_text="UTC Date of publication of the advisory" |
775 | 797 | ) |
| 798 | + weaknesses = models.JSONField(blank=True, default=list, help_text="A list of CWE ids") |
776 | 799 | date_collected = models.DateTimeField(help_text="UTC Date on which the advisory was collected") |
777 | 800 | date_improved = models.DateTimeField( |
778 | 801 | blank=True, |
@@ -805,6 +828,7 @@ def to_advisory_data(self) -> AdvisoryData: |
805 | 828 | affected_packages=[AffectedPackage.from_dict(pkg) for pkg in self.affected_packages], |
806 | 829 | references=[Reference.from_dict(ref) for ref in self.references], |
807 | 830 | date_published=self.date_published, |
| 831 | + weaknesses=self.weaknesses, |
808 | 832 | ) |
809 | 833 |
|
810 | 834 |
|
|
0 commit comments