Commit 8ed484c5 by James Cammarata

Additional fixes for safe_eval

parent cc4129f6
...@@ -1040,22 +1040,23 @@ def safe_eval(expr, locals={}, include_exceptions=False): ...@@ -1040,22 +1040,23 @@ def safe_eval(expr, locals={}, include_exceptions=False):
# visitor class defined below. # visitor class defined below.
SAFE_NODES = set( SAFE_NODES = set(
( (
ast.Expression, ast.Add,
ast.Attribute,
ast.BinOp,
ast.Call,
ast.Compare, ast.Compare,
ast.Str,
ast.List,
ast.Tuple,
ast.Dict, ast.Dict,
ast.Call, ast.Div,
ast.Expression,
ast.List,
ast.Load, ast.Load,
ast.BinOp, ast.Mult,
ast.UnaryOp,
ast.Num, ast.Num,
ast.Name, ast.Name,
ast.Add, ast.Str,
ast.Sub, ast.Sub,
ast.Mult, ast.Tuple,
ast.Div, ast.UnaryOp,
) )
) )
...@@ -1089,10 +1090,12 @@ def safe_eval(expr, locals={}, include_exceptions=False): ...@@ -1089,10 +1090,12 @@ def safe_eval(expr, locals={}, include_exceptions=False):
def generic_visit(self, node): def generic_visit(self, node):
if type(node) not in SAFE_NODES: if type(node) not in SAFE_NODES:
raise Exception("invalid expression (%s)" % expr) raise Exception("invalid expression (%s)" % expr)
super(CleansingNodeVisitor, self).generic_visit(node) elif isinstance(node, ast.Call):
def visit_Call(self, call): if not isinstance(node.func, ast.Attribute) and node.func.id not in CALL_WHITELIST:
if call.func.id not in CALL_WHITELIST: raise Exception("invalid function: %s" % node.func.id)
raise Exception("invalid function: %s" % call.func.id) # iterate over all child nodes
for child_node in ast.iter_child_nodes(node):
super(CleansingNodeVisitor, self).visit(child_node)
if not isinstance(expr, basestring): if not isinstance(expr, basestring):
# already templated to a datastructure, perhaps? # already templated to a datastructure, perhaps?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment