class Paperclip::MediaTypeSpoofDetector

Public Class Methods

new(file, name, content_type) click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 7
def initialize(file, name, content_type)
  @file = file
  @name = name
  @content_type = content_type || ""
end
using(file, name, content_type) click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 3
def self.using(file, name, content_type)
  new(file, name, content_type)
end

Public Instance Methods

spoofed?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 13
def spoofed?
  if has_name? && media_type_mismatch? && mapping_override_mismatch?
    Paperclip.log("Content Type Spoof: Filename #{File.basename(@name)} (#{supplied_content_type} from Headers, #{content_types_from_name.map(&:to_s)} from Extension), content type discovered from file command: #{calculated_content_type}. See documentation to allow this combination.")
    true
  else
    false
  end
end

Private Instance Methods

calculated_content_type() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 68
def calculated_content_type
  @calculated_content_type ||= type_from_file_command.chomp
end
calculated_media_type() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 72
def calculated_media_type
  @calculated_media_type ||= calculated_content_type.split("/").first
end
calculated_type_mismatch?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 42
def calculated_type_mismatch?
  supplied_media_type.present? &&
    !calculated_content_type.include?(supplied_media_type)
end
content_types_from_name() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 60
def content_types_from_name
  @content_types_from_name ||= MIME::Types.type_for(@name)
end
extension_type_mismatch?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 36
def extension_type_mismatch?
  supplied_media_type.present? &&
    has_extension? &&
    !media_types_from_name.include?(supplied_media_type)
end
filename_extension() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 89
def filename_extension
  File.extname(@name.to_s.downcase).sub(/^\./, '').to_sym
end
has_extension?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 28
def has_extension?
  File.extname(@name).present?
end
has_name?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 24
def has_name?
  @name.present?
end
mapped_content_type() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 85
def mapped_content_type
  Paperclip.options[:content_type_mappings][filename_extension]
end
mapping_override_mismatch?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 47
def mapping_override_mismatch?
  !Array(mapped_content_type).include?(calculated_content_type)
end
media_type_mismatch?() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 32
def media_type_mismatch?
  extension_type_mismatch? || calculated_type_mismatch?
end
media_types_from_name() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 64
def media_types_from_name
  @media_types_from_name ||= content_types_from_name.collect(&:media_type)
end
supplied_content_type() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 52
def supplied_content_type
  @content_type
end
supplied_media_type() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 56
def supplied_media_type
  @content_type.split("/").first
end
type_from_file_command() click to toggle source
# File lib/paperclip/media_type_spoof_detector.rb, line 76
def type_from_file_command
  begin
    Paperclip.run("file", "-b --mime :file", file: @file.path).
      split(/[:;\s]+/).first
  rescue Terrapin::CommandLineError
    ""
  end
end