Skip to content

Commit

Permalink
Add support for CWE
Browse files Browse the repository at this point in the history
Reference: aboutcode-org#651
Signed-off-by: Ziad <ziadhany2016@gmail.com>
  • Loading branch information
ziadhany committed Jun 24, 2022
1 parent c94ed57 commit 473b364
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ yarl==1.7.2
zipp==3.8.0
dateparser==1.1.1
fetchcode==0.1.0
cwe==1.6
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ install_requires =
defusedxml>=0.7.1
Markdown>=3.3.0
dateparser>=1.1.1
cwe==1.6

# networking
GitPython>=3.1.17
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.0.4 on 2022-06-24 21:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('vulnerabilities', '0015_alter_vulnerabilityseverity_unique_together_and_more'),
]

operations = [
migrations.AlterUniqueTogether(
name='vulnerabilityseverity',
unique_together=set(),
),
migrations.AddField(
model_name='vulnerabilityseverity',
name='cwe_ids',
field=models.JSONField(blank=True, default=list, help_text="Example: {'CWE-327', .. }"),
),
migrations.AlterUniqueTogether(
name='vulnerabilityseverity',
unique_together={('reference', 'scoring_system', 'value', 'cwe_ids')},
),
]
16 changes: 15 additions & 1 deletion vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import json
import logging
import uuid

from cwe import Database
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator
from django.core.validators import MinValueValidator
Expand Down Expand Up @@ -306,11 +306,25 @@ class VulnerabilitySeverity(models.Model):

value = models.CharField(max_length=50, help_text="Example: 9.0, Important, High")

cwe_ids = models.JSONField(blank=True, default=list, help_text="Example: {327, .. }")

@property
def cwe_details(self):
if not self.cwe_ids:
return None
db = Database()
details = {}
for cwe_id in json.dumps(self.cwe_ids):
weakness = db.get(cwe_id)
details.add(weakness.to_dict())
return details

class Meta:
unique_together = (
"reference",
"scoring_system",
"value",
"cwe_ids",
)


Expand Down
20 changes: 19 additions & 1 deletion vulnerabilities/templates/vulnerability.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,29 @@ <h3>Severity</h3>
<td>{{ ref.reference_id }}</td>

{% endif %}

</tr>
{% endfor %}
{% endfor %}
{% endfor %}

</table>

{% for ref in object_list %}
{% for obj in ref.severities %}
{% if cwe_ids %}
<h3>Weaknesses:</h3>
{% for cwe_id in cwe_ids %}
<a href="https://cwe.mitre.org/data/definitions/{{ cwe_id }}.html">
CWE-{{cwe_id}}
</a>
{{ cwe_details }}
{% endfor %}
{% else %}
<h3>Weaknesses:</h3>
<p>No CWEs</p>
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}

{% if vulnerability.vulnerable_to.all %}
Expand Down

0 comments on commit 473b364

Please sign in to comment.