|
| 1 | +# |
| 2 | +# |
| 3 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 4 | +# VulnerableCode is a trademark of nexB Inc. |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 7 | +# See https://github.com/nexB/vulnerablecode for support or download. |
| 8 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 9 | +# |
| 10 | + |
| 11 | +import logging |
| 12 | +from datetime import datetime |
| 13 | +from typing import Any |
| 14 | +from typing import Iterable |
| 15 | +from typing import List |
| 16 | +from typing import Mapping |
| 17 | +from typing import Optional |
| 18 | +from urllib.parse import urljoin |
| 19 | + |
| 20 | +import pytz |
| 21 | +from bs4 import BeautifulSoup |
| 22 | +from packageurl import PackageURL |
| 23 | +from univers.version_range import RpmVersionRange |
| 24 | + |
| 25 | +from vulnerabilities.importer import AdvisoryData |
| 26 | +from vulnerabilities.importer import AffectedPackage |
| 27 | +from vulnerabilities.importer import Importer |
| 28 | +from vulnerabilities.importer import Reference |
| 29 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 30 | +from vulnerabilities.references import WireSharkReference |
| 31 | +from vulnerabilities.references import XsaReference |
| 32 | +from vulnerabilities.references import ZbxReference |
| 33 | +from vulnerabilities.severity_systems import SCORING_SYSTEMS |
| 34 | +from vulnerabilities.utils import fetch_response |
| 35 | +from vulnerabilities.utils import is_cve |
| 36 | + |
| 37 | +LOGGER = logging.getLogger(__name__) |
| 38 | +BASE_URL = "https://alas.aws.amazon.com/" |
| 39 | +other_url = "https://explore.alas.aws.amazon.com/{cve_id.json}" # use this in the url in code to get details for the specific cve. |
| 40 | + |
| 41 | + |
| 42 | +class AmazonLinuxImporter(Importer): |
| 43 | + spdx_license_expression = "CC BY 4.0" # check if this is correct |
| 44 | + license_url = " " # todo |
| 45 | + |
| 46 | + importer_name = "Amazon Linux Importer" |
| 47 | + |
| 48 | + def advisory_data(self) -> Iterable[AdvisoryData]: |
| 49 | + amazon_linux_1_url = BASE_URL + "/index.html" |
| 50 | + amazon_linux_2_url = BASE_URL + "/alas2.html" |
| 51 | + amazon_linux_2023_url = BASE_URL + "/alas2023.html" |
| 52 | + amazonlinux_advisories_pages = [ |
| 53 | + amazon_linux_1_url, |
| 54 | + amazon_linux_2_url, |
| 55 | + amazon_linux_2023_url, |
| 56 | + ] |
| 57 | + alas_dict = {} |
| 58 | + for amazonlinux_advisories_page in amazonlinux_advisories_pages: |
| 59 | + alas_dict.update(fetch_alas_id_and_advisory_links(amazonlinux_advisories_page)) |
| 60 | + |
| 61 | + for alas_id, alas_url in alas_dict.items(): |
| 62 | + # It iterates through alas_dict to get alas ids and alas url |
| 63 | + if alas_id and alas_url: |
| 64 | + alas_advisory_page_content = fetch_response(alas_url).content |
| 65 | + yield process_advisory_data(alas_id, alas_advisory_page_content, alas_url) |
| 66 | + |
| 67 | + |
| 68 | +def fetch_alas_id_and_advisory_links(page_url: str) -> dict[str, str]: |
| 69 | + """ |
| 70 | + Return a dictionary where 'ALAS' entries are the keys and |
| 71 | + their corresponding advisory page links are the values. |
| 72 | + """ |
| 73 | + |
| 74 | + page_response_content = fetch_response(page_url).content |
| 75 | + # Parse the HTML content |
| 76 | + soup = BeautifulSoup(page_response_content, "html.parser") |
| 77 | + alas_dict = {} |
| 78 | + |
| 79 | + if page_url == "https://alas.aws.amazon.com/index.html": |
| 80 | + # Find all relevant ALAS links and their IDs |
| 81 | + for row in soup.find_all("tr", id=True): |
| 82 | + alas_id = row["id"] |
| 83 | + link_tag = row.find("a", href=True) |
| 84 | + if link_tag: |
| 85 | + full_url = "https://alas.aws.amazon.com/" + link_tag["href"] |
| 86 | + alas_dict[alas_id] = full_url |
| 87 | + |
| 88 | + elif page_url == "https://alas.aws.amazon.com/alas2.html": |
| 89 | + # Find all relevant ALAS links and their IDs |
| 90 | + for row in soup.find_all("tr", id=True): |
| 91 | + alas_id = row["id"] |
| 92 | + link_tag = row.find("a", href=True) |
| 93 | + if link_tag: |
| 94 | + full_url = "https://alas.aws.amazon.com/AL2" + link_tag["href"] |
| 95 | + alas_dict[alas_id] = full_url |
| 96 | + |
| 97 | + else: |
| 98 | + # Find all relevant ALAS links and their IDs |
| 99 | + for row in soup.find_all("tr", id=True): |
| 100 | + alas_id = row["id"] |
| 101 | + link_tag = row.find("a", href=True) |
| 102 | + if link_tag: |
| 103 | + full_url = "https://alas.aws.amazon.com/AL2023/" + link_tag["href"] |
| 104 | + alas_dict[alas_id] = full_url |
| 105 | + return alas_dict |
| 106 | + |
| 107 | + |
| 108 | +def process_advisory_data(alas_id, alas_advisory_page_content, alas_url) -> Optional[AdvisoryData]: |
| 109 | + |
| 110 | + soup = BeautifulSoup(alas_advisory_page_content, "html.parser") |
| 111 | + aliases = [] |
| 112 | + aliases.append(alas_id) |
| 113 | + |
| 114 | + # Find the advisory release date |
| 115 | + release_date_span = next( |
| 116 | + ( |
| 117 | + span |
| 118 | + for span in soup.find_all("span", class_="alas-info") |
| 119 | + if "Advisory Release Date:" in span.get_text(strip=True) |
| 120 | + ), |
| 121 | + None, |
| 122 | + ) |
| 123 | + |
| 124 | + release_date = ( |
| 125 | + release_date_span.get_text(strip=True).split(":", 1)[1].strip() |
| 126 | + if release_date_span |
| 127 | + else None |
| 128 | + ) |
| 129 | + date_published = get_date_published(release_date) |
| 130 | + |
| 131 | + # Extract Issue Overview (all points of issue overviews texts) |
| 132 | + issue_overview = [] |
| 133 | + for p in soup.find("div", id="issue_overview").find_all("p"): |
| 134 | + issue_overview.append(p.text.strip()) |
| 135 | + summary = create_summary(issue_overview) |
| 136 | + |
| 137 | + # Extract Affected Packages (list of strings) |
| 138 | + processed_affected_packages = [] |
| 139 | + affected_packages_section = soup.find("div", id="affected_packages") |
| 140 | + if affected_packages_section: |
| 141 | + affected_packages = affected_packages_section.find_all("p") |
| 142 | + affected_packages = [pkg.text.strip() for pkg in affected_packages] |
| 143 | + |
| 144 | + # getting new packages |
| 145 | + new_packages_div = soup.find("div", id="new_packages") |
| 146 | + |
| 147 | + # Extract the text elements between <br /> tags within this div |
| 148 | + if new_packages_div: |
| 149 | + new_packages_list = [ |
| 150 | + element.strip() for element in new_packages_div.pre.stripped_strings if element.strip() |
| 151 | + ] |
| 152 | + else: |
| 153 | + new_packages_list = [] |
| 154 | + |
| 155 | + for package in affected_packages: |
| 156 | + purl = PackageURL(type="rpm", namespace="alas.aws.amazon", name=package) |
| 157 | + # fixed_version = get_fixed_versions(new_packages_list) |
| 158 | + processed_affected_packages.append( |
| 159 | + AffectedPackage(package=purl, affected_version_range=None, fixed_version=None) |
| 160 | + ) |
| 161 | + |
| 162 | + cve_list = [] |
| 163 | + for link in soup.find("div", id="references").find_all("a", href=True): |
| 164 | + if "CVE-" in link.text: |
| 165 | + cve_list.append((link.text.strip(), "https://alas.aws.amazon.com" + link["href"])) |
| 166 | + |
| 167 | + references: List[Reference] = [] |
| 168 | + for cve_id, cve_url in cve_list: |
| 169 | + cve_json_url = f"https://explore.alas.aws.amazon.com/{cve_id}" |
| 170 | + response = fetch_response(cve_json_url) |
| 171 | + |
| 172 | + # Parse the JSON data |
| 173 | + cve_info = response.json() |
| 174 | + severity_scores = cve_info.get("scores", []) |
| 175 | + severity = [] |
| 176 | + for score in severity_scores: |
| 177 | + severity.append( |
| 178 | + VulnerabilitySeverity( |
| 179 | + system=SCORING_SYSTEMS[score.get("type", "").lower()], |
| 180 | + value=score.get("score", ""), |
| 181 | + scoring_elements=score.get("vector", ""), |
| 182 | + ) |
| 183 | + ) |
| 184 | + references.append(Reference(reference_id=cve_id, url=cve_url, severities=severity)) |
| 185 | + |
| 186 | + url = alas_url |
| 187 | + |
| 188 | + return AdvisoryData( |
| 189 | + aliases=aliases, |
| 190 | + date_published=date_published, |
| 191 | + summary=summary, |
| 192 | + references=references, |
| 193 | + affected_packages=processed_affected_packages, |
| 194 | + url=url, |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +def get_date_published(release_date_string): |
| 199 | + |
| 200 | + # Parse the date and time |
| 201 | + date_part = release_date_string[:16] |
| 202 | + time_zone = release_date_string[17:] |
| 203 | + |
| 204 | + # Convert to datetime object (naive) |
| 205 | + naive_date = datetime.strptime(date_part, "%Y-%m-%d %H:%M") |
| 206 | + |
| 207 | + # Convert to aware datetime by adding the Pacific time zone |
| 208 | + timezone = pytz.timezone("America/Los_Angeles") |
| 209 | + date_published = timezone.localize(naive_date) |
| 210 | + return date_published |
| 211 | + |
| 212 | + |
| 213 | +def create_summary(summary_point: List): |
| 214 | + summary = ". ".join(summary_point) |
| 215 | + |
| 216 | + # Add a period at the end if the final sentence doesn't end with one |
| 217 | + if not summary.endswith("."): |
| 218 | + summary += "." |
| 219 | + return summary |
0 commit comments