Please review pull request #698: Bug/2.7.x/14060 incorrect quoting in shell exec provider opened by (daniel-pittman)

Description:

(#14060) Fix quoting of commands to interpolate inside the shell.

The shell exec provider was supposed to emulate the behaviour of 0.25 exec,
which was to pass the content through the default shell to get it executed.

Unfortunately, it got quoting a bit wrong, and ended up interpolating
variables at the wrong point - it used double quotes where single quotes were
really what was desired.

Thanks to Matthew Byng-Maddick for the fix.

  • Opened: Fri Apr 20 21:15:05 UTC 2012
  • Based on: puppetlabs:2.7.x (8310d33462382d1ff51f5bfdc48f49503be489dd)
  • Requested merge: daniel-pittman:bug/2.7.x/14060-incorrect-quoting-in-shell-exec-provider (00ef57307517512a1158a7cb1a1f6f70681fdb67)

Diff follows:

diff --git a/lib/puppet/provider/exec/shell.rb b/lib/puppet/provider/exec/shell.rb
index ad21710..0569d14 100644
--- a/lib/puppet/provider/exec/shell.rb
+++ b/lib/puppet/provider/exec/shell.rb
@@ -16,7 +16,7 @@
   EOT
 
   def run(command, check = false)
-    command = %Q{/bin/sh -c "#{command.gsub(/"/,'\"')}"}
+    command = "/bin/sh -c '" + command.gsub(/'/,"'\"'\"'") + "'"
     super(command, check)
   end
 
diff --git a/spec/unit/provider/exec/shell_spec.rb b/spec/unit/provider/exec/shell_spec.rb
index 62036a7..aafb572 100755
--- a/spec/unit/provider/exec/shell_spec.rb
+++ b/spec/unit/provider/exec/shell_spec.rb
@@ -1,50 +1,53 @@
 #!/usr/bin/env rspec
 require 'spec_helper'
 
-provider_class = Puppet::Type.type(:exec).provider(:shell)
-
-describe provider_class, :unless => Puppet.features.microsoft_windows? do
-  before :each do
-    @resource = Puppet::Resource.new(:exec, 'foo')
-    @provider = provider_class.new(@resource)
-  end
+describe Puppet::Type.type(:exec).provider(:shell), :unless => Puppet.features.microsoft_windows? do
+  let :resource do Puppet::Resource.new(:exec, 'foo') end
+  let :provider do described_class.new(resource) end
 
   describe "#run" do
     it "should be able to run builtin shell commands" do
-      output, status = @provider.run("if [ 1 = 1 ]; then echo 'blah'; fi")
+      output, status = provider.run("if [ 1 = 1 ]; then echo 'blah'; fi")
       status.exitstatus.should == 0
       output.should == "blah\n"
     end
 
     it "should be able to run commands with single quotes in them" do
-      output, status = @provider.run("echo 'foo  bar'")
+      output, status = provider.run("echo 'foo  bar'")
       status.exitstatus.should == 0
       output.should == "foo  bar\n"
     end
 
     it "should be able to run commands with double quotes in them" do
-      output, status = @provider.run('echo "foo  bar"')
+      output, status = provider.run('echo "foo  bar"')
       status.exitstatus.should == 0
       output.should == "foo  bar\n"
     end
 
     it "should be able to run multiple commands separated by a semicolon" do
-      output, status = @provider.run("echo 'foo' ; echo 'bar'")
+      output, status = provider.run("echo 'foo' ; echo 'bar'")
       status.exitstatus.should == 0
       output.should == "foo\nbar\n"
     end
 
     it "should be able to read values from the environment parameter" do
-      @resource[:environment] = "FOO=bar"
-      output, status = @provider.run("echo $FOO")
+      resource[:environment] = "FOO=bar"
+      output, status = provider.run("echo $FOO")
       status.exitstatus.should == 0
       output.should == "bar\n"
     end
+
+    it "#14060: should interpolate inside the subshell, not outside it" do
+      resource[:environment] = "foo=outer"
+      output, status = provider.run("foo=inner; echo \"foo is $foo\"")
+      status.exitstatus.should == 0
+      output.should == "foo is inner\n"
+    end
   end
 
   describe "#validatecmd" do
     it "should always return true because builtins don't need path or to be fully qualified" do
-      @provider.validatecmd('whateverdoesntmatter').should == true
+      provider.validatecmd('whateverdoesntmatter').should == true
     end
   end
 end

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to