|
| 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 csv |
| 11 | +from pathlib import Path |
| 12 | +from typing import Iterable |
| 13 | + |
| 14 | +from fetchcode.vcs import fetch_via_vcs |
| 15 | + |
| 16 | +from vulnerabilities.importer import AdvisoryData |
| 17 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 18 | +from vulnerabilities.pipes.advisory import append_patch_classifications |
| 19 | + |
| 20 | + |
| 21 | +class ProjectKBMSR2019Pipeline(VulnerableCodeBaseImporterPipelineV2): |
| 22 | + """ |
| 23 | + ProjectKB Importer Pipeline |
| 24 | + Collect advisory from ProjectKB data: |
| 25 | + - CSV database https://github.com/SAP/project-kb/blob/main/MSR2019/dataset/vulas_db_msr2019_release.csv |
| 26 | + """ |
| 27 | + |
| 28 | + pipeline_id = "project-kb-MSR-2019_v2" |
| 29 | + spdx_license_expression = "Apache-2.0" |
| 30 | + license_url = "https://github.com/SAP/project-kb/blob/main/LICENSE.txt" |
| 31 | + repo_url = "git+https://github.com/SAP/project-kb" |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def steps(cls): |
| 35 | + return (cls.clone_repo, cls.collect_and_store_advisories, cls.clean_downloads) |
| 36 | + |
| 37 | + def clone_repo(self): |
| 38 | + self.log("Cloning ProjectKB advisory data...") |
| 39 | + self.vcs_response = fetch_via_vcs(self.repo_url) |
| 40 | + |
| 41 | + def advisories_count(self): |
| 42 | + csv_path = Path(self.vcs_response.dest_dir) / "MSR2019/dataset/vulas_db_msr2019_release.csv" |
| 43 | + |
| 44 | + with open(csv_path, newline="", encoding="utf-8") as f: |
| 45 | + reader = csv.reader(f) |
| 46 | + next(reader, None) |
| 47 | + count = sum(1 for _ in reader) |
| 48 | + |
| 49 | + self.log(f"Estimated advisories to process: {count}") |
| 50 | + return count |
| 51 | + |
| 52 | + def collect_advisories(self) -> Iterable[AdvisoryData]: |
| 53 | + self.log("Collecting fix commits from ProjectKB ( vulas_db_msr2019_release )...") |
| 54 | + csv_path = Path(self.vcs_response.dest_dir) / "MSR2019/dataset/vulas_db_msr2019_release.csv" |
| 55 | + |
| 56 | + with open(csv_path, newline="", encoding="utf-8") as f: |
| 57 | + reader = csv.reader(f) |
| 58 | + next(reader, None) # skip header |
| 59 | + rows = [r for r in reader if len(r) == 4 and r[0]] # vuln_id, vcs_url, commit_hash, poc |
| 60 | + |
| 61 | + for vuln_id, vcs_url, commit_hash, _ in rows: |
| 62 | + if not vuln_id or not vcs_url or not commit_hash: |
| 63 | + continue |
| 64 | + |
| 65 | + patches = [] |
| 66 | + affected_packages = [] |
| 67 | + references = [] |
| 68 | + append_patch_classifications( |
| 69 | + url=vcs_url, |
| 70 | + commit_hash=commit_hash, |
| 71 | + patch_text=None, |
| 72 | + affected_packages=affected_packages, |
| 73 | + references=references, |
| 74 | + patches=patches, |
| 75 | + ) |
| 76 | + |
| 77 | + yield AdvisoryData( |
| 78 | + advisory_id=vuln_id, |
| 79 | + affected_packages=affected_packages, |
| 80 | + patches=patches, |
| 81 | + references_v2=references, |
| 82 | + url="https://github.com/SAP/project-kb/blob/main/MSR2019/dataset/vulas_db_msr2019_release.csv", |
| 83 | + ) |
| 84 | + |
| 85 | + def clean_downloads(self): |
| 86 | + """Remove the cloned repository from disk.""" |
| 87 | + self.log("Removing cloned repository...") |
| 88 | + if self.vcs_response: |
| 89 | + self.vcs_response.delete() |
| 90 | + |
| 91 | + def on_failure(self): |
| 92 | + """Ensure cleanup happens on pipeline failure.""" |
| 93 | + self.clean_downloads() |
0 commit comments