Skip to content

Commit

Permalink
support commands/users/groups with spaces or other strange characters
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Feb 18, 2019
1 parent baff92c commit 18e44f9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'digest/sha1'
require 'securerandom'
require 'shellwords'

# @author Lee Hambley
module SSHKit
Expand Down Expand Up @@ -145,7 +146,7 @@ def should_map?

def within(&_block)
return yield unless options[:in]
sprintf("cd #{options[:in]} && %s", yield)
"cd #{options[:in].shellescape} && #{yield}"
end

def environment_hash
Expand All @@ -155,8 +156,7 @@ def environment_hash
def environment_string
environment_hash.collect do |key,value|
key_string = key.is_a?(Symbol) ? key.to_s.upcase : key.to_s
escaped_value = value.to_s.gsub(/"/, '\"')
%{#{key_string}="#{escaped_value}"}
"#{key_string}=#{value.shellescape}"
end.join(' ')
end

Expand All @@ -167,22 +167,22 @@ def with(&_block)

def user(&_block)
return yield unless options[:user]
"sudo -u #{options[:user]} #{environment_string + " " unless environment_string.empty?}-- sh -c '#{yield}'"
"sudo -u #{options[:user].shellescape} #{environment_string + " " unless environment_string.empty?}-- sh -c #{yield.shellescape}"
end

def in_background(&_block)
return yield unless options[:run_in_background]
sprintf("( nohup %s > /dev/null & )", yield)
"( nohup #{yield} > /dev/null & )"
end

def umask(&_block)
return yield unless SSHKit.config.umask
sprintf("umask #{SSHKit.config.umask} && %s", yield)
"umask #{SSHKit.config.umask} && #{yield}"
end

def group(&_block)
return yield unless options[:group]
%Q(sg #{options[:group]} -c "#{yield}")
"sg #{options[:group].shellescape} -c #{yield.shellescape}"
# We could also use the so-called heredoc format perhaps:
#"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
end
Expand Down Expand Up @@ -213,7 +213,9 @@ def with_redaction

def to_s
if should_map?
[SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ')
arguments = Array(args)
arguments = (arguments.any? ? arguments.shelljoin : [])
[SSHKit.config.command_map[command.to_sym], *arguments].join(" ")
else
command.to_s
end
Expand Down

0 comments on commit 18e44f9

Please sign in to comment.