Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
MIME type detection of Paperclip::MediaTypeSpoofDetector doesn't work…
Browse files Browse the repository at this point in the history
… with old versions of file.

Please see #2527 for details.
  • Loading branch information
Clemens Fuchslocher authored and sidraval committed Jan 30, 2018
1 parent 406ab45 commit a99ffec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/paperclip/media_type_spoof_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def calculated_media_type

def type_from_file_command
begin
Paperclip.run("file", "-b --mime :file", :file => @file.path).split(/[:;]\s+/).first
Paperclip.run("file", "-b --mime :file", file: @file.path).
split(/[:;\s]+/).first
rescue Cocaine::CommandLineError
""
end
Expand Down
14 changes: 14 additions & 0 deletions spec/paperclip/file_command_content_type_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@
assert_equal "application/octet-stream",
Paperclip::FileCommandContentTypeDetector.new("windows").detect
end

context "#type_from_file_command" do
let(:detector) { Paperclip::FileCommandContentTypeDetector.new("html") }

it "does work with the output of old versions of file" do
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
expect(detector.detect).to eq("text/html")
end

it "does work with the output of new versions of file" do
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
expect(detector.detect).to eq("text/html")
end
end
end
15 changes: 15 additions & 0 deletions spec/paperclip/media_type_spoof_detector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,19 @@
Paperclip.options[:content_type_mappings] = {}
end
end

context "#type_from_file_command" do
let(:file) { File.new(fixture_file("empty.html")) }
let(:detector) { Paperclip::MediaTypeSpoofDetector.new(file, "html", "") }

it "does work with the output of old versions of file" do
Paperclip.stubs(:run).returns("text/html charset=us-ascii")
expect(detector.send(:type_from_file_command)).to eq("text/html")
end

it "does work with the output of new versions of file" do
Paperclip.stubs(:run).returns("text/html; charset=us-ascii")
expect(detector.send(:type_from_file_command)).to eq("text/html")
end
end
end

0 comments on commit a99ffec

Please sign in to comment.