|
| 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 | +import logging |
| 10 | +from pathlib import Path |
| 11 | +from typing import Iterable |
| 12 | + |
| 13 | +import saneyaml |
| 14 | + |
| 15 | +from vulnerabilities.importer import AdvisoryData |
| 16 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 17 | +from vulnerabilities.utils import get_advisory_url |
| 18 | +from fetchcode.vcs import fetch_via_vcs |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class OSSFuzzImporterPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 24 | + pipeline_id = "oss_fuzz_importer_v2" |
| 25 | + spdx_license_expression = "CC-BY-4.0" |
| 26 | + license_url = "https://github.com/google/oss-fuzz-vulns/blob/main/LICENSE" |
| 27 | + repo_url = "git+https://github.com/google/oss-fuzz-vulns" |
| 28 | + unfurl_version_ranges = True |
| 29 | + |
| 30 | + @classmethod |
| 31 | + def steps(cls): |
| 32 | + return ( |
| 33 | + cls.clone, |
| 34 | + cls.collect_and_store_advisories, |
| 35 | + cls.clean_downloads, |
| 36 | + ) |
| 37 | + |
| 38 | + def clone(self): |
| 39 | + self.log(f"Cloning `{self.repo_url}`") |
| 40 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 41 | + |
| 42 | + def advisories_count(self): |
| 43 | + vulns_directory = Path(self.vcs_response.dest_dir) / "vulns" |
| 44 | + return sum(1 for _ in vulns_directory.rglob("*.yaml")) |
| 45 | + |
| 46 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 47 | + from vulnerabilities.importers.osv import parse_advisory_data_v2 |
| 48 | + |
| 49 | + base_directory = Path(self.vcs_response.dest_dir) |
| 50 | + vulns_directory = base_directory / "vulns" |
| 51 | + |
| 52 | + for advisory in vulns_directory.rglob("*.yaml"): |
| 53 | + advisory_url = get_advisory_url( |
| 54 | + file=advisory, |
| 55 | + base_path=base_directory, |
| 56 | + url="https://github.com/google/oss-fuzz-vulns/blob/main/", |
| 57 | + ) |
| 58 | + advisory_text = advisory.read_text() |
| 59 | + advisory_dict = saneyaml.load(advisory_text) |
| 60 | + yield parse_advisory_data_v2( |
| 61 | + raw_data=advisory_dict, |
| 62 | + supported_ecosystems=["generic"], |
| 63 | + advisory_url=advisory_url, |
| 64 | + advisory_text=advisory_text, |
| 65 | + ) |
| 66 | + |
| 67 | + def clean_downloads(self): |
| 68 | + if self.vcs_response: |
| 69 | + self.log(f"Removing cloned repository") |
| 70 | + self.vcs_response.delete() |
| 71 | + |
| 72 | + def on_failure(self): |
| 73 | + self.clean_downloads() |
0 commit comments