|
| 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 code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | +import json |
| 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 VulCodeDataSource(GitDataSource): |
| 33 | + def __enter__(self): |
| 34 | + super(VulCodeDataSource, self).__enter__() |
| 35 | + |
| 36 | + if not getattr(self, "_added_files", None): |
| 37 | + self._added_files, self._updated_files = self.file_changes( |
| 38 | + file_ext="json", |
| 39 | + ) |
| 40 | + |
| 41 | + def updated_advisories(self): |
| 42 | + |
| 43 | + advisories = [] |
| 44 | + for file in self._added_files.union(self._updated_files): |
| 45 | + with open(file) as f: |
| 46 | + data = json.load(f) |
| 47 | + references = [] |
| 48 | + |
| 49 | + for ref in data["references"]: |
| 50 | + references.append(Reference(url=ref["url"], reference_id=ref["reference_id"])) |
| 51 | + |
| 52 | + advisories.append( |
| 53 | + Advisory( |
| 54 | + identifier=data["identifier"], |
| 55 | + summary=data["summary"], |
| 56 | + impacted_package_urls=[ |
| 57 | + PackageURL.from_string(purl) for purl in data["vulnerable_packages"] |
| 58 | + ], |
| 59 | + resolved_package_urls=[ |
| 60 | + PackageURL.from_string(purl) for purl in data["resolved_packages"] |
| 61 | + ], |
| 62 | + vuln_references=references, |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + return self.batch_advisories(advisories) |
0 commit comments