2020-04-21 11:21:10 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Spam
|
|
|
|
class SpamVerdictService
|
|
|
|
include AkismetMethods
|
|
|
|
include SpamConstants
|
|
|
|
|
2021-06-21 08:07:45 -04:00
|
|
|
def initialize(user:, target:, options:, context: {})
|
2020-04-21 11:21:10 -04:00
|
|
|
@target = target
|
2020-06-08 02:08:19 -04:00
|
|
|
@user = user
|
2020-04-21 11:21:10 -04:00
|
|
|
@options = options
|
2021-04-30 20:10:00 -04:00
|
|
|
@context = context
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-04-30 20:10:00 -04:00
|
|
|
spamcheck_result = nil
|
2021-05-07 17:10:34 -04:00
|
|
|
spamcheck_attribs = {}
|
2021-05-17 11:10:15 -04:00
|
|
|
spamcheck_error = false
|
2021-04-30 20:10:00 -04:00
|
|
|
|
|
|
|
external_spam_check_round_trip_time = Benchmark.realtime do
|
2021-05-17 11:10:15 -04:00
|
|
|
spamcheck_result, spamcheck_attribs, spamcheck_error = spamcheck_verdict
|
2021-04-30 20:10:00 -04:00
|
|
|
end
|
|
|
|
|
2021-05-17 11:10:15 -04:00
|
|
|
label = spamcheck_error ? 'ERROR' : spamcheck_result.to_s.upcase
|
|
|
|
|
|
|
|
histogram.observe( { result: label }, external_spam_check_round_trip_time )
|
|
|
|
|
|
|
|
# assign result to a var for logging it before reassigning to nil when monitorMode is true
|
2021-05-07 17:10:34 -04:00
|
|
|
original_spamcheck_result = spamcheck_result
|
|
|
|
|
|
|
|
spamcheck_result = nil if spamcheck_attribs&.fetch("monitorMode", "false") == "true"
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
akismet_result = akismet_verdict
|
|
|
|
|
|
|
|
# filter out anything we don't recognise, including nils.
|
2021-04-30 20:10:00 -04:00
|
|
|
valid_results = [spamcheck_result, akismet_result].compact.select { |r| SUPPORTED_VERDICTS.key?(r) }
|
|
|
|
|
2020-05-22 05:08:09 -04:00
|
|
|
# Treat nils - such as service unavailable - as ALLOW
|
|
|
|
return ALLOW unless valid_results.any?
|
|
|
|
|
|
|
|
# Favour the most restrictive result.
|
2021-04-30 20:10:00 -04:00
|
|
|
final_verdict = valid_results.min_by { |v| SUPPORTED_VERDICTS[v][:priority] }
|
|
|
|
|
|
|
|
logger.info(class: self.class.name,
|
|
|
|
akismet_verdict: akismet_verdict,
|
2021-05-07 17:10:34 -04:00
|
|
|
spam_check_verdict: original_spamcheck_result,
|
|
|
|
extra_attributes: spamcheck_attribs,
|
2021-04-30 20:10:00 -04:00
|
|
|
spam_check_rtt: external_spam_check_round_trip_time.real,
|
|
|
|
final_verdict: final_verdict,
|
|
|
|
username: user.username,
|
|
|
|
user_id: user.id,
|
|
|
|
target_type: target.class.to_s,
|
|
|
|
project_id: target.project_id
|
|
|
|
)
|
|
|
|
|
|
|
|
final_verdict
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-21 08:07:45 -04:00
|
|
|
attr_reader :user, :target, :options, :context
|
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
|
|
|
|
|
2021-04-30 20:10:00 -04:00
|
|
|
def spamcheck_verdict
|
2020-05-22 05:08:09 -04:00
|
|
|
return unless Gitlab::CurrentSettings.spam_check_endpoint_enabled
|
|
|
|
|
|
|
|
begin
|
2021-05-07 17:10:34 -04:00
|
|
|
result, attribs, _error = spamcheck_client.issue_spam?(spam_issue: target, user: user, context: context)
|
2021-04-30 20:10:00 -04:00
|
|
|
# @TODO log if error is not nil https://gitlab.com/gitlab-org/gitlab/-/issues/329545
|
2020-05-22 05:08:09 -04:00
|
|
|
|
2021-10-22 08:09:12 -04:00
|
|
|
return [nil, attribs] unless result
|
|
|
|
|
|
|
|
[result, attribs]
|
2021-05-07 17:10:34 -04:00
|
|
|
|
2021-04-30 20:10:00 -04:00
|
|
|
rescue StandardError => e
|
2020-05-29 05:08:06 -04:00
|
|
|
Gitlab::ErrorTracking.log_exception(e)
|
2021-05-17 11:10:15 -04:00
|
|
|
|
2021-04-30 20:10:00 -04:00
|
|
|
# Default to ALLOW if any errors occur
|
2021-05-17 11:10:15 -04:00
|
|
|
[ALLOW, attribs, true]
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-30 20:10:00 -04:00
|
|
|
def spamcheck_client
|
|
|
|
@spamcheck_client ||= Gitlab::Spamcheck::Client.new
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
|
|
|
|
2021-04-30 20:10:00 -04:00
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::AppJsonLogger.build
|
2020-05-22 05:08:09 -04:00
|
|
|
end
|
2021-05-17 11:10:15 -04:00
|
|
|
|
|
|
|
def histogram
|
|
|
|
@histogram ||= Gitlab::Metrics.histogram(:gitlab_spamcheck_request_duration_seconds, 'Request duration to the anti-spam service')
|
|
|
|
end
|
2020-04-21 11:21:10 -04:00
|
|
|
end
|
|
|
|
end
|