Skip to content

Commit 00676e2

Browse files
committed
Fix Content-Type allowlist bypass vulnerability remained
Refs. GHSA-vfmv-jfc5-pjjw
1 parent a18e13c commit 00676e2

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

lib/carrierwave/sanitized_file.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def sanitize(name)
307307
def declared_content_type
308308
@declared_content_type ||
309309
if @file.respond_to?(:content_type) && @file.content_type
310-
@file.content_type.to_s.chomp
310+
Marcel::MimeType.for(declared_type: @file.content_type.to_s.chomp)
311311
end
312312
end
313313

spec/sanitized_file_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,33 @@
306306

307307
expect { sanitized_file.content_type }.not_to raise_error
308308
end
309+
310+
it "uses the first one when multiple mime types are given using a semicolon" do
311+
file = File.open(file_path("bork.txt"))
312+
allow(file).to receive(:content_type) { 'image/png; text/html' }
313+
314+
sanitized_file = CarrierWave::SanitizedFile.new(file)
315+
316+
expect(sanitized_file.content_type).to eq("image/png")
317+
end
318+
319+
it "uses the first one when multiple mime types are given using a comma" do
320+
file = File.open(file_path("bork.txt"))
321+
allow(file).to receive(:content_type) { 'image/png, text/html' }
322+
323+
sanitized_file = CarrierWave::SanitizedFile.new(file)
324+
325+
expect(sanitized_file.content_type).to eq("image/png")
326+
end
327+
328+
it "drops content type parameters" do
329+
file = File.open(file_path("bork.txt"))
330+
allow(file).to receive(:content_type) { 'text/html; charset=utf-8' }
331+
332+
sanitized_file = CarrierWave::SanitizedFile.new(file)
333+
334+
expect(sanitized_file.content_type).to eq("text/html")
335+
end
309336
end
310337

311338
describe "#content_type=" do

spec/uploader/content_type_allowlist_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,6 @@
8787
expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError)
8888
end
8989
end
90-
91-
context "when the allowlist contains charset" do
92-
before do
93-
allow(uploader).to receive(:content_type_allowlist).and_return(%r{text/plain;\s*charset=utf-8})
94-
end
95-
96-
it "accepts the content with allowed charset" do
97-
allow(bork_file).to receive(:content_type).and_return('text/plain; charset=utf-8')
98-
expect { uploader.cache!(bork_file) }.not_to raise_error
99-
end
100-
101-
it "rejects the content without charset" do
102-
allow(bork_file).to receive(:content_type).and_return('text/plain')
103-
expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError)
104-
end
105-
end
10690
end
10791

10892
context "when there is a whitelist" do

0 commit comments

Comments
 (0)