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

content_type_whitelist.rb « uploaders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 64bde16cb6906a764d4735dd508ec05cb340c013 (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
# frozen_string_literal: true

# Currently we run CarrierWave 1.3.1 which means we can not whitelist files
# by their content type through magic header parsing.
#
# This is a patch to hold us over until we get to CarrierWave 2 :) It's a mashup of
# CarrierWave's lib/carrierwave/uploader/content_type_whitelist.rb and
# lib/carrierwave/sanitized_file.rb
#
# Include this concern and add a content_type_whitelist method to get the same
# behavior as you would with CarrierWave 2.
#
# This is not an exact replacement as we don't override
# SanitizedFile#content_type but we do set the content_type attribute when we
# check the whitelist.
#
# Remove this after moving to CarrierWave 2, though on practical terms it shouldn't
# break anything if left for a while.
module ContentTypeWhitelist
  module Concern
    extend ActiveSupport::Concern

    private

    # CarrierWave calls this method as part of it's before :cache callbacks.
    # Here we override and extend CarrierWave's method that does not parse the
    # magic headers.
    def check_content_type_whitelist!(new_file)
      if content_type_whitelist
        content_type = mime_magic_content_type(new_file.path)

        unless whitelisted_content_type?(content_type)
          message = I18n.translate(:"errors.messages.content_type_whitelist_error", allowed_types: Array(content_type_whitelist).join(", "))
          raise CarrierWave::IntegrityError, message
        end
      end
    end

    def whitelisted_content_type?(content_type)
      Array(content_type_whitelist).any? { |item| content_type =~ /#{item}/ }
    end

    def mime_magic_content_type(path)
      if path
        File.open(path) do |file|
          Gitlab::Utils::MimeType.from_io(file) || 'invalid/invalid'
        end
      end
    rescue Errno::ENOENT
      nil
    end
  end
end