|
| 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 | +import re |
| 12 | +from datetime import timezone |
| 13 | +from typing import Iterable |
| 14 | + |
| 15 | +from bs4 import BeautifulSoup |
| 16 | +from dateutil import parser as dateparser |
| 17 | +from packageurl import PackageURL |
| 18 | +from univers.version_range import OpensslVersionRange |
| 19 | + |
| 20 | +from vulnerabilities.importer import AdvisoryData |
| 21 | +from vulnerabilities.importer import AffectedPackage |
| 22 | +from vulnerabilities.importer import Reference |
| 23 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 24 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipeline |
| 25 | +from vulnerabilities.severity_systems import SCORING_SYSTEMS |
| 26 | +from vulnerabilities.utils import fetch_response |
| 27 | +from vulnerabilities.utils import get_item |
| 28 | + |
| 29 | +logging.basicConfig(level=logging.INFO) |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class OpenSSLImporterPipeline(VulnerableCodeBaseImporterPipeline): |
| 34 | + """Collect Advisories from Openssl""" |
| 35 | + |
| 36 | + pipeline_id = "openssl_importer" |
| 37 | + spdx_license_expression = "OpenSSL-standalone" |
| 38 | + license_url = "https://spdx.org/licenses/OpenSSL-standalone.html" |
| 39 | + root_url = "https://openssl-library.org/news/vulnerabilities/index.html" |
| 40 | + importer_name = "OpenSSL Importer" |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def steps(cls): |
| 44 | + return ( |
| 45 | + cls.collect_and_store_advisories, |
| 46 | + cls.import_new_advisories, |
| 47 | + ) |
| 48 | + |
| 49 | + # num of advisories |
| 50 | + def advisories_count(self) -> int: |
| 51 | + return fetch_count_advisories(self.root_url) |
| 52 | + |
| 53 | + # parse the response data |
| 54 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 55 | + raw_data = fetch_advisory_data(self.root_url) |
| 56 | + for data in raw_data: |
| 57 | + yield to_advisory_data(data) |
| 58 | + |
| 59 | + |
| 60 | +# fetch the html content |
| 61 | +def fetch_html_response(url): |
| 62 | + try: |
| 63 | + response = fetch_response(url).content |
| 64 | + soup = BeautifulSoup(response, "html.parser") |
| 65 | + return soup |
| 66 | + except: |
| 67 | + logger.error(f"Failed to fetch URL {url}") |
| 68 | + |
| 69 | + |
| 70 | +def fetch_count_advisories(url): |
| 71 | + soup = fetch_html_response(url) |
| 72 | + advisories = soup.find_all("h3") |
| 73 | + return len(advisories) |
| 74 | + |
| 75 | + |
| 76 | +# fetch the content from the html data |
| 77 | +def fetch_advisory_data(url): |
| 78 | + advisories = [] |
| 79 | + soup = fetch_html_response(url) |
| 80 | + # all the CVEs are h3 with id="CVE-.." |
| 81 | + for cve_section in soup.find_all("h3"): |
| 82 | + data_output = { |
| 83 | + "date_published": "", |
| 84 | + "CVE": "", |
| 85 | + "affected_packages": [], |
| 86 | + "references": [], |
| 87 | + "summary": "", |
| 88 | + "severity": "", |
| 89 | + } |
| 90 | + |
| 91 | + # CVE is in a link |
| 92 | + data_output["CVE"] = cve_section.find("a").text |
| 93 | + |
| 94 | + # the <dl> tag in this section |
| 95 | + dl = cve_section.find_next_sibling("dl") |
| 96 | + for dt, dd in zip( |
| 97 | + dl.find_all("dt"), dl.find_all("dd") |
| 98 | + ): # combines both the lists,for better iteration |
| 99 | + key = dt.text |
| 100 | + value = dd.text |
| 101 | + |
| 102 | + # Severity |
| 103 | + if key == "Severity": |
| 104 | + data_output["severity"] = value |
| 105 | + # Published Date |
| 106 | + elif key == "Published at": |
| 107 | + data_output["date_published"] = value |
| 108 | + # Affected Packages |
| 109 | + elif key == "Affected": |
| 110 | + affected_list = [li.text.strip() for li in dd.find_all("li")] |
| 111 | + data_output["affected_packages"] = affected_list |
| 112 | + # references |
| 113 | + elif key == "References": |
| 114 | + references = [a["href"] for a in dd.find_all("a")] |
| 115 | + data_output["references"] = references |
| 116 | + |
| 117 | + # for summary |
| 118 | + for sibling in dl.find_next_siblings(): |
| 119 | + if sibling.name == "h2" or sibling.name == "h3": |
| 120 | + break |
| 121 | + if sibling.name == "p": |
| 122 | + if "Issue summary:" in sibling.text: |
| 123 | + data_output["summary"] = sibling.text.strip("Issue summary:") |
| 124 | + |
| 125 | + # append all the output data to the list |
| 126 | + advisories.append(data_output) |
| 127 | + |
| 128 | + # return the list with all the advisory data |
| 129 | + return advisories |
| 130 | + |
| 131 | + |
| 132 | +""" |
| 133 | +{ |
| 134 | + 'date_published': '11 February 2025', |
| 135 | + 'CVE': 'CVE-2024-12797', |
| 136 | + 'affected_packages': [ |
| 137 | + 'from 3.4.0 before 3.4.1', |
| 138 | + 'from 3.3.0 before 3.3.3', |
| 139 | + 'from 3.2.0 before 3.2.4' |
| 140 | + ], |
| 141 | + 'references': ['https://www.cve.org/CVERecord?id=CVE-2024-12797', 'https://openssl-library.org/news/secadv/20250211.txt', 'https://github.com/openssl/openssl/commit/738d4f9fdeaad57660dcba50a619fafced3fd5e9', 'https://github.com/openssl/openssl/commit/87ebd203feffcf92ad5889df92f90bb0ee10a699', 'https://github.com/openssl/openssl/commit/798779d43494549b611233f92652f0da5328fbe7'], |
| 142 | + 'summary': 'Clients using RFC7250 Raw Public Keys (RPKs) to authenticate a\nserver may fail to notice that the server was not authenticated, because\nhandshakes don’t abort as expected when the SSL_VERIFY_PEER verification mode\nis set.', |
| 143 | + 'severity': 'High' |
| 144 | +} |
| 145 | +""" |
| 146 | + |
| 147 | + |
| 148 | +# parse the advisory data |
| 149 | +def to_advisory_data(raw_data) -> AdvisoryData: |
| 150 | + # alias |
| 151 | + aliases = [get_item(raw_data, "CVE")] |
| 152 | + |
| 153 | + # published data |
| 154 | + date_published = get_item(raw_data, "date_published") |
| 155 | + parsed_date_published = dateparser.parse(date_published, yearfirst=True).replace( |
| 156 | + tzinfo=timezone.utc |
| 157 | + ) |
| 158 | + |
| 159 | + # affected packages |
| 160 | + affected_packages = [] |
| 161 | + affected_package_out = get_item(raw_data, "affected_packages") |
| 162 | + for affected in affected_package_out: |
| 163 | + if "fips" in affected: |
| 164 | + break |
| 165 | + versions = re.findall(r"(?<=from\s)([^\s]+)|(?<=before\s)([^\s]+)", affected) |
| 166 | + versions = [v for group in versions for v in group if v] # ['1.0.1', '1.0.1j'] |
| 167 | + affected_version_range = OpensslVersionRange.from_versions(versions) |
| 168 | + affected_packages.append( |
| 169 | + AffectedPackage( |
| 170 | + package=PackageURL(type="openssl", name="openssl"), |
| 171 | + affected_version_range=affected_version_range, |
| 172 | + ) |
| 173 | + ) |
| 174 | + |
| 175 | + # Severity |
| 176 | + severity = VulnerabilitySeverity( |
| 177 | + system=SCORING_SYSTEMS["generic_textual"], value=get_item(raw_data, "severity") |
| 178 | + ) |
| 179 | + |
| 180 | + # Reference |
| 181 | + references = [] |
| 182 | + for reference in get_item(raw_data, "references"): |
| 183 | + references.append(Reference(severities=[severity], reference_id=aliases[0], url=reference)) |
| 184 | + |
| 185 | + # summary |
| 186 | + summary = get_item(raw_data, "summary") |
| 187 | + |
| 188 | + return AdvisoryData( |
| 189 | + aliases=aliases, |
| 190 | + summary=summary, |
| 191 | + affected_packages=affected_packages, |
| 192 | + references=references, |
| 193 | + date_published=parsed_date_published, |
| 194 | + url="https://openssl-library.org/news/vulnerabilities/index.html" + "#" + aliases[0], |
| 195 | + ) |
0 commit comments