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

Bug with content type detection #2424

Closed
n-rodriguez opened this issue Sep 19, 2019 · 7 comments · Fixed by #2474
Closed

Bug with content type detection #2424

n-rodriguez opened this issue Sep 19, 2019 · 7 comments · Fixed by #2474

Comments

@n-rodriguez
Copy link

Hi there!

I think it should be nil here :

MimeMagic.by_magic(file).try(:type) || 'invalid/invalid'

(like in the rescue block) to let mini_mime_content_type have a chance to find the right mime type.

@mshibuya
Copy link
Member

This is intentional, see #1942.

@n-rodriguez
Copy link
Author

This is intentional, see #1942.

I understand, but for some reason it breaks CSV files detection.

@n-rodriguez
Copy link
Author

Hi there! Any news?

@inkstak
Copy link
Contributor

inkstak commented Apr 23, 2020

I have the same problem with CSV and plain text files.

@inkstak
Copy link
Contributor

inkstak commented Apr 23, 2020

Example :

> CSV.open('foo.csv', 'w') { |f| f << %w[a b] }
> File.open('foo.csv') { |file| MimeMagic.by_magic(file) }
 => nil

The original plot about using MimeMagic was to mitigate a CVE on images files, right ?
Can we return the "invalid/invalid" only in such cases ?

type = File.open(path) do |file|
  MimeMagic.by_magic(file)&.type
end

if type.match?('image/') && type != MimeMagic.by_path(path)&.type
  type = 'invalid/invalid' 
end

type

@tiagoovieira
Copy link

Has anyone worked on a workaround on this?

I used Mini Mime to fetch the content_type as set it manually.

@inkstak
Copy link
Contributor

inkstak commented Nov 17, 2020

class DocumentUploader < CarrierWave::Uploader::Base

  def content_type
    type = super

    # FIXME: https://github.com/carrierwaveuploader/carrierwave/pull/2474
    if type == 'invalid/invalid'
      type = ::MiniMime.lookup_by_filename(path).try(:content_type)
      type = 'invalid/invalid' unless type.nil? || type.start_with?('text/')
    end

    # Carrierwave always returns a string, even when content type is missing
    type.to_s
  end
end

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

Successfully merging a pull request may close this issue.

4 participants