mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07:00
safe_eval fix (#57188)
* just dont pass locals - also fix globals - added tests * fixed tests
This commit is contained in:
parent
99f9f49eca
commit
b9b0b23015
10 changed files with 73 additions and 12 deletions
|
@ -42,10 +42,14 @@ def safe_eval(expr, locals=None, include_exceptions=False):
|
|||
|
||||
# define certain JSON types
|
||||
# eg. JSON booleans are unknown to python eval()
|
||||
JSON_TYPES = {
|
||||
OUR_GLOBALS = {
|
||||
'__builtins__': {}, # avoid global builtins as per eval docs
|
||||
'false': False,
|
||||
'null': None,
|
||||
'true': True,
|
||||
# also add back some builtins we do need
|
||||
'True': True,
|
||||
'False': False,
|
||||
}
|
||||
|
||||
# this is the whitelist of AST nodes we are going to
|
||||
|
@ -138,7 +142,7 @@ def safe_eval(expr, locals=None, include_exceptions=False):
|
|||
# Note: passing our own globals and locals here constrains what
|
||||
# callables (and other identifiers) are recognized. this is in
|
||||
# addition to the filtering of builtins done in CleansingNodeVisitor
|
||||
result = eval(compiled, JSON_TYPES, dict(locals))
|
||||
result = eval(compiled, OUR_GLOBALS, dict(locals))
|
||||
|
||||
if include_exceptions:
|
||||
return (result, None)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue