Skip to content

Commit

Permalink
Merge pull request #2562 from pjmartorell/fix/csv-file-content-type
Browse files Browse the repository at this point in the history
Fix bug when reading content_type from a CSV object linked to a file
  • Loading branch information
mshibuya authored Apr 17, 2021
2 parents 2dba2c1 + 68e41d2 commit 1c7bdd2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/carrierwave/sanitized_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def marcel_magic_content_type
end

if type.nil?
type = Marcel::Magic.by_path(file).try(:type)
type = Marcel::Magic.by_path(path).try(:type)
type = 'invalid/invalid' unless type.nil? || type.start_with?('text/')
end

Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/addresses.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
8 changes: 8 additions & 0 deletions spec/sanitized_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@
expect(sanitized_file.content_type).to eq("image/jpeg")
end

it "reads content type of a CSV linked to a file" do
file = File.open(file_path('addresses.csv'))
csv_file = CSV.new(file)
sanitized_file = CarrierWave::SanitizedFile.new(csv_file)

expect(sanitized_file.content_type).to eq("text/csv")
end

it "does not allow spoofing of the mime type" do
file = File.open(file_path("zip.png"))

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'tempfile'
require 'time'
require 'logger'
require 'csv'

require 'carrierwave'
require 'timecop'
Expand Down

0 comments on commit 1c7bdd2

Please sign in to comment.