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

yaml.rb « loader « config « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cedab1545c5cc5b4142893274dd0aaa6c70ecb9 (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

module Gitlab
  module Config
    module Loader
      class Yaml
        DataTooLargeError = Class.new(Loader::FormatError)

        include Gitlab::Utils::StrongMemoize

        MAX_YAML_SIZE = 1.megabyte
        MAX_YAML_DEPTH = 100

        def initialize(config)
          @config = YAML.safe_load(config, [Symbol], [], true)
        rescue Psych::Exception => e
          raise Loader::FormatError, e.message
        end

        def valid?
          hash? && !too_big?
        end

        def load!
          raise DataTooLargeError, 'The parsed YAML is too big' if too_big?
          raise Loader::FormatError, 'Invalid configuration format' unless hash?

          @config.deep_symbolize_keys
        end

        private

        def hash?
          @config.is_a?(Hash)
        end

        def too_big?
          return false unless Feature.enabled?(:ci_yaml_limit_size, default_enabled: true)

          !deep_size.valid?
        end

        def deep_size
          strong_memoize(:deep_size) do
            Gitlab::Utils::DeepSize.new(@config,
              max_size: MAX_YAML_SIZE,
              max_depth: MAX_YAML_DEPTH)
          end
        end
      end
    end
  end
end