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
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
package Bugzilla::Extension::AntiSpam;
use strict;
use warnings;
use base qw(Bugzilla::Extension);
use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::Util qw(remote_ip trick_taint);
use Email::Address;
use Encode;
use Socket;
use Sys::Syslog qw(:DEFAULT setlogsock);
our $VERSION = '1';
#
# project honeypot integration
#
sub _project_honeypot_blocking {
my ($self, $api_key, $login) = @_;
my $ip = remote_ip();
return unless $ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
my $lookup = "$api_key.$4.$3.$2.$1.dnsbl.httpbl.org";
return unless my $packed = gethostbyname($lookup);
my $honeypot = inet_ntoa($packed);
return unless $honeypot =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
my ($status, $days, $threat, $type) = ($1, $2, $3, $4);
return if $status != 127
|| $threat < Bugzilla->params->{honeypot_threat_threshold};
_syslog(sprintf("[audit] blocked <%s> from creating %s, honeypot %s", $ip, $login, $honeypot));
ThrowUserError('account_creation_restricted');
}
sub config_modify_panels {
my ($self, $args) = @_;
push @{ $args->{panels}->{auth}->{params} }, {
name => 'honeypot_api_key',
type => 't',
default => '',
};
push @{ $args->{panels}->{auth}->{params} }, {
name => 'honeypot_threat_threshold',
type => 't',
default => '32',
};
}
#
# comment blocking
#
sub _comment_blocking {
my ($self, $params) = @_;
# as we want to use this sparingly, we only block comments on bugs which
# the user didn't report, and skip it completely if the user is in the
# editbugs group.
my $user = Bugzilla->user;
return if $user->in_group('editbugs');
# new bug
return unless $params->{bug_id};
# existing bug
my $bug = ref($params->{bug_id})
? $params->{bug_id}
: Bugzilla::Bug->new($params->{bug_id});
return if $bug->reporter->id == $user->id;
my $blocklist = Bugzilla->dbh->selectcol_arrayref(
'SELECT word FROM antispam_comment_blocklist'
);
return unless @$blocklist;
my $regex = '\b(?:' . join('|', map { quotemeta } @$blocklist) . ')\b';
if ($params->{thetext} =~ /$regex/i) {
ThrowUserError('antispam_comment_blocked');
}
}
#
# domain blocking
#
sub _domain_blocking {
my ($self, $login) = @_;
my $address = Email::Address->new(undef, $login);
my $blocked = Bugzilla->dbh->selectrow_array(
"SELECT 1 FROM antispam_domain_blocklist WHERE domain=?",
undef,
$address->host
);
if ($blocked) {
_syslog(sprintf("[audit] blocked <%s> from creating %s, blacklisted domain", remote_ip(), $login));
ThrowUserError('account_creation_restricted');
}
}
#
# ip blocking
#
sub _ip_blocking {
my ($self, $login) = @_;
my $ip = remote_ip();
trick_taint($ip);
my $blocked = Bugzilla->dbh->selectrow_array(
"SELECT 1 FROM antispam_ip_blocklist WHERE ip_address=?",
undef,
$ip
);
if ($blocked) {
_syslog(sprintf("[audit] blocked <%s> from creating %s, blacklisted IP", $ip, $login));
ThrowUserError('account_creation_restricted');
}
}
#
# cc count restrictions
#
sub bug_before_create {
my ($self, $args) = @_;
$self->_cc_limit($args->{params}, 'cc');
}
sub bug_start_of_set_all {
my ($self, $args) = @_;
$self->_cc_limit($args->{params}, 'newcc');
}
sub _cc_limit {
my ($self, $params, $cc_field) = @_;
return unless exists $params->{$cc_field};
my $user = Bugzilla->user;
my $cc_count = ref($params->{$cc_field}) ? scalar(@{ $params->{$cc_field} }) : 1;
my $limit_count = Bugzilla->params->{antispam_cc_limit_count};
my $limit_age = Bugzilla->params->{antispam_cc_limit_age};
if ($cc_count > $limit_count && $user->creation_age < $limit_age) {
_syslog(sprintf("[audit] blocked <%s> from CC'ing %s users", $user->login, $cc_count));
delete $params->{$cc_field};
if (exists $params->{cc} && exists $params->{cc}->{add}) {
delete $params->{cc}->{add};
}
}
}
#
# spam user disabling
#
sub comment_after_add_tag {
my ($self, $args) = @_;
my $tag = lc($args->{tag});
return unless $tag eq 'spam' or $tag eq 'abusive' or $tag eq 'abuse';
my $comment = $args->{comment};
my $author = $comment->author;
# exclude disabled users
return if !$author->is_enabled;
# exclude users by group
return if $author->in_group(Bugzilla->params->{antispam_spammer_exclude_group});
# exclude users who are no longer new
return if !$author->is_new;
# exclude users who haven't made enough comments
my $count = $tag eq 'spam'
? Bugzilla->params->{antispam_spammer_comment_count}
: Bugzilla->params->{antispam_abusive_comment_count};
return if $author->comment_count < $count;
# get user's comments
trick_taint($tag);
my $comments = Bugzilla->dbh->selectall_arrayref("
SELECT longdescs.comment_id,longdescs_tags.id
FROM longdescs
LEFT JOIN longdescs_tags
ON longdescs_tags.comment_id = longdescs.comment_id
AND longdescs_tags.tag = ?
WHERE longdescs.who = ?
ORDER BY longdescs.bug_when
", undef, $tag, $author->id);
# this comment needs to be counted too
my $comment_id = $comment->id;
foreach my $ra (@$comments) {
if ($ra->[0] == $comment_id) {
$ra->[1] = 1;
last;
}
}
# throw away comment id and negate bool to make it a list of not-spam/abuse
$comments = [ map { $_->[1] ? 0 : 1 } @$comments ];
my $reason;
# check if the first N comments are spam/abuse
if (!scalar(grep { $_ } @$comments[0..($count - 1)])) {
$reason = "first $count comments are $tag";
}
# check if the last N comments are spam/abuse
elsif (!scalar(grep { $_ } @$comments[-$count..-1])) {
$reason = "last $count comments are $tag";
}
# disable
if ($reason) {
$author->set_disabledtext(
$tag eq 'spam'
? Bugzilla->params->{antispam_spammer_disable_text}
: Bugzilla->params->{antispam_abusive_disable_text}
);
$author->set_disable_mail(1);
$author->update();
_syslog(sprintf("[audit] antispam disabled <%s>: %s", $author->login, $reason));
}
}
#
# hooks
#
sub object_end_of_create_validators {
my ($self, $args) = @_;
if ($args->{class} eq 'Bugzilla::Comment') {
$self->_comment_blocking($args->{params});
}
}
sub user_verify_login {
my ($self, $args) = @_;
if (my $api_key = Bugzilla->params->{honeypot_api_key}) {
$self->_project_honeypot_blocking($api_key, $args->{login});
}
$self->_ip_blocking($args->{login});
$self->_domain_blocking($args->{login});
}
sub editable_tables {
my ($self, $args) = @_;
my $tables = $args->{tables};
# allow these tables to be edited with the EditTables extension
$tables->{antispam_domain_blocklist} = {
id_field => 'id',
order_by => 'domain',
blurb => 'List of fully qualified domain names to block at account creation time.',
group => 'can_configure_antispam',
};
$tables->{antispam_comment_blocklist} = {
id_field => 'id',
order_by => 'word',
blurb => "List of whole words that will cause comments containing \\b\$word\\b to be blocked.\n" .
"This only applies to comments on bugs which the user didn't report.\n" .
"Users in the editbugs group are exempt from comment blocking.",
group => 'can_configure_antispam',
};
$tables->{antispam_ip_blocklist} = {
id_field => 'id',
order_by => 'ip_address',
blurb => 'List of IPv4 addresses which are prevented from creating accounts.',
group => 'can_configure_antispam',
};
}
sub config_add_panels {
my ($self, $args) = @_;
my $modules = $args->{panel_modules};
$modules->{AntiSpam} = "Bugzilla::Extension::AntiSpam::Config";
}
#
# installation
#
sub install_before_final_checks {
if (!Bugzilla::Group->new({ name => 'can_configure_antispam' })) {
Bugzilla::Group->create({
name => 'can_configure_antispam',
description => 'Can configure Anti-Spam measures',
isbuggroup => 0,
});
}
}
sub db_schema_abstract_schema {
my ($self, $args) = @_;
$args->{'schema'}->{'antispam_domain_blocklist'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
domain => {
TYPE => 'VARCHAR(255)',
NOTNULL => 1,
},
comment => {
TYPE => 'VARCHAR(255)',
NOTNULL => 1,
},
],
INDEXES => [
antispam_domain_blocklist_idx => {
FIELDS => [ 'domain' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'antispam_comment_blocklist'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
word => {
TYPE => 'VARCHAR(255)',
NOTNULL => 1,
},
],
INDEXES => [
antispam_comment_blocklist_idx => {
FIELDS => [ 'word' ],
TYPE => 'UNIQUE',
},
],
};
$args->{'schema'}->{'antispam_ip_blocklist'} = {
FIELDS => [
id => {
TYPE => 'MEDIUMSERIAL',
NOTNULL => 1,
PRIMARYKEY => 1,
},
ip_address => {
TYPE => 'VARCHAR(15)',
NOTNULL => 1,
},
comment => {
TYPE => 'VARCHAR(255)',
NOTNULL => 1,
},
],
INDEXES => [
antispam_ip_blocklist_idx => {
FIELDS => [ 'ip_address' ],
TYPE => 'UNIQUE',
},
],
};
}
#
# utilities
#
sub _syslog {
my $message = shift;
openlog('apache', 'cons,pid', 'local4');
syslog('notice', encode_utf8($message));
closelog();
}
__PACKAGE__->NAME;
|