Skip to content

Commit 1288789

Browse files
committed
add test for elixir security
Signed-off-by: Tushar912 <tushar.912u@gmail.com>
1 parent aa3865e commit 1288789

3 files changed

Lines changed: 104 additions & 9 deletions

File tree

vulnerabilities/importers/elixir_security.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535
from vulnerabilities.data_source import Reference
3636

3737
class ElixirSecurityDataSource(GitDataSource):
38-
def __enter__(self):
39-
super(AlpineDataSource, self).__enter__()
38+
def __enter__(self):
39+
super(ElixirSecurityDataSource, self).__enter__()
4040

4141
if not getattr(self, "_added_files", None):
4242
self._added_files, self._updated_files = self.file_changes(
4343
recursive=True, file_ext="yml",subdir="./packages"
4444
)
4545

46+
4647
def updated_advisories(self) -> Set[Advisory]:
4748
files = self._updated_files
4849
advisories = []
@@ -71,11 +72,12 @@ def generate_all_versions_list(pkg_name):
7172
versions_list.append(release['version'])
7273
return versions_list
7374

74-
75-
@staticmethod
76-
def get_pkg_from_range(versions_list,pkg_name):
75+
76+
def get_pkg_from_range(self,versions_list,pkg_name):
7777
pkg_versions = []
78-
all_versions_list = generate_all_versions_list(pkg_name)
78+
all_versions_list = self.generate_all_versions_list(pkg_name)
79+
if versions_list is None:
80+
return
7981
for version in versions_list:
8082
if re.match('^>=',version):
8183
index = all_versions_list.index(version[3:])
@@ -95,12 +97,19 @@ def process_file(self,path):
9597
with open(path) as f:
9698
yaml_file = yaml.safe_load(f)
9799
pkg_name = yaml_file['package']
98-
safe_pkg_versions = self.get_pkg_from_range(yaml_file['patched_versions']+yaml_file['unaffected_versions'],pkg_name)
100+
safe_pkg_versions =[]
101+
if yaml_file.get('unaffected_versions'):
102+
safe_pkg_versions = self.get_pkg_from_range(yaml_file['patched_versions']+yaml_file['unaffected_versions'],pkg_name)
103+
else:
104+
safe_pkg_versions = self.get_pkg_from_range(yaml_file['patched_versions'],pkg_name)
99105
cve_id = yaml_file['cve']
100-
safe_purls ={ PackageURL(name=pkg_name,
101-
type=pkg_type,
106+
safe_purls = []
107+
if safe_pkg_versions is not None:
108+
safe_purls ={ PackageURL(name=pkg_name,
109+
type='hex',
102110
version=version)
103111
for version in safe_pkg_versions}
112+
104113
vuln_reference = [Reference(
105114
url=yaml_file['link'],
106115
)]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
id: 2aae6e3a-24a3-4d5f-86ff-b964eaf7c6d1
3+
package: coherence
4+
disclosure_date: 2017-08-02
5+
cve: 2018-20301
6+
link: https://github.com/smpallen99/coherence/issues/270
7+
title: |
8+
Permissive parameters and privilege escalation
9+
description: |
10+
The Coherence library has "Mass Assignment"-like vulnerabilities.
11+
patched_versions:
12+
- ">= 0.5.2"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 os
24+
from unittest import TestCase
25+
from collections import OrderedDict
26+
27+
from vulnerabilities.data_source import Reference
28+
from packageurl import PackageURL
29+
30+
from vulnerabilities.importers.elixir_security import ElixirSecurityDataSource
31+
from vulnerabilities.data_source import Advisory
32+
33+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
34+
35+
36+
class TestElixirSecurityDataSource(TestCase):
37+
38+
@classmethod
39+
def setUpClass(cls):
40+
data_source_cfg = {
41+
'repository_url': 'https://test.net', }
42+
cls.data_src = ElixirSecurityDataSource(1, config=data_source_cfg)
43+
44+
def test_generate_all_versions_list(self):
45+
package = 'coherence'
46+
actual_list = self.data_src.generate_all_versions_list(package)
47+
expected_list = ['0.5.2', '0.5.1', '0.5.0', '0.4.0', '0.3.1', '0.3.0', '0.2.0', '0.1.3', '0.1.2', '0.1.1', '0.1.0']
48+
assert actual_list == expected_list
49+
50+
def test_process_file(self):
51+
52+
path = os.path.join(BASE_DIR, "test_data/elixir_security/test_file.yml")
53+
expected_data = Advisory(
54+
summary=('The Coherence library has "Mass Assignment"-like vulnerabilities.\n'),
55+
56+
impacted_package_urls=[],
57+
58+
resolved_package_urls={
59+
PackageURL(
60+
type='hex',
61+
name='coherence',
62+
version='0.5.2',
63+
),
64+
},
65+
vuln_references=[
66+
Reference(
67+
url="https://github.com/smpallen99/coherence/issues/270"
68+
)
69+
],
70+
cve_id='2018-20301')
71+
72+
found_data = self.data_src.process_file(path)
73+
74+
assert expected_data == found_data

0 commit comments

Comments
 (0)