|
| 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 typing import Iterable |
| 11 | + |
| 12 | +from vulnerabilities import severity_systems |
| 13 | +from vulnerabilities.importer import AdvisoryData |
| 14 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 15 | +from vulnerabilities.management.commands.commit_export import logger |
| 16 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 17 | +from vulnerabilities.utils import fetch_yaml |
| 18 | +from vulnerabilities.utils import is_cve |
| 19 | + |
| 20 | + |
| 21 | +class SUSESeverityScoreImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 22 | + spdx_license_expression = "CC-BY-4.0" |
| 23 | + license_url = "https://ftp.suse.com/pub/projects/security/yaml/LICENSE" |
| 24 | + pipeline_id = "suse_importer_v2" |
| 25 | + url = "https://ftp.suse.com/pub/projects/security/yaml/suse-cvss-scores.yaml" |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def steps(cls): |
| 29 | + return ( |
| 30 | + cls.fetch_advisories, |
| 31 | + cls.collect_and_store_advisories, |
| 32 | + ) |
| 33 | + |
| 34 | + def fetch_advisories(self): |
| 35 | + self.score_data = fetch_yaml(self.url) |
| 36 | + |
| 37 | + def advisories_count(self): |
| 38 | + return sum(1 for _ in self.score_data) |
| 39 | + |
| 40 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 41 | + systems_by_version = { |
| 42 | + "2.0": severity_systems.CVSSV2, |
| 43 | + "3": severity_systems.CVSSV3, |
| 44 | + "3.1": severity_systems.CVSSV31, |
| 45 | + "4": severity_systems.CVSSV4, |
| 46 | + } |
| 47 | + |
| 48 | + for cve_id in self.score_data or []: |
| 49 | + severities = [] |
| 50 | + for cvss_score in self.score_data[cve_id].get("cvss") or []: |
| 51 | + cvss_version = cvss_score.get("version") or "" |
| 52 | + scoring_system = systems_by_version.get(cvss_version) |
| 53 | + if not scoring_system: |
| 54 | + logger.error(f"Unsupported CVSS version: {cvss_version}") |
| 55 | + continue |
| 56 | + base_score = str(cvss_score.get("score") or "") |
| 57 | + vector = str(cvss_score.get("vector") or "") |
| 58 | + score = VulnerabilitySeverity( |
| 59 | + system=scoring_system, |
| 60 | + value=base_score, |
| 61 | + scoring_elements=vector, |
| 62 | + ) |
| 63 | + severities.append(score) |
| 64 | + |
| 65 | + if not is_cve(cve_id): |
| 66 | + continue |
| 67 | + |
| 68 | + yield AdvisoryData( |
| 69 | + advisory_id=cve_id, |
| 70 | + aliases=[], |
| 71 | + summary="", |
| 72 | + severities=severities, |
| 73 | + url=self.url, |
| 74 | + ) |
0 commit comments