Skip to content

Commit b06f85a

Browse files
committed
Faster Weakness Lookup
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 9e691ad commit b06f85a

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

vulnerabilities/models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
from vulnerabilities.utils import normalize_purl
5858
from vulnerabilities.utils import purl_to_dict
5959
from vulnerablecode import __version__ as VULNERABLECODE_VERSION
60+
from cwe2.weakness import Weakness as DBWeakness
61+
from cwe2.mappings import xml_database_path
62+
import xml.etree.ElementTree as ET
6063

6164
logger = logging.getLogger(__name__)
6265

@@ -470,6 +473,21 @@ def get_severity_vectors_and_values(self):
470473

471474
return severity_vectors, severity_values
472475

476+
def get_cwes(self):
477+
"""Yield CWE Weakness objects"""
478+
for cwe_category in self.cwe_files:
479+
cwe_category.seek(0)
480+
reader = csv.DictReader(cwe_category)
481+
for row in reader:
482+
yield DBWeakness(*list(row.values())[0:-1])
483+
tree = ET.parse(xml_database_path)
484+
root = tree.getroot()
485+
for tag_num in [1, 2]: # Categories , Views
486+
tag = root[tag_num]
487+
for child in tag:
488+
yield DBWeakness(*[child.attrib["ID"], child.attrib.get("Name"),None,child.attrib.get("Status"),child[0].text])
489+
490+
Database.get_cwes = get_cwes
473491

474492
def get_cwes(self):
475493
"""Yield CWE Weakness objects"""

0 commit comments

Comments
 (0)