|
Description:
Provides a retricted enviroment for dynamically executing a subset of the Python language suitable for configuration scripts, end-user oriented scripting and storing data in a user-friendly way.
Source: Text Source
import inspect, compiler.ast
import thread, time
DEBUG = False
all_ast_nodes = \
[name for (name, obj) in inspect.getmembers(compiler.ast)
if inspect.isclass(obj) and issubclass(obj, compiler.ast.Node)]
all_builtins = \
[name for (name, obj) in inspect.getmembers(__builtins__)
if inspect.isbuiltin(obj) or (inspect.isclass(obj) and \
not issubclass(obj, Exception))]
def classname(obj):
return obj.__class__.__name__
def is_valid_ast_node(name):
return name in all_ast_nodes
def is_valid_builtin(name):
return name in all_builtins
def get_node_lineno(node):
return (node.lineno) and node.lineno or 0
unallowed_ast_nodes = [
'Backquote',
'Exec',
'From',
'Import',
'Raise',
'TryExcept', 'TryFinally',
]
unallowed_builtins = [
'__import__',
'compile',
'delattr',
'dir',
'eval', 'execfile', 'file',
'getattr', 'globals', 'hasattr',
'input',
'locals',
'open',
'raw_input',
'reload',
'setattr',
'vars',
]
for ast_name in unallowed_ast_nodes:
assert(is_valid_ast_node(ast_name))
for name in unallowed_builtins:
assert(is_valid_builtin(name))
def is_unallowed_ast_node(kind):
return kind in unallowed_ast_nodes
def is_unallowed_builtin(name):
return name in unallowed_builtins
unallowed_attr = [
'im_class', 'im_func', 'im_self',
'func_code', 'func_defaults', 'func_globals', 'func_name',
'tb_frame', 'tb_next',
'f_back', 'f_builtins', 'f_code', 'f_exc_traceback',
'f_exc_type', 'f_exc_value', 'f_globals', 'f_locals']
def is_unallowed_attr(name):
return (name[:2] == '__' and name[-2:] == '__') or \
(name in unallowed_attr)
class SafeEvalError(object):
"""
Base class for all which occur while walking the AST.
Attributes:
errmsg = short decription about the nature of the error
lineno = line offset to where error occured in source code
"""
def __init__(self, errmsg, lineno):
self.errmsg, self.lineno = errmsg, lineno
def __str__(self):
return "line %d : %s" % (self.lineno, self.errmsg)
class SafeEvalASTNodeError(SafeEvalError):
"Expression/statement in AST evaluates to a restricted AST node type."
pass
class SafeEvalBuiltinError(SafeEvalError):
"Expression/statement in tried to access a restricted builtin."
pass
class SafeEvalAttrError(SafeEvalError):
"Expression/statement in tried to access a restricted attribute."
pass
class SafeEvalVisitor(object):
"""
Data-driven visitor which walks the AST for some code and makes
sure it doesn't contain any expression/statements which are
declared as restricted in 'unallowed_ast_nodes'. We'll also make
sure that there aren't any attempts to access/lookup restricted
builtin declared in 'unallowed_builtins'. By default we also won't
allow access to lowlevel stuff which can be used to dynamically
access non-local envrioments.
Interface:
walk(ast) = validate AST and return True if AST is 'safe'
Attributes:
errors = list of SafeEvalError if walk() returned False
Implementation:
The visitor will automatically generate methods for all of the
available AST node types and redirect them to self.ok or self.fail
reflecting the configuration in 'unallowed_ast_nodes'. While
walking the AST we simply forward the validating step to each of
node callbacks which take care of reporting errors.
"""
def __init__(self):
"Initialize visitor by generating callbacks for all AST node types."
self.errors = []
for ast_name in all_ast_nodes:
if getattr(self, 'visit' + ast_name, None): continue
if is_unallowed_ast_node(ast_name):
setattr(self, 'visit' + ast_name, self.fail)
else:
setattr(self, 'visit' + ast_name, self.ok)
def walk(self, ast):
"Validate each node in AST and return True if AST is 'safe'."
self.visit(ast)
return self.errors == []
def visit(self, node, *args):
"Recursively validate node and all of its children."
fn = getattr(self, 'visit' + classname(node))
if DEBUG: self.trace(node)
fn(node, *args)
for child in node.getChildNodes():
self.visit(child, *args)
def visitName(self, node, *args):
"Disallow any attempts to access a restricted builtin/attr."
name = node.getChildren()[0]
lineno = get_node_lineno(node)
if is_unallowed_builtin(name):
self.errors.append(SafeEvalBuiltinError( \
"access to builtin '%s' is denied" % name, lineno))
elif is_unallowed_attr(name):
self.errors.append(SafeEvalAttrError( \
"access to attribute '%s' is denied" % name, lineno))
def visitGetattr(self, node, *args):
"Disallow any attempts to access a restricted attribute."
name = node.attrname
lineno = get_node_lineno(node)
if is_unallowed_attr(name):
self.errors.append(SafeEvalAttrError( \
"access to attribute '%s' is denied" % name, lineno))
def ok(self, node, *args):
"Default callback for 'harmless' AST nodes."
pass
def fail(self, node, *args):
"Default callback for unallowed AST nodes."
lineno = get_node_lineno(node)
self.errors.append(SafeEvalASTNodeError( \
"execution of '%s' statements is denied" % classname(node),
lineno))
def trace(self, node):
"Debugging utility for tracing the validation of AST nodes."
print classname(node)
for attr in dir(node):
if attr[:2] != '__':
print ' ' * 4, "%-15.15s" % attr, getattr(node, attr)
class SafeEvalException(Exception):
"Base class for all safe-eval related errors."
pass
class SafeEvalCodeException(SafeEvalException):
"""
Exception class for reporting all errors which occured while
validating AST for source code in safe_eval().
Attributes:
code = raw source code which failed to validate
errors = list of SafeEvalError
"""
def __init__(self, code, errors):
self.code, self.errors = code, errors
def __str__(self):
return '\n'.join([str(err) for err in self.errors])
class SafeEvalContextException(SafeEvalException):
"""
Exception class for reporting unallowed objects found in the dict
intended to be used as the local enviroment in safe_eval().
Attributes:
keys = list of keys of the unallowed objects
errors = list of strings describing the nature of the error
for each key in 'keys'
"""
def __init__(self, keys, errors):
self.keys, self.errors = keys, errors
def __str__(self):
return '\n'.join([str(err) for err in self.errors])
class SafeEvalTimeoutException(SafeEvalException):
"""
Exception class for reporting that code evaluation execeeded
the given timelimit.
Attributes:
timeout = time limit in seconds
"""
def __init__(self, timeout):
self.timeout = timeout
def __str__(self):
return "Timeout limit execeeded (%s secs) during exec" % self.timeout
def exec_timed(code, context, timeout_secs):
"""
Dynamically execute 'code' using 'context' as the global enviroment.
SafeEvalTimeoutException is raised if execution does not finish within
the given timelimit.
"""
assert(timeout_secs > 0)
signal_finished = False
def alarm(secs):
def wait(secs):
for n in xrange(timeout_secs):
time.sleep(1)
if signal_finished: break
else:
thread.interrupt_main()
thread.start_new_thread(wait, (secs,))
try:
alarm(timeout_secs)
exec code in context
signal_finished = True
except KeyboardInterrupt:
raise SafeEvalTimeoutException(timeout_secs)
def safe_eval(code, context = {}, timeout_secs = 5):
"""
Validate source code and make sure it contains no unauthorized
expression/statements as configured via 'unallowed_ast_nodes' and
'unallowed_builtins'. By default this means that code is not
allowed import modules or access dangerous builtins like 'open' or
'eval'. If code is considered 'safe' it will be executed via
'exec' using 'context' as the global environment. More details on
how code is executed can be found in the Python Reference Manual
section 6.14 (ignore the remark on '__builtins__'). The 'context'
enviroment is also validated and is not allowed to contain modules
or builtins. The following exception will be raised on errors:
if 'context' contains unallowed objects =
SafeEvalContextException
if code is didn't validate and is considered 'unsafe' =
SafeEvalCodeException
if code did not execute within the given timelimit =
SafeEvalTimeoutException
"""
ctx_errkeys, ctx_errors = [], []
for (key, obj) in context.items():
if inspect.isbuiltin(obj):
ctx_errkeys.append(key)
ctx_errors.append("key '%s' : unallowed builtin %s" % (key, obj))
if inspect.ismodule(obj):
ctx_errkeys.append(key)
ctx_errors.append("key '%s' : unallowed module %s" % (key, obj))
if ctx_errors:
raise SafeEvalContextException(ctx_errkeys, ctx_errors)
ast = compiler.parse(code)
checker = SafeEvalVisitor()
if checker.walk(ast):
exec_timed(code, context, timeout_secs)
else:
raise SafeEvalCodeException(code, checker.errors)
import unittest
class TestSafeEval(unittest.TestCase):
def test_builtin(self):
self.assertRaises(SafeEvalException,
safe_eval, "open('test.txt', 'w')")
def test_getattr(self):
self.assertRaises(SafeEvalException, \
safe_eval, "getattr(int, '__abs__')")
def test_func_globals(self):
self.assertRaises(SafeEvalException, \
safe_eval, "def x(): pass; print x.func_globals")
def test_lowlevel(self):
self.assertRaises(SafeEvalException, \
safe_eval, "().__class__.mro()[1].__subclasses__()")
def test_timeout_ok(self):
def test(): time.sleep(2)
env = {'test':test}
safe_eval("test()", env, timeout_secs = 5)
def test_timeout_exceed(self):
self.assertRaises(SafeEvalException, \
safe_eval, "while 1: pass")
def test_invalid_context(self):
env = {'f' : __builtins__.open, 'g' : time}
self.assertRaises(SafeEvalException, \
safe_eval, "print 1", env)
def test_callback(self):
self.value = 0
def test(): self.value = 1
env = {'test':test}
safe_eval("test()", env)
self.assertEqual(self.value, 1)
if __name__ == "__main__":
unittest.main()
Discussion:
Introducing "eval" into the picture is not for the faint of heart and is unusable as a frontend for end-user targeted scripting due to __builtins__. This module implements a restricted enviroment which makes "eval" somewhat secure albeit for a subset for the Python language, which is perfectly fine for configuration scripts etc.
By default the following restrictions are imposed:
* importing modules is disabled
* unsafe builtins are disabled
* timeout limit ('while 1:pass' can't block forever)
* getattr, setattr, delattr are disabled
* lowlevel attributes like __subclasses__ are disabled
* enviroment passed to 'eval' can't contain modules or builtins
* no exception handling is allowed
None of these restrictions should have any effect on the kind of code you need for storing data or doing basic scripting.
The implementation is quite simple, we inspect the abstract syntax tree (AST) and make sure that the code doesn't try to access dangerous builtins/attributes or execute unsage statements like importing modules.
|