1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Spam::SpamVerdictService, feature_category: :instance_resiliency do
include_context 'includes Spam constants'
let(:fake_ip) { '1.2.3.4' }
let(:fake_user_agent) { 'fake-user-agent' }
let(:fake_referer) { 'fake-http-referer' }
let(:env) do
{ 'action_dispatch.remote_ip' => fake_ip,
'HTTP_USER_AGENT' => fake_user_agent,
'HTTP_REFERER' => fake_referer }
end
let(:verdict_value) { ::Spamcheck::SpamVerdict::Verdict::ALLOW }
let(:verdict_score) { 0.01 }
let(:verdict_evaluated) { true }
let(:response) do
response = ::Spamcheck::SpamVerdict.new
response.verdict = verdict_value
response.score = verdict_score
response.evaluated = verdict_evaluated
response
end
let(:spam_client_result) do
Gitlab::Spamcheck::Result.new(response)
end
let(:check_for_spam) { true }
let_it_be(:user) { create(:user) }
let_it_be(:issue) { create(:issue, author: user) }
let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }
let(:service) do
described_class.new(user: user, target: target, options: {})
end
shared_examples 'execute spam verdict service' do
subject(:execute) { service.execute }
before do
allow(service).to receive(:get_akismet_verdict).and_return(nil)
allow(service).to receive(:get_spamcheck_verdict).and_return(nil)
end
context 'if all services return nil' do
it 'renders ALLOW verdict' do
is_expected.to eq ALLOW
end
end
context 'if only one service returns a verdict' do
context 'and it is supported' do
before do
allow(service).to receive(:get_akismet_verdict).and_return(DISALLOW)
end
it 'renders that verdict' do
is_expected.to eq DISALLOW
end
end
context 'and it is unexpected' do
before do
allow(service).to receive(:get_akismet_verdict).and_return("unexpected")
end
it 'allows' do
is_expected.to eq ALLOW
end
end
end
context 'if more than one service returns a verdict' do
context 'and they are supported' do
before do
allow(service).to receive(:get_akismet_verdict).and_return(DISALLOW)
allow(service).to receive(:get_spamcheck_verdict).and_return(BLOCK_USER)
end
it 'renders the more restrictive verdict' do
is_expected.to eq BLOCK_USER
end
end
context 'and one is supported' do
before do
allow(service).to receive(:get_akismet_verdict).and_return('nonsense')
allow(service).to receive(:get_spamcheck_verdict).and_return(BLOCK_USER)
end
it 'renders the more restrictive verdict' do
is_expected.to eq BLOCK_USER
end
end
context 'and none are supported' do
before do
allow(service).to receive(:get_akismet_verdict).and_return('nonsense')
allow(service).to receive(:get_spamcheck_verdict).and_return('rubbish')
end
it 'renders the more restrictive verdict' do
is_expected.to eq ALLOW
end
end
end
context 'if allow_possible_spam application setting is true' do
before do
stub_application_setting(allow_possible_spam: true)
end
context 'and a service returns a verdict that should be overridden' do
before do
allow(service).to receive(:get_spamcheck_verdict).and_return(BLOCK_USER)
end
it 'overrides and renders the override verdict' do
is_expected.to eq OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM
end
end
context 'and a service returns a verdict that does not need to be overridden' do
before do
allow(service).to receive(:get_spamcheck_verdict).and_return(ALLOW)
end
it 'does not override and renders the original verdict' do
is_expected.to eq ALLOW
end
end
end
context 'if allow_possible_spam user custom attribute is set' do
before do
UserCustomAttribute.upsert_custom_attributes(
[{
user_id: user.id,
key: 'allow_possible_spam',
value: 'does not matter'
}]
)
end
context 'and a service returns a verdict that should be overridden' do
before do
allow(service).to receive(:get_spamcheck_verdict).and_return(BLOCK_USER)
end
it 'overrides and renders the override verdict' do
is_expected.to eq OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM
end
end
context 'and a service returns a verdict that does not need to be overridden' do
before do
allow(service).to receive(:get_spamcheck_verdict).and_return(ALLOW)
end
it 'does not override and renders the original verdict' do
is_expected.to eq ALLOW
end
end
end
context 'records metrics' do
let(:histogram) { instance_double(Prometheus::Client::Histogram) }
using RSpec::Parameterized::TableSyntax
where(:verdict, :label) do
Spam::SpamConstants::ALLOW | 'ALLOW'
Spam::SpamConstants::CONDITIONAL_ALLOW | 'CONDITIONAL_ALLOW'
Spam::SpamConstants::BLOCK_USER | 'BLOCK'
Spam::SpamConstants::DISALLOW | 'DISALLOW'
Spam::SpamConstants::NOOP | 'NOOP'
end
with_them do
before do
allow(Gitlab::Metrics).to receive(:histogram).with(:gitlab_spamcheck_request_duration_seconds, anything).and_return(histogram)
allow(service).to receive(:get_spamcheck_verdict).and_return(verdict)
end
it 'records duration with labels' do
expect(histogram).to receive(:observe).with(a_hash_including(result: label), anything)
execute
end
end
end
end
shared_examples 'akismet verdict' do
let(:target) { issue }
subject(:get_akismet_verdict) { service.send(:get_akismet_verdict) }
context 'if Akismet is enabled' do
before do
stub_application_setting(akismet_enabled: true)
allow_next_instance_of(Spam::AkismetService) do |service|
allow(service).to receive(:spam?).and_return(akismet_result)
end
end
context 'if Akismet considers it spam' do
let(:akismet_result) { true }
context 'if reCAPTCHA is enabled' do
before do
stub_application_setting(recaptcha_enabled: true)
end
it 'returns conditionally allow verdict' do
is_expected.to eq CONDITIONAL_ALLOW
end
end
context 'if reCAPTCHA is not enabled' do
before do
stub_application_setting(recaptcha_enabled: false)
end
it 'renders disallow verdict' do
is_expected.to eq DISALLOW
end
end
end
context 'if Akismet does not consider it spam' do
let(:akismet_result) { false }
it 'renders allow verdict' do
is_expected.to eq ALLOW
end
end
end
context 'if Akismet is not enabled' do
before do
stub_application_setting(akismet_enabled: false)
end
it 'renders allow verdict' do
is_expected.to eq ALLOW
end
end
end
shared_examples 'spamcheck verdict' do
subject(:get_spamcheck_verdict) { service.send(:get_spamcheck_verdict) }
context 'if a Spam Check endpoint enabled and set to a URL' do
let(:spam_check_body) { {} }
let(:endpoint_url) { "grpc://www.spamcheckurl.com/spam_check" }
let(:spam_client) do
Gitlab::Spamcheck::Client.new
end
before do
stub_application_setting(spam_check_endpoint_enabled: true)
stub_application_setting(spam_check_endpoint_url: endpoint_url)
end
context 'if the endpoint is accessible' do
let(:user_scores) { Abuse::UserTrustScore.new(user) }
before do
allow(service).to receive(:spamcheck_client).and_return(spam_client)
allow(spam_client).to receive(:spam?).and_return(spam_client_result)
end
context 'if the result is a NOOP verdict' do
let(:verdict_evaluated) { false }
let(:verdict_value) { ::Spamcheck::SpamVerdict::Verdict::NOOP }
it 'returns the verdict' do
is_expected.to eq(NOOP)
expect(user_scores.spam_score).to eq(0.0)
end
end
context 'the result is a valid verdict' do
let(:verdict_score) { 0.05 }
let(:verdict_value) { ::Spamcheck::SpamVerdict::Verdict::ALLOW }
context 'the result was evaluated' do
it 'returns the verdict and updates the spam score' do
is_expected.to eq(ALLOW)
expect(user_scores.spam_score).to be_within(0.000001).of(verdict_score)
end
end
context 'the result was not evaluated' do
let(:verdict_evaluated) { false }
it 'returns the verdict and does not update the spam score' do
expect(subject).to eq(ALLOW)
expect(user_scores.spam_score).to eq(0.0)
end
end
end
context 'when recaptcha is enabled' do
before do
allow(Gitlab::Recaptcha).to receive(:enabled?).and_return(true)
end
using RSpec::Parameterized::TableSyntax
where(:verdict_value, :expected, :verdict_score) do
::Spamcheck::SpamVerdict::Verdict::ALLOW | ::Spam::SpamConstants::ALLOW | 0.1
::Spamcheck::SpamVerdict::Verdict::CONDITIONAL_ALLOW | ::Spam::SpamConstants::CONDITIONAL_ALLOW | 0.5
::Spamcheck::SpamVerdict::Verdict::DISALLOW | ::Spam::SpamConstants::DISALLOW | 0.8
::Spamcheck::SpamVerdict::Verdict::BLOCK | ::Spam::SpamConstants::BLOCK_USER | 0.9
end
with_them do
it "returns expected spam constant and updates the spam score" do
is_expected.to eq(expected)
expect(user_scores.spam_score).to be_within(0.000001).of(verdict_score)
end
end
end
context 'when recaptcha is disabled' do
before do
allow(Gitlab::Recaptcha).to receive(:enabled?).and_return(false)
end
using RSpec::Parameterized::TableSyntax
where(:verdict_value, :expected) do
::Spamcheck::SpamVerdict::Verdict::ALLOW | ::Spam::SpamConstants::ALLOW
::Spamcheck::SpamVerdict::Verdict::CONDITIONAL_ALLOW | ::Spam::SpamConstants::CONDITIONAL_ALLOW
::Spamcheck::SpamVerdict::Verdict::DISALLOW | ::Spam::SpamConstants::DISALLOW
::Spamcheck::SpamVerdict::Verdict::BLOCK | ::Spam::SpamConstants::BLOCK_USER
end
with_them do
it "returns expected spam constant" do
is_expected.to eq(expected)
end
end
end
context 'the requested is aborted' do
before do
allow(spam_client).to receive(:spam?).and_raise(GRPC::Aborted)
end
it 'returns nil' do
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
an_instance_of(GRPC::Aborted), error: ::Spam::SpamConstants::ERROR_TYPE
)
is_expected.to be_nil
end
end
context 'if the endpoint times out' do
before do
allow(spam_client).to receive(:spam?).and_raise(GRPC::DeadlineExceeded)
end
it 'returns nil' do
expect(Gitlab::ErrorTracking).to receive(:log_exception).with(
an_instance_of(GRPC::DeadlineExceeded), error: ::Spam::SpamConstants::ERROR_TYPE
)
is_expected.to be_nil
end
end
end
end
context 'if a Spam Check endpoint is not set' do
before do
stub_application_setting(spam_check_endpoint_url: nil)
end
it 'returns nil' do
is_expected.to be_nil
end
end
context 'if Spam Check endpoint is not enabled' do
before do
stub_application_setting(spam_check_endpoint_enabled: false)
end
it 'returns nil' do
is_expected.to be_nil
end
end
end
describe '#execute' do
describe 'issue' do
let(:target) { issue }
context 'when issue is publicly visible' do
before do
allow(issue).to receive(:publicly_visible?).and_return(true)
end
it_behaves_like 'execute spam verdict service'
end
context 'when issue is not publicly visible' do
before do
allow(issue).to receive(:publicly_visible?).and_return(false)
allow(service).to receive(:get_spamcheck_verdict).and_return(BLOCK_USER)
end
it 'overrides and renders the override verdict' do
expect(service.execute).to eq OVERRIDE_VIA_ALLOW_POSSIBLE_SPAM
end
end
end
describe 'snippet' do
let(:target) { snippet }
it_behaves_like 'execute spam verdict service'
end
end
describe '#get_akismet_verdict' do
describe 'issue' do
let(:target) { issue }
it_behaves_like 'akismet verdict'
end
describe 'snippet' do
let(:target) { snippet }
it_behaves_like 'akismet verdict'
end
end
describe '#get_spamcheck_verdict' do
describe 'issue' do
let(:target) { issue }
it_behaves_like 'spamcheck verdict'
end
describe 'snippet' do
let(:target) { snippet }
it_behaves_like 'spamcheck verdict'
end
end
end
|