diff --git a/vulnerabilities/models.py b/vulnerabilities/models.py index 7bfc1ba11..fecde5c0b 100644 --- a/vulnerabilities/models.py +++ b/vulnerabilities/models.py @@ -382,20 +382,14 @@ def get_related_purls(self): return [p.package_url for p in self.packages.distinct().all()] def aggregate_fixed_and_affected_packages(self): - from vulnerabilities.utils import get_purl_version_class + from vulnerabilities.views import get_purl_version_class sorted_fixed_by_packages = self.fixed_by_packages.filter(is_ghost=False).order_by( "type", "namespace", "name", "qualifiers", "subpath" ) - if sorted_fixed_by_packages: - sorted_fixed_by_packages.first().calculate_version_rank - sorted_affected_packages = self.affected_packages.all() - if sorted_affected_packages: - sorted_affected_packages.first().calculate_version_rank - grouped_fixed_by_packages = { key: list(group) for key, group in groupby( @@ -503,6 +497,32 @@ def get_cwes(self): Database.get_cwes = get_cwes +def get_cwes(self): + """Yield CWE Weakness objects""" + for cwe_category in self.cwe_files: + cwe_category.seek(0) + reader = csv.DictReader(cwe_category) + for row in reader: + yield DBWeakness(*list(row.values())[0:-1]) + tree = ET.parse(xml_database_path) + root = tree.getroot() + for tag_num in [1, 2]: # Categories , Views + tag = root[tag_num] + for child in tag: + yield DBWeakness( + *[ + child.attrib["ID"], + child.attrib.get("Name"), + None, + child.attrib.get("Status"), + child[0].text, + ] + ) + + +Database.get_cwes = get_cwes + + class Weakness(models.Model): """ A Common Weakness Enumeration model diff --git a/vulnerabilities/templates/api_user_creation_form.html b/vulnerabilities/templates/api_user_creation_form.html index c7b2291f0..7714b8601 100644 --- a/vulnerabilities/templates/api_user_creation_form.html +++ b/vulnerabilities/templates/api_user_creation_form.html @@ -35,7 +35,7 @@


-
+ {% endblock %} diff --git a/vulnerabilities/templates/vulnerability_details.html b/vulnerabilities/templates/vulnerability_details.html index 7001c8f3b..da8466706 100644 --- a/vulnerabilities/templates/vulnerability_details.html +++ b/vulnerabilities/templates/vulnerability_details.html @@ -614,4 +614,4 @@ } -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/vulnerabilities/templates/vulnerability_package_details.html b/vulnerabilities/templates/vulnerability_package_details.html index 21fb52192..e116e6fa9 100644 --- a/vulnerabilities/templates/vulnerability_package_details.html +++ b/vulnerabilities/templates/vulnerability_package_details.html @@ -85,4 +85,4 @@ } -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/vulnerabilities/utils.py b/vulnerabilities/utils.py index 8c777610d..37a33f4fe 100644 --- a/vulnerabilities/utils.py +++ b/vulnerabilities/utils.py @@ -567,7 +567,7 @@ def normalize_purl(purl: Union[PackageURL, str]): def get_purl_version_class(purl): - RANGE_CLASS_BY_SCHEMES["apk"] = AlpineLinuxVersionRange + RANGE_CLASS_BY_SCHEMES["alpine"] = AlpineLinuxVersionRange purl_version_class = None check_version_class = RANGE_CLASS_BY_SCHEMES.get(purl.type, None) if check_version_class: diff --git a/vulnerabilities/views.py b/vulnerabilities/views.py index a2df48634..4346ce2e0 100644 --- a/vulnerabilities/views.py +++ b/vulnerabilities/views.py @@ -32,12 +32,36 @@ from vulnerabilities.forms import VulnerabilitySearchForm from vulnerabilities.severity_systems import EPSS from vulnerabilities.severity_systems import SCORING_SYSTEMS +from vulnerabilities.utils import get_purl_version_class from vulnerablecode import __version__ as VULNERABLECODE_VERSION from vulnerablecode.settings import env PAGE_SIZE = 20 +def purl_sort_key(purl: models.Package): + """ + Return a sort key for the built-in sorted() function when sorting a list + of Package objects. If the Package ``type`` is supported by univers, apply + the univers version class to the Package ``version``, and otherwise use the + ``version`` attribute as is. + """ + purl_version_class = get_purl_version_class(purl) + purl_sort_version = purl.version + if purl_version_class: + purl_sort_version = purl_version_class(purl.version) + return (purl.type, purl.namespace, purl.name, purl_sort_version, purl.qualifiers, purl.subpath) + + +def get_purl_version_class(purl: models.Package): + RANGE_CLASS_BY_SCHEMES["apk"] = AlpineLinuxVersionRange + purl_version_class = None + check_version_class = RANGE_CLASS_BY_SCHEMES.get(purl.type, None) + if check_version_class: + purl_version_class = check_version_class.version_class + return purl_version_class + + class PackageSearch(ListView): model = models.Package template_name = "packages.html"