Welcome to mirror list, hosted at ThFree Co, Russian Federation.

upload_type_check.rb « uploaders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2837b00166023580f744152c038319d7452289e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# frozen_string_literal: true

# Ensure that uploaded files are what they say they are for security and
# handling purposes. The checks are not 100% reliable so we err on the side of
# caution and allow by default, and deny when we're confident of a fail state.
#
# Include this concern, then call `check_upload_type` to check all
# uploads. Attach a `mime_type` or `extensions` parameter to only check
# specific upload types. Both parameters will be normalized to a MIME type and
# checked against the inferred MIME type of the upload content and filename
# extension.
#
# class YourUploader
#   include UploadTypeCheck::Concern
#   check_upload_type mime_types: ['image/png', /image\/jpe?g/]
#
#   # or...
#
#   check_upload_type extensions: ['png', 'jpg', 'jpeg']
# end
#
# The mime_types parameter can accept `NilClass`, `String`, `Regexp`,
# `Array[String, Regexp]`. This matches the CarrierWave `extension_whitelist`
# and `content_type_whitelist` family of behavior.
#
# The extensions parameter can accept `NilClass`, `String`, `Array[String]`.
module UploadTypeCheck
  module Concern
    extend ActiveSupport::Concern

    class_methods do
      def check_upload_type(mime_types: nil, extensions: nil)
        define_method :check_upload_type_callback do |file|
          magic_file = MagicFile.new(file.to_file)

          # Map file extensions back to mime types.
          if extensions
            mime_types = Array(mime_types) +
                         Array(extensions).map { |e| MimeMagic::EXTENSIONS[e] }
          end

          if mime_types.nil? || magic_file.matches_mime_types?(mime_types)
            check_content_matches_extension!(magic_file)
          end
        end
        before :cache, :check_upload_type_callback
      end
    end

    def check_content_matches_extension!(magic_file)
      return if magic_file.ambiguous_type?

      if magic_file.magic_type != magic_file.ext_type
        raise CarrierWave::IntegrityError, 'Content type does not match file extension'
      end
    end
  end

  # Convenience class to wrap MagicMime objects.
  class MagicFile
    attr_reader :file

    def initialize(file)
      @file = file
    end

    def magic_type
      @magic_type ||= MimeMagic.by_magic(file)
    end

    def ext_type
      @ext_type ||= MimeMagic.by_path(file.path)
    end

    def magic_type_type
      magic_type&.type
    end

    def ext_type_type
      ext_type&.type
    end

    def matches_mime_types?(mime_types)
      Array(mime_types).any? do |mt|
        magic_type_type =~ /\A#{mt}\z/ || ext_type_type =~ /\A#{mt}\z/
      end
    end

    # - Both types unknown or text/plain.
    # - Ambiguous magic type with text extension. Plain text file.
    # - Text magic type with ambiguous extension. TeX file missing extension.
    def ambiguous_type?
      (ext_type.to_s.blank? && magic_type.to_s.blank?) ||
      (magic_type.to_s.blank? && ext_type_type == 'text/plain') ||
      (ext_type.to_s.blank? && magic_type_type == 'text/plain')
    end
  end
end