|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +import asyncio |
| 24 | +import dataclasses |
| 25 | +import re |
| 26 | + |
| 27 | + |
| 28 | +import requests |
| 29 | +from bs4 import BeautifulSoup |
| 30 | +from dephell_specifier import RangeSpecifier |
| 31 | +from packageurl import PackageURL |
| 32 | + |
| 33 | +from vulnerabilities.data_source import Advisory |
| 34 | +from vulnerabilities.data_source import DataSource |
| 35 | +from vulnerabilities.data_source import DataSourceConfiguration |
| 36 | +from vulnerabilities.data_source import Reference |
| 37 | +from vulnerabilities.helpers import create_etag |
| 38 | +from vulnerabilities.package_managers import MavenVersionAPI |
| 39 | + |
| 40 | + |
| 41 | +@dataclasses.dataclass |
| 42 | +class ApacheTomcatDataSourceConfiguration(DataSourceConfiguration): |
| 43 | + etags: dict |
| 44 | + |
| 45 | + |
| 46 | +class ApacheTomcatDataSource(DataSource): |
| 47 | + |
| 48 | + CONFIG_CLASS = ApacheTomcatDataSourceConfiguration |
| 49 | + base_url = "https://tomcat.apache.org/security-{}" |
| 50 | + |
| 51 | + def __init__(self, *args, **kwargs): |
| 52 | + super().__init__(*args, **kwargs) |
| 53 | + self.version_api = MavenVersionAPI() |
| 54 | + asyncio.run(self.version_api.load_api({"org.apache.tomcat:tomcat"})) |
| 55 | + |
| 56 | + def updated_advisories(self): |
| 57 | + advisories = [] |
| 58 | + for advisory_page in self.fetch_pages(): |
| 59 | + advisories.extend(self.to_advisories(advisory_page)) |
| 60 | + return self.batch_advisories(advisories) |
| 61 | + |
| 62 | + def fetch_pages(self): |
| 63 | + tomcat_major_versions = {i[0] for i in self.version_api.get("org.apache.tomcat:tomcat")} |
| 64 | + for version in tomcat_major_versions: |
| 65 | + page_url = self.base_url.format(version) |
| 66 | + if create_etag(self, page_url, "ETag"): |
| 67 | + yield requests.get(page_url).content |
| 68 | + |
| 69 | + def to_advisories(self, apache_tomcat_advisory_html): |
| 70 | + advisories = [] |
| 71 | + page_soup = BeautifulSoup(apache_tomcat_advisory_html, features="lxml") |
| 72 | + pageh3s = page_soup.find_all("h3") |
| 73 | + vuln_headings = [i for i in pageh3s if "Fixed in Apache Tomcat" in i.text] |
| 74 | + for data in vuln_headings: |
| 75 | + fixed_version = data.text.split("Fixed in Apache Tomcat")[-1].strip() |
| 76 | + details_div = data.find_next_sibling() |
| 77 | + |
| 78 | + for anchor_tag in details_div.find_all("a"): |
| 79 | + if "cve.mitre.org" not in anchor_tag["href"]: |
| 80 | + continue |
| 81 | + |
| 82 | + cve_id = re.search(r"CVE-\d*-\d*", anchor_tag.text).group() |
| 83 | + references = [] |
| 84 | + affected_packages = [] |
| 85 | + paragraph = anchor_tag.find_parent() |
| 86 | + |
| 87 | + while paragraph and "Affects:" not in paragraph.text: |
| 88 | + for ref in paragraph.find_all("a"): |
| 89 | + references.append(Reference(url=ref["href"])) |
| 90 | + |
| 91 | + paragraph = paragraph.find_next_sibling() |
| 92 | + |
| 93 | + if not paragraph: |
| 94 | + # At the end of details_div |
| 95 | + continue |
| 96 | + |
| 97 | + for version_range in parse_version_ranges(paragraph.text): |
| 98 | + affected_packages.extend( |
| 99 | + [ |
| 100 | + PackageURL( |
| 101 | + type="maven", namespace="apache", name="tomcat", version=version |
| 102 | + ) |
| 103 | + for version in self.version_api.get("org.apache.tomcat:tomcat") |
| 104 | + if version in version_range |
| 105 | + ] |
| 106 | + ) |
| 107 | + |
| 108 | + fixed_package = [ |
| 109 | + PackageURL( |
| 110 | + type="maven", namespace="apache", name="tomcat", version=fixed_version |
| 111 | + ) |
| 112 | + ] |
| 113 | + |
| 114 | + advisories.append( |
| 115 | + Advisory( |
| 116 | + summary="", |
| 117 | + impacted_package_urls=affected_packages, |
| 118 | + resolved_package_urls=fixed_package, |
| 119 | + cve_id=cve_id, |
| 120 | + vuln_references=references, |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + return advisories |
| 125 | + |
| 126 | + |
| 127 | +def parse_version_ranges(string): |
| 128 | + """ |
| 129 | + This method yields Rangespecifier objects obtained by |
| 130 | + parsing `string`. |
| 131 | + >> list(parse_version_ranges("Affects: 9.0.0.M1 to 9.0.0.M9")) |
| 132 | + [RangeSpecifier(<=9.0.0.M9,>=9.0.0.M1)] |
| 133 | +
|
| 134 | + >> list(parse_version_ranges("Affects: 9.0.0.M1")) |
| 135 | + [RangeSpecifier(>=9.0.0.M1<=9.0.0.M1)] |
| 136 | +
|
| 137 | + >> list(parse_version_ranges("Affects: 9.0.0.M1 to 9.0.0.M9, 1.2.3 to 3.4.5")) |
| 138 | + [RangeSpecifier(<=9.0.0.M9,>=9.0.0.M1), RangeSpecifier(<=3.4.5,>=1.2.3)] |
| 139 | + """ |
| 140 | + version_rng_txt = string.split("Affects:")[-1].strip() |
| 141 | + version_ranges = version_rng_txt.split(",") |
| 142 | + for version_range in version_ranges: |
| 143 | + if "to" in version_range: |
| 144 | + lower_bound, upper_bound = version_range.split("to") |
| 145 | + elif "-" in version_range and not any([i.isalpha() for i in version_range]): |
| 146 | + lower_bound, upper_bound = version_range.split("-") |
| 147 | + else: |
| 148 | + lower_bound = upper_bound = version_range |
| 149 | + |
| 150 | + yield RangeSpecifier(">=" + lower_bound + "<=" + upper_bound) |
0 commit comments