debian-mirror-gitlab/app/services/spam/spam_verdict_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.3 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Spam
class SpamVerdictService
include AkismetMethods
include SpamConstants
2022-10-11 01:57:18 +05:30
def initialize(user:, target:, options:, context: {}, extra_features: {})
2020-05-24 23:13:21 +05:30
@target = target
2020-06-23 00:09:42 +05:30
@user = user
2020-05-24 23:13:21 +05:30
@options = options
2021-06-08 01:23:25 +05:30
@context = context
2022-10-11 01:57:18 +05:30
@extra_features = extra_features
2020-05-24 23:13:21 +05:30
end
def execute
2023-07-09 08:55:56 +05:30
spamcheck_verdict = nil
2021-06-08 01:23:25 +05:30
external_spam_check_round_trip_time = Benchmark.realtime do
2023-07-09 08:55:56 +05:30
spamcheck_verdict = get_spamcheck_verdict
2021-06-08 01:23:25 +05:30
end
2023-07-09 08:55:56 +05:30
histogram.observe({ result: spamcheck_verdict.upcase }, external_spam_check_round_trip_time) if spamcheck_verdict
2021-06-08 01:23:25 +05:30
2023-07-09 08:55:56 +05:30
akismet_verdict = get_akismet_verdict
2020-06-23 00:09:42 +05:30
# filter out anything we don't recognise, including nils.
2023-07-09 08:55:56 +05:30
valid_verdicts = [spamcheck_verdict, akismet_verdict].compact.select { |r| SUPPORTED_VERDICTS.key?(r) }
2021-06-08 01:23:25 +05:30
2020-06-23 00:09:42 +05:30
# Treat nils - such as service unavailable - as ALLOW
2023-07-09 08:55:56 +05:30
return ALLOW unless valid_verdicts.any?
2020-06-23 00:09:42 +05:30
2023-07-09 08:55:56 +05:30
# Favour the most restrictive verdict
final_verdict = valid_verdicts.min_by { |v| SUPPORTED_VERDICTS[v][:priority] }
2022-05-07 20:08:51 +05:30
2023-04-23 21:23:45 +05:30
# The target can override the verdict via the `allow_possible_spam` application setting
2023-07-09 08:55:56 +05:30
final_verdict = OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM if override_via_allow_possible_spam?(verdict: final_verdict)
2021-06-08 01:23:25 +05:30
logger.info(class: self.class.name,
akismet_verdict: akismet_verdict,
2023-07-09 08:55:56 +05:30
spam_check_verdict: spamcheck_verdict,
2021-06-08 01:23:25 +05:30
spam_check_rtt: external_spam_check_round_trip_time.real,
2023-07-09 08:55:56 +05:30
final_verdict: final_verdict,
2021-06-08 01:23:25 +05:30
username: user.username,
user_id: user.id,
target_type: target.class.to_s,
project_id: target.project_id
)
2023-07-09 08:55:56 +05:30
final_verdict
2020-06-23 00:09:42 +05:30
end
private
2022-10-11 01:57:18 +05:30
attr_reader :user, :target, :options, :context, :extra_features
2020-06-23 00:09:42 +05:30
2023-07-09 08:55:56 +05:30
def get_akismet_verdict
2020-05-24 23:13:21 +05:30
if akismet.spam?
2020-06-23 00:09:42 +05:30
Gitlab::Recaptcha.enabled? ? CONDITIONAL_ALLOW : DISALLOW
2020-05-24 23:13:21 +05:30
else
ALLOW
end
end
2023-07-09 08:55:56 +05:30
def get_spamcheck_verdict
2020-06-23 00:09:42 +05:30
return unless Gitlab::CurrentSettings.spam_check_endpoint_enabled
begin
2023-07-09 08:55:56 +05:30
result = spamcheck_client.spam?(spammable: target, user: user, context: context, extra_features: extra_features)
2021-06-08 01:23:25 +05:30
2023-07-09 08:55:56 +05:30
if result.evaluated? && Feature.enabled?(:user_spam_scores)
Abuse::TrustScore.create!(user: user, score: result.score, source: :spamcheck)
end
2021-12-11 22:18:48 +05:30
2023-07-09 08:55:56 +05:30
result.verdict
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2022-10-11 01:57:18 +05:30
Gitlab::ErrorTracking.log_exception(e, error: ERROR_TYPE)
2023-07-09 08:55:56 +05:30
nil
2020-06-23 00:09:42 +05:30
end
end
2022-05-07 20:08:51 +05:30
def override_via_allow_possible_spam?(verdict:)
# If the verdict is already going to allow (because current verdict's priority value is greater
# than the override verdict's priority value), then we don't need to override it.
return false if SUPPORTED_VERDICTS[verdict][:priority] > SUPPORTED_VERDICTS[OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM][:priority]
target.allow_possible_spam?
end
2021-06-08 01:23:25 +05:30
def spamcheck_client
@spamcheck_client ||= Gitlab::Spamcheck::Client.new
end
def logger
@logger ||= Gitlab::AppJsonLogger.build
2020-06-23 00:09:42 +05:30
end
2021-06-08 01:23:25 +05:30
def histogram
@histogram ||= Gitlab::Metrics.histogram(:gitlab_spamcheck_request_duration_seconds, 'Request duration to the anti-spam service')
2020-06-23 00:09:42 +05:30
end
2020-05-24 23:13:21 +05:30
end
end