Skip to content

Commit 8d66f64

Browse files
authored
Merge pull request #464 from Pushpit07/collect_xen
Collect xen
2 parents dd3c0e2 + 6ef62fa commit 8d66f64

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ addopts =
3232
--ignore=vulnerabilities/importers/ubuntu_usn.py
3333
--ignore=vulnerabilities/importers/mozilla.py
3434
--ignore=vulnerabilities/importers/mattermost.py
35+
--ignore=vulnerabilities/importers/xen.py
3536
--ignore=vulnerabilities/management/commands/create_cpe_to_purl_map.py
3637
--ignore=vulnerabilities/lib_oval.py

vulnerabilities/fixtures/openssl.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40764,5 +40764,19 @@
4076440764
]
4076540765
}
4076640766
}
40767+
},
40768+
{
40769+
"model": "vulnerabilities.importer",
40770+
"pk": 17,
40771+
"fields": {
40772+
"name": "xen",
40773+
"license": "gplv2",
40774+
"last_run": null,
40775+
"data_source": "XenDataSource",
40776+
"data_source_cfg": {
40777+
"etags": {},
40778+
"db_url": "https://xenbits.xen.org/xsa/xsa.json"
40779+
}
40780+
}
4076740781
}
4076840782
]

vulnerabilities/importers/xen.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)