Skip to content

Commit 5fa26ba

Browse files
committed
fix and unify shell escaping for user/group/directory
1 parent df110b2 commit 5fa26ba

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/sshkit/command.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'digest/sha1'
22
require 'securerandom'
3+
require 'shellwords'
34

45
# @author Lee Hambley
56
module SSHKit
@@ -145,7 +146,7 @@ def should_map?
145146

146147
def within(&_block)
147148
return yield unless options[:in]
148-
"cd #{options[:in]} && #{yield}"
149+
"cd #{options[:in].shellescape} && #{yield}"
149150
end
150151

151152
def environment_hash
@@ -167,7 +168,7 @@ def with(&_block)
167168

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

173174
def in_background(&_block)
@@ -182,7 +183,7 @@ def umask(&_block)
182183

183184
def group(&_block)
184185
return yield unless options[:group]
185-
%Q(sg #{options[:group]} -c "#{yield}")
186+
"sg #{options[:group].to_s.shellescape} -c #{yield.shellescape}"
186187
# We could also use the so-called heredoc format perhaps:
187188
#"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
188189
end

test/unit/test_command.rb

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ def test_including_the_env_with_string_keys
5151
def test_double_quotes_are_escaped_in_env
5252
SSHKit.config = nil
5353
c = Command.new(:rails, 'server', env: {foo: 'asdf"hjkl'})
54-
assert_equal %{( export FOO="asdf\\\"hjkl" ; /usr/bin/env rails server )}, c.to_command
54+
assert_equal %{( export FOO="asdf\\"hjkl" ; /usr/bin/env rails server )}, c.to_command
5555
end
5656

5757
def test_percentage_symbol_handled_in_env
5858
SSHKit.config = nil
5959
c = Command.new(:rails, 'server', env: {foo: 'asdf%hjkl'}, user: "anotheruser")
60-
assert_equal %{( export FOO="asdf%hjkl" ; sudo -u anotheruser FOO=\"asdf%hjkl\" -- sh -c '/usr/bin/env rails server' )}, c.to_command
60+
assert_equal %{( export FOO="asdf%hjkl" ; sudo -u anotheruser FOO=\"asdf%hjkl\" -- sh -c /usr/bin/env\\ rails\\ server )}, c.to_command
6161
end
6262

6363
def test_including_the_env_doesnt_addressively_escape
@@ -84,6 +84,11 @@ def test_working_in_a_given_directory
8484
assert_equal "cd /opt/sites && /usr/bin/env ls -l", c.to_command
8585
end
8686

87+
def test_working_in_a_given_weird_directory
88+
c = Command.new(:ls, '-l', in: "/opt/sites and stuff")
89+
assert_equal "cd /opt/sites\\ and\\ stuff && /usr/bin/env ls -l", c.to_command
90+
end
91+
8792
def test_working_in_a_given_directory_with_env
8893
c = Command.new(:ls, '-l', in: "/opt/sites", env: {a: :b})
8994
assert_equal %{cd /opt/sites && ( export A="b" ; /usr/bin/env ls -l )}, c.to_command
@@ -97,17 +102,27 @@ def test_having_a_host_passed
97102

98103
def test_working_as_a_given_user
99104
c = Command.new(:whoami, user: :anotheruser)
100-
assert_equal "sudo -u anotheruser -- sh -c '/usr/bin/env whoami'", c.to_command
105+
assert_equal "sudo -u anotheruser -- sh -c /usr/bin/env\\ whoami", c.to_command
106+
end
107+
108+
def test_working_as_a_given_weird_user
109+
c = Command.new(:whoami, user: "mr space |")
110+
assert_equal "sudo -u mr\\ space\\ \\| -- sh -c /usr/bin/env\\ whoami", c.to_command
101111
end
102112

103113
def test_working_as_a_given_group
104114
c = Command.new(:whoami, group: :devvers)
105-
assert_equal 'sg devvers -c "/usr/bin/env whoami"', c.to_command
115+
assert_equal 'sg devvers -c /usr/bin/env\\ whoami', c.to_command
116+
end
117+
118+
def test_working_as_a_given_weird_group
119+
c = Command.new(:whoami, group: "space | group")
120+
assert_equal "sg space\\ \\|\\ group -c /usr/bin/env\\ whoami", c.to_command
106121
end
107122

108123
def test_working_as_a_given_user_and_group
109124
c = Command.new(:whoami, user: :anotheruser, group: :devvers)
110-
assert_equal %Q(sudo -u anotheruser -- sh -c 'sg devvers -c "/usr/bin/env whoami"'), c.to_command
125+
assert_equal %Q(sudo -u anotheruser -- sh -c sg\\ devvers\\ -c\\ /usr/bin/env\\\\\\ whoami), c.to_command
111126
end
112127

113128
def test_umask
@@ -125,13 +140,13 @@ def test_umask_with_working_directory
125140
def test_umask_with_working_directory_and_user
126141
SSHKit.config.umask = '007'
127142
c = Command.new(:touch, 'somefile', in: '/var', user: 'alice')
128-
assert_equal "cd /var && umask 007 && sudo -u alice -- sh -c '/usr/bin/env touch somefile'", c.to_command
143+
assert_equal "cd /var && umask 007 && sudo -u alice -- sh -c /usr/bin/env\\ touch\\ somefile", c.to_command
129144
end
130145

131146
def test_umask_with_env_and_working_directory_and_user
132147
SSHKit.config.umask = '007'
133148
c = Command.new(:touch, 'somefile', user: 'bob', env: {a: 'b'}, in: '/var')
134-
assert_equal %{cd /var && umask 007 && ( export A="b" ; sudo -u bob A="b" -- sh -c '/usr/bin/env touch somefile' )}, c.to_command
149+
assert_equal %{cd /var && umask 007 && ( export A="b" ; sudo -u bob A="b" -- sh -c /usr/bin/env\\ touch\\ somefile )}, c.to_command
135150
end
136151

137152
def test_verbosity_defaults_to_logger_info

0 commit comments

Comments
 (0)