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