2018-07-05 06:18:17 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
class WebHookService
|
|
|
|
class InternalErrorResponse
|
2020-01-27 04:08:32 -05:00
|
|
|
ERROR_MESSAGE = 'internal error'
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
attr_reader :body, :headers, :code
|
|
|
|
|
2021-05-06 17:10:07 -04:00
|
|
|
def success?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirection?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def internal_server_error?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
def initialize
|
2018-03-13 18:38:25 -04:00
|
|
|
@headers = Gitlab::HTTP::Response::Headers.new({})
|
2017-04-27 06:08:57 -04:00
|
|
|
@body = ''
|
2020-01-27 04:08:32 -05:00
|
|
|
@code = ERROR_MESSAGE
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 11:10:02 -04:00
|
|
|
REQUEST_BODY_SIZE_LIMIT = 25.megabytes
|
2022-06-07 14:09:27 -04:00
|
|
|
# Response body is for UI display only. It does not make much sense to save
|
|
|
|
# whatever the receivers throw back at us
|
|
|
|
RESPONSE_BODY_SIZE_LIMIT = 8.kilobytes
|
|
|
|
# The headers are for debugging purpose. They are displayed on the UI only.
|
|
|
|
RESPONSE_HEADERS_COUNT_LIMIT = 50
|
|
|
|
RESPONSE_HEADERS_SIZE_LIMIT = 1.kilobytes
|
2020-03-04 16:07:54 -05:00
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
attr_accessor :hook, :data, :hook_name, :request_options
|
2021-06-09 05:10:18 -04:00
|
|
|
attr_reader :uniqueness_token
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2020-03-04 16:07:54 -05:00
|
|
|
def self.hook_to_event(hook_name)
|
|
|
|
hook_name.to_s.singularize.titleize
|
|
|
|
end
|
|
|
|
|
2022-02-16 01:12:24 -05:00
|
|
|
def initialize(hook, data, hook_name, uniqueness_token = nil, force: false)
|
2017-04-27 06:08:57 -04:00
|
|
|
@hook = hook
|
2022-03-22 05:07:15 -04:00
|
|
|
@data = data.to_h
|
2017-09-07 07:29:12 -04:00
|
|
|
@hook_name = hook_name.to_s
|
2021-06-09 05:10:18 -04:00
|
|
|
@uniqueness_token = uniqueness_token
|
2022-02-16 01:12:24 -05:00
|
|
|
@force = force
|
2019-07-26 06:21:52 -04:00
|
|
|
@request_options = {
|
|
|
|
timeout: Gitlab.config.gitlab.webhook_timeout,
|
|
|
|
allow_local_requests: hook.allow_local_requests?
|
|
|
|
}
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
2022-02-16 01:12:24 -05:00
|
|
|
def disabled?
|
|
|
|
!@force && !hook.executable?
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
def execute
|
2022-02-16 01:12:24 -05:00
|
|
|
return { status: :error, message: 'Hook disabled' } if disabled?
|
2021-05-06 17:10:07 -04:00
|
|
|
|
2022-01-26 07:18:17 -05:00
|
|
|
if recursion_blocked?
|
|
|
|
log_recursion_blocked
|
|
|
|
return { status: :error, message: 'Recursive webhook blocked' }
|
|
|
|
end
|
2022-01-11 01:10:58 -05:00
|
|
|
|
|
|
|
Gitlab::WebHooks::RecursionDetection.register!(hook)
|
|
|
|
|
2018-10-18 05:23:24 -04:00
|
|
|
start_time = Gitlab::Metrics::System.monotonic_time
|
2017-04-27 06:08:57 -04:00
|
|
|
|
|
|
|
response = if parsed_url.userinfo.blank?
|
2022-07-15 14:09:50 -04:00
|
|
|
make_request(parsed_url.to_s)
|
2017-04-27 06:08:57 -04:00
|
|
|
else
|
|
|
|
make_request_with_auth
|
|
|
|
end
|
|
|
|
|
|
|
|
log_execution(
|
|
|
|
response: response,
|
2018-10-18 05:23:24 -04:00
|
|
|
execution_duration: Gitlab::Metrics::System.monotonic_time - start_time
|
2017-04-27 06:08:57 -04:00
|
|
|
)
|
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
{
|
|
|
|
status: :success,
|
|
|
|
http_status: response.code,
|
2021-07-01 17:08:38 -04:00
|
|
|
message: response.body
|
2017-07-20 11:12:06 -04:00
|
|
|
}
|
2021-05-19 14:10:39 -04:00
|
|
|
rescue *Gitlab::HTTP::HTTP_ERRORS,
|
2021-01-12 13:11:03 -05:00
|
|
|
Gitlab::Json::LimitedEncoder::LimitExceeded, URI::InvalidURIError => e
|
2020-08-13 17:10:04 -04:00
|
|
|
execution_duration = Gitlab::Metrics::System.monotonic_time - start_time
|
2022-07-15 14:09:50 -04:00
|
|
|
error_message = e.to_s
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
log_execution(
|
|
|
|
response: InternalErrorResponse.new,
|
2020-08-13 17:10:04 -04:00
|
|
|
execution_duration: execution_duration,
|
2022-07-15 14:09:50 -04:00
|
|
|
error_message: error_message
|
2017-04-27 06:08:57 -04:00
|
|
|
)
|
|
|
|
|
2020-08-13 17:10:04 -04:00
|
|
|
Gitlab::AppLogger.error("WebHook Error after #{execution_duration.to_i.seconds}s => #{e}")
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
{
|
|
|
|
status: :error,
|
2022-07-15 14:09:50 -04:00
|
|
|
message: error_message
|
2017-07-20 11:12:06 -04:00
|
|
|
}
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def async_execute
|
2021-06-14 11:09:48 -04:00
|
|
|
Gitlab::ApplicationContext.with_context(hook.application_context) do
|
2022-06-16 05:09:15 -04:00
|
|
|
break log_rate_limited if rate_limit!
|
2022-01-26 07:18:17 -05:00
|
|
|
break log_recursion_blocked if recursion_blocked?
|
2022-01-11 01:10:58 -05:00
|
|
|
|
2022-01-28 10:14:45 -05:00
|
|
|
params = {
|
|
|
|
recursion_detection_request_uuid: Gitlab::WebHooks::RecursionDetection::UUID.instance.request_uuid
|
|
|
|
}.compact
|
2022-01-11 01:10:58 -05:00
|
|
|
|
2022-01-28 10:14:45 -05:00
|
|
|
WebHookWorker.perform_async(hook.id, data, hook_name, params)
|
2021-05-14 05:10:24 -04:00
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parsed_url
|
2022-07-15 14:09:50 -04:00
|
|
|
@parsed_url ||= URI.parse(hook.interpolated_url)
|
|
|
|
rescue WebHook::InterpolationError => e
|
|
|
|
# Behavior-preserving fallback.
|
|
|
|
Gitlab::ErrorTracking.track_exception(e)
|
|
|
|
@parsed_url = URI.parse(hook.url)
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_request(url, basic_auth = false)
|
2018-03-13 18:38:25 -04:00
|
|
|
Gitlab::HTTP.post(url,
|
2020-08-12 11:10:02 -04:00
|
|
|
body: Gitlab::Json::LimitedEncoder.encode(data, limit: REQUEST_BODY_SIZE_LIMIT),
|
2022-01-11 01:10:58 -05:00
|
|
|
headers: build_headers,
|
2017-04-27 06:08:57 -04:00
|
|
|
verify: hook.enable_ssl_verification,
|
2018-03-13 18:38:25 -04:00
|
|
|
basic_auth: basic_auth,
|
|
|
|
**request_options)
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_request_with_auth
|
2022-07-15 14:09:50 -04:00
|
|
|
post_url = parsed_url.to_s.gsub("#{parsed_url.userinfo}@", '')
|
2017-04-27 06:08:57 -04:00
|
|
|
basic_auth = {
|
|
|
|
username: CGI.unescape(parsed_url.user),
|
2018-06-25 10:44:29 -04:00
|
|
|
password: CGI.unescape(parsed_url.password.presence || '')
|
2017-04-27 06:08:57 -04:00
|
|
|
}
|
|
|
|
make_request(post_url, basic_auth)
|
|
|
|
end
|
|
|
|
|
2022-03-22 05:07:15 -04:00
|
|
|
def log_execution(response:, execution_duration:, error_message: nil)
|
2021-06-09 05:10:18 -04:00
|
|
|
category = response_category(response)
|
|
|
|
log_data = {
|
2022-03-22 05:07:15 -04:00
|
|
|
trigger: hook_name,
|
|
|
|
url: hook.url,
|
2017-04-27 06:08:57 -04:00
|
|
|
execution_duration: execution_duration,
|
2022-01-11 01:10:58 -05:00
|
|
|
request_headers: build_headers,
|
2022-03-22 05:07:15 -04:00
|
|
|
request_data: data,
|
2022-06-07 14:09:27 -04:00
|
|
|
response_headers: safe_response_headers(response),
|
2017-08-01 10:30:29 -04:00
|
|
|
response_body: safe_response_body(response),
|
2017-04-27 06:08:57 -04:00
|
|
|
response_status: response.code,
|
|
|
|
internal_error_message: error_message
|
2021-06-09 05:10:18 -04:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:12:24 -05:00
|
|
|
if @force # executed as part of test - run log-execution inline.
|
|
|
|
::WebHooks::LogExecutionService.new(hook: hook, log_data: log_data, response_category: category).execute
|
|
|
|
else
|
2022-06-07 14:09:27 -04:00
|
|
|
queue_log_execution_with_retry(log_data, category)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_log_execution_with_retry(log_data, category)
|
|
|
|
retried = false
|
|
|
|
begin
|
|
|
|
::WebHooks::LogExecutionWorker.perform_async(hook.id, log_data, category, uniqueness_token)
|
|
|
|
rescue Gitlab::SidekiqMiddleware::SizeLimiter::ExceedLimitError
|
|
|
|
raise if retried
|
|
|
|
|
|
|
|
# Strip request data
|
|
|
|
log_data[:request_data] = ::WebHookLog::OVERSIZE_REQUEST_DATA
|
|
|
|
retried = true
|
|
|
|
retry
|
2022-02-16 01:12:24 -05:00
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
2021-06-09 05:10:18 -04:00
|
|
|
def response_category(response)
|
2021-05-06 17:10:07 -04:00
|
|
|
if response.success? || response.redirection?
|
2021-06-09 05:10:18 -04:00
|
|
|
:ok
|
2021-05-06 17:10:07 -04:00
|
|
|
elsif response.internal_server_error?
|
2021-06-09 05:10:18 -04:00
|
|
|
:error
|
2021-05-06 17:10:07 -04:00
|
|
|
else
|
2021-06-09 05:10:18 -04:00
|
|
|
:failed
|
2021-05-06 17:10:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
def build_headers
|
2017-04-27 06:08:57 -04:00
|
|
|
@headers ||= begin
|
2022-01-11 01:10:58 -05:00
|
|
|
headers = {
|
2017-04-27 06:08:57 -04:00
|
|
|
'Content-Type' => 'application/json',
|
2020-11-01 19:08:40 -05:00
|
|
|
'User-Agent' => "GitLab/#{Gitlab::VERSION}",
|
2022-01-11 01:10:58 -05:00
|
|
|
Gitlab::WebHooks::GITLAB_EVENT_HEADER => self.class.hook_to_event(hook_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
headers['X-Gitlab-Token'] = Gitlab::Utils.remove_line_breaks(hook.token) if hook.token.present?
|
|
|
|
headers.merge!(Gitlab::WebHooks::RecursionDetection.header(hook))
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make response headers more stylish
|
|
|
|
# Net::HTTPHeader has downcased hash with arrays: { 'content-type' => ['text/html; charset=utf-8'] }
|
|
|
|
# This method format response to capitalized hash with strings: { 'Content-Type' => 'text/html; charset=utf-8' }
|
2022-06-07 14:09:27 -04:00
|
|
|
# rubocop:disable Style/HashTransformValues
|
|
|
|
def safe_response_headers(response)
|
|
|
|
response.headers.each_capitalized.first(RESPONSE_HEADERS_COUNT_LIMIT).to_h do |header_key, header_value|
|
|
|
|
[enforce_utf8(header_key), string_size_limit(enforce_utf8(header_value), RESPONSE_HEADERS_SIZE_LIMIT)]
|
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
2022-06-07 14:09:27 -04:00
|
|
|
# rubocop:enable Style/HashTransformValues
|
2017-08-01 10:30:29 -04:00
|
|
|
|
|
|
|
def safe_response_body(response)
|
|
|
|
return '' unless response.body
|
|
|
|
|
2022-06-07 14:09:27 -04:00
|
|
|
response_body = enforce_utf8(response.body)
|
|
|
|
string_size_limit(response_body, RESPONSE_BODY_SIZE_LIMIT)
|
2017-08-01 10:30:29 -04:00
|
|
|
end
|
2021-05-14 05:10:24 -04:00
|
|
|
|
2022-06-16 05:09:15 -04:00
|
|
|
# Increments rate-limit counter.
|
|
|
|
# Returns true if hook should be rate-limited.
|
|
|
|
def rate_limit!
|
|
|
|
Gitlab::WebHooks::RateLimiter.new(hook).rate_limit!
|
2021-05-14 05:10:24 -04:00
|
|
|
end
|
|
|
|
|
2022-01-11 01:10:58 -05:00
|
|
|
def recursion_blocked?
|
|
|
|
Gitlab::WebHooks::RecursionDetection.block?(hook)
|
|
|
|
end
|
|
|
|
|
2022-01-26 07:18:17 -05:00
|
|
|
def log_rate_limited
|
2022-06-08 14:09:19 -04:00
|
|
|
log_auth_error('Webhook rate limit exceeded')
|
2021-05-14 05:10:24 -04:00
|
|
|
end
|
2022-01-11 01:10:58 -05:00
|
|
|
|
2022-01-26 07:18:17 -05:00
|
|
|
def log_recursion_blocked
|
2022-06-08 14:09:19 -04:00
|
|
|
log_auth_error(
|
|
|
|
'Recursive webhook blocked from executing',
|
|
|
|
recursion_detection: ::Gitlab::WebHooks::RecursionDetection.to_log(hook)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_auth_error(message, params = {})
|
2022-01-11 01:10:58 -05:00
|
|
|
Gitlab::AuthLogger.error(
|
2022-06-08 14:09:19 -04:00
|
|
|
params.merge(
|
|
|
|
{ message: message, hook_id: hook.id, hook_type: hook.type, hook_name: hook_name },
|
|
|
|
Gitlab::ApplicationContext.current
|
|
|
|
)
|
2022-01-11 01:10:58 -05:00
|
|
|
)
|
|
|
|
end
|
2022-06-07 14:09:27 -04:00
|
|
|
|
|
|
|
def string_size_limit(str, limit)
|
|
|
|
str.truncate_bytes(limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enforce_utf8(str)
|
|
|
|
Gitlab::EncodingHelper.encode_utf8(str)
|
|
|
|
end
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|