|
| 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 json |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +from fetchcode.vcs import fetch_via_vcs |
| 13 | + |
| 14 | +from vulnerabilities.models import AdvisoryV2 |
| 15 | +from vulnerabilities.models import CodeFixV2 |
| 16 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 17 | + |
| 18 | + |
| 19 | +class CollectFixCommitsAospDatasetPipeline(VulnerableCodePipeline): |
| 20 | + """ |
| 21 | + Pipeline to collect fix commits from Aosp Dataset: |
| 22 | + """ |
| 23 | + |
| 24 | + pipeline_id = "aosp_dataset_fix_commits" |
| 25 | + spdx_license_expression = "Apache-2.0" |
| 26 | + license_url = "https://github.com/quarkslab/aosp_dataset/blob/master/LICENSE" |
| 27 | + importer_name = "aosp_dataset" |
| 28 | + qualified_name = "aosp_dataset_fix_commits" |
| 29 | + repo_url = "git+https://github.com/quarkslab/aosp_dataset" |
| 30 | + |
| 31 | + @classmethod |
| 32 | + def steps(cls): |
| 33 | + return ( |
| 34 | + cls.clone, |
| 35 | + cls.collect_fix_commits, |
| 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 collect_fix_commits(self): |
| 43 | + self.log(f"Processing aosp_dataset fix commits.") |
| 44 | + base_path = Path(self.vcs_response.dest_dir) / "cves" |
| 45 | + for file_path in base_path.rglob("*.json"): |
| 46 | + if not file_path.name.startswith("CVE-"): |
| 47 | + continue |
| 48 | + |
| 49 | + with open(file_path) as f: |
| 50 | + vulnerability_data = json.load(f) |
| 51 | + |
| 52 | + vulnerability_id = vulnerability_data.get("cveId") |
| 53 | + if not vulnerability_id: |
| 54 | + continue |
| 55 | + |
| 56 | + try: |
| 57 | + advisories = AdvisoryV2.objects.filter(advisory_id__iendswith=vulnerability_id) |
| 58 | + except AdvisoryV2.DoesNotExist: |
| 59 | + self.log(f"Can't find vulnerability_id: {vulnerability_id}") |
| 60 | + continue |
| 61 | + |
| 62 | + for advisory in advisories: |
| 63 | + for commit_data in vulnerability_data.get("fixes", []): |
| 64 | + vcs_url = commit_data.get("patchUrl") |
| 65 | + for impact in advisory.impacted_packages.all(): |
| 66 | + for package in impact.affecting_packages.all(): |
| 67 | + code_fix, created = CodeFixV2.objects.get_or_create( |
| 68 | + commits=[vcs_url], |
| 69 | + advisory=advisory, |
| 70 | + affected_package=package, |
| 71 | + ) |
| 72 | + |
| 73 | + if created: |
| 74 | + self.log( |
| 75 | + f"Created CodeFix entry for vulnerability_id: {vulnerability_id} with VCS URL {vcs_url}" |
| 76 | + ) |
| 77 | + |
| 78 | + def clean_downloads(self): |
| 79 | + if self.vcs_response: |
| 80 | + self.log(f"Removing cloned repository") |
| 81 | + self.vcs_response.delete() |
| 82 | + |
| 83 | + def on_failure(self): |
| 84 | + self.clean_downloads() |
0 commit comments