Skip to content

Commit 0414a1b

Browse files
committed
elix security importer
Signed-off-by: Tushar912 <tushar.912u@gmail.com>
1 parent 7f8ae63 commit 0414a1b

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

vulnerabilities/importer_yielder.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@
226226
'data_source': 'PostgreSQLDataSource',
227227
'data_source_cfg': {},
228228
},
229+
{
230+
'name': 'elixir_security' ,
231+
'license': '',
232+
'last_run': None,
233+
'data_source': 'ElixirSecurityDataSource',
234+
'data_source_cfg': {
235+
'repository_url': 'https://github.com/dependabot/elixir-security-advisories'
236+
},
237+
},
229238

230239
]
231240

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright (c) 2017 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 yaml
24+
import re
25+
import json
26+
import requests
27+
from typing import Set
28+
from typing import List
29+
30+
from packageurl import PackageURL
31+
32+
from vulnerabilities.data_source import GitDataSource
33+
from vulnerabilities.data_source import GitDataSourceConfiguration
34+
from vulnerabilities.data_source import Advisory
35+
from vulnerabilities.data_source import Reference
36+
37+
class ElixirSecurityDataSource(GitDataSource):
38+
def __enter__(self):
39+
super(AlpineDataSource, self).__enter__()
40+
41+
if not getattr(self, "_added_files", None):
42+
self._added_files, self._updated_files = self.file_changes(
43+
recursive=True, file_ext="yml",subdir="./packages"
44+
)
45+
46+
def updated_advisories(self) -> Set[Advisory]:
47+
files = self._updated_files
48+
advisories = []
49+
for f in files:
50+
processed_data = self.process_file(f)
51+
if processed_data:
52+
advisories.append(processed_data)
53+
return self.batch_advisories(advisories)
54+
55+
def added_advisories(self) -> Set[Advisory]:
56+
files = self._added_files
57+
advisories = []
58+
for f in files:
59+
processed_data = self.process_file(f)
60+
if processed_data:
61+
advisories.append(processed_data)
62+
return self.batch_advisories(advisories)
63+
64+
@staticmethod
65+
def generate_all_versions_list(pkg_name):
66+
resp = requests.get(f"https://hex.pm/api/packages/{pkg_name}")
67+
resp = resp.content
68+
json_resp = json.loads(resp)
69+
versions_list = []
70+
for release in json_resp['releases']:
71+
versions_list.append(release['version'])
72+
return versions_list
73+
74+
75+
@staticmethod
76+
def get_pkg_from_range(versions_list,pkg_name):
77+
pkg_versions = []
78+
all_versions_list = generate_all_versions_list(pkg_name)
79+
for version in versions_list:
80+
if re.match('^>=',version):
81+
index = all_versions_list.index(version[3:])
82+
pkg_versions = pkg_versions + all_versions_list[0:index+1]
83+
elif re.match('^>',version):
84+
index = all_versions_list.index(version[2:])
85+
pkg_versions = pkg_versions + all_versions_list[0:index]
86+
elif re.match('^<',version):
87+
index = all_versions_list.index(version[2:])
88+
pkg_versions = pkg_versions + all_versions_list[index+1:-1]
89+
else :
90+
pkg_versions.append(version[3:])
91+
return pkg_versions
92+
93+
94+
def process_file(self,path):
95+
with open(path) as f:
96+
yaml_file = yaml.safe_load(f)
97+
pkg_name = yaml_file['package']
98+
safe_pkg_versions = self.get_pkg_from_range(yaml_file['patched_versions']+yaml_file['unaffected_versions'],pkg_name)
99+
cve_id = yaml_file['cve']
100+
safe_purls ={ PackageURL(name=pkg_name,
101+
type=pkg_type,
102+
version=version)
103+
for version in safe_pkg_versions}
104+
vuln_reference = [Reference(
105+
url=yaml_file['link'],
106+
)]
107+
108+
return Advisory(
109+
summary=yaml_file['description'],
110+
impacted_package_urls=[],
111+
resolved_package_urls=safe_purls,
112+
cve_id=cve_id,
113+
vuln_references=vuln_reference)

0 commit comments

Comments
 (0)