Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on rails. Paperclip mp3 validation failing

Ok, I am allowing users to upload mp3's. Now for some reason only some mp3 files will upload and others will not. I cannot find any distinguishable difference between the working and non working files.

class Song < ActiveRecord::Base
  belongs_to :user
  has_attached_file :audio, :restricted_characters => /[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%#]/, dependent: :destroy
  validates_attachment_presence :audio
  validates_attachment_content_type :audio, :content_type => [ 'audio/mpeg', 'audio/mp3' ]
  validates_attachment_size :audio, :less_than => 20.megabytes
end

The server output for failing files is;

Command :: file -b --mime '/tmp/acf7bcfce06ffcaa55511087ea2e486f20160427-7322-y1lyj6.mp3'
[paperclip] Content Type Spoof: Filename leyinnata.mp3 (audio/mp3 from Headers, ["audio/mpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.

So the file is being seen as application/octet-stream instead of audio/mp3 and I don't know why.

I heeded to the suggestion of reading the documentation and found a possible solution:

paperclip.rb

Paperclip.options[:content_type_mappings] = {
  :audio=> 'application/octet-stream'
}

this does nothing. (I restarted the server)

I cannot understand why it won't work and I am now getting very frustrated. Any help would be appreciated, thanks.

UPDATE:

Specifying more audio file types doesn't seem to make any difference.

validates_attachment_content_type :audio, :content_type => [ 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]

I have also tried adding application/octet-stream to validates_attachment_content_type eg.

validates_attachment_content_type :audio, :content_type => [ 'application/octet-stream', 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]

No luck.

I saw someone add

Paperclip.options[:content_type_mappings] = {
  audio: "application/octet-stream"
}

to their environment.rb files. This doesn't work for me either.

UPDATE 2:

In paperclip.rb, adding:

module Paperclip
  # do not require any validations
  REQUIRED_VALIDATORS = []

  # do not complain when missing validations
  class Attachment
    def missing_required_validator?
      false
    end
  end

  # skip media type spoof detection
  module Validators
    class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        true
      end
    end
  end
end

lets me upload what I need, but this skips spoofing validations and I imagine could be dangerous. My users are also allowed to upload images in a separate part of the website and I know that I would now be vunerable to attacks.

UPDATE 3:

I have added

module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

to my paperclip.rb. This seems to be working ok. If anyone can come up with better solution then I'd love to hear it otherwise I'll answer my own question.

like image 380
Rob Hughes Avatar asked Nov 16 '25 12:11

Rob Hughes


1 Answers

I solved it by removing everything that I put in the 'updates' and adding:

Paperclip.options[:content_type_mappings] = {
  mp3: 'application/octet-stream'
}

to paperclip.rb

like image 158
Rob Hughes Avatar answered Nov 19 '25 05:11

Rob Hughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!