-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
39 lines (32 loc) · 1.4 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import unittest
from execgate import gated_exec, gated_eval, GateSecurityError
class GatedExecTests(unittest.TestCase):
def test_normal_exec(self):
locals_dict = {}
gated_exec('foo = 1 + 1', None, locals_dict)
self.assertEqual(locals_dict['foo'], 2)
def test_exec_import_attack(self):
self.assertRaises(NameError, gated_exec, "__import__('os')")
def test_exec_underscore_search_attack(self):
self.assertRaises(GateSecurityError, gated_exec,
"""# courtesy of http://www.reddit.com/r/Python/comments/hftnp/ask_rpython_recovering_cleared_globals/c1v3l4i
lookup = lambda n: [x for x in (1).__class__.__base__.__subclasses__() if x.__name__ == n][0]
try:
lookup('Codec')().decode('')
except lookup('BaseException') as e:
del lookup
__builtins__ = e.__traceback__.tb_next.tb_frame.f_globals['__builtins__']
""")
def test_normal_eval(self):
self.assertEqual(gated_eval('1 + 1'), 2)
def test_eval_import_attack(self):
self.assertRaises(NameError, gated_eval, "__import__('os')")
def test_eval_underscore_search_attack(self):
self.assertRaises(GateSecurityError, gated_eval,
"""# courtesy of http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
[
c for c in ().__class__.__base__.__subclasses__()
if c.__name__ == 'catch_warnings'
][0]()._module.__builtins__""")
if __name__ == 'main':
unittest.main()