Commit cebd8df7 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch 'cablett-spam-endpoint-params' into 'master'

Send params to external spam endpoint

See merge request gitlab-org/gitlab!33240
parents 909df758 f004f6b2
...@@ -22,15 +22,18 @@ module SpamCheckMethods ...@@ -22,15 +22,18 @@ module SpamCheckMethods
# a dirty instance, which means it should be already assigned with the new # a dirty instance, which means it should be already assigned with the new
# attribute values. # attribute values.
# rubocop:disable Gitlab/ModuleWithInstanceVariables # rubocop:disable Gitlab/ModuleWithInstanceVariables
def spam_check(spammable, user) def spam_check(spammable, user, action:)
raise ArgumentError.new('Please provide an action, such as :create') unless action
Spam::SpamActionService.new( Spam::SpamActionService.new(
spammable: spammable, spammable: spammable,
request: @request request: @request,
user: user,
context: { action: action }
).execute( ).execute(
api: @api, api: @api,
recaptcha_verified: @recaptcha_verified, recaptcha_verified: @recaptcha_verified,
spam_log_id: @spam_log_id, spam_log_id: @spam_log_id)
user: user)
end end
# rubocop:enable Gitlab/ModuleWithInstanceVariables # rubocop:enable Gitlab/ModuleWithInstanceVariables
end end
...@@ -15,7 +15,7 @@ module Issues ...@@ -15,7 +15,7 @@ module Issues
end end
def before_create(issue) def before_create(issue)
spam_check(issue, current_user) spam_check(issue, current_user, action: :create)
issue.move_to_end issue.move_to_end
# current_user (defined in BaseService) is not available within run_after_commit block # current_user (defined in BaseService) is not available within run_after_commit block
......
...@@ -18,7 +18,7 @@ module Issues ...@@ -18,7 +18,7 @@ module Issues
end end
def before_update(issue, skip_spam_check: false) def before_update(issue, skip_spam_check: false)
spam_check(issue, current_user) unless skip_spam_check spam_check(issue, current_user, action: :update) unless skip_spam_check
end end
def after_update(issue) def after_update(issue)
......
...@@ -13,7 +13,7 @@ module Snippets ...@@ -13,7 +13,7 @@ module Snippets
@snippet.author = current_user @snippet.author = current_user
spam_check(@snippet, current_user) spam_check(@snippet, current_user, action: :create)
if save_and_commit if save_and_commit
UserAgentDetailService.new(@snippet, @request).create UserAgentDetailService.new(@snippet, @request).create
......
...@@ -14,7 +14,7 @@ module Snippets ...@@ -14,7 +14,7 @@ module Snippets
end end
update_snippet_attributes(snippet) update_snippet_attributes(snippet)
spam_check(snippet, current_user) spam_check(snippet, current_user, action: :update)
if save_and_commit(snippet) if save_and_commit(snippet)
Gitlab::UsageDataCounters::SnippetCounter.count(:update) Gitlab::UsageDataCounters::SnippetCounter.count(:update)
......
...@@ -7,9 +7,11 @@ module Spam ...@@ -7,9 +7,11 @@ module Spam
attr_accessor :target, :request, :options attr_accessor :target, :request, :options
attr_reader :spam_log attr_reader :spam_log
def initialize(spammable:, request:) def initialize(spammable:, request:, user:, context: {})
@target = spammable @target = spammable
@request = request @request = request
@user = user
@context = context
@options = {} @options = {}
if @request if @request
...@@ -22,7 +24,7 @@ module Spam ...@@ -22,7 +24,7 @@ module Spam
end end
end end
def execute(api: false, recaptcha_verified:, spam_log_id:, user:) def execute(api: false, recaptcha_verified:, spam_log_id:)
if recaptcha_verified if recaptcha_verified
# If it's a request which is already verified through reCAPTCHA, # If it's a request which is already verified through reCAPTCHA,
# update the spam log accordingly. # update the spam log accordingly.
...@@ -40,6 +42,8 @@ module Spam ...@@ -40,6 +42,8 @@ module Spam
private private
attr_reader :user, :context
def allowlisted?(user) def allowlisted?(user)
user.respond_to?(:gitlab_employee) && user.gitlab_employee? user.respond_to?(:gitlab_employee) && user.gitlab_employee?
end end
...@@ -75,7 +79,7 @@ module Spam ...@@ -75,7 +79,7 @@ module Spam
description: target.spam_description, description: target.spam_description,
source_ip: options[:ip_address], source_ip: options[:ip_address],
user_agent: options[:user_agent], user_agent: options[:user_agent],
noteable_type: target.class.to_s, noteable_type: notable_type,
via_api: api via_api: api
} }
) )
...@@ -85,8 +89,14 @@ module Spam ...@@ -85,8 +89,14 @@ module Spam
def spam_verdict_service def spam_verdict_service
SpamVerdictService.new(target: target, SpamVerdictService.new(target: target,
user: user,
request: @request, request: @request,
options: options) options: options,
context: context.merge(target_type: notable_type))
end
def notable_type
@notable_type ||= target.class.to_s
end end
end end
end end
...@@ -5,11 +5,12 @@ module Spam ...@@ -5,11 +5,12 @@ module Spam
include AkismetMethods include AkismetMethods
include SpamConstants include SpamConstants
def initialize(target:, request:, options:, verdict_params: {}) def initialize(user:, target:, request:, options:, context: {})
@target = target @target = target
@request = request @request = request
@user = user
@options = options @options = options
@verdict_params = assemble_verdict_params(verdict_params) @verdict_params = assemble_verdict_params(context)
end end
def execute def execute
...@@ -27,7 +28,7 @@ module Spam ...@@ -27,7 +28,7 @@ module Spam
private private
attr_reader :target, :request, :options, :verdict_params attr_reader :user, :target, :request, :options, :verdict_params
def akismet_verdict def akismet_verdict
if akismet.spam? if akismet.spam?
...@@ -66,11 +67,23 @@ module Spam ...@@ -66,11 +67,23 @@ module Spam
end end
end end
def assemble_verdict_params(params) def assemble_verdict_params(context)
return {} unless endpoint_url return {} unless endpoint_url.present?
params.merge({ project = target.try(:project)
user_id: target.author_id
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)
}) })
end end
......
...@@ -24,7 +24,7 @@ describe Spam::SpamActionService do ...@@ -24,7 +24,7 @@ describe Spam::SpamActionService do
end end
describe '#initialize' do describe '#initialize' do
subject { described_class.new(spammable: issue, request: request) } subject { described_class.new(spammable: issue, request: request, user: user) }
context 'when the request is nil' do context 'when the request is nil' do
let(:request) { nil } let(:request) { nil }
...@@ -53,7 +53,7 @@ describe Spam::SpamActionService do ...@@ -53,7 +53,7 @@ describe Spam::SpamActionService do
shared_examples 'only checks for spam if a request is provided' do shared_examples 'only checks for spam if a request is provided' do
context 'when request is missing' do context 'when request is missing' do
subject { described_class.new(spammable: issue, request: nil) } subject { described_class.new(spammable: issue, request: nil, user: user) }
it "doesn't check as spam" do it "doesn't check as spam" do
subject subject
...@@ -78,9 +78,9 @@ describe Spam::SpamActionService do ...@@ -78,9 +78,9 @@ describe Spam::SpamActionService do
let_it_be(:existing_spam_log) { create(:spam_log, user: user, recaptcha_verified: false) } let_it_be(:existing_spam_log) { create(:spam_log, user: user, recaptcha_verified: false) }
subject do subject do
described_service = described_class.new(spammable: issue, request: request) described_service = described_class.new(spammable: issue, request: request, user: user)
allow(described_service).to receive(:allowlisted?).and_return(allowlisted) allow(described_service).to receive(:allowlisted?).and_return(allowlisted)
described_service.execute(user: user, api: nil, recaptcha_verified: recaptcha_verified, spam_log_id: existing_spam_log.id) described_service.execute(api: nil, recaptcha_verified: recaptcha_verified, spam_log_id: existing_spam_log.id)
end end
before do before do
......
...@@ -16,9 +16,10 @@ describe Spam::SpamVerdictService do ...@@ -16,9 +16,10 @@ describe Spam::SpamVerdictService do
let(:request) { double(:request, env: env) } let(:request) { double(:request, env: env) }
let(:check_for_spam) { true } let(:check_for_spam) { true }
let(:issue) { build(:issue) } let_it_be(:user) { create(:user) }
let(:issue) { build(:issue, author: user) }
let(:service) do let(:service) do
described_class.new(target: issue, request: request, options: {}) described_class.new(user: user, target: issue, request: request, options: {})
end end
describe '#execute' do describe '#execute' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment