Merge pull request #2606 from alexdunae/scrub-invalid-filenames

Remove invalid byte sequences from the sanitized filename
This commit is contained in:
Mitsuhiro Shibuya 2022-05-01 12:20:26 +09:00 committed by GitHub
commit 54f0ef9b1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 0 deletions

View File

@ -291,6 +291,7 @@ module CarrierWave
# Sanitize the filename, to prevent hacking
def sanitize(name)
name = name.scrub
name = name.tr("\\", "/") # work-around for IE
name = File.basename(name)
name = name.gsub(sanitize_regexp,"_")

View File

@ -151,6 +151,11 @@ describe CarrierWave::SanitizedFile do
expect(sanitized_file).to receive(:original_filename).at_least(:once).and_return("ТестоВый Ёжик.jpg")
expect(sanitized_file.filename).to eq("ТестоВый_Ёжик.jpg")
end
it "should remove invalid byte sequences from the filename" do
expect(sanitized_file).to receive(:original_filename).at_least(:once).and_return("test\xDD.jpg")
expect(sanitized_file.filename).to eq("test_.jpg")
end
end
describe "#filename with an overridden sanitize_regexp" do