|
| 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 | +import logging |
| 11 | +from pathlib import Path |
| 12 | +from traceback import format_exc as traceback_format_exc |
| 13 | +from typing import Iterable |
| 14 | + |
| 15 | +from dateutil.parser import parse |
| 16 | +from fetchcode.vcs import fetch_via_vcs |
| 17 | +from packageurl import PackageURL |
| 18 | +from univers.version_range import OpensslVersionRange |
| 19 | + |
| 20 | +from vulnerabilities import severity_systems |
| 21 | +from vulnerabilities.importer import AdvisoryData |
| 22 | +from vulnerabilities.importer import AffectedPackageV2 |
| 23 | +from vulnerabilities.importer import PatchData |
| 24 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 25 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 26 | +from vulnerabilities.pipes import openssl |
| 27 | +from vulnerabilities.utils import build_description |
| 28 | +from vulnerabilities.utils import create_weaknesses_list |
| 29 | +from vulnerabilities.utils import get_item |
| 30 | +from vulnerabilities.utils import load_json |
| 31 | + |
| 32 | + |
| 33 | +class OpenSSLImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 34 | + """Import OpenSSL Advisories""" |
| 35 | + |
| 36 | + pipeline_id = "openssl_importer_v2" |
| 37 | + spdx_license_expression = "Apache-2.0" |
| 38 | + importer_name = "OpenSSL Importer V2" |
| 39 | + |
| 40 | + license_url = "https://github.com/openssl/openssl/blob/master/LICENSE.txt" |
| 41 | + repo_url = "git+https://github.com/openssl/release-metadata/" |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def steps(cls): |
| 45 | + return ( |
| 46 | + cls.clone, |
| 47 | + cls.collect_and_store_advisories, |
| 48 | + cls.clean_downloads, |
| 49 | + ) |
| 50 | + |
| 51 | + def clone(self): |
| 52 | + self.log(f"Cloning `{self.repo_url}`") |
| 53 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 54 | + self.advisory_path = Path(self.vcs_response.dest_dir) |
| 55 | + |
| 56 | + def advisories_count(self): |
| 57 | + vuln_directory = self.advisory_path / "secjson" |
| 58 | + return sum(1 for _ in vuln_directory.glob("CVE-*.json")) |
| 59 | + |
| 60 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 61 | + vuln_directory = self.advisory_path / "secjson" |
| 62 | + |
| 63 | + for advisory in vuln_directory.glob("CVE-*.json"): |
| 64 | + yield self.to_advisory_data(advisory) |
| 65 | + |
| 66 | + def to_advisory_data(self, file: Path) -> Iterable[AdvisoryData]: |
| 67 | + # TODO: Collect the advisory credits, see https://github.com/aboutcode-org/vulnerablecode/issues/2121 |
| 68 | + |
| 69 | + affected_packages = [] |
| 70 | + severities = [] |
| 71 | + references = [] |
| 72 | + patches = [] |
| 73 | + fix_commits = {} |
| 74 | + cwe_string = None |
| 75 | + |
| 76 | + data = load_json(file) |
| 77 | + advisory_text = file.read_text() |
| 78 | + advisory = get_item(data, "containers", "cna") |
| 79 | + description = get_item(advisory, "descriptions", 0, "value") |
| 80 | + title = advisory.get("title") |
| 81 | + date_published = parse(get_item(advisory, "datePublic")) |
| 82 | + cve = get_item(data, "cveMetadata", "cveId") |
| 83 | + severity_score = get_item(advisory, "metrics", 0, "other", "content", "text") |
| 84 | + |
| 85 | + for reference in get_item(advisory, "references") or []: |
| 86 | + ref_name = reference.get("name") |
| 87 | + ref_url = reference.get("url") |
| 88 | + if not ref_url: |
| 89 | + continue |
| 90 | + |
| 91 | + tag = get_item(reference, "tags", 0) or "" |
| 92 | + tag = tag.lower() |
| 93 | + references.append(openssl.get_reference(ref_name, tag, ref_url)) |
| 94 | + |
| 95 | + if tag != "patch": |
| 96 | + continue |
| 97 | + |
| 98 | + if not ref_name: |
| 99 | + patches.append(PatchData(patch_url=ref_url)) |
| 100 | + continue |
| 101 | + |
| 102 | + fix_commits[ref_name.split()[0]] = ref_url |
| 103 | + |
| 104 | + for affected in get_item(advisory, "affected", 0, "versions") or []: |
| 105 | + if affected.get("status") != "affected": |
| 106 | + continue |
| 107 | + fixed_by_commit_patches = [] |
| 108 | + affected_constraints = None |
| 109 | + fixed_version = None |
| 110 | + |
| 111 | + try: |
| 112 | + affected_constraints, fixed_version = openssl.parse_affected_fixed(affected) |
| 113 | + except Exception as e: |
| 114 | + self.log( |
| 115 | + f"Failed to parse OpenSSL version for: {cve} with error {e!r}:\n{traceback_format_exc()}", |
| 116 | + level=logging.ERROR, |
| 117 | + ) |
| 118 | + continue |
| 119 | + |
| 120 | + fixed_version_range = ( |
| 121 | + OpensslVersionRange.from_versions([fixed_version]) if fixed_version else None |
| 122 | + ) |
| 123 | + |
| 124 | + affected_version_range = ( |
| 125 | + OpensslVersionRange(constraints=affected_constraints) |
| 126 | + if affected_constraints |
| 127 | + else None |
| 128 | + ) |
| 129 | + |
| 130 | + if fixed_version and (commit_url := fix_commits.get(fixed_version)): |
| 131 | + if patch := openssl.get_commit_patch( |
| 132 | + url=commit_url, |
| 133 | + logger=self.log, |
| 134 | + ): |
| 135 | + fixed_by_commit_patches.append(patch) |
| 136 | + |
| 137 | + affected_packages.append( |
| 138 | + AffectedPackageV2( |
| 139 | + package=PackageURL(type="openssl", name="openssl"), |
| 140 | + affected_version_range=affected_version_range, |
| 141 | + fixed_version_range=fixed_version_range, |
| 142 | + fixed_by_commit_patches=fixed_by_commit_patches, |
| 143 | + ) |
| 144 | + ) |
| 145 | + |
| 146 | + if severity_score: |
| 147 | + severities.append( |
| 148 | + VulnerabilitySeverity( |
| 149 | + system=severity_systems.OPENSSL, |
| 150 | + value=severity_score, |
| 151 | + url=f"https://openssl-library.org/news/secjson/{cve.lower()}.json", |
| 152 | + ) |
| 153 | + ) |
| 154 | + |
| 155 | + if "problemTypes" in advisory: |
| 156 | + problem_type = get_item(advisory, "problemTypes", 0, "descriptions", 0) |
| 157 | + cwe_string = problem_type.get("cweId") |
| 158 | + |
| 159 | + weaknesses = create_weaknesses_list([cwe_string]) if cwe_string else [] |
| 160 | + |
| 161 | + return AdvisoryData( |
| 162 | + advisory_id=cve, |
| 163 | + aliases=[], |
| 164 | + summary=build_description(summary=title, description=description), |
| 165 | + date_published=date_published, |
| 166 | + affected_packages=affected_packages, |
| 167 | + references_v2=references, |
| 168 | + severities=severities, |
| 169 | + weaknesses=weaknesses, |
| 170 | + patches=patches, |
| 171 | + url=f"https://github.com/openssl/release-metadata/blob/main/secjson/{cve}.json", |
| 172 | + original_advisory_text=advisory_text, |
| 173 | + ) |
| 174 | + |
| 175 | + def clean_downloads(self): |
| 176 | + if self.vcs_response: |
| 177 | + self.log(f"Removing cloned repository") |
| 178 | + self.vcs_response.delete() |
| 179 | + |
| 180 | + def on_failure(self): |
| 181 | + self.clean_downloads() |
0 commit comments