Skip to content

Commit 98451b4

Browse files
committed
Update the aosp importer to correctly parse CodeCommit using packageurl-python
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 59d3380 commit 98451b4

File tree

5 files changed

+29
-82
lines changed

5 files changed

+29
-82
lines changed

vulnerabilities/importer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ def from_url(cls, url):
194194
return cls(url=url)
195195

196196

197+
"""
198+
199+
"""
200+
VCS_URLS_SUPPORTED_TYPES = {"github", "bitbucket", "gitlab"}
201+
202+
197203
@dataclasses.dataclass(eq=True)
198204
@functools.total_ordering
199205
class CodePatchData:

vulnerabilities/pipelines/v2_importers/aosp_importer.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
import dateparser
1616
from fetchcode.vcs import fetch_via_vcs
17+
from packageurl.contrib.purl2url import get_repo_url
1718
from packageurl.contrib.url2purl import url2purl
1819

1920
from aboutcode.hashid import get_core_purl
21+
from vulnerabilities.importer import VCS_URLS_SUPPORTED_TYPES
2022
from vulnerabilities.importer import AdvisoryData
2123
from vulnerabilities.importer import AffectedPackageV2
2224
from vulnerabilities.importer import CodePatchData
2325
from vulnerabilities.importer import ReferenceV2
2426
from vulnerabilities.importer import VulnerabilitySeverity
2527
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
2628
from vulnerabilities.severity_systems import GENERIC
27-
from vulnerabilities.utils import VCS_URLS_SUPPORTED_TYPES
28-
from vulnerabilities.utils import parse_commit_url
2929

3030

3131
class AospImporterPipeline(VulnerableCodeBaseImporterPipelineV2):
@@ -96,24 +96,29 @@ def collect_advisories(self):
9696
purl = url2purl(commit_url)
9797
base_purl = get_core_purl(purl)
9898

99-
if base_purl and base_purl.type in VCS_URLS_SUPPORTED_TYPES:
100-
vcs_url, commit_hash = parse_commit_url(url=commit_url)
99+
purl_string = base_purl.to_string()
100+
vcs_url = get_repo_url(purl_string)
101101

102-
fixed_commit = CodePatchData(
103-
commit_hash=commit_hash,
104-
vcs_url=vcs_url,
102+
if not base_purl or base_purl.type not in VCS_URLS_SUPPORTED_TYPES:
103+
references.append(
104+
ReferenceV2(
105+
reference_id=commit_id,
106+
reference_type="commit",
107+
url=commit_url,
108+
)
105109
)
110+
continue
106111

107-
affected_package = AffectedPackageV2(
108-
package=base_purl,
109-
fixed_by_commits=[fixed_commit],
110-
)
111-
affected_packages.append(affected_package)
112-
else:
113-
ref = ReferenceV2(
114-
reference_id=commit_id, reference_type="commit", url=commit_url
115-
)
116-
references.append(ref)
112+
fixed_commit = CodePatchData(
113+
commit_hash=purl.version,
114+
vcs_url=vcs_url,
115+
)
116+
117+
affected_package = AffectedPackageV2(
118+
package=base_purl,
119+
fixed_by_commits=[fixed_commit],
120+
)
121+
affected_packages.append(affected_package)
117122

118123
url = (
119124
"https://raw.githubusercontent.com/quarkslab/aosp_dataset/refs/heads/master/cves/"

vulnerabilities/tests/test_data/aosp/CVE-aosp_test3-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"fixed_by_commits": [
2020
{
2121
"commit_hash": "0048b4837affd153897ed1222283492070027aa9",
22-
"vcs_url": "https://github.com/torvalds/linux.git",
22+
"vcs_url": "https://github.com/torvalds/linux",
2323
"commit_patch": null
2424
}
2525
]

vulnerabilities/tests/test_utils.py

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from vulnerabilities.utils import get_item
1818
from vulnerabilities.utils import get_severity_range
1919
from vulnerabilities.utils import nearest_patched_package
20-
from vulnerabilities.utils import parse_commit_url
2120
from vulnerabilities.utils import resolve_version_range
2221
from vulnerabilities.utils import split_markdown_front_matter
2322

@@ -152,33 +151,3 @@ def test_resolve_version_range_without_ignorable_versions():
152151
def test_get_severity_range():
153152
assert get_severity_range({""}) is None
154153
assert get_severity_range({}) is None
155-
156-
157-
@pytest.mark.parametrize(
158-
"url,expected_repo,expected_commit",
159-
[
160-
(
161-
"https://github.com/aboutcode-org/vulnerablecode/commit/98e516011d6e096e25247b82fc5f196bbeecff10",
162-
"https://github.com/aboutcode-org/vulnerablecode.git",
163-
"98e516011d6e096e25247b82fc5f196bbeecff10",
164-
),
165-
(
166-
"https://gitlab.com/gitlab-org/gitlab-development-kit/-/commit/1bd329f740c39ef97f1a2a5b2a760a4fe544ca14",
167-
"https://gitlab.com/gitlab-org/gitlab-development-kit.git",
168-
"1bd329f740c39ef97f1a2a5b2a760a4fe544ca14",
169-
),
170-
(
171-
"https://bitbucket.org/cpointe/fermenter/commits/6ba6f9fb354117bbf0d5f78cb8a80804bae694dd",
172-
"https://bitbucket.org/cpointe/fermenter.git",
173-
"6ba6f9fb354117bbf0d5f78cb8a80804bae694dd",
174-
),
175-
],
176-
)
177-
def test_valid_commit_urls(url, expected_repo, expected_commit):
178-
result = parse_commit_url(url)
179-
assert result == (expected_repo, expected_commit)
180-
181-
182-
def test_invalid_url():
183-
with pytest.raises(ValueError):
184-
parse_commit_url("https://example.com/not/a/commit/url")

vulnerabilities/utils.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -681,36 +681,3 @@ def create_registry(pipelines):
681681
registry[key] = pipeline
682682

683683
return registry
684-
685-
686-
VCS_URLS_SUPPORTED_TYPES = {"github", "bitbucket", "gitlab"}
687-
688-
689-
def parse_commit_url(url: str):
690-
"""Parse a GitHub/GitLab/Bitbucket commit URL and return a repo_url and commit."""
691-
692-
patterns = [
693-
# GitHub: https://github.com/user/repo/commit/<sha>
694-
(
695-
r"^https?://github\.com/([^/]+)/([^/]+)/commit/([a-f0-9]+)$",
696-
"https://github.com/{user}/{repo}.git",
697-
),
698-
# GitLab: https://gitlab.com/user/repo/-/commit/<sha>
699-
(
700-
r"^https?://gitlab\.com/([^/]+)/([^/]+)/-/commit/([a-f0-9]+)$",
701-
"https://gitlab.com/{user}/{repo}.git",
702-
),
703-
# Bitbucket: https://bitbucket.org/user/repo/commits/<sha>
704-
(
705-
r"^https?://bitbucket\.org/([^/]+)/([^/]+)/commits/([a-f0-9]+)$",
706-
"https://bitbucket.org/{user}/{repo}.git",
707-
),
708-
]
709-
710-
for regex, repo_template in patterns:
711-
m = re.match(regex, url, re.IGNORECASE)
712-
if m:
713-
user, repo, commit = m.groups()
714-
return repo_template.format(user=user, repo=repo), commit
715-
716-
raise ValueError(f"Unsupported commit URL format {url}")

0 commit comments

Comments
 (0)