2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
module Gitlab
|
2019-02-15 15:39:39 +05:30
|
|
|
module Config
|
|
|
|
module Loader
|
|
|
|
class Yaml
|
2019-07-07 11:18:12 +05:30
|
|
|
DataTooLargeError = Class.new(Loader::FormatError)
|
|
|
|
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
MAX_YAML_SIZE = 1.megabyte
|
|
|
|
MAX_YAML_DEPTH = 100
|
|
|
|
|
2016-06-16 23:09:34 +05:30
|
|
|
def initialize(config)
|
|
|
|
@config = YAML.safe_load(config, [Symbol], [], true)
|
2018-03-17 18:26:18 +05:30
|
|
|
rescue Psych::Exception => e
|
2019-02-15 15:39:39 +05:30
|
|
|
raise Loader::FormatError, e.message
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2019-07-07 11:18:12 +05:30
|
|
|
hash? && !too_big?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def load!
|
2019-07-07 11:18:12 +05:30
|
|
|
raise DataTooLargeError, 'The parsed YAML is too big' if too_big?
|
|
|
|
raise Loader::FormatError, 'Invalid configuration format' unless hash?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
@config.deep_symbolize_keys
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|