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
3761bc11
Commit
3761bc11
authored
Jun 30, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
When parsing json from untrusted sources, remove templating tags
parent
d36195a3
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
41 additions
and
19 deletions
+41
-19
lib/ansible/inventory/script.py
+1
-1
lib/ansible/runner/__init__.py
+1
-1
lib/ansible/runner/return_data.py
+1
-2
lib/ansible/utils/__init__.py
+38
-4
lib/ansible/utils/template.py
+0
-11
No files found.
lib/ansible/inventory/script.py
View file @
3761bc11
...
...
@@ -49,7 +49,7 @@ class InventoryScript(object):
def
_parse
(
self
,
err
):
all_hosts
=
{}
self
.
raw
=
utils
.
parse_json
(
self
.
data
)
self
.
raw
=
utils
.
parse_json
(
self
.
data
,
from_remote
=
True
)
all
=
Group
(
'all'
)
groups
=
dict
(
all
=
all
)
group
=
None
...
...
lib/ansible/runner/__init__.py
View file @
3761bc11
...
...
@@ -507,7 +507,7 @@ class Runner(object):
cmd2
=
"rm -rf
%
s >/dev/null 2>&1"
%
tmp
self
.
_low_level_exec_command
(
conn
,
cmd2
,
tmp
,
sudoable
=
False
)
data
=
utils
.
parse_json
(
res
[
'stdout'
])
data
=
utils
.
parse_json
(
res
[
'stdout'
]
,
from_remote
=
True
)
if
'parsed'
in
data
and
data
[
'parsed'
]
==
False
:
data
[
'msg'
]
+=
res
[
'stderr'
]
return
ReturnData
(
conn
=
conn
,
result
=
data
)
...
...
lib/ansible/runner/return_data.py
View file @
3761bc11
...
...
@@ -43,8 +43,7 @@ class ReturnData(object):
self
.
diff
=
diff
if
type
(
self
.
result
)
in
[
str
,
unicode
]:
self
.
result
=
utils
.
parse_json
(
self
.
result
)
self
.
result
=
utils
.
parse_json
(
self
.
result
,
from_remote
=
True
)
if
self
.
host
is
None
:
raise
Exception
(
"host not set"
)
...
...
lib/ansible/utils/__init__.py
View file @
3761bc11
...
...
@@ -313,7 +313,38 @@ def json_loads(data):
return
json
.
loads
(
data
)
def
parse_json
(
raw_data
):
def
_clean_data
(
orig_data
):
''' remove template tags from a string '''
data
=
orig_data
if
isinstance
(
orig_data
,
basestring
):
for
pattern
,
replacement
in
((
'{{'
,
'{#'
),
(
'}}'
,
'#}'
),
(
'{
%
'
,
'{#'
),
(
'
%
}'
,
'#}'
)):
data
=
data
.
replace
(
pattern
,
replacement
)
return
data
def
_clean_data_struct
(
orig_data
):
'''
walk a complex data structure, and use _clean_data() to
remove any template tags that may exist
'''
if
isinstance
(
orig_data
,
dict
):
data
=
orig_data
.
copy
()
for
key
in
data
:
new_key
=
_clean_data_struct
(
key
)
new_val
=
_clean_data_struct
(
data
[
key
])
if
key
!=
new_key
:
del
data
[
key
]
data
[
new_key
]
=
new_val
elif
isinstance
(
orig_data
,
list
):
data
=
orig_data
[:]
for
i
in
range
(
0
,
len
(
data
)):
data
[
i
]
=
_clean_data_struct
(
data
[
i
])
elif
isinstance
(
orig_data
,
basestring
):
data
=
_clean_data
(
orig_data
)
else
:
data
=
orig_data
return
data
def
parse_json
(
raw_data
,
from_remote
=
False
):
''' this version for module return data only '''
orig_data
=
raw_data
...
...
@@ -322,7 +353,7 @@ def parse_json(raw_data):
data
=
filter_leading_non_json_lines
(
raw_data
)
try
:
re
turn
json
.
loads
(
data
)
re
sults
=
json
.
loads
(
data
)
except
:
# not JSON, but try "Baby JSON" which allows many of our modules to not
# require JSON and makes writing modules in bash much simpler
...
...
@@ -332,7 +363,6 @@ def parse_json(raw_data):
except
:
print
"failed to parse json: "
+
data
raise
for
t
in
tokens
:
if
"="
not
in
t
:
raise
errors
.
AnsibleError
(
"failed to parse:
%
s"
%
orig_data
)
...
...
@@ -347,7 +377,11 @@ def parse_json(raw_data):
results
[
key
]
=
value
if
len
(
results
.
keys
())
==
0
:
return
{
"failed"
:
True
,
"parsed"
:
False
,
"msg"
:
orig_data
}
return
results
if
from_remote
:
results
=
_clean_data_struct
(
results
)
return
results
def
smush_braces
(
data
):
''' smush Jinaj2 braces so unresolved templates like {{ foo }} don't get parsed weird by key=value code '''
...
...
lib/ansible/utils/template.py
View file @
3761bc11
...
...
@@ -80,7 +80,6 @@ class Flags:
FILTER_PLUGINS
=
None
_LISTRE
=
re
.
compile
(
r"(\w+)\[(\d+)\]"
)
JINJA2_OVERRIDE
=
'#jinja2:'
def
lookup
(
name
,
*
args
,
**
kwargs
):
from
ansible
import
utils
...
...
@@ -228,16 +227,6 @@ def template_from_file(basedir, path, vars, vault_password=None):
except
:
raise
errors
.
AnsibleError
(
"unable to read
%
s"
%
realpath
)
# Get jinja env overrides from template
if
data
.
startswith
(
JINJA2_OVERRIDE
):
eol
=
data
.
find
(
'
\n
'
)
line
=
data
[
len
(
JINJA2_OVERRIDE
):
eol
]
data
=
data
[
eol
+
1
:]
for
pair
in
line
.
split
(
','
):
(
key
,
val
)
=
pair
.
split
(
':'
)
setattr
(
environment
,
key
.
strip
(),
ast
.
literal_eval
(
val
.
strip
()))
environment
.
template_class
=
J2Template
try
:
t
=
environment
.
from_string
(
data
)
...
...
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