Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not validate content_type properly when use carrierwave-aws #5

Open
robinbortlik opened this issue Jun 25, 2015 · 5 comments
Open

Not validate content_type properly when use carrierwave-aws #5

robinbortlik opened this issue Jun 25, 2015 · 5 comments

Comments

@robinbortlik
Copy link

Hi,

we use in our application carrierwave-aws gem for storing files to S3. And we also use the file_validators gem for validating content_type of uploaded file.

What happen is that when we assign file (for example pdf file and we check that content_type is 'application/pdf'). The record is valid and we can store it. But when we load the record from database and it load file from S3, then record is invalid. The file behave like 'application/octstream' instead of 'application/pdf' . But if I ask for record.content_type => 'application/pdf'

So I think the problem is on this line https://github.com/musaffa/file_validators/blob/master/lib/file_validators/validators/file_content_type_validator.rb#L59

If there happen any error with content_type detection it as default return 'application/octstream' .

If you can give me an idea, how to fix it, I will do it.

Thanks a lot.

@robinbortlik robinbortlik changed the title Not validate content_type when use carrierwave-aws Not validate content_type properly when use carrierwave-aws Jun 25, 2015
@musaffa
Copy link
Owner

musaffa commented Jul 2, 2015

@robinbortlik I'm also having this problem with the remote file repository like S3. Currently the content type file validator cannot guess the file as the file is wrapped on a different object when it is fetched from a remote repository. I'll try to solve this issue. As a workaround for now, you can use carrierwave's filemagic integration

@robinbortlik
Copy link
Author

Finally I went for custom solution. I implemented simple content_type validator like this

class ContentTypeValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if value.present? && !allowed_content_types.include?(value.content_type)
      record.errors.add(attribute, :invalid)
    end
  end

  private
  def allowed_content_types
    options[:allow].to_a
  end
end

and used it in model

validates :file, content_type: { allow: ['application/pdf'] }

And it works quite well :)

@musaffa
Copy link
Owner

musaffa commented Jul 2, 2015

Yes it should :p . But not quite secure though. I will keep this issue open until it is solved.

@xpepermint
Copy link

xpepermint commented May 3, 2016

+1, my workaround is

validates :video, file_size: { less_than_or_equal_to: 50.megabytes, if: ->(a){ a.video_changed? } }

@ollieh-m
Copy link

ollieh-m commented Oct 3, 2017

I was having a similar issue where images in S3 were being identified by the file_content_type_validator as of type application/octstream. I found that when the validator deferred to value.content_type, the type was application/octstream, but when I passed a mode into the validation (i.e. allow: ['image/jpeg'], mode: :relaxed), the type was correctly identified. Basically this worked but this didn't. I'm not sure why, but others who find their way to this issue might find joy adding mode: :relaxed or mode: :strict to their validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants