-
-
Notifications
You must be signed in to change notification settings - Fork 302
Add VMware Photon Importer #2198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Samk1710
wants to merge
5
commits into
aboutcode-org:main
Choose a base branch
from
Samk1710:add-vmware-photon-advisories
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
763eed6
Add VMware Photon Importer
Samk1710 9068f65
Fix repo_url
Samk1710 b3f6a50
Fetch Photon CVE data sources dynamically from index
Samk1710 4f6d2c7
Refactor per review
Samk1710 80eb6a6
Refcator and add docstring to fetch method
Samk1710 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
vulnerabilities/pipelines/v2_importers/vmware_photon_importer_v2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # VulnerableCode is a trademark of nexB Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
| # See https://github.com/aboutcode-org/vulnerablecode for support or download. | ||
| # See https://aboutcode.org for more information about nexB OSS projects. | ||
| # | ||
|
|
||
| import json | ||
| import re | ||
| from typing import Iterable | ||
|
|
||
| from packageurl import PackageURL | ||
| from univers.version_constraint import VersionConstraint | ||
| from univers.version_range import RpmVersionRange | ||
| from univers.versions import RpmVersion | ||
|
|
||
| from vulnerabilities.importer import AdvisoryDataV2 | ||
| from vulnerabilities.importer import AffectedPackageV2 | ||
| from vulnerabilities.importer import VulnerabilitySeverity | ||
| from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 | ||
| from vulnerabilities.severity_systems import GENERIC | ||
| from vulnerabilities.utils import fetch_response | ||
|
|
||
|
|
||
| class VmwarePhotonImporterPipeline(VulnerableCodeBaseImporterPipelineV2): | ||
| """Collect advisories from Vmware Photon Advisory. | ||
|
|
||
| Example of advisory | ||
| { | ||
| "cve_id": "CVE-2020-11979", | ||
| "pkg": "apache-ant", | ||
| "cve_score": 7.5, | ||
| "aff_ver": "all versions before 1.10.8-2.ph1 are vulnerable", | ||
| "res_ver": "1.10.8-2.ph1" | ||
| } | ||
| """ | ||
|
|
||
| pipeline_id = "vmware_photon_importer_v2" | ||
| spdx_license_expression = "CC BY-SA 4.0" | ||
| license_url = "https://creativecommons.org/licenses/by-sa/4.0" | ||
| repo_url = "https://packages.vmware.com/photon/photon_cve_metadata/" | ||
|
|
||
| precedence = 100 | ||
|
|
||
| @classmethod | ||
| def steps(cls): | ||
| return ( | ||
| cls.fetch, | ||
| cls.group_records_by_cve, | ||
| cls.collect_and_store_advisories, | ||
| ) | ||
|
|
||
| def fetch(self): | ||
| """ | ||
| Fetches all JSON files from the repository index and extracts records. Each record is enriched with the source URL(contains the photon version) for traceability. | ||
| """ | ||
| self.records = [] | ||
| response = fetch_response(self.repo_url) | ||
| photon_files = re.findall(r'href="(cve_data_photon[0-9.]+\.json)"', response.text) | ||
|
|
||
| for file_name in photon_files: | ||
| source_url = self.repo_url + file_name | ||
| self.log(f"Fetching `{source_url}`") | ||
| response = fetch_response(source_url) | ||
| for record in response.json(): | ||
| record["source_url"] = source_url | ||
| self.records.append(record) | ||
| self.log(f"Fetched {len(self.records):,d} total records from {len(photon_files)} sources") | ||
|
Samk1710 marked this conversation as resolved.
|
||
|
|
||
| def group_records_by_cve(self): | ||
| """ | ||
| A particular CVE may have more than one record. This method groups records by CVE ID and filters "Not Affected" records. | ||
| """ | ||
| self.cve_to_records = {} | ||
| skipped_non_affected = 0 | ||
|
|
||
| for record in self.records: | ||
| cve_id = record.get("cve_id") | ||
|
|
||
| # Skip records that are marked as "Not Affected" | ||
| if record.get("status") == "Not Affected": | ||
| skipped_non_affected += 1 | ||
| continue | ||
|
|
||
| self.cve_to_records.setdefault(cve_id, []).append(record) | ||
|
|
||
| self.log( | ||
| f"Grouped {len(self.records):,d} records into {len(self.cve_to_records):,d} unique CVEs " | ||
| f"(skipped {skipped_non_affected:,d} non-affected)" | ||
| ) | ||
|
|
||
| def advisories_count(self) -> int: | ||
| return len(self.cve_to_records) | ||
|
|
||
| def collect_advisories(self) -> Iterable[AdvisoryDataV2]: | ||
| for cve_id, records in self.cve_to_records.items(): | ||
| affected_packages = [] | ||
|
|
||
| for record in records: | ||
| pkg_name = record.get("pkg") | ||
| aff_ver = record.get("aff_ver") | ||
| res_ver = record.get("res_ver") | ||
|
|
||
| # Example PURL Format: pkg:rpm/vmware/apache-ant?distro=photon | ||
| purl = PackageURL( | ||
| type="rpm", | ||
| namespace="vmware", | ||
| name=pkg_name, | ||
| qualifiers={"distro": "photon"}, | ||
| ) | ||
|
|
||
| ver_match = re.match(r"all versions before (.+) are vulnerable", aff_ver) | ||
|
|
||
| if not ver_match: | ||
| self.log(f"Could not extract affected version from aff_ver: {aff_ver!r}") | ||
| continue | ||
|
|
||
| aff_ver = ver_match.group(1) | ||
| affected_version_range = RpmVersionRange( | ||
| constraints=[ | ||
| VersionConstraint( | ||
| comparator="<", | ||
| version=RpmVersion(aff_ver), | ||
| ) | ||
| ] | ||
| ) | ||
|
Samk1710 marked this conversation as resolved.
|
||
|
|
||
| fixed_version_range = RpmVersionRange.from_versions([res_ver]) | ||
|
|
||
| affected_packages.append( | ||
| AffectedPackageV2( | ||
| package=purl, | ||
| affected_version_range=affected_version_range, | ||
| fixed_version_range=fixed_version_range, | ||
| ) | ||
| ) | ||
|
|
||
| severities = [] | ||
| cve_score = records[0].get("cve_score") | ||
| severities.append( | ||
| VulnerabilitySeverity( | ||
| system=GENERIC, | ||
| value=str(cve_score), | ||
| ) | ||
| ) | ||
|
Samk1710 marked this conversation as resolved.
|
||
|
|
||
| yield AdvisoryDataV2( | ||
| advisory_id=cve_id, | ||
| affected_packages=affected_packages, | ||
| severities=severities, | ||
| url=records[0].get("source_url", self.repo_url), | ||
| original_advisory_text=json.dumps(records, indent=2, ensure_ascii=False), | ||
| ) | ||
51 changes: 51 additions & 0 deletions
51
vulnerabilities/tests/pipelines/v2_importers/test_vmware_photon_importer_v2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # VulnerableCode is a trademark of nexB Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
| # See https://github.com/aboutcode-org/vulnerablecode for support or download. | ||
| # See https://aboutcode.org for more information about nexB OSS projects. | ||
| # | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
| from unittest import TestCase | ||
| from unittest.mock import Mock | ||
| from unittest.mock import patch | ||
|
|
||
| from vulnerabilities.pipelines.v2_importers.vmware_photon_importer_v2 import ( | ||
| VmwarePhotonImporterPipeline, | ||
| ) | ||
| from vulnerabilities.tests import util_tests | ||
|
|
||
| TEST_DATA = Path(__file__).parent.parent.parent / "test_data" / "vmware_photon" | ||
|
|
||
|
|
||
| class TestVmwarePhotonImporterPipeline(TestCase): | ||
| @patch("vulnerabilities.pipelines.v2_importers.vmware_photon_importer_v2.fetch_response") | ||
| def test_collect_advisories(self, mock_fetch): | ||
| sample_path = TEST_DATA / "data.json" | ||
| sample_data = json.loads(sample_path.read_text(encoding="utf-8")) | ||
|
|
||
| index_html = """ | ||
| <a href="cve_data_photon4.0.json">cve_data_photon4.0.json</a> | ||
| """ | ||
|
|
||
| def side_effect(url): | ||
| if url == "https://packages.vmware.com/photon/photon_cve_metadata/": | ||
| return Mock(text=index_html) | ||
| if "cve_data_photon4.0.json" in url: | ||
| return Mock(json=lambda: sample_data) | ||
| return None | ||
|
|
||
| mock_fetch.side_effect = side_effect | ||
|
|
||
| pipeline = VmwarePhotonImporterPipeline() | ||
| pipeline.fetch() | ||
| pipeline.group_records_by_cve() | ||
|
|
||
| advisories = [data.to_dict() for data in list(pipeline.collect_advisories())] | ||
| assert len(advisories) == 2 | ||
|
|
||
| expected_file = TEST_DATA / "expected.json" | ||
| util_tests.check_results_against_json(advisories, expected_file) |
106 changes: 106 additions & 0 deletions
106
vulnerabilities/tests/test_data/vmware_photon/data.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| [ | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-devel", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-drivers-gpu", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-drivers-sound", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-docs", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-drivers-intel-sgx", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-oprofile", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-tools", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-python3-perf", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "bpftool", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-4.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-4.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2024-43853", | ||
| "pkg": "linux-aws", | ||
| "cve_score": 5.5, | ||
| "aff_ver": "all versions before 5.10.224-3.ph4 are vulnerable", | ||
| "res_ver": "5.10.224-3.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2021-45417", | ||
| "pkg": "aide", | ||
| "cve_score": 7.8, | ||
| "aff_ver": "all versions before 0.16.2-3.ph4 are vulnerable", | ||
| "res_ver": "0.16.2-3.ph4", | ||
| "status": "Fixed" | ||
| }, | ||
| { | ||
| "cve_id": "CVE-2018-1085", | ||
| "pkg": "ansible", | ||
| "cve_score": 9.8, | ||
| "aff_ver": "NA", | ||
| "res_ver": "NA", | ||
| "status": "Not Affected" | ||
| } | ||
| ] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.