|
| 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 typing import Iterable |
| 13 | + |
| 14 | +from dateutil.parser import parse |
| 15 | +from fetchcode.vcs import fetch_via_vcs |
| 16 | +from packageurl import PackageURL |
| 17 | +from pytz import UTC |
| 18 | +from univers.version_constraint import validate_comparators |
| 19 | +from univers.version_range import GemVersionRange |
| 20 | + |
| 21 | +from vulnerabilities.importer import AdvisoryData |
| 22 | +from vulnerabilities.importer import AffectedPackageV2 |
| 23 | +from vulnerabilities.importer import ReferenceV2 |
| 24 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 25 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 26 | +from vulnerabilities.severity_systems import CVSSV2 |
| 27 | +from vulnerabilities.severity_systems import CVSSV3 |
| 28 | +from vulnerabilities.severity_systems import CVSSV4 |
| 29 | +from vulnerabilities.utils import build_description |
| 30 | +from vulnerabilities.utils import get_advisory_url |
| 31 | +from vulnerabilities.utils import load_yaml |
| 32 | + |
| 33 | +logger = logging.getLogger(__name__) |
| 34 | + |
| 35 | + |
| 36 | +class RubyImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 37 | + license_url = "https://github.com/rubysec/ruby-advisory-db/blob/master/LICENSE.txt" |
| 38 | + repo_url = "git+https://github.com/rubysec/ruby-advisory-db" |
| 39 | + importer_name = "Ruby Importer" |
| 40 | + pipeline_id = "ruby_importer_v2" |
| 41 | + spdx_license_expression = "LicenseRef-scancode-public-domain-disclaimer" |
| 42 | + notice = """ |
| 43 | + If you submit code or data to the ruby-advisory-db that is copyrighted by |
| 44 | + yourself, upon submission you hereby agree to release it into the public |
| 45 | + domain. |
| 46 | +
|
| 47 | + The data imported from the ruby-advisory-db have been filtered to exclude |
| 48 | + any non-public domain data from the data copyrighted by the Open |
| 49 | + Source Vulnerability Database (http://osvdb.org). |
| 50 | +
|
| 51 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 52 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 53 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 54 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 55 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 56 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 57 | + SOFTWARE. |
| 58 | + """ |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def steps(cls): |
| 62 | + return ( |
| 63 | + cls.clone, |
| 64 | + cls.collect_and_store_advisories, |
| 65 | + cls.clean_downloads, |
| 66 | + ) |
| 67 | + |
| 68 | + def clone(self): |
| 69 | + self.log(f"Cloning `{self.repo_url}`") |
| 70 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 71 | + |
| 72 | + def advisories_count(self): |
| 73 | + base_path = Path(self.vcs_response.dest_dir) |
| 74 | + return sum(1 for _ in base_path.rglob("*.yml")) |
| 75 | + |
| 76 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 77 | + base_path = Path(self.vcs_response.dest_dir) |
| 78 | + for file_path in base_path.rglob("*.yml"): |
| 79 | + if file_path.name.startswith("OSVDB-"): |
| 80 | + continue |
| 81 | + |
| 82 | + if "gems" in file_path.parts: |
| 83 | + subdir = "gems" |
| 84 | + elif "rubies" in file_path.parts: |
| 85 | + subdir = "rubies" |
| 86 | + else: |
| 87 | + continue |
| 88 | + |
| 89 | + raw_data = load_yaml(file_path) |
| 90 | + advisory_id = str(file_path.relative_to(base_path).with_suffix("")) |
| 91 | + advisory_url = get_advisory_url( |
| 92 | + file=file_path, |
| 93 | + base_path=base_path, |
| 94 | + url="https://github.com/rubysec/ruby-advisory-db/blob/master/", |
| 95 | + ) |
| 96 | + yield parse_ruby_advisory(advisory_id, raw_data, subdir, advisory_url) |
| 97 | + |
| 98 | + def clean_downloads(self): |
| 99 | + if self.vcs_response: |
| 100 | + self.log(f"Removing cloned repository") |
| 101 | + self.vcs_response.delete() |
| 102 | + |
| 103 | + def on_failure(self): |
| 104 | + self.clean_downloads() |
| 105 | + |
| 106 | + |
| 107 | +def parse_ruby_advisory(advisory_id, record, schema_type, advisory_url): |
| 108 | + """ |
| 109 | + Parse a ruby advisory file and return an AdvisoryData or None. |
| 110 | + Each advisory file contains the advisory information in YAML format. |
| 111 | + Schema: https://github.com/rubysec/ruby-advisory-db/tree/master/spec/schemas |
| 112 | + """ |
| 113 | + if schema_type == "gems": |
| 114 | + package_name = record.get("gem") |
| 115 | + |
| 116 | + if not package_name: |
| 117 | + logger.error("Invalid package name") |
| 118 | + return |
| 119 | + |
| 120 | + purl = PackageURL(type="gem", name=package_name) |
| 121 | + return AdvisoryData( |
| 122 | + advisory_id=advisory_id, |
| 123 | + aliases=get_aliases(record), |
| 124 | + summary=get_summary(record), |
| 125 | + affected_packages=get_affected_packages(record, purl), |
| 126 | + references_v2=get_references(record), |
| 127 | + severities=get_severities(record), |
| 128 | + date_published=get_publish_time(record), |
| 129 | + url=advisory_url, |
| 130 | + ) |
| 131 | + |
| 132 | + elif schema_type == "rubies": |
| 133 | + engine = record.get("engine") # engine enum: [jruby, rbx, ruby] |
| 134 | + if not engine: |
| 135 | + logger.error("Invalid engine name") |
| 136 | + return |
| 137 | + |
| 138 | + purl = PackageURL(type="ruby", name=engine) |
| 139 | + return AdvisoryData( |
| 140 | + advisory_id=advisory_id, |
| 141 | + aliases=get_aliases(record), |
| 142 | + summary=get_summary(record), |
| 143 | + affected_packages=get_affected_packages(record, purl), |
| 144 | + severities=get_severities(record), |
| 145 | + references=get_references(record), |
| 146 | + date_published=get_publish_time(record), |
| 147 | + url=advisory_url, |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +def get_affected_packages(record, purl): |
| 152 | + """ |
| 153 | + Return AffectedPackage objects one for each affected_version_range and invert the safe_version_ranges |
| 154 | + ( patched_versions , unaffected_versions ) then passing the purl and the inverted safe_version_range |
| 155 | + to the AffectedPackage object |
| 156 | + """ |
| 157 | + affected_packages = [] |
| 158 | + for unaffected_version in record.get("unaffected_versions", []): |
| 159 | + try: |
| 160 | + affected_version_range = GemVersionRange.from_native(unaffected_version).invert() |
| 161 | + validate_comparators(affected_version_range.constraints) |
| 162 | + affected_packages.append( |
| 163 | + AffectedPackageV2( |
| 164 | + package=purl, |
| 165 | + affected_version_range=affected_version_range, |
| 166 | + fixed_version_range=None, |
| 167 | + ) |
| 168 | + ) |
| 169 | + except ValueError as e: |
| 170 | + logger.error( |
| 171 | + f"Invalid VersionRange Constraints for unaffected_version: {unaffected_version} - error: {e}" |
| 172 | + ) |
| 173 | + |
| 174 | + for patched_version in record.get("patched_versions", []): |
| 175 | + try: |
| 176 | + fixed_version_range = GemVersionRange.from_native(patched_version) |
| 177 | + validate_comparators(fixed_version_range.constraints) |
| 178 | + affected_packages.append( |
| 179 | + AffectedPackageV2( |
| 180 | + package=purl, |
| 181 | + affected_version_range=None, |
| 182 | + fixed_version_range=fixed_version_range, |
| 183 | + ) |
| 184 | + ) |
| 185 | + except ValueError as e: |
| 186 | + logger.error( |
| 187 | + f"Invalid VersionRange Constraints for patched_version: {patched_version} - error: {e}" |
| 188 | + ) |
| 189 | + |
| 190 | + return affected_packages |
| 191 | + |
| 192 | + |
| 193 | +def get_aliases(record) -> [str]: |
| 194 | + aliases = [] |
| 195 | + if record.get("cve"): |
| 196 | + aliases.append("CVE-{}".format(record.get("cve"))) |
| 197 | + if record.get("osvdb"): |
| 198 | + aliases.append("OSV-{}".format(record.get("osvdb"))) |
| 199 | + if record.get("ghsa"): |
| 200 | + aliases.append("GHSA-{}".format(record.get("ghsa"))) |
| 201 | + return aliases |
| 202 | + |
| 203 | + |
| 204 | +def get_references(record) -> [ReferenceV2]: |
| 205 | + references = [] |
| 206 | + if record.get("url"): |
| 207 | + references.append( |
| 208 | + ReferenceV2( |
| 209 | + url=record.get("url"), |
| 210 | + ) |
| 211 | + ) |
| 212 | + return references |
| 213 | + |
| 214 | + |
| 215 | +def get_severities(record): |
| 216 | + """ |
| 217 | + Extract CVSS severity and return a list of VulnerabilitySeverity objects |
| 218 | + """ |
| 219 | + |
| 220 | + severities = [] |
| 221 | + cvss_v4 = record.get("cvss_v4") |
| 222 | + if cvss_v4: |
| 223 | + severities.append( |
| 224 | + VulnerabilitySeverity(system=CVSSV4, value=cvss_v4), |
| 225 | + ) |
| 226 | + |
| 227 | + cvss_v3 = record.get("cvss_v3") |
| 228 | + if cvss_v3: |
| 229 | + severities.append(VulnerabilitySeverity(system=CVSSV3, value=cvss_v3)) |
| 230 | + |
| 231 | + cvss_v2 = record.get("cvss_v2") |
| 232 | + if cvss_v2: |
| 233 | + severities.append(VulnerabilitySeverity(system=CVSSV2, value=cvss_v2)) |
| 234 | + |
| 235 | + return severities |
| 236 | + |
| 237 | + |
| 238 | +def get_publish_time(record): |
| 239 | + date = record.get("date") |
| 240 | + if not date: |
| 241 | + return |
| 242 | + return parse(date).replace(tzinfo=UTC) |
| 243 | + |
| 244 | + |
| 245 | +def get_summary(record): |
| 246 | + title = record.get("title") or "" |
| 247 | + description = record.get("description") or "" |
| 248 | + return build_description(summary=title, description=description) |
0 commit comments