Skip to content

Commit 6fc641b

Browse files
committed
Merge branch 'commit-aosp' into advisory-fix-commit-1
2 parents 4b177df + a6b59f4 commit 6fc641b

File tree

6 files changed

+237
-0
lines changed

6 files changed

+237
-0
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,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 datetime import timezone
13+
from pathlib import Path
14+
15+
import dateparser
16+
from fetchcode.vcs import fetch_via_vcs
17+
18+
from vulnerabilities.importer import AdvisoryData
19+
from vulnerabilities.importer import ReferenceV2
20+
from vulnerabilities.importer import VulnerabilitySeverity
21+
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
22+
from vulnerabilities.severity_systems import GENERIC
23+
24+
25+
class AospImporterPipeline(VulnerableCodeBaseImporterPipelineV2):
26+
"""
27+
Pipeline to collect fix commits from Aosp Dataset:
28+
"""
29+
30+
pipeline_id = "aosp_dataset_fix_commits"
31+
spdx_license_expression = "Apache-2.0"
32+
license_url = "https://github.com/quarkslab/aosp_dataset/blob/master/LICENSE"
33+
importer_name = "aosp_dataset"
34+
qualified_name = "aosp_dataset_fix_commits"
35+
36+
@classmethod
37+
def steps(cls):
38+
return (
39+
cls.clone,
40+
cls.collect_and_store_advisories,
41+
cls.clean_downloads,
42+
)
43+
44+
def clone(self):
45+
self.repo_url = "git+https://github.com/quarkslab/aosp_dataset"
46+
self.log(f"Cloning `{self.repo_url}`")
47+
self.vcs_response = fetch_via_vcs(self.repo_url)
48+
49+
def advisories_count(self):
50+
root = Path(self.vcs_response.dest_dir)
51+
return sum(1 for _ in root.rglob("*.json"))
52+
53+
def collect_advisories(self):
54+
self.log(f"Processing aosp_dataset fix commits.")
55+
base_path = Path(self.vcs_response.dest_dir) / "cves"
56+
for file_path in base_path.rglob("*.json"):
57+
if not file_path.name.startswith("CVE-"):
58+
continue
59+
60+
with open(file_path) as f:
61+
vulnerability_data = json.load(f)
62+
63+
vulnerability_id = vulnerability_data.get("cveId", [])
64+
if (
65+
not vulnerability_id or "," in vulnerability_id
66+
): # escape invalid multiple CVE-2017-13077, CVE-2017-13078
67+
continue
68+
69+
summary = vulnerability_data.get("vulnerabilityType")
70+
date_reported = vulnerability_data.get("dateReported")
71+
date_published = dateparser.parse(date_reported) if date_reported else None
72+
if date_published and not date_published.tzinfo:
73+
date_published = date_published.replace(tzinfo=timezone.utc)
74+
75+
severities = []
76+
severity_value = vulnerability_data.get("severity")
77+
if severity_value:
78+
severities.append(
79+
VulnerabilitySeverity(
80+
system=GENERIC,
81+
value=severity_value,
82+
)
83+
)
84+
85+
references = []
86+
for commit_data in vulnerability_data.get("fixes", []):
87+
vcs_url = commit_data.get("patchUrl")
88+
commit_id = commit_data.get("commitId")
89+
90+
"""
91+
https://us.codeaurora.org/cgit/quic/la/kernel/msm/commit/?id=17bfaf64ad503d2e6607d2d3e0956f25bf07eb43
92+
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=f54e18f1b831c92f6512d2eedb224cd63d607d3d
93+
https://android.googlesource.com/platform/system/bt/+/514139f4b40cbb035bb92f3e24d5a389d75db9e6
94+
https://source.codeaurora.org/quic/la//kernel/msm-4.4/commit/?id=b108c651cae9913da1ab163cb4e5f7f2db87b747
95+
96+
Check if commit in the url split based on it to get the commit hash and the vcs url
97+
if not split base on /+/
98+
"""
99+
100+
if not vcs_url:
101+
continue
102+
103+
fixed_by_commits = []
104+
repo_url, commit_id = url.split("/+/")
105+
106+
fixed_commit = CodeCommitData(
107+
commit_hash=commit_hash,
108+
url=vcs_url,
109+
)
110+
111+
fixed_by_commits.append(fixed_commit)
112+
113+
yield AdvisoryData(
114+
advisory_id=vulnerability_id,
115+
summary=summary,
116+
references_v2=references,
117+
severities=severities,
118+
fixed_by_commits=fixed_by_commits,
119+
date_published=date_published,
120+
url=f"https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/{file_path.name}",
121+
)
122+
123+
def clean_downloads(self):
124+
"""Cleanup any temporary repository data."""
125+
self.log("Cleaning up local repository resources.")
126+
if hasattr(self, "repo") and self.repo.working_dir:
127+
shutil.rmtree(path=self.repo.working_dir)
128+
129+
def on_failure(self):
130+
"""Ensure cleanup is always performed on failure."""
131+
self.clean_downloads()
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_advisories():
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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": "commit",
11+
"url": "https://source.codeaurora.org/quic/la/kernel/msm-5.4/commit/?id=d6876813add62f3cac7c429a41cc8710005d69e8"
12+
}
13+
],
14+
"severities": [
15+
{
16+
"system": "generic_textual",
17+
"value": "High",
18+
"scoring_elements": ""
19+
}
20+
],
21+
"date_published": null,
22+
"weaknesses": [],
23+
"url": "https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/CVE-aosp_test1.json"
24+
},
25+
{
26+
"advisory_id": "CVE-2017-13282",
27+
"aliases": [],
28+
"summary": "Remote Code Execution Vulnerability",
29+
"affected_packages": [],
30+
"references_v2": [
31+
{
32+
"reference_id": "",
33+
"reference_type": "commit",
34+
"url": "https://android.googlesource.com/platform/system/bt/+/6ecbbc093f4383e90cbbf681cd55da1303a8ef94"
35+
}
36+
],
37+
"severities": [
38+
{
39+
"system": "generic_textual",
40+
"value": "Critical",
41+
"scoring_elements": ""
42+
}
43+
],
44+
"date_published": "2018-04-04T00:00:00+00:00",
45+
"weaknesses": [],
46+
"url": "https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/CVE-aosp_test2.json"
47+
}
48+
]
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)