|
| 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 re |
| 25 | +from typing import List, Set |
| 26 | + |
| 27 | +import yaml |
| 28 | + |
| 29 | +from dephell_specifier import RangeSpecifier |
| 30 | +from packageurl import PackageURL |
| 31 | +from vulnerabilities.data_source import Advisory, GitDataSource, Reference |
| 32 | +from vulnerabilities.package_managers import GitHubTagsAPI |
| 33 | + |
| 34 | + |
| 35 | +class IstioDataSource(GitDataSource): |
| 36 | + def __enter__(self): |
| 37 | + super(IstioDataSource, self).__enter__() |
| 38 | + |
| 39 | + if not getattr(self, "_added_files", None): |
| 40 | + self._added_files, self._updated_files = self.file_changes( |
| 41 | + recursive=True, file_ext="md", subdir="./content/en/news/security" |
| 42 | + ) |
| 43 | + self.version_api = GitHubTagsAPI() |
| 44 | + self.set_api() |
| 45 | + |
| 46 | + def set_api(self): |
| 47 | + asyncio.run(self.version_api.load_api(["istio/istio"])) |
| 48 | + |
| 49 | + def updated_advisories(self) -> Set[Advisory]: |
| 50 | + files = self._updated_files |
| 51 | + advisories = [] |
| 52 | + for f in files: |
| 53 | + processed_data = self.process_file(f) |
| 54 | + if processed_data: |
| 55 | + advisories.extend(processed_data) |
| 56 | + return self.batch_advisories(advisories) |
| 57 | + |
| 58 | + def get_pkg_versions_from_ranges(self, version_range_list): |
| 59 | + """Takes a list of version ranges(affected) of a package |
| 60 | + as parameter and returns a tuple of safe package versions and |
| 61 | + vulnerable package versions""" |
| 62 | + all_version = self.version_api.get("istio/istio") |
| 63 | + safe_pkg_versions = [] |
| 64 | + vuln_pkg_versions = [] |
| 65 | + version_ranges = [RangeSpecifier(r) for r in version_range_list] |
| 66 | + for version in all_version: |
| 67 | + if any([version in v for v in version_ranges]): |
| 68 | + vuln_pkg_versions.append(version) |
| 69 | + |
| 70 | + safe_pkg_versions = set(all_version) - set(vuln_pkg_versions) |
| 71 | + return safe_pkg_versions, vuln_pkg_versions |
| 72 | + |
| 73 | + def get_data_from_yaml_lines(self, yaml_lines): |
| 74 | + """Return a mapping of data from a iterable of yaml_lines |
| 75 | + for example : |
| 76 | + ['title: ISTIO-SECURITY-2019-001', |
| 77 | + 'description: Incorrect access control.','cves: [CVE-2019-12243]'] |
| 78 | +
|
| 79 | + would give {'title':'ISTIO-SECURITY-2019-001', |
| 80 | + 'description': 'Incorrect access control.', |
| 81 | + 'cves': '[CVE-2019-12243]'} |
| 82 | + """ |
| 83 | + |
| 84 | + return yaml.safe_load("\n".join(yaml_lines)) |
| 85 | + |
| 86 | + def get_yaml_lines(self, lines): |
| 87 | + """The istio advisory file contains lines similar to yaml format . |
| 88 | + This function extracts those lines and return an iterable of lines |
| 89 | +
|
| 90 | + for example : |
| 91 | + lines = |
| 92 | + --- |
| 93 | + title: ISTIO-SECURITY-2019-001 |
| 94 | + description: Incorrect access control. |
| 95 | + cves: [CVE-2019-12243] |
| 96 | + --- |
| 97 | +
|
| 98 | + get_yaml_lines(lines) would return |
| 99 | + ['title: ISTIO-SECURITY-2019-001','description: Incorrect access control.' |
| 100 | + ,'cves: [CVE-2019-12243]'] |
| 101 | + """ |
| 102 | + |
| 103 | + for index, line in enumerate(lines): |
| 104 | + line = line.strip() |
| 105 | + if line.startswith("---") and index == 0: |
| 106 | + continue |
| 107 | + elif line.endswith("---"): |
| 108 | + break |
| 109 | + else: |
| 110 | + yield line |
| 111 | + |
| 112 | + def process_file(self, path): |
| 113 | + |
| 114 | + advisories = [] |
| 115 | + |
| 116 | + data = self.get_data_from_md(path) |
| 117 | + |
| 118 | + releases = [] |
| 119 | + if data.get("releases"): |
| 120 | + for release in data["releases"]: |
| 121 | + # If it is of form "All versions prior to x" |
| 122 | + if "All releases" in release: |
| 123 | + release = release.strip() |
| 124 | + release = release.split(" ") |
| 125 | + releases.append("<" + release[4]) |
| 126 | + # If it is of form "a to b" |
| 127 | + elif "to" in release: |
| 128 | + release = release.strip() |
| 129 | + release = release.split(" ") |
| 130 | + lbound = ">=" + release[0] |
| 131 | + ubound = "<=" + release[2] |
| 132 | + releases.append(lbound + "," + ubound) |
| 133 | + # If it is a single release |
| 134 | + elif is_release(release): |
| 135 | + releases.append(release) |
| 136 | + |
| 137 | + data["release_ranges"] = releases |
| 138 | + |
| 139 | + if not data.get("cves"): |
| 140 | + data["cves"] = [""] |
| 141 | + |
| 142 | + for cve_id in data["cves"]: |
| 143 | + |
| 144 | + if not cve_id.startswith("CVE"): |
| 145 | + cve_id = "" |
| 146 | + |
| 147 | + safe_pkg_versions = [] |
| 148 | + vuln_pkg_versions = [] |
| 149 | + |
| 150 | + if not data.get("release_ranges"): |
| 151 | + data["release_ranges"] = [] |
| 152 | + |
| 153 | + safe_pkg_versions, vuln_pkg_versions = self.get_pkg_versions_from_ranges( |
| 154 | + data["release_ranges"] |
| 155 | + ) |
| 156 | + |
| 157 | + safe_purls_golang = { |
| 158 | + PackageURL(type="golang", name="istio", version=version) |
| 159 | + for version in safe_pkg_versions |
| 160 | + } |
| 161 | + |
| 162 | + safe_purls_github = { |
| 163 | + PackageURL(type="github", name="istio", version=version) |
| 164 | + for version in safe_pkg_versions |
| 165 | + } |
| 166 | + safe_purls = safe_purls_github.union(safe_purls_golang) |
| 167 | + |
| 168 | + vuln_purls_golang = { |
| 169 | + PackageURL(type="golang", name="istio", version=version) |
| 170 | + for version in vuln_pkg_versions |
| 171 | + } |
| 172 | + |
| 173 | + vuln_purls_github = { |
| 174 | + PackageURL(type="github", name="istio", version=version) |
| 175 | + for version in vuln_pkg_versions |
| 176 | + } |
| 177 | + vuln_purls = vuln_purls_github.union(vuln_purls_golang) |
| 178 | + |
| 179 | + advisories.append( |
| 180 | + Advisory( |
| 181 | + summary=data["description"], |
| 182 | + impacted_package_urls=vuln_purls, |
| 183 | + resolved_package_urls=safe_purls, |
| 184 | + vulnerability_id=cve_id, |
| 185 | + ) |
| 186 | + ) |
| 187 | + |
| 188 | + return advisories |
| 189 | + |
| 190 | + def get_data_from_md(self, path): |
| 191 | + """Return a mapping of vulnerability data from istio . The data is |
| 192 | + in the form of yaml_lines inside a .md file. |
| 193 | + """ |
| 194 | + |
| 195 | + with open(path) as f: |
| 196 | + yaml_lines = self.get_yaml_lines(f) |
| 197 | + return self.get_data_from_yaml_lines(yaml_lines) |
| 198 | + |
| 199 | + is_release = re.compile(r"^[\d.]+$", re.IGNORECASE).match |
0 commit comments