Skip to content

Commit 5431690

Browse files
committed
avoid duplicate calls to env methods
1 parent 5fa26ba commit 5431690

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ appear at the top.
66
## [Unreleased][]
77

88
* Your contribution here!
9+
* [#453](https://github.com/capistrano/sshkit/pull/453): fix and unify shell escaping for user/group/directory - [@grosser](https://github.com/grosser)
910

1011
## [1.18.2][] (2019-02-03)
1112

lib/sshkit/backends/abstract.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'shellwords'
2+
13
module SSHKit
24

35
module Backend
@@ -81,12 +83,12 @@ def execute(*args)
8183
def within(directory, &_block)
8284
(@pwd ||= []).push directory.to_s
8385
execute <<-EOTEST, verbosity: Logger::DEBUG
84-
if test ! -d #{File.join(@pwd)}
85-
then echo "Directory does not exist '#{File.join(@pwd)}'" 1>&2
86+
if test ! -d #{File.join(@pwd).shellescape}
87+
then echo "Directory does not exist '#{File.join(@pwd).shellescape}'" 1>&2
8688
false
8789
fi
88-
EOTEST
89-
yield
90+
EOTEST
91+
yield
9092
ensure
9193
@pwd.pop
9294
end
@@ -108,8 +110,8 @@ def as(who, &_block)
108110
@group = nil
109111
end
110112
execute <<-EOTEST, verbosity: Logger::DEBUG
111-
if ! sudo -u #{@user} whoami > /dev/null
112-
then echo "You cannot switch to user '#{@user}' using sudo, please check the sudoers file" 1>&2
113+
if ! sudo -u #{@user.to_s.shellescape} whoami > /dev/null
114+
then echo "You cannot switch to user '#{@user.to_s.shellescape}' using sudo, please check the sudoers file" 1>&2
113115
false
114116
fi
115117
EOTEST

lib/sshkit/command.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ def environment_string
162162
end
163163

164164
def with(&_block)
165-
return yield unless environment_hash.any?
166-
"( export #{environment_string} ; #{yield} )"
165+
env_string = environment_string
166+
return yield if env_string.empty?
167+
"( export #{env_string} ; #{yield} )"
167168
end
168169

169170
def user(&_block)
170171
return yield unless options[:user]
171-
"sudo -u #{options[:user].to_s.shellescape} #{environment_string + " " unless environment_string.empty?}-- sh -c #{yield.shellescape}"
172+
env_string = environment_string
173+
"sudo -u #{options[:user].to_s.shellescape} #{env_string + " " unless env_string.empty?}-- sh -c #{yield.shellescape}"
172174
end
173175

174176
def in_background(&_block)

0 commit comments

Comments
 (0)