|
| 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/nexB/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | +import logging |
| 10 | +import os |
| 11 | +from typing import Iterable |
| 12 | + |
| 13 | +import saneyaml |
| 14 | +from fetchcode.vcs.git import fetch_via_git |
| 15 | + |
| 16 | +from vulnerabilities.importer import AdvisoryData |
| 17 | +from vulnerabilities.importer import Importer |
| 18 | +from vulnerabilities.importers.osv import parse_advisory_data |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class OSS_FuzzImporter(Importer): |
| 24 | + license_url = "https://github.com/google/oss-fuzz-vulns/blob/main/LICENSE" |
| 25 | + spdx_license_expression = "CC-BY-4.0" |
| 26 | + url = "git+https://github.com/google/oss-fuzz-vulns" |
| 27 | + |
| 28 | + def advisory_data(self) -> Iterable[AdvisoryData]: |
| 29 | + for file in fork_and_get_files(self.url): |
| 30 | + yield parse_advisory_data(file, supported_ecosystem="oss-fuzz") |
| 31 | + |
| 32 | + |
| 33 | +class ForkError(Exception): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +def fork_and_get_files(url) -> dict: |
| 38 | + """ |
| 39 | + Fetch the github repository and go to vulns directory , |
| 40 | + then open directories one by one and return a file . |
| 41 | + """ |
| 42 | + try: |
| 43 | + fork_directory = fetch_via_git(url=url) |
| 44 | + except Exception as e: |
| 45 | + logger.error(f"Can't clone url {url}") |
| 46 | + raise ForkError(url) from e |
| 47 | + |
| 48 | + advisory_dirs = os.path.join(fork_directory.dest_dir, "vulns") |
| 49 | + for root, _, files in os.walk(advisory_dirs): |
| 50 | + for file in files: |
| 51 | + if not file.endswith(".yaml"): |
| 52 | + logger.warning(f"unsupported file {file}") |
| 53 | + else: |
| 54 | + with open(os.path.join(root, file), "r") as f: |
| 55 | + yield saneyaml.load(f.read()) |
0 commit comments