diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-24 18:15:38 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-04-24 18:15:38 +0300 |
commit | efbf661c4224d481c57d0346e26983a805e5ec93 (patch) | |
tree | 4736f287350884cb49d84a09c52c8c2e1b851080 /app/services/spam | |
parent | 4720346c2e10e1ff62a20b39dfc9866eb88858e6 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/spam')
-rw-r--r-- | app/services/spam/spam_verdict_service.rb | 51 |
1 files changed, 16 insertions, 35 deletions
diff --git a/app/services/spam/spam_verdict_service.rb b/app/services/spam/spam_verdict_service.rb index 4ec07bb4c5f..387d36cc7d1 100644 --- a/app/services/spam/spam_verdict_service.rb +++ b/app/services/spam/spam_verdict_service.rb @@ -14,57 +14,47 @@ module Spam end def execute - spamcheck_result = nil - spamcheck_attribs = {} - spamcheck_error = false + spamcheck_verdict = nil external_spam_check_round_trip_time = Benchmark.realtime do - spamcheck_result, spamcheck_attribs, spamcheck_error = spamcheck_verdict + spamcheck_verdict = get_spamcheck_verdict end - label = spamcheck_error ? 'ERROR' : spamcheck_result.to_s.upcase + histogram.observe({ result: spamcheck_verdict.upcase }, external_spam_check_round_trip_time) if spamcheck_verdict - 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 - original_spamcheck_result = spamcheck_result - - spamcheck_result = nil if spamcheck_attribs&.fetch("monitorMode", "false") == "true" - - akismet_result = akismet_verdict + akismet_verdict = get_akismet_verdict # filter out anything we don't recognise, including nils. - valid_results = [spamcheck_result, akismet_result].compact.select { |r| SUPPORTED_VERDICTS.key?(r) } + valid_verdicts = [spamcheck_verdict, akismet_verdict].compact.select { |r| SUPPORTED_VERDICTS.key?(r) } # Treat nils - such as service unavailable - as ALLOW - return ALLOW unless valid_results.any? + return ALLOW unless valid_verdicts.any? - # Favour the most restrictive result. - verdict = valid_results.min_by { |v| SUPPORTED_VERDICTS[v][:priority] } + # Favour the most restrictive verdict + final_verdict = valid_verdicts.min_by { |v| SUPPORTED_VERDICTS[v][:priority] } # The target can override the verdict via the `allow_possible_spam` application setting - verdict = OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM if override_via_allow_possible_spam?(verdict: verdict) + final_verdict = OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM if override_via_allow_possible_spam?(verdict: final_verdict) logger.info(class: self.class.name, akismet_verdict: akismet_verdict, - spam_check_verdict: original_spamcheck_result, - extra_attributes: spamcheck_attribs, + spam_check_verdict: spamcheck_verdict, spam_check_rtt: external_spam_check_round_trip_time.real, - final_verdict: verdict, + final_verdict: final_verdict, username: user.username, user_id: user.id, target_type: target.class.to_s, project_id: target.project_id ) - verdict + final_verdict end private attr_reader :user, :target, :options, :context, :extra_features - def akismet_verdict + def get_akismet_verdict if akismet.spam? Gitlab::Recaptcha.enabled? ? CONDITIONAL_ALLOW : DISALLOW else @@ -72,23 +62,14 @@ module Spam end end - def spamcheck_verdict + def get_spamcheck_verdict return unless Gitlab::CurrentSettings.spam_check_endpoint_enabled begin - result, attribs, _error = spamcheck_client.spam?(spammable: target, user: user, context: context, - extra_features: extra_features) - # @TODO log if error is not nil https://gitlab.com/gitlab-org/gitlab/-/issues/329545 - - return [nil, attribs] unless result - - [result, attribs] - + spamcheck_client.spam?(spammable: target, user: user, context: context, extra_features: extra_features).verdict rescue StandardError => e Gitlab::ErrorTracking.log_exception(e, error: ERROR_TYPE) - - # Default to ALLOW if any errors occur - [ALLOW, attribs, true] + nil end end |