Paperclip + RSpec: content_type validation

淺唱寂寞╮ 提交于 2019-12-01 09:27:46

问题


I am using Paperclip in my Rails application for attaching images. I declared validation for content_type in my model as

 validates_attachment :image,
  :content_type => { :content_type => ["image/jpg", "image/gif", "image/png"] }

I have two examples, one with a valid image and other with an invalid image
For an invalid image, i just renamed a .txt file to a .png

 it "Image is valid" do
    image = File.new("#{Rails.root}/spec/support/right.png")
    expect(FactoryGirl.build(:pin, image: image)).to be_valid
 end
 it "Image is invalid" do
   image = File.new("#{Rails.root}/spec/support/wrong.png")
   expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type)
 end

I expected that both my examples should run successfully. BUT, my second example fails. I don't get any error for content_type of wrong.png.

I thought that Paperclip's content_type validation would actually check file format(binary data encoding) of an uploaded file. BUT it seems that here, its just checking for the file extension. Does this validation only check extension of an uploaded file?

I maybe missing something here(configuration?). Is there any other validation available in Paperclip to achieve this? Or should I opt for a Custom Validator in this case?


回答1:


This issue is resolved in Paperclip's latest version 4.1.1 released on February 21, 2014.

Both of my following examples pass correctly now.

it "Image is valid" do
    image = File.new("#{Rails.root}/spec/support/right.png")
    expect(FactoryGirl.build(:pin, image: image)).to be_valid
end
it "Image is invalid" do
   image = File.new("#{Rails.root}/spec/support/wrong.png")
   expect(FactoryGirl.build(:pin, image: image)).to have(1).errors_on(:image_content_type)
end

After a little bit of research found out that, When I upload an invalid image,

For example: spoof(renamed) wrong.txt file as wrong.png and upload.

In prior release of Paperclip, wrong.png passes the content_type validation with flying colors without giving any error because Paperclip only used to check the extensions of the uploaded file and not content within.

Whereas, In the current release of Paperclip 4.1.1, same spoofed wrong.png fails the validation and throws the following error in view:

Image has an extension that does not match its contents

Upon investigating server log entries, I found the following:

Command :: file -b --mime-type '/var/folders/tg/8sxl1vss4fb0sqtcrv3lzcfm0000gn/T/a7f21d0002b0d9d91eb158d702cd930320140317-531-swkmb8' [paperclip] Content Type Spoof: Filename wrong.png (["image/png"]), content type discovered from file command: text/plain. See documentation to allow this combination.

Here, you can see that Paperclip actually checked the content of the uploaded file stating text/plain and also erred out saying Content Type Spoof.

Hope my findings will help others to understand how Paperclip's content-type validation has improved over the time.



来源:https://stackoverflow.com/questions/21610920/paperclip-rspec-content-type-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!