2018-08-03 17:22:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-21 23:11:53 +00:00
|
|
|
module Spammable
|
|
|
|
extend ActiveSupport::Concern
|
2016-07-29 00:02:56 +00:00
|
|
|
|
2018-08-27 12:35:31 +00:00
|
|
|
class_methods do
|
2016-08-05 22:10:08 +00:00
|
|
|
def attr_spammable(attr, options = {})
|
|
|
|
spammable_attrs << [attr.to_s, options]
|
2016-07-29 00:02:56 +00:00
|
|
|
end
|
|
|
|
end
|
2016-07-21 23:11:53 +00:00
|
|
|
|
|
|
|
included do
|
2017-06-08 15:16:27 +00:00
|
|
|
has_one :user_agent_detail, as: :subject, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
|
2016-08-09 17:43:47 +00:00
|
|
|
|
2016-07-21 23:11:53 +00:00
|
|
|
attr_accessor :spam
|
2020-04-21 15:21:10 +00:00
|
|
|
attr_accessor :needs_recaptcha
|
2017-01-27 16:25:39 +00:00
|
|
|
attr_accessor :spam_log
|
2020-04-21 15:21:10 +00:00
|
|
|
|
2017-11-21 15:15:24 +00:00
|
|
|
alias_method :spam?, :spam
|
2020-04-21 15:21:10 +00:00
|
|
|
alias_method :needs_recaptcha?, :needs_recaptcha
|
2016-08-09 17:43:47 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
# if spam errors are added before validation, they will be wiped
|
2020-02-26 09:08:47 +00:00
|
|
|
after_validation :invalidate_if_spam, on: [:create, :update]
|
2016-07-29 00:02:56 +00:00
|
|
|
|
|
|
|
cattr_accessor :spammable_attrs, instance_accessor: false do
|
|
|
|
[]
|
|
|
|
end
|
2016-08-09 17:43:47 +00:00
|
|
|
|
|
|
|
delegate :ip_address, :user_agent, to: :user_agent_detail, allow_nil: true
|
2016-07-29 00:02:56 +00:00
|
|
|
end
|
|
|
|
|
2017-02-10 10:41:22 +00:00
|
|
|
def submittable_as_spam_by?(current_user)
|
|
|
|
current_user && current_user.admin? && submittable_as_spam?
|
|
|
|
end
|
|
|
|
|
2016-08-09 17:43:47 +00:00
|
|
|
def submittable_as_spam?
|
2016-07-29 00:02:56 +00:00
|
|
|
if user_agent_detail
|
2017-08-31 09:47:03 +00:00
|
|
|
user_agent_detail.submittable? && Gitlab::CurrentSettings.current_application_settings.akismet_enabled
|
2016-07-29 00:02:56 +00:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-07 09:09:13 +00:00
|
|
|
def needs_recaptcha!
|
2020-04-21 15:21:10 +00:00
|
|
|
self.needs_recaptcha = true
|
2020-04-07 09:09:13 +00:00
|
|
|
end
|
|
|
|
|
2021-01-29 00:09:17 +00:00
|
|
|
##
|
|
|
|
# Indicates if a recaptcha should be rendered before allowing this model to be saved.
|
|
|
|
#
|
|
|
|
def render_recaptcha?
|
|
|
|
return false unless Gitlab::Recaptcha.enabled?
|
|
|
|
|
|
|
|
return false if self.errors.count > 1 # captcha should not be rendered if are still other errors
|
|
|
|
|
|
|
|
self.needs_recaptcha?
|
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def spam!
|
|
|
|
self.spam = true
|
2020-04-07 09:09:13 +00:00
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def clear_spam_flags!
|
|
|
|
self.spam = false
|
|
|
|
self.needs_recaptcha = false
|
|
|
|
end
|
2020-04-07 09:09:13 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def invalidate_if_spam
|
|
|
|
if needs_recaptcha? && Gitlab::Recaptcha.enabled?
|
|
|
|
recaptcha_error!
|
|
|
|
elsif needs_recaptcha? || spam?
|
2020-04-07 09:09:13 +00:00
|
|
|
unrecoverable_spam_error!
|
|
|
|
end
|
2017-02-01 18:15:59 +00:00
|
|
|
end
|
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def recaptcha_error!
|
|
|
|
self.errors.add(:base, "Your #{spammable_entity_type} has been recognized as spam. "\
|
|
|
|
"Please, change the content or solve the reCAPTCHA to proceed.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def unrecoverable_spam_error!
|
|
|
|
self.errors.add(:base, "Your #{spammable_entity_type} has been recognized as spam and has been discarded.")
|
|
|
|
end
|
|
|
|
|
2017-02-01 18:15:59 +00:00
|
|
|
def spammable_entity_type
|
|
|
|
self.class.name.underscore
|
2016-07-30 04:18:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def spam_title
|
2016-08-09 17:43:47 +00:00
|
|
|
attr = self.class.spammable_attrs.find do |_, options|
|
2016-08-05 22:10:08 +00:00
|
|
|
options.fetch(:spam_title, false)
|
|
|
|
end
|
|
|
|
|
2017-08-04 02:20:34 +00:00
|
|
|
public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
|
2016-07-30 04:18:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def spam_description
|
2016-08-09 17:43:47 +00:00
|
|
|
attr = self.class.spammable_attrs.find do |_, options|
|
2016-08-05 22:10:08 +00:00
|
|
|
options.fetch(:spam_description, false)
|
|
|
|
end
|
2016-07-29 00:02:56 +00:00
|
|
|
|
2017-08-04 02:20:34 +00:00
|
|
|
public_send(attr.first) if attr && respond_to?(attr.first.to_sym) # rubocop:disable GitlabSecurity/PublicSend
|
2016-08-05 22:10:08 +00:00
|
|
|
end
|
2016-07-29 00:02:56 +00:00
|
|
|
|
|
|
|
def spammable_text
|
2016-08-09 17:43:47 +00:00
|
|
|
result = self.class.spammable_attrs.map do |attr|
|
2017-08-04 02:20:34 +00:00
|
|
|
public_send(attr.first) # rubocop:disable GitlabSecurity/PublicSend
|
2016-07-29 00:02:56 +00:00
|
|
|
end
|
2016-08-05 22:10:08 +00:00
|
|
|
|
2016-07-29 00:02:56 +00:00
|
|
|
result.reject(&:blank?).join("\n")
|
|
|
|
end
|
|
|
|
|
2016-08-05 22:10:08 +00:00
|
|
|
# Override in Spammable if further checks are necessary
|
2021-07-20 06:08:37 +00:00
|
|
|
def check_for_spam?(user:)
|
2016-08-09 17:43:47 +00:00
|
|
|
true
|
2016-07-21 23:11:53 +00:00
|
|
|
end
|
2019-09-25 03:06:21 +00:00
|
|
|
|
|
|
|
# Override in Spammable if differs
|
|
|
|
def allow_possible_spam?
|
|
|
|
Feature.enabled?(:allow_possible_spam, project)
|
|
|
|
end
|
2016-07-21 23:11:53 +00:00
|
|
|
end
|