-
Notifications
You must be signed in to change notification settings - Fork 70
/
lint-rules-from-freqs
executable file
·338 lines (289 loc) · 10.2 KB
/
lint-rules-from-freqs
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
#!/usr/bin/perl
#
# <@LICENSE>
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# </@LICENSE>
# any tests that get less than this % of matches on *both* spam or nonspam, are
# reported.
my $LOW_MATCHES_PERCENT = 0.03;
my $scoreset = 0;
sub usage {
die "
lint-rules-from-freqs: perform 'lint' testing on SpamAssassin rules and scores
usage: ./lint-rules-from-freqs [-f falsefreqs] < freqs > badtests
This analyzes SpamAssassin tests, based on the hit frequencies and S/O ratios
from a mass-check logfile pair.
The 'freqs' argument is the frequency of hits in all messages ('hit-frequencies
-x -p' output).
The 'falsefreqs' argument is frequencies of hits in false-positives and
false-negatives only ('hit-frequencies -x -p -f' output).
";
}
my $opt_falsefreqs;
while ($#ARGV >= 0) {
$_ = shift @ARGV;
if (/^-f/) { $_ = shift @ARGV; $opt_falsefreqs = $_; }
elsif (/^-s/) { $_ = shift @ARGV; $scoreset = $_; }
else { usage(); }
}
print "BAD TESTS REPORT\n";
readrules();
print "\n" .((scalar keys %rulefile) + 1). " rules found.\n";
print "\nRule file syntax issues:\n\n";
lintrules();
if ($opt_falsefreqs) {
open (FALSE, "<$opt_falsefreqs");
while (<FALSE>) {
if (!/^\s*([\d\.]+)/) {
my ($overall, $spam, $nons, $so, $score, $name) = split (' ');
next unless ($name =~ /\S/);
$falsefreqs_spam{$name} = $spam;
$falsefreqs_nons{$name} = $nons;
$falsefreqs_so{$name} = $so;
}
}
close FALSE;
}
while (<>) {
if (!/^\s*([\d\.]+)/) {
$output{'a_header'} = $_; next;
}
my $badrule;
my ($overall, $spam, $nons, $so, $score, $name) = split (' ');
next unless ($name =~ /\S/);
my $ffspam = $falsefreqs_spam{$name};
my $ffnons = $falsefreqs_nons{$name};
my $ffso = $falsefreqs_so{$name};
my $tf = $tflags{$name};
next if ($tf =~ /net/ && ($scoreset % 2) == 0);
next if ($tf =~ /userconf/);
if ($overall == 0.0 && $spam == 0.0 && $nons == 0.0) { # sanity!
$badrule = 'no matches';
} else {
if ($score < 0.0) {
# negative score with more spams than nonspams? bad rule.
if ($tf !~ /nice/ && $so > 0.5 && $score < 0.5) {
$badrule = 'non-nice but -ve score';
}
if ($tf =~ /nice/ && $so > 0.5 && $score < 0.5) {
if ($ffso < 0.5) {
$badrule = 'fn';
} else {
# ignore, the FNs are overridden by other tests so it doesn't
# affect the overall results.
}
}
# low number of matches overall
if ($nons < $LOW_MATCHES_PERCENT)
{ $badrule ||= ''; $badrule .= ', low matches'; }
} elsif ($score > 0.0) {
# positive score with more nonspams than spams? bad.
if ($tf =~ /nice/ && $so < 0.5 && $score > 0.5) {
$badrule = 'nice but +ve score';
}
if ($tf !~ /nice/ && $so < 0.5 && $score > 0.5) {
if ($ffso > 0.5) {
$badrule = 'fp';
} else {
# ignore, the FPs are overridden by other tests so it doesn't
# affect the overall results.
}
}
# low number of matches overall
if ($spam < $LOW_MATCHES_PERCENT)
{ $badrule ||= ''; $badrule .= ', low matches'; }
} elsif ($score == 0.0) {
$badrule = 'score is 0';
}
}
if (defined $badrule) {
$badrule =~ s/^, //; chomp;
$output{$badrule} .= $_ . " ($badrule)\n";
}
}
# do all but 'no/low matches' first
print "\nHigh-priority issues:\n\n";
foreach my $badness (sort keys %output) {
next if ($badness eq 'no matches');
next if ($badness eq 'low matches');
print $output{$badness};
delete $output{$badness};
}
# now go back and do the other 2 (if they're there)
print "\nLow-priority issues:\n\n";
foreach my $badness (sort keys %output) {
next unless defined ($output{$badness});
print $output{$badness};
delete $output{$badness};
}
exit;
sub concat_rule_lang {
my $rule = shift;
my $lang = shift;
if (defined $lang && $lang ne '') {
return "[$lang]_$rule";
} else {
return $rule;
}
}
# note: do not use parse-rules-for-masses here, we need to do linting instead
# of your average parse
sub readrules {
my @files = <../rules/[0-9]*.cf>;
my $file;
%rulesfound = ();
%langs = ();
foreach $file (@files) {
open (IN, "<$file");
while (<IN>) {
s/#.*$//g; s/^\s+//; s/\s+$//; next if /^$/;
# make all the foo-bar stuff foo_bar
1 while s/^(\S+)-/\1_/g;
1 while s/^(lang\s+\S+\s+\S+)-/\1_/g;
my $lang = '';
if (s/^lang\s+(\S+)\s+//) {
$lang = $1; $langs{$1} = 1;
}
if (/^(header|rawbody|body|full|uri|meta|mimeheader)\s+(\S+)\s+/) {
$rulesfound{$2} = 1;
$rulefile{$2} ||= $file;
$scorefile{$1} = $file;
$score{$2} ||= 1.0;
$tflags{$2} ||= '';
$descfile{$2} ||= $file; # a rule with no score or desc is OK
$description{$2}->{$lang} = undef;
if (/^body\s+\S+\s+eval:/) {
# ignored
} elsif (/^body\s+\S+\s+(.*)$/) {
my $re = $1;
# If there's a ( in a rule where it should be (?:, flag it.
# but ignore [abc(] ...
if ($re =~ /[^\\]\([^\?]/ && $re !~ /\[[^\]]*[^\\]\(/) {
print "warning: non-(?:...) capture in regexp in $file: $_\n";
}
if ($re =~ /\.[\*\+]/) {
print "warning: .* in regexp in $file: $_\n";
}
if ($re =~ /[^\\]\{(\d*),?(\d*?)\}/) {
if ($1 > 120 || $2 > 120) {
print "warning: long .{n} in regexp in $file: $_\n";
}
}
}
} elsif (/^describe\s+(\S+)\s+(.*?)\s*$/) {
$rulesfound{$1} = 1;
$descfile{concat_rule_lang ($1, $lang)} ||= $file;
$descfile{$1} ||= $file;
$description{$1}->{$lang} = $2;
} elsif (/^tflags\s+(\S+)\s+(.+)$/) {
$rulesfound{$1} = 1;
$tflags{$1} = $2;
$tflagsfile{concat_rule_lang ($1, $lang)} = $file;
$tflagsfile{$1} = $file;
} elsif (/^score\s+(\S+)\s+(.+)$/) {
$rulesfound{$1} = 1;
$scorefile{concat_rule_lang ($1, $lang)} = $file;
$scorefile{$1} = $file;
$score{$1} = $2;
} elsif (/^(clear_report_template|clear_spamtrap_template|report|spamtrap|
clear_terse_report_template|terse_report|
required_score|ok_locales|ok_languages|test|lang|
spamphrase|whitelist_from|require_version|
clear_unsafe_report_template|unsafe_report|
(?:bayes_)?auto_learn_threshold_nonspam|(?:bayes_)?auto_learn_threshold_spam|
(?:bayes_)?auto_learn
)/x) {
next;
} else {
print "warning: unknown rule in $file: $_\n";
}
}
close IN;
}
@langsfound = sort keys %langs;
@rulesfound = sort keys %rulesfound;
}
sub lintrules {
my %possible_renames = ();
foreach my $rule (@rulesfound) {
my $match = $rule;
$match =~ s/_\d+[^_]+$//gs; # trim e.g. "_20K"
$match =~ s/[^A-Z]+//gs; # trim numbers etc.
if (defined ($rulefile{$rule}) && $possible_renames{$match} !~ / \Q$rule\E\b/) {
$possible_renames{$match} .= " ".$rule;
}
$possible_rename_matches{$rule} = $match;
}
foreach my $lang ('', @langsfound) {
foreach my $baserule (@rulesfound) {
next if ( $baserule =~ /^__/ || $baserule =~ /^T_/ );
my $rule = concat_rule_lang ($baserule, $lang);
my $f = $descfile{$rule};
my $warned = '';
if (defined $f && !defined ($rulefile{$rule})
&& !defined ($rulefile{$baserule}))
{
print "warning: $baserule has description, but no rule: $f\n";
$warned .= ' lamedesc';
}
# Check our convention for rule length
if ( (($lang ne '' && defined($rulefile{$rule})) || ($lang eq '' && defined ($rulefile{$baserule}))) && length $baserule > 22 ) {
print "warning: $baserule has a name longer than 22 chars: $f\n";
}
# Check our convention for rule length
if ( (($lang ne '' && defined($rulefile{$rule})) || ($lang eq '' && defined ($rulefile{$baserule}))) && defined $description{$baserule}->{$lang} && length $description{$baserule}->{$lang} > 50 ) {
print "warning: $baserule has a description longer than 50 chars: $f\n";
}
# lang rule trumps normal rule
$f = $rulefile{$rule} || $rulefile{$baserule};
# if the rule exists, and the language/rule description doesn't exist ...
if ( defined $f && !defined $description{$baserule}->{$lang} )
{
print "warning: $baserule exists, ",( $lang ne '' ? "lang $lang, " : "" ),"but has no description: $f\n";
$warned .= ' lamedesc';
}
$f = $scorefile{$rule};
if (defined $f && !defined ($rulefile{$rule})
&& !defined ($rulefile{$baserule}))
{
print "warning: $baserule has score, but no rule: $f\n";
$warned .= ' lamescore';
}
my $r = $possible_rename_matches{$rule};
if ($warned ne '' && defined $r) {
my @matches = split (' ', $possible_renames{$r});
if (scalar @matches != 0) {
my $text = '';
# now try and figure out "nearby" rules with no description/score
foreach my $baser (@matches) {
my $blang;
if ($descfile{$rule} =~ /text_(\S\S)\./) {
$blang = $1;
}
my $r = concat_rule_lang ($baser, $blang);
#warn "$r $descfile{$r} $descfile{$baser}";
next if ($warned =~ /lamedesc/ && (defined $descfile{$r}));
next if ($warned =~ /lamescore/ && (defined $scorefile{$r}));
$text .= " $baser";
}
if ($text ne '') {
print "warning: (possible renamed rule? $text)\n";
}
}
}
}
}
}