From dc48d27dd2c49e95a719d7f7596f0b68e0ddef7f Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Wed, 3 Feb 2016 19:11:55 +0100 Subject: [PATCH 1/2] Defined JSON booleans in global context for python eval() We define 'false' and 'true' as variables so that python eval() recognizes them as False and True. This fixes #14291. --- lib/ansible/template/safe_eval.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ansible/template/safe_eval.py b/lib/ansible/template/safe_eval.py index ffb48611e2..174724c7ba 100644 --- a/lib/ansible/template/safe_eval.py +++ b/lib/ansible/template/safe_eval.py @@ -41,6 +41,13 @@ 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, + '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 @@ -116,7 +123,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, {}, dict(locals)) + result = eval(compiled, JSON_TYPES, dict(locals)) if include_exceptions: return (result, None) From 2e171610e06ebcb326ef69ffa89ae46ea1081418 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Thu, 4 Feb 2016 14:19:46 +0100 Subject: [PATCH 2/2] Also add 'null' as a possible JSON value --- lib/ansible/template/safe_eval.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ansible/template/safe_eval.py b/lib/ansible/template/safe_eval.py index 174724c7ba..d82373a6fb 100644 --- a/lib/ansible/template/safe_eval.py +++ b/lib/ansible/template/safe_eval.py @@ -45,6 +45,7 @@ def safe_eval(expr, locals={}, include_exceptions=False): # eg. JSON booleans are unknown to python eval() JSON_TYPES = { 'false': False, + 'null': None, 'true': True, }