Fixing RO Security Vulnerabilities
[osm/RO.git] / NG-RO / osm_ng_ro / tests / test_ns.py
1 #######################################################################################
2 # Copyright ETSI Contributors and Others.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #######################################################################################
17
18 import unittest
19 from unittest.mock import Mock, patch
20
21 from jinja2 import (
22 Environment,
23 select_autoescape,
24 StrictUndefined,
25 TemplateError,
26 TemplateNotFound,
27 UndefinedError,
28 )
29 from osm_ng_ro.ns import Ns, NsException
30
31
32 class TestNs(unittest.TestCase):
33 def setUp(self):
34 self.ns = Ns()
35
36 @patch("jinja2.Environment.__init__")
37 def test__parse_jinja2_undefined_error(self, env_mock: Mock):
38 cloud_init_content = None
39 params = None
40 context = None
41
42 env_mock.side_effect = UndefinedError("UndefinedError occurred.")
43
44 with self.assertRaises(NsException):
45 self.ns._parse_jinja2(
46 cloud_init_content=cloud_init_content, params=params, context=context
47 )
48
49 @patch("jinja2.Environment.__init__")
50 def test__parse_jinja2_template_error(self, env_mock: Mock):
51 cloud_init_content = None
52 params = None
53 context = None
54
55 env_mock.side_effect = TemplateError("TemplateError occurred.")
56
57 with self.assertRaises(NsException):
58 self.ns._parse_jinja2(
59 cloud_init_content=cloud_init_content, params=params, context=context
60 )
61
62 @patch("jinja2.Environment.__init__")
63 def test__parse_jinja2_template_not_found(self, env_mock: Mock):
64 cloud_init_content = None
65 params = None
66 context = None
67
68 env_mock.side_effect = TemplateNotFound("TemplateNotFound occurred.")
69
70 with self.assertRaises(NsException):
71 self.ns._parse_jinja2(
72 cloud_init_content=cloud_init_content, params=params, context=context
73 )
74
75 def test_rendering_jinja2_temp_without_special_characters(self):
76 cloud_init_content = """
77 disk_setup:
78 ephemeral0:
79 table_type: {{type}}
80 layout: True
81 overwrite: {{is_override}}
82 runcmd:
83 - [ ls, -l, / ]
84 - [ sh, -xc, "echo $(date) '{{command}}'" ]
85 """
86 params = {
87 "type": "mbr",
88 "is_override": "False",
89 "command": "; mkdir abc",
90 }
91 context = "cloud-init for VM"
92 expected_result = """
93 disk_setup:
94 ephemeral0:
95 table_type: mbr
96 layout: True
97 overwrite: False
98 runcmd:
99 - [ ls, -l, / ]
100 - [ sh, -xc, "echo $(date) '; mkdir abc'" ]
101 """
102 result = self.ns._parse_jinja2(
103 cloud_init_content=cloud_init_content, params=params, context=context
104 )
105 self.assertEqual(result, expected_result)
106
107 def test_rendering_jinja2_temp_with_special_characters(self):
108 cloud_init_content = """
109 disk_setup:
110 ephemeral0:
111 table_type: {{type}}
112 layout: True
113 overwrite: {{is_override}}
114 runcmd:
115 - [ ls, -l, / ]
116 - [ sh, -xc, "echo $(date) '{{command}}'" ]
117 """
118 params = {
119 "type": "mbr",
120 "is_override": "False",
121 "command": "& rm -rf",
122 }
123 context = "cloud-init for VM"
124 expected_result = """
125 disk_setup:
126 ephemeral0:
127 table_type: mbr
128 layout: True
129 overwrite: False
130 runcmd:
131 - [ ls, -l, / ]
132 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
133 """
134 result = self.ns._parse_jinja2(
135 cloud_init_content=cloud_init_content, params=params, context=context
136 )
137 self.assertNotEqual(result, expected_result)
138
139 def test_rendering_jinja2_temp_with_special_characters_autoescape_is_false(self):
140 with patch("osm_ng_ro.ns.Environment") as mock_environment:
141 mock_environment.return_value = Environment(
142 undefined=StrictUndefined,
143 autoescape=select_autoescape(default_for_string=False, default=False),
144 )
145 cloud_init_content = """
146 disk_setup:
147 ephemeral0:
148 table_type: {{type}}
149 layout: True
150 overwrite: {{is_override}}
151 runcmd:
152 - [ ls, -l, / ]
153 - [ sh, -xc, "echo $(date) '{{command}}'" ]
154 """
155 params = {
156 "type": "mbr",
157 "is_override": "False",
158 "command": "& rm -rf /",
159 }
160 context = "cloud-init for VM"
161 expected_result = """
162 disk_setup:
163 ephemeral0:
164 table_type: mbr
165 layout: True
166 overwrite: False
167 runcmd:
168 - [ ls, -l, / ]
169 - [ sh, -xc, "echo $(date) '& rm -rf /'" ]
170 """
171 result = self.ns._parse_jinja2(
172 cloud_init_content=cloud_init_content,
173 params=params,
174 context=context,
175 )
176 self.assertEqual(result, expected_result)