Skip to content

Commit

Permalink
Use string interpolation for environment variables to avoid escaping …
Browse files Browse the repository at this point in the history
…issues with sprintf (#280)

Use string interpolation instead of sprintf to avoid escaping issues
  • Loading branch information
Sinjo authored and mattbrictson committed Oct 5, 2016
1 parent 7fff680 commit 72a39a2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ appear at the top.

* Add your entries below here, remember to credit yourself however you want
to be credited!
* Use string interpolation for environment variables to avoid escaping issues
with sprintf
[PR #280](https://github.com/capistrano/sshkit/pull/280)
@Sinjo - Chris Sinjakli

## [1.11.3][] (2016-09-16)

Expand Down
4 changes: 2 additions & 2 deletions lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ def environment_string

def with(&_block)
return yield unless environment_hash.any?
sprintf("( export #{environment_string} ; %s )", yield)
"( export #{environment_string} ; #{yield} )"
end

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

def in_background(&_block)
Expand Down
6 changes: 6 additions & 0 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def test_double_quotes_are_escaped_in_env
assert_equal %{( export FOO="asdf\\\"hjkl" ; /usr/bin/env rails server )}, c.to_command
end

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

def test_including_the_env_doesnt_addressively_escape
SSHKit.config = nil
c = Command.new(:rails, 'server', env: {path: '/example:$PATH'})
Expand Down

0 comments on commit 72a39a2

Please sign in to comment.