|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +import yaml |
| 24 | + |
| 25 | +from packageurl import PackageURL |
| 26 | + |
| 27 | +from vulnerabilities.data_source import GitDataSource |
| 28 | +from vulnerabilities.data_source import Advisory |
| 29 | +from vulnerabilities.data_source import Reference |
| 30 | + |
| 31 | + |
| 32 | +class KaybeeDataSource(GitDataSource): |
| 33 | + def __enter__(self): |
| 34 | + super(KaybeeDataSource, self).__enter__() |
| 35 | + self._added_files, self._updated_files = self.file_changes( |
| 36 | + recursive=True, |
| 37 | + file_ext="yaml", |
| 38 | + ) |
| 39 | + print(self._added_files.union(self._updated_files)) |
| 40 | + |
| 41 | + def updated_advisories(self): |
| 42 | + advisories = [] |
| 43 | + for yaml_file in self._added_files.union(self._updated_files): |
| 44 | + print(yaml_file) |
| 45 | + advisories.append(yaml_file_to_advisory(yaml_file)) |
| 46 | + print(advisories[-1]) |
| 47 | + |
| 48 | + return self.batch_advisories(advisories) |
| 49 | + |
| 50 | + |
| 51 | +def yaml_file_to_advisory(yaml_path): |
| 52 | + impacted_packages = [] |
| 53 | + resolved_packages = [] |
| 54 | + references = [] |
| 55 | + |
| 56 | + data = load_yaml(yaml_path) |
| 57 | + vuln_id = data["vulnerability_id"] |
| 58 | + summary = "\n".join([note["text"] for note in data["notes"]]) |
| 59 | + |
| 60 | + for entry in data.get("artifacts", []): |
| 61 | + package = PackageURL.from_string(entry["id"]) |
| 62 | + if entry["affected"]: |
| 63 | + impacted_packages.append(package) |
| 64 | + |
| 65 | + else: |
| 66 | + resolved_packages.append(package) |
| 67 | + |
| 68 | + for fix in data.get("fixes", []): |
| 69 | + for commit in fix["commits"]: |
| 70 | + references.append(Reference(url=f"{commit['repository']}/{commit['id']}")) |
| 71 | + |
| 72 | + return Advisory( |
| 73 | + cve_id=vuln_id, |
| 74 | + summary=summary, |
| 75 | + impacted_package_urls=impacted_packages, |
| 76 | + resolved_package_urls=resolved_packages, |
| 77 | + vuln_references=references, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +# TODO refactor all such commonly needed helpers into one single module |
| 82 | +def load_yaml(path): |
| 83 | + with open(path) as f: |
| 84 | + return yaml.safe_load(f) |
0 commit comments