2018-11-29 06:44:48 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Config
|
|
|
|
module Entry
|
|
|
|
module LegacyValidationHelpers
|
|
|
|
private
|
|
|
|
|
2020-08-07 11:10:17 -04:00
|
|
|
def validate_duration(value, parser = nil)
|
|
|
|
return false unless value.is_a?(String)
|
|
|
|
|
|
|
|
if parser && parser.respond_to?(:validate_duration)
|
|
|
|
parser.validate_duration(value)
|
|
|
|
else
|
|
|
|
ChronicDuration.parse(value)
|
|
|
|
end
|
2018-11-29 06:44:48 -05:00
|
|
|
rescue ChronicDuration::DurationParseError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-08-07 11:10:17 -04:00
|
|
|
def validate_duration_limit(value, limit, parser = nil)
|
2018-11-29 06:44:48 -05:00
|
|
|
return false unless value.is_a?(String)
|
|
|
|
|
2020-08-07 11:10:17 -04:00
|
|
|
if parser && parser.respond_to?(:validate_duration_limit)
|
|
|
|
parser.validate_duration_limit(value, limit)
|
|
|
|
else
|
|
|
|
ChronicDuration.parse(value).second.from_now <
|
|
|
|
ChronicDuration.parse(limit).second.from_now
|
|
|
|
end
|
2018-11-29 06:44:48 -05:00
|
|
|
rescue ChronicDuration::DurationParseError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_array_of_strings(values)
|
|
|
|
values.is_a?(Array) && values.all? { |value| validate_string(value) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_array_of_strings_or_regexps(values)
|
|
|
|
values.is_a?(Array) && values.all? { |value| validate_string_or_regexp(value) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_variables(variables)
|
2020-07-20 05:09:22 -04:00
|
|
|
variables.is_a?(Hash) && variables.flatten.all?(&method(:validate_alphanumeric))
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_array_value_variables(variables)
|
2018-11-29 06:44:48 -05:00
|
|
|
variables.is_a?(Hash) &&
|
2020-07-20 05:09:22 -04:00
|
|
|
variables.keys.all?(&method(:validate_alphanumeric)) &&
|
|
|
|
variables.values.all?(&:present?) &&
|
|
|
|
variables.values.flatten(1).all?(&method(:validate_alphanumeric))
|
|
|
|
end
|
|
|
|
|
2020-10-16 05:09:06 -04:00
|
|
|
def validate_string_or_hash_value_variables(variables, allowed_value_data)
|
|
|
|
variables.is_a?(Hash) &&
|
|
|
|
variables.keys.all?(&method(:validate_alphanumeric)) &&
|
|
|
|
variables.values.all? { |value| validate_string_or_hash_value_variable(value, allowed_value_data) }
|
|
|
|
end
|
|
|
|
|
2020-07-20 05:09:22 -04:00
|
|
|
def validate_alphanumeric(value)
|
|
|
|
validate_string(value) || validate_integer(value)
|
2018-11-29 06:44:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_integer(value)
|
|
|
|
value.is_a?(Integer)
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_string(value)
|
|
|
|
value.is_a?(String) || value.is_a?(Symbol)
|
|
|
|
end
|
|
|
|
|
2020-10-16 05:09:06 -04:00
|
|
|
def validate_string_or_hash_value_variable(value, allowed_value_data)
|
|
|
|
if value.is_a?(Hash)
|
|
|
|
(value.keys - allowed_value_data).empty? && value.values.all?(&method(:validate_alphanumeric))
|
|
|
|
else
|
|
|
|
validate_alphanumeric(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-29 06:44:48 -05:00
|
|
|
def validate_regexp(value)
|
2019-02-25 09:41:52 -05:00
|
|
|
Gitlab::UntrustedRegexp::RubySyntax.valid?(value)
|
2018-11-29 06:44:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_string_or_regexp(value)
|
|
|
|
return true if value.is_a?(Symbol)
|
|
|
|
return false unless value.is_a?(String)
|
|
|
|
|
2019-02-25 09:41:52 -05:00
|
|
|
if Gitlab::UntrustedRegexp::RubySyntax.matches_syntax?(value)
|
|
|
|
validate_regexp(value)
|
2018-11-29 06:44:48 -05:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_boolean(value)
|
|
|
|
value.in?([true, false])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|