Defined JSON booleans in global context for python eval()

We define 'false', 'true' and 'null' as variables so that python eval() recognizes them as False, True and None.

This is a backport of a fix from 2.0.0.2 which also affects 1.9.4 (See issue #14291 and PR #14293)

This fixes #14291 for 1.9.4.
This commit is contained in:
Dag Wieers 2016-02-05 17:24:48 +01:00
parent ab0904d051
commit b6e6c52b12

View file

@ -1365,6 +1365,14 @@ def safe_eval(expr, locals={}, include_exceptions=False):
http://stackoverflow.com/questions/12523516/using-ast-and-whitelists-to-make-pythons-eval-safe
'''
# define certain JSON types
# eg. JSON booleans are unknown to python eval()
JSON_TYPES = {
'false': False,
'null': None,
'true': True,
}
# this is the whitelist of AST nodes we are going to
# allow in the evaluation. Any node type other than
# those listed here will raise an exception in our custom
@ -1428,7 +1436,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
parsed_tree = ast.parse(expr, mode='eval')
cnv.visit(parsed_tree)
compiled = compile(parsed_tree, expr, 'eval')
result = eval(compiled, {}, locals)
result = eval(compiled, JSON_TYPES, dict(locals))
if include_exceptions:
return (result, None)