Skip to content

Commit

Permalink
Don't use single-quote to quote windows command lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Aug 6, 2010
1 parent ba69750 commit 00c0daf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/paperclip/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ def invalid_variables

def shell_quote(string)
return "" if string.nil? or string.blank?
string.split("'").map{|m| "'#{m}'" }.join("\\'")
if self.class.unix?
string.split("'").map{|m| "'#{m}'" }.join("\\'")
else
%{"#{string}"}
end
end

def bit_bucket
return "2>NUL" unless File.exist?("/dev/null")
"2>/dev/null"
self.class.unix? ? "2>/dev/null" : "2>NUL"
end

def self.unix?
File.exist?("/dev/null")
end
end
end
29 changes: 29 additions & 0 deletions test/command_line_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class CommandLineTest < Test::Unit::TestCase
def setup
Paperclip::CommandLine.path = nil
File.stubs(:exist?).with("/dev/null").returns(true)
end

should "take a command and parameters and produce a shell command for bash" do
Expand All @@ -24,6 +25,15 @@ def setup
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
end

should "quote command line options differently if we're on windows" do
File.stubs(:exist?).with("/dev/null").returns(false)
cmd = Paperclip::CommandLine.new("convert",
":one :{two}",
:one => "a.jpg",
:two => "b.png")
assert_equal 'convert "a.jpg" "b.png"', cmd.command
end

should "be able to quote and interpolate dangerous variables" do
cmd = Paperclip::CommandLine.new("convert",
":one :two",
Expand All @@ -32,6 +42,15 @@ def setup
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
end

should "be able to quote and interpolate dangerous variables even on windows" do
File.stubs(:exist?).with("/dev/null").returns(false)
cmd = Paperclip::CommandLine.new("convert",
":one :two",
:one => "`rm -rf`.jpg",
:two => "ha'ha.png")
assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
end

should "add redirection to get rid of stderr in bash" do
File.stubs(:exist?).with("/dev/null").returns(true)
cmd = Paperclip::CommandLine.new("convert",
Expand Down Expand Up @@ -96,4 +115,14 @@ def setup
Paperclip.expects(:log).with("convert a.jpg b.png")
cmd.run
end

should "detect that the system is unix or windows based on presence of /dev/null" do
File.stubs(:exist?).returns(true)
assert Paperclip::CommandLine.unix?
end

should "detect that the system is not unix or windows based on absence of /dev/null" do
File.stubs(:exist?).returns(false)
assert ! Paperclip::CommandLine.unix?
end
end

0 comments on commit 00c0daf

Please sign in to comment.