Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
eeb59736
Commit
eeb59736
authored
Jun 26, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Further safe_eval fixes
parent
14cf3c4d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
20 deletions
+13
-20
lib/ansible/utils/__init__.py
+13
-20
No files found.
lib/ansible/utils/__init__.py
View file @
eeb59736
...
...
@@ -45,7 +45,6 @@ import getpass
import
sys
import
json
#import vault
from
vault
import
VaultLib
VERBOSITY
=
0
...
...
@@ -69,6 +68,11 @@ try:
except
:
pass
try
:
import
builtin
except
ImportError
:
import
__builtin__
as
builtin
KEYCZAR_AVAILABLE
=
False
try
:
try
:
...
...
@@ -1039,7 +1043,6 @@ def safe_eval(expr, locals={}, include_exceptions=False):
SAFE_NODES
=
set
(
(
ast
.
Add
,
ast
.
Attribute
,
ast
.
BinOp
,
ast
.
Call
,
ast
.
Compare
,
...
...
@@ -1066,34 +1069,24 @@ def safe_eval(expr, locals={}, include_exceptions=False):
)
)
# builtin functions that are safe to call
BUILTIN_WHITELIST
=
[
'abs'
,
'all'
,
'any'
,
'basestring'
,
'bin'
,
'bool'
,
'buffer'
,
'bytearray'
,
'bytes'
,
'callable'
,
'chr'
,
'cmp'
,
'coerce'
,
'complex'
,
'copyright'
,
'credits'
,
'dict'
,
'dir'
,
'divmod'
,
'enumerate'
,
'exit'
,
'float'
,
'format'
,
'frozenset'
,
'getattr'
,
'globals'
,
'hasattr'
,
'hash'
,
'hex'
,
'id'
,
'int'
,
'intern'
,
'isinstance'
,
'issubclass'
,
'iter'
,
'len'
,
'license'
,
'list'
,
'locals'
,
'long'
,
'map'
,
'max'
,
'memoryview'
,
'min'
,
'next'
,
'oct'
,
'ord'
,
'pow'
,
'print'
,
'property'
,
'quit'
,
'range'
,
'reversed'
,
'round'
,
'set'
,
'slice'
,
'sorted'
,
'str'
,
'sum'
,
'tuple'
,
'unichr'
,
'unicode'
,
'vars'
,
'xrange'
,
'zip'
,
]
filter_list
=
[]
for
filter
in
filter_loader
.
all
():
filter_list
.
extend
(
filter
.
filters
()
.
keys
())
CALL_WHITELIST
=
BUILTIN_WHITELIST
+
filter_list
+
C
.
DEFAULT_CALLABLE_WHITELIST
CALL_WHITELIST
=
C
.
DEFAULT_CALLABLE_WHITELIST
+
filter_list
class
CleansingNodeVisitor
(
ast
.
NodeVisitor
):
def
generic_visit
(
self
,
node
):
def
generic_visit
(
self
,
node
,
inside_call
=
False
):
if
type
(
node
)
not
in
SAFE_NODES
:
raise
Exception
(
"invalid expression (
%
s)"
%
expr
)
elif
isinstance
(
node
,
ast
.
Call
):
if
not
isinstance
(
node
.
func
,
ast
.
Attribute
)
and
node
.
func
.
id
not
in
CALL_WHITELIST
:
raise
Exception
(
"invalid function:
%
s"
%
node
.
func
.
id
)
inside_call
=
True
elif
isinstance
(
node
,
ast
.
Name
)
and
inside_call
:
if
hasattr
(
builtin
,
node
.
id
)
and
node
.
id
not
in
CALL_WHITELIST
:
raise
Exception
(
"invalid function:
%
s"
%
node
.
id
)
# iterate over all child nodes
for
child_node
in
ast
.
iter_child_nodes
(
node
):
s
uper
(
CleansingNodeVisitor
,
self
)
.
visit
(
child_node
)
s
elf
.
generic_visit
(
child_node
,
inside_call
)
if
not
isinstance
(
expr
,
basestring
):
# already templated to a datastructure, perhaps?
...
...
@@ -1101,9 +1094,9 @@ def safe_eval(expr, locals={}, include_exceptions=False):
return
(
expr
,
None
)
return
expr
cnv
=
CleansingNodeVisitor
()
try
:
parsed_tree
=
ast
.
parse
(
expr
,
mode
=
'eval'
)
cnv
=
CleansingNodeVisitor
()
cnv
.
visit
(
parsed_tree
)
compiled
=
compile
(
parsed_tree
,
expr
,
'eval'
)
result
=
eval
(
compiled
,
{},
locals
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment