Skip to content

Commit 4317871

Browse files
committed
Fix Content-Type allowlist bypass vulnerability remained
Refs. GHSA-vfmv-jfc5-pjjw
1 parent 0fcff94 commit 4317871

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
@@ -324,7 +324,7 @@ def sanitize(name)
324324

325325
def existing_content_type
326326
if @file.respond_to?(:content_type) && @file.content_type
327-
@file.content_type.to_s.chomp
327+
Marcel::MimeType.for(declared_type: @file.content_type.to_s.chomp)
328328
end
329329
end
330330

spec/sanitized_file_spec.rb

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

275275
expect { sanitized_file.content_type }.not_to raise_error
276276
end
277+
278+
it "uses the first one when multiple mime types are given using a semicolon" do
279+
file = File.open(file_path("bork.txt"))
280+
allow(file).to receive(:content_type) { 'image/png; text/html' }
281+
282+
sanitized_file = CarrierWave::SanitizedFile.new(file)
283+
284+
expect(sanitized_file.content_type).to eq("image/png")
285+
end
286+
287+
it "uses the first one when multiple mime types are given using a comma" do
288+
file = File.open(file_path("bork.txt"))
289+
allow(file).to receive(:content_type) { 'image/png, text/html' }
290+
291+
sanitized_file = CarrierWave::SanitizedFile.new(file)
292+
293+
expect(sanitized_file.content_type).to eq("image/png")
294+
end
295+
296+
it "drops content type parameters" do
297+
file = File.open(file_path("bork.txt"))
298+
allow(file).to receive(:content_type) { 'text/html; charset=utf-8' }
299+
300+
sanitized_file = CarrierWave::SanitizedFile.new(file)
301+
302+
expect(sanitized_file.content_type).to eq("text/html")
303+
end
277304
end
278305

279306
describe "#content_type=" do

spec/uploader/content_type_whitelist_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)