23

I've deployed a rails app to heroku, it was working just fine until I changed domains, locally it works fine and now I've started getting the following error: Paperclip::Errors::MissingRequiredValidatorError.

the s3 env variables are set on heroku and I can't find why this error occurs, help is appreciated.

EDIT: for future reference, it had nothing to do with heroku, once re-bundling, it'll throw the same error locally, as the answer suggest.

Itai Sagi
  • 5,537
  • 12
  • 49
  • 73

4 Answers4

52

With version 4.0 (published 2 days ago) Paperclip requires file/mime validation. Simply add the following to your model:

validates_attachment_content_type :file_name, :content_type => %w(image/jpeg image/jpg image/png)

IMPORTANT: replace file_name and content type so that it fits your project.

It works locally because you probably have not updated paperclip yet.

ozahorulia
  • 9,798
  • 8
  • 48
  • 72
sebvst
  • 753
  • 1
  • 13
  • 20
  • 3
    Also, it seams you need this after `has_attached_file` – complistic Feb 03 '14 at 01:11
  • See Security Validations section of Paperclip docs for additional options as well: https://github.com/thoughtbot/paperclip – steakchaser Feb 03 '14 at 20:45
  • 2
    Additionally, you can also pass `do_not_validate_attachment_file_type :file_name` to ignore this security requirement. **Only use this under controlled circumstances where you trust your users' uploads.** – Joshua Pinter Apr 12 '14 at 20:47
2

Paperclip (version 4) is now secure by default. You must validate either the mime type or the filename, or explicitly turn off validation.

This is to prevent content type spoofing, e.g. uploading a php file instead of an image which will then become publicly accessible.

The recommended solution looks like this:

validates_attachment_content_type :image, :content_type => /\Aimage/

Documentation here: https://github.com/thoughtbot/paperclip#security-validations

Note this is not just an S3 issue.

superluminary
  • 47,086
  • 25
  • 151
  • 148
2

or just put this and ignore it all

do_not_validate_attachment_file_type :image
Maysam Torabi
  • 3,672
  • 2
  • 28
  • 31
0

Try this one -

validates_attachment_content_type :file_name, :content_type => /\Aimage/.*\Z/

Chitra
  • 1,294
  • 2
  • 13
  • 28