Skip to content

Commit bf38a7b

Browse files
committed
Add initial support for collecting aosp commits.
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent dcb0511 commit bf38a7b

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

vulnerabilities/improvers/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
from vulnerabilities.pipelines import flag_ghost_packages
2020
from vulnerabilities.pipelines import populate_vulnerability_summary_pipeline
2121
from vulnerabilities.pipelines import remove_duplicate_advisories
22+
from vulnerabilities.pipelines.v2_improvers import (
23+
collect_commits_aosp_dataset as collect_commits_aosp_v2,
24+
)
2225
from vulnerabilities.pipelines.v2_improvers import compute_advisory_todo as compute_advisory_todo_v2
2326
from vulnerabilities.pipelines.v2_improvers import compute_package_risk as compute_package_risk_v2
2427
from vulnerabilities.pipelines.v2_improvers import (
@@ -68,5 +71,6 @@
6871
compute_version_rank_v2.ComputeVersionRankPipeline,
6972
compute_advisory_todo_v2.ComputeToDo,
7073
compute_advisory_todo.ComputeToDo,
74+
collect_commits_aosp_v2.CollectFixCommitsAospDatasetPipeline,
7175
]
7276
)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)