Skip to content

Commit f064033

Browse files
committed
Add a test for the aosp importer
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent bf38a7b commit f064033

File tree

8 files changed

+191
-88
lines changed

8 files changed

+191
-88
lines changed

vulnerabilities/importers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from vulnerabilities.pipelines import nvd_importer
4242
from vulnerabilities.pipelines import pypa_importer
4343
from vulnerabilities.pipelines import pysec_importer
44+
from vulnerabilities.pipelines.v2_importers import aosp_importer
4445
from vulnerabilities.pipelines.v2_importers import apache_httpd_importer as apache_httpd_v2
4546
from vulnerabilities.pipelines.v2_importers import archlinux_importer as archlinux_importer_v2
4647
from vulnerabilities.pipelines.v2_importers import curl_importer as curl_importer_v2
@@ -81,6 +82,7 @@
8182
mozilla_importer_v2.MozillaImporterPipeline,
8283
github_osv_importer_v2.GithubOSVImporterPipeline,
8384
redhat_importer_v2.RedHatImporterPipeline,
85+
aosp_importer.AospImporterPipeline,
8486
nvd_importer.NVDImporterPipeline,
8587
github_importer.GitHubAPIImporterPipeline,
8688
gitlab_importer.GitLabImporterPipeline,

vulnerabilities/improvers/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
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-
)
2522
from vulnerabilities.pipelines.v2_improvers import compute_advisory_todo as compute_advisory_todo_v2
2623
from vulnerabilities.pipelines.v2_improvers import compute_package_risk as compute_package_risk_v2
2724
from vulnerabilities.pipelines.v2_improvers import (
@@ -71,6 +68,5 @@
7168
compute_version_rank_v2.ComputeVersionRankPipeline,
7269
compute_advisory_todo_v2.ComputeToDo,
7370
compute_advisory_todo.ComputeToDo,
74-
collect_commits_aosp_v2.CollectFixCommitsAospDatasetPipeline,
7571
]
7672
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 json
11+
import shutil
12+
from pathlib import Path
13+
14+
import dateparser
15+
from django.core.exceptions import ValidationError
16+
from fetchcode.vcs import fetch_via_vcs
17+
18+
from vulnerabilities.importer import AdvisoryData
19+
from vulnerabilities.importer import ReferenceV2
20+
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
21+
22+
23+
class AospImporterPipeline(VulnerableCodeBaseImporterPipelineV2):
24+
"""
25+
Pipeline to collect fix commits from Aosp Dataset:
26+
"""
27+
28+
pipeline_id = "aosp_dataset_fix_commits"
29+
spdx_license_expression = "Apache-2.0"
30+
license_url = "https://github.com/quarkslab/aosp_dataset/blob/master/LICENSE"
31+
importer_name = "aosp_dataset"
32+
qualified_name = "aosp_dataset_fix_commits"
33+
34+
@classmethod
35+
def steps(cls):
36+
return (
37+
cls.clone,
38+
cls.collect_and_store_advisories,
39+
cls.clean_downloads,
40+
)
41+
42+
def clone(self):
43+
self.repo_url = "git+https://github.com/quarkslab/aosp_dataset"
44+
self.log(f"Cloning `{self.repo_url}`")
45+
self.vcs_response = fetch_via_vcs(self.repo_url)
46+
47+
def advisories_count(self):
48+
root = Path(self.vcs_response.dest_dir)
49+
return sum(1 for _ in root.rglob("*.json"))
50+
51+
def collect_advisories(self):
52+
self.log(f"Processing aosp_dataset fix commits.")
53+
base_path = Path(self.vcs_response.dest_dir) / "cves"
54+
for file_path in base_path.rglob("*.json"):
55+
if not file_path.name.startswith("CVE-"):
56+
continue
57+
58+
with open(file_path) as f:
59+
vulnerability_data = json.load(f)
60+
61+
vulnerability_id = vulnerability_data.get("cveId", [])
62+
if (
63+
not vulnerability_id or "," in vulnerability_id
64+
): # escape invalid multiple CVE-2017-13077, CVE-2017-13078
65+
continue
66+
67+
summary = vulnerability_data.get("vulnerabilityType")
68+
date_reported = vulnerability_data.get("dateReported")
69+
date_published = dateparser.parse(date_reported) if date_reported else None
70+
71+
references = []
72+
for commit_data in vulnerability_data.get("fixes", []):
73+
vcs_url = commit_data.get("patchUrl")
74+
75+
if not vcs_url:
76+
continue
77+
78+
ref = ReferenceV2(reference_type="commit", url=vcs_url)
79+
references.append(ref)
80+
81+
yield AdvisoryData(
82+
advisory_id=vulnerability_id,
83+
summary=summary,
84+
references_v2=references,
85+
date_published=date_published,
86+
url=f"https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/{file_path.name}",
87+
)
88+
89+
def clean_downloads(self):
90+
"""Cleanup any temporary repository data."""
91+
self.log("Cleaning up local repository resources.")
92+
if hasattr(self, "repo") and self.repo.working_dir:
93+
shutil.rmtree(path=self.repo.working_dir)
94+
95+
def on_failure(self):
96+
"""Ensure cleanup is always performed on failure."""
97+
self.clean_downloads()

vulnerabilities/pipelines/v2_improvers/collect_commits_aosp_dataset.py

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 os
11+
from pathlib import Path
12+
from unittest.mock import Mock
13+
14+
import pytest
15+
16+
from vulnerabilities.pipelines.v2_importers.aosp_importer import AospImporterPipeline
17+
from vulnerabilities.tests import util_tests
18+
19+
TEST_DATA = Path(__file__).parent.parent.parent / "test_data" / "aosp"
20+
21+
22+
@pytest.mark.django_db
23+
def test_aosp_advisories1():
24+
expected_file = os.path.join(TEST_DATA, "aosp_advisoryv2-expected.json")
25+
pipeline = AospImporterPipeline()
26+
pipeline.vcs_response = Mock(dest_dir=TEST_DATA)
27+
result = [adv.to_dict() for adv in pipeline.collect_advisories()]
28+
util_tests.check_results_against_json(result, expected_file)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
{
3+
"advisory_id": "CVE-2021-30294",
4+
"aliases": [],
5+
"summary": "Vulnerability",
6+
"affected_packages": [],
7+
"references_v2": [
8+
{
9+
"reference_id": "",
10+
"reference_type": "",
11+
"url": "https://source.codeaurora.org/quic/la/kernel/msm-5.4/commit/?id=d6876813add62f3cac7c429a41cc8710005d69e8"
12+
}
13+
],
14+
"severities": [],
15+
"date_published": null,
16+
"weaknesses": [],
17+
"url": "https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/CVE-aosp_test1.json"
18+
},
19+
{
20+
"advisory_id": "CVE-2017-13282",
21+
"aliases": [],
22+
"summary": "Remote Code Execution Vulnerability",
23+
"affected_packages": [],
24+
"references_v2": [
25+
{
26+
"reference_id": "",
27+
"reference_type": "",
28+
"url": "https://android.googlesource.com/platform/system/bt/+/6ecbbc093f4383e90cbbf681cd55da1303a8ef94"
29+
}
30+
],
31+
"severities": [],
32+
"date_published": "2018-04-04T00:00:00",
33+
"weaknesses": [],
34+
"url": "https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/CVE-aosp_test2.json"
35+
}
36+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"cveId": "CVE-2021-30294",
3+
"dateReported": null,
4+
"vulnerabilityType": "Vulnerability",
5+
"language": "c",
6+
"fixes": [
7+
{
8+
"commitId": "",
9+
"patchUrl": "https://source.codeaurora.org/quic/la/kernel/msm-5.4/commit/?id=d6876813add62f3cac7c429a41cc8710005d69e8"
10+
}
11+
],
12+
"severity": "High",
13+
"component": "Qualcomm Display"
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"cveId": "CVE-2017-13282",
3+
"dateReported": "2018-04-04",
4+
"vulnerabilityType": "Remote Code Execution Vulnerability",
5+
"language": "c",
6+
"fixes": [
7+
{
8+
"commitId": "6ecbbc093f4383e90cbbf681cd55da1303a8ef94",
9+
"patchUrl": "https://android.googlesource.com/platform/system/bt/+/6ecbbc093f4383e90cbbf681cd55da1303a8ef94"
10+
}
11+
],
12+
"severity": "Critical",
13+
"component": "System"
14+
}

0 commit comments

Comments
 (0)