Commit 4cc9d3e2 authored by GitLab Bot's avatar GitLab Bot

Add latest changes from gitlab-org/security/gitlab@12-10-stable-ee

parent e81a7b71
...@@ -4,6 +4,16 @@ module Prometheus ...@@ -4,6 +4,16 @@ module Prometheus
class ProxyVariableSubstitutionService < BaseService class ProxyVariableSubstitutionService < BaseService
include Stepable include Stepable
VARIABLE_INTERPOLATION_REGEX = /
%{ # Variable needs to be wrapped in these chars.
\s* # Allow whitespace before and after the variable name.
(?<variable> # Named capture.
\w+ # Match one or more word characters.
)
\s*
}
/x.freeze
steps :validate_variables, steps :validate_variables,
:add_params_to_result, :add_params_to_result,
:substitute_params, :substitute_params,
...@@ -46,6 +56,14 @@ module Prometheus ...@@ -46,6 +56,14 @@ module Prometheus
success(result) success(result)
end end
def substitute_ruby_variables(result)
return success(result) unless query(result)
result[:params][:query] = gsub(query(result), full_context)
success(result)
end
def substitute_liquid_variables(result) def substitute_liquid_variables(result)
return success(result) unless query(result) return success(result) unless query(result)
...@@ -57,26 +75,20 @@ module Prometheus ...@@ -57,26 +75,20 @@ module Prometheus
error(e.message) error(e.message)
end end
def substitute_ruby_variables(result) def gsub(string, context)
return success(result) unless query(result) # Search for variables of the form `%{variable}` in the string and replace
# them with their value.
# The % operator doesn't replace variables if the hash contains string string.gsub(VARIABLE_INTERPOLATION_REGEX) do |match|
# keys. # Replace with the value of the variable, or if there is no such variable,
result[:params][:query] = query(result) % predefined_context.symbolize_keys # replace the invalid variable with itself. So,
# `up{instance="%{invalid_variable}"}` will remain
success(result) # `up{instance="%{invalid_variable}"}` after substitution.
rescue TypeError, ArgumentError => exception context.fetch($~[:variable], match)
log_error(exception.message) end
Gitlab::ErrorTracking.track_exception(exception, {
template_string: query(result),
variables: predefined_context
})
error(_('Malformed string'))
end end
def predefined_context def predefined_context
@predefined_context ||= Gitlab::Prometheus::QueryVariables.call(@environment) Gitlab::Prometheus::QueryVariables.call(@environment).stringify_keys
end end
def full_context def full_context
......
---
title: Use `gsub` instead of the Ruby `%` operator to perform variable substitution in Prometheus proxy API
merge_request:
author:
type: security
...@@ -12434,9 +12434,6 @@ msgstr "" ...@@ -12434,9 +12434,6 @@ msgstr ""
msgid "Makes this issue confidential." msgid "Makes this issue confidential."
msgstr "" msgstr ""
msgid "Malformed string"
msgstr ""
msgid "Manage" msgid "Manage"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment