diff options
Diffstat (limited to 'lib/gitlab/config')
-rw-r--r-- | lib/gitlab/config/entry/validators.rb | 17 | ||||
-rw-r--r-- | lib/gitlab/config/entry/validators/nested_array_helpers.rb | 46 | ||||
-rw-r--r-- | lib/gitlab/config/loader/yaml.rb | 8 |
3 files changed, 60 insertions, 11 deletions
diff --git a/lib/gitlab/config/entry/validators.rb b/lib/gitlab/config/entry/validators.rb index 88786ed82ff..8120f2c1243 100644 --- a/lib/gitlab/config/entry/validators.rb +++ b/lib/gitlab/config/entry/validators.rb @@ -268,17 +268,16 @@ module Gitlab end end - class StringOrNestedArrayOfStringsValidator < NestedArrayOfStringsValidator - def validate_each(record, attribute, value) - unless validate_string_or_nested_array_of_strings(value) - record.errors.add(attribute, 'should be a string or an array containing strings and arrays of strings') - end - end + class StringOrNestedArrayOfStringsValidator < ActiveModel::EachValidator + include LegacyValidationHelpers + include NestedArrayHelpers - private + def validate_each(record, attribute, value) + max_level = options.fetch(:max_level, 1) - def validate_string_or_nested_array_of_strings(values) - validate_string(values) || validate_nested_array_of_strings(values) + unless validate_string(value) || validate_nested_array(value, max_level, &method(:validate_string)) + record.errors.add(attribute, "should be a string or a nested array of strings up to #{max_level} levels deep") + end end end diff --git a/lib/gitlab/config/entry/validators/nested_array_helpers.rb b/lib/gitlab/config/entry/validators/nested_array_helpers.rb new file mode 100644 index 00000000000..9f5d17d74b0 --- /dev/null +++ b/lib/gitlab/config/entry/validators/nested_array_helpers.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module Gitlab + module Config + module Entry + module Validators + # Include this module to validate deeply nested array of values + # + # class MyNestedValidator < ActiveModel::EachValidator + # include NestedArrayHelpers + # + # def validate_each(record, attribute, value) + # max_depth = options.fetch(:max_depth, 1) + # + # unless validate_nested_array(value, max_depth) { |v| v.is_a?(Integer) } + # record.errors.add(attribute, "is invalid") + # end + # end + # end + # + module NestedArrayHelpers + def validate_nested_array(value, max_depth = 1, &validator_proc) + return false unless value.is_a?(Array) + + validate_nested_array_recursively(value, max_depth, &validator_proc) + end + + private + + # rubocop: disable Performance/RedundantBlockCall + # Disables Rubocop rule for easier readability reasons. + def validate_nested_array_recursively(value, nesting_level, &validator_proc) + return true if validator_proc.call(value) + return false if nesting_level <= 0 + return false unless value.is_a?(Array) + + value.all? do |element| + validate_nested_array_recursively(element, nesting_level - 1, &validator_proc) + end + end + # rubocop: enable Performance/RedundantBlockCall + end + end + end + end +end diff --git a/lib/gitlab/config/loader/yaml.rb b/lib/gitlab/config/loader/yaml.rb index cb3fc49944c..80c9abecd8e 100644 --- a/lib/gitlab/config/loader/yaml.rb +++ b/lib/gitlab/config/loader/yaml.rb @@ -12,8 +12,12 @@ module Gitlab MAX_YAML_SIZE = 1.megabyte MAX_YAML_DEPTH = 100 - def initialize(config) - @config = YAML.safe_load(config, [Symbol], [], true) + def initialize(config, additional_permitted_classes: []) + @config = YAML.safe_load(config, + permitted_classes: [Symbol, *additional_permitted_classes], + permitted_symbols: [], + aliases: true + ) rescue Psych::Exception => e raise Loader::FormatError, e.message end |