Skip to content

Commit

Permalink
Fix escaped quotes bug in Command#group (#425)
Browse files Browse the repository at this point in the history
* Fix escaped quotes bug in Command#group

Command#group incorrectly escapes double quotes, resulting in a syntax
error when specifying the group a command should be executed as. This
issue manifested when user command quotes [changed from double quotes to
single
quotes](fbe1775#diff-4eaa2c55e6802df7b49a8e7a2f7584d5). This fix removes the double quote escaping in order to prevent a syntax error when executing the command.

* fixup! Fix escaped quotes bug in Command#group

* fixup! Fix escaped quotes bug in Command#group
  • Loading branch information
pblesi authored and mattbrictson committed May 15, 2018
1 parent 0b60c66 commit 40995bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ appear at the top.

## [Unreleased][]

* [#425](https://github.com/capistrano/sshkit/pull/425): Command#group incorrectly escapes double quotes, resulting in a a syntax error when specifying the group execution using `as`. This issue manifested when user command quotes changed from double quotes to single quotes. This fix removes the double quote escaping - [@pblesi](https://github.com/pblesi).
* Your contribution here!

## [1.16.0][] (2018-02-03)
Expand Down
2 changes: 1 addition & 1 deletion lib/sshkit/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def umask(&_block)

def group(&_block)
return yield unless options[:group]
"sg #{options[:group]} -c \\\"%s\\\"" % %Q{#{yield}}
%Q(sg #{options[:group]} -c "#{yield}")
# We could also use the so-called heredoc format perhaps:
#"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}}
end
Expand Down
14 changes: 14 additions & 0 deletions test/functional/backends/test_netssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def test_simple_netssh
], command_lines
end

def test_group_netssh
Netssh.new(a_host) do
as user: :root, group: :admin do
execute :touch, 'restart.txt'
end
end.run

command_lines = @output.lines.select { |line| line.start_with?('Command:') }
assert_equal [
"Command: if ! sudo -u root whoami > /dev/null; then echo \"You cannot switch to user 'root' using sudo, please check the sudoers file\" 1>&2; false; fi\n",
"Command: sudo -u root -- sh -c 'sg admin -c \"/usr/bin/env touch restart.txt\"'\n"
], command_lines
end

def test_capture
captured_command_result = nil
Netssh.new(a_host) do |_host|
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ def test_working_as_a_given_user

def test_working_as_a_given_group
c = Command.new(:whoami, group: :devvers)
assert_equal "sg devvers -c \\\"/usr/bin/env whoami\\\"", c.to_command
assert_equal 'sg devvers -c "/usr/bin/env whoami"', c.to_command
end

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

def test_umask
Expand Down

0 comments on commit 40995bf

Please sign in to comment.