Skip to content

Commit 57c8c93

Browse files
authored
Merge pull request #2 from nexB/cve_search_api
#1 Query cve-search' database for package vulnerabilities
2 parents 6e7f5d8 + 09413f9 commit 57c8c93

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

api_data.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
4+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
5+
# The VulnerableCode software is licensed under the Apache License version 2.0.
6+
# Data generated with VulnerableCode requires an acknowledgment.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
16+
# derivative work, you must accompany this data with the following acknowledgment:
17+
#
18+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
19+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
20+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
21+
# for any legal advice.
22+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
23+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
24+
25+
import json
26+
from urllib.request import urlopen
27+
28+
29+
def data_cve_circl(name, version=None):
30+
"""
31+
Output cve-ids, if any, related to a package.
32+
Take as input, a package name, type, package version and
33+
query cve-search' dataset for any reported vulnerabilities
34+
"""
35+
url = f'https://cve.circl.lu/api/search/{name}'
36+
37+
if version:
38+
url += f'/{version}'
39+
40+
raw_data = urlopen(url).read()
41+
data = json.loads(raw_data)
42+
43+
if version:
44+
return data
45+
46+
return data['data']
47+
48+
49+
def extract_fields(data, fields_names):
50+
"""
51+
Return requested data fields using data generated by
52+
cve-search' api. Takes as input data, fields requested
53+
"""
54+
return [{name: item.get(name) for name in fields_names}
55+
for item in data]

test_api_data.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
3+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
4+
# The VulnerableCode software is licensed under the Apache License version 2.0.
5+
# Data generated with VulnerableCode requires an acknowledgment.
6+
#
7+
# You may not use this software except in compliance with the License.
8+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
9+
# Unless required by applicable law or agreed to in writing, software distributed
10+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
# specific language governing permissions and limitations under the License.
13+
#
14+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
15+
# derivative work, you must accompany this data with the following acknowledgment:
16+
#
17+
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
18+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
19+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
20+
# for any legal advice.
21+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
22+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
23+
24+
import api_data as api
25+
import json
26+
27+
test_data = """
28+
[{
29+
"Modified": "2008-11-15T00:00:00",
30+
"Published": "2007-02-19T21:28:00",
31+
"access": {
32+
"authentication": "NONE",
33+
"complexity": "MEDIUM",
34+
"vector": "NETWORK"
35+
},
36+
"cvss": 4.3,
37+
"cvss-time": "2007-02-20T14:55:00",
38+
"id": "CVE-2007-1004",
39+
"impact": {
40+
"availability": "NONE",
41+
"confidentiality": "NONE",
42+
"integrity": "PARTIAL"
43+
},
44+
"reason": "Link",
45+
"references": [
46+
"http://securityreason.com/securityalert/2264",
47+
"http://www.securityfocus.com/archive/1/archive/1/460369/100/0/threaded",
48+
"http://www.securityfocus.com/archive/1/archive/1/460412/100/0/threaded",
49+
"http://www.securityfocus.com/archive/1/archive/1/460617/100/0/threaded",
50+
"http://www.securityfocus.com/bid/22601",
51+
"http://xforce.iss.net/xforce/xfdb/32580"
52+
],
53+
"summary": "Mozilla Firefox might allow remote",
54+
"vulnerable_configuration": [
55+
"cpe:2.3:a:mozilla:firefox:2.0:rc3"
56+
],
57+
"vulnerable_configuration_cpe_2_2": [
58+
"cpe:/a:mozilla:firefox:2.0:rc3"
59+
]}]
60+
"""
61+
62+
63+
def test_extract_fields_data():
64+
fields_names = ['id', 'cvss', 'summary']
65+
data = json.loads(test_data)
66+
extracted_data = api.extract_fields(data=data, fields_names=fields_names)
67+
68+
assert extracted_data == [{'cvss': 4.3, 'id': 'CVE-2007-1004',
69+
'summary': 'Mozilla Firefox might allow remote'}]
70+
71+
72+
def test_extract_fields():
73+
fields_names = []
74+
data = json.loads(test_data)
75+
extracted_data = api.extract_fields(data=data, fields_names=fields_names)
76+
assert extracted_data == [{}]
77+
78+
fields_names = ['']
79+
extracted_data = api.extract_fields(data=data, fields_names=fields_names)
80+
assert extracted_data == [{'': None}]
81+
82+
fields_names = ['invalid_field']
83+
extracted_data = api.extract_fields(data=data, fields_names=fields_names)
84+
assert extracted_data == [{'invalid_field': None}]

0 commit comments

Comments
 (0)