|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +from django import template |
| 11 | +from packageurl import PackageURL |
| 12 | + |
| 13 | +register = template.Library() |
| 14 | + |
| 15 | + |
| 16 | +@register.filter |
| 17 | +def format_diff_for_ui(changes) -> dict: |
| 18 | + """ |
| 19 | + Example: |
| 20 | + { |
| 21 | + 'affected_packages': { |
| 22 | + 'added': [{'package': {'type': 'pypi', 'name': 'requests', 'version': '2.25.0'}, 'affected_version_range': '==2.25.0', 'fixed_version_range': '==2.25.1'}], |
| 23 | + 'removed': [{'package': {'type': 'pypi', 'name': 'requests', 'version': '2.24.0'}, 'affected_version_range': '==2.24.0', 'fixed_version_range': '==2.24.1'}] |
| 24 | + } |
| 25 | + } |
| 26 | + should result in: |
| 27 | + { |
| 28 | + 'Affected Packages': { |
| 29 | + 'added': [{'header': 'Affected package', 'attributes': [('PURL', 'pkg:pypi/requests@2.25.0'), ('Affected version', '==2.25.0'), ('Fixed Version', '==2.25.1')] }], |
| 30 | + 'removed': [{'header': 'Affected package', 'attributes': [('PURL', 'pkg:pypi/requests@2.24.0'), ('Affected version', '==2.24.0'), ('Fixed Version', '==2.24.1')] }] |
| 31 | + } |
| 32 | + } |
| 33 | + """ |
| 34 | + formatted = {} |
| 35 | + |
| 36 | + for field, change in changes.items(): |
| 37 | + label = field.replace("_", " ").title() |
| 38 | + |
| 39 | + if "old" in change or "new" in change: |
| 40 | + formatted[label] = change |
| 41 | + continue |
| 42 | + |
| 43 | + formatted[label] = {"added": [], "removed": []} |
| 44 | + |
| 45 | + for change_type in ["added", "removed"]: |
| 46 | + for item in change.get(change_type, []): |
| 47 | + if field == "affected_packages": |
| 48 | + attributes = [] |
| 49 | + |
| 50 | + if package := item.get("package"): |
| 51 | + package_string = ( |
| 52 | + str(PackageURL(**package)) |
| 53 | + if isinstance(package, dict) |
| 54 | + else str(package) |
| 55 | + ) |
| 56 | + attributes.append(("PURL", package_string)) |
| 57 | + |
| 58 | + if affected_version_range := item.get("affected_version_range"): |
| 59 | + attributes.append(("Affected version", affected_version_range)) |
| 60 | + |
| 61 | + if fixed_version_range := item.get("fixed_version_range"): |
| 62 | + attributes.append(("Fixed Version", fixed_version_range)) |
| 63 | + |
| 64 | + formatted[label][change_type].append( |
| 65 | + {"header": "Affected package", "attributes": attributes} |
| 66 | + ) |
| 67 | + |
| 68 | + elif field == "references": |
| 69 | + attributes = [] |
| 70 | + |
| 71 | + if reference_url := item.get("url"): |
| 72 | + attributes.append(("URL", reference_url)) |
| 73 | + |
| 74 | + if reference_id := item.get("reference_id"): |
| 75 | + attributes.append(("ID", reference_id)) |
| 76 | + |
| 77 | + formatted[label][change_type].append( |
| 78 | + {"header": "Reference", "attributes": attributes} |
| 79 | + ) |
| 80 | + |
| 81 | + elif field == "severities": |
| 82 | + attributes = [] |
| 83 | + |
| 84 | + if scoring_system := item.get("system") or item.get("scoring_system"): |
| 85 | + scoring_system_string = str(scoring_system) |
| 86 | + if "cvss" in scoring_system_string.lower(): |
| 87 | + scoring_system_string = scoring_system_string.upper() |
| 88 | + else: |
| 89 | + scoring_system_string = scoring_system_string.replace("_", " ").title() |
| 90 | + attributes.append(("System", scoring_system_string)) |
| 91 | + |
| 92 | + if severity_value := item.get("value"): |
| 93 | + attributes.append(("Value", severity_value)) |
| 94 | + |
| 95 | + if scoring_elements := item.get("scoring_elements"): |
| 96 | + attributes.append(("Elements", scoring_elements)) |
| 97 | + |
| 98 | + formatted[label][change_type].append( |
| 99 | + {"header": "Severity", "attributes": attributes} |
| 100 | + ) |
| 101 | + |
| 102 | + elif field == "patches": |
| 103 | + attributes = [] |
| 104 | + |
| 105 | + if patch_url := (item.get("url") or item.get("repository")): |
| 106 | + attributes.append(("URL", patch_url)) |
| 107 | + |
| 108 | + if patch_commit := item.get("commit"): |
| 109 | + attributes.append(("Commit", patch_commit)) |
| 110 | + |
| 111 | + formatted[label][change_type].append( |
| 112 | + {"header": "Patch", "attributes": attributes} |
| 113 | + ) |
| 114 | + |
| 115 | + else: |
| 116 | + formatted[label][change_type].append(item) |
| 117 | + |
| 118 | + return formatted |
0 commit comments