- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecgate.py
More file actions
Latest commit
31 lines (25 loc) · 1.11 KB
/
Copy pathexecgate.py
File metadata and controls
31 lines (25 loc) · 1.11 KB
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
importast
classGateSecurityError(Exception): pass
classGatedNodeVisitor(ast.NodeVisitor):
defvisit_Attribute(self, node):
if('__'innode.attr):
raiseGateSecurityError("Attempting to access private attribute")
defgated_compile(source, filename='<unknown>', mode='exec'):
code_node=ast.parse(source, filename, mode)
GatedNodeVisitor().visit(code_node)
returncompile(code_node, filename, mode)
defgated_exec_eval(source, globals={}, locals={}, mode='exec'):
ifglobalsisNone:
globals= { '__builtins__': {} }
ifglobals.get('__builtins__', None) isNone:
globals['__builtins__'] = {}
ifmodeis'exec':
returnexec(gated_compile(source, '<string>', mode), globals, locals)
elifmodeis'eval':
returneval(gated_compile(source, '<string>', mode), globals, locals)
else:
raiseGatedEvalException('invalid mode {}'.format(mode))
defgated_exec(source, globals={}, locals={}):
gated_exec_eval(source, globals, locals)
defgated_eval(source, globals={}, locals={}):
returngated_exec_eval(source, globals, locals, 'eval')