|
| 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 bz2 |
| 24 | +import dataclasses |
| 25 | +import json |
| 26 | + |
| 27 | +import requests |
| 28 | +from packageurl import PackageURL |
| 29 | + |
| 30 | +from vulnerabilities.importer import Importer |
| 31 | +from vulnerabilities.importer import Advisory |
| 32 | +from vulnerabilities.importer import Reference |
| 33 | +from vulnerabilities.helpers import create_etag |
| 34 | +from vulnerabilities.helpers import is_cve |
| 35 | + |
| 36 | + |
| 37 | +class XenImporter(Importer): |
| 38 | + CONFIG_CLASS = XenDBConfiguration |
| 39 | + |
| 40 | + def updated_advisories(self): |
| 41 | + advisories = [] |
| 42 | + if create_etag(data_src=self, url=self.config.db_url, etag_key="etag"): |
| 43 | + advisories.extend(self.to_advisories(fetch(self.config.db_url))) |
| 44 | + |
| 45 | + return self.batch_advisories(advisories) |
| 46 | + |
| 47 | + def create_etag(self, url): |
| 48 | + etag = requests.head(url).headers.get("etag") |
| 49 | + if not etag: |
| 50 | + return True |
| 51 | + |
| 52 | + elif url in self.config.etags: |
| 53 | + if self.config.etags[url] == etag: |
| 54 | + return False |
| 55 | + |
| 56 | + self.config.etags[url] = etag |
| 57 | + return True |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def to_advisories(xen_db): |
| 61 | + advisories = [] |
| 62 | + for xsa in xen_db[0]["xsas"]: |
| 63 | + reference = get_xen_references(xsa["xsa"]) |
| 64 | + title = xsa.get("title", [""]) |
| 65 | + for cve in xsa.get("cve", [""]): |
| 66 | + if not is_cve(cve): |
| 67 | + cve = "" |
| 68 | + |
| 69 | + advisories.append( |
| 70 | + Advisory( |
| 71 | + vulnerability_id=cve, |
| 72 | + summary=title, |
| 73 | + references=[reference], |
| 74 | + ) |
| 75 | + ) |
| 76 | + return advisories |
| 77 | + |
| 78 | + |
| 79 | +def get_xen_references(xsa_id): |
| 80 | + return Reference( |
| 81 | + reference_id="XSA-" + xsa_id, |
| 82 | + url="https://xenbits.xen.org/xsa/advisory-{}.html".format(xsa_id), |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def fetch(url): |
| 87 | + response = requests.get(url).content |
| 88 | + return json.loads(response) |
0 commit comments