2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Spam
|
|
|
|
class SpamVerdictService
|
|
|
|
include AkismetMethods
|
|
|
|
include SpamConstants
|
|
|
|
|
2020-06-08 02:08:19 -04:00
|
|
|
def initialize(user:, target:, request:, options:, context: {})
|
2020-04-21 11:21:10 -04:00
|
|
|
@target = target
|
|
|
|
@request = request
|
2020-06-08 02:08:19 -04:00
|
|
|
@user = user
|
2020-04-21 11:21:10 -04:00
|
|
|
@options = options
|
2020-06-08 02:08:19 -04:00
|
|
|
@verdict_params = assemble_verdict_params(context)
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2020-07-13 05:09:29 -04:00
|
|
|
external_spam_check_result = external_verdict
|
2020-05-22 05:08:09 -04:00
|
|
|
akismet_result = akismet_verdict
|
|
|
|
|
|
|
|
# filter out anything we don't recognise, including nils.
|
|
|
|
valid_results = [external_spam_check_result, akismet_result].compact.select { |r| SUPPORTED_VERDICTS.key?(r) }
|
|
|
|
# Treat nils - such as service unavailable - as ALLOW
|
|
|
|
return ALLOW unless valid_results.any?
|
|
|
|
|
|
|
|
# Favour the most restrictive result.
|
|
|
|
valid_results.min_by { |v| SUPPORTED_VERDICTS[v][:priority] }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-06-08 02:08:19 -04:00
|
|
|
attr_reader :user, :target, :request, :options, :verdict_params
|
2020-05-22 05:08:09 -04:00
|
|
|
|
|
|
|
def akismet_verdict
|
2020-04-21 11:21:10 -04:00
|
|
|
if akismet.spam?
|
2020-05-25 23:08:02 -04:00
|
|
|
Gitlab::Recaptcha.enabled? ? CONDITIONAL_ALLOW : DISALLOW
|
2020-04-21 11:21:10 -04:00
|
|
|
else
|
|
|
|
ALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-13 05:09:29 -04:00
|
|
|
def external_verdict
|
2020-05-22 05:08:09 -04:00
|
|
|
return unless Gitlab::CurrentSettings.spam_check_endpoint_enabled
|
|
|
|
return if endpoint_url.blank?
|
|
|
|
|
|
|
|
begin
|
2020-05-29 05:08:06 -04:00
|
|
|
result = Gitlab::HTTP.post(endpoint_url, body: verdict_params.to_json, headers: { 'Content-Type' => 'application/json' })
|
|
|
|
return unless result
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
json_result = Gitlab::Json.parse(result).with_indifferent_access
|
|
|
|
# @TODO metrics/logging
|
|
|
|
# Expecting:
|
|
|
|
# error: (string or nil)
|
2020-06-21 23:08:17 -04:00
|
|
|
# verdict: (string or nil)
|
2020-05-22 05:08:09 -04:00
|
|
|
# @TODO log if json_result[:error]
|
|
|
|
|
|
|
|
json_result[:verdict]
|
2020-05-29 05:08:06 -04:00
|
|
|
rescue *Gitlab::HTTP::HTTP_ERRORS => e
|
|
|
|
# @TODO: log error via try_post https://gitlab.com/gitlab-org/gitlab/-/issues/219223
|
|
|
|
Gitlab::ErrorTracking.log_exception(e)
|
2020-07-10 20:09:17 -04:00
|
|
|
nil
|
2020-05-22 05:08:09 -04:00
|
|
|
rescue
|
|
|
|
# @TODO log
|
|
|
|
ALLOW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-08 02:08:19 -04:00
|
|
|
def assemble_verdict_params(context)
|
|
|
|
return {} unless endpoint_url.present?
|
|
|
|
|
|
|
|
project = target.try(:project)
|
|
|
|
|
|
|
|
context.merge({
|
|
|
|
target: {
|
|
|
|
title: target.spam_title,
|
|
|
|
description: target.spam_description,
|
|
|
|
type: target.class.to_s
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
created_at: user.created_at,
|
|
|
|
email: user.email,
|
|
|
|
username: user.username
|
|
|
|
},
|
|
|
|
user_in_project: user.authorized_project?(project)
|
2020-05-22 05:08:09 -04:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_url
|
|
|
|
@endpoint_url ||= Gitlab::CurrentSettings.current_application_settings.spam_check_endpoint_url
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|