Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vulnerabilities/templates/api_user_creation_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ <h2 class="subtitle mb-0 pt-2 mb-2">
</section>

<br/>
<form action = "" method = "post">
<!-- <form action = "" method = "post">
{% csrf_token %}
{% for field in form %}
<div class="field mt-2">
Expand All @@ -46,6 +46,6 @@ <h2 class="subtitle mb-0 pt-2 mb-2">
</div>
{% endfor %}
<input class="button is-link mt-5" type="submit" value="Request my API Key">
</form>
</form> -->
</section>
{% endblock %}
2 changes: 1 addition & 1 deletion vulnerabilities/templates/vulnerability_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,4 @@
}
</script>

{% endblock %}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@
}
</script>

{% endblock %}
{% endblock %}
2 changes: 1 addition & 1 deletion vulnerabilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions vulnerabilities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading