Fix up the new expand_user method.

quoting anywhere in the user_home_path interferes with shell expansion
so we have to check it for validity ourselves.
This commit is contained in:
Toshio Kuratomi 2014-11-11 13:54:03 -08:00
parent bc4272d2a2
commit 565e5bbdfc

View file

@ -16,9 +16,12 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import pipes
import ansible.constants as C
_USER_HOME_PATH_RE = re.compile(r'^~[_.A-Za-z0-9][-_.A-Za-z0-9]*$')
class ShellModule(object):
def env_prefix(self, **kwargs):
@ -59,9 +62,21 @@ class ShellModule(object):
cmd += ' && echo %s' % basetmp
return cmd
def expand_user(self, user_path):
# Quote the user portion but leave the tilde to be expanded
return 'echo ~%s' % pipes.quote(user_path[1:])
def expand_user(self, user_home_path):
''' Return a command to expand tildes in a path
It can be either "~" or "~username". We use the POSIX definition of
a username:
http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_426
http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_276
'''
# Check that the user_path to expand is safe
if user_home_path != '~':
if not _USER_HOME_PATH_RE.match(user_home_path):
# pipes.quote will make the shell return the string verbatim
user_home_path = pipes.quote(user_home_path)
return 'echo %s' % user_home_path
def checksum(self, path, python_interp):
path = pipes.quote(path)