lib/Mail/AuthenticationResults/SpamAssassin.pm

filter: Statementstmt 93.0% 94 / 101 Branchbran 64.7% 22 / 34 Conditioncond 25.0% 3 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 95.0% 19 / 20 Podpod 100% 5 / 5 totaltotal 80.0% 144 / 180 CC 25 SCAR 18.6
CC
25
Cov
81%
authentication_results_spf_fail
18.2
authentication_results_dmarc_list_override
7.2
_authentication_results_spf_fail_sub
5.2
CRAP 6.4
« blib/lib/Mail/AuthenticationResults/SpamAssassin.pm ↑ Summary
1 package Mail::AuthenticationResults::SpamAssassin;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use feature qw(postderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings qw(experimental::postderef); ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: SpamAssassin plugin for parsing Authentication-Results headers via Mail::AuthenticationResults
7 1 our $VERSION = '1.20180927'; # VERSION
8  
9  
10 1 ▶use Mail::SpamAssassin::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @ISA = qw(Mail::SpamAssassin::Plugin);
12  
13 1 ▶use Mail::AuthenticationResults 1.20180923;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mail::AuthenticationResults::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mail::AuthenticationResults::Header::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub new {
19 1 ▶ my ($class, $mailsa) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
20 1 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
21 1 my $self = $class->SUPER::new($mailsa);
22 1 bless ($self, $class);
23 1 $self->register_eval_rule('authentication_results_dmarc_list_override');
24 1 $self->register_eval_rule('authentication_results_has_key_value');
25 1 $self->register_eval_rule('authentication_results_spf_fail');
26 1 return $self;
27 }
28  
29  
30 sub parse_config {
31 1 ▶ my ( $self, $opts ) = @_;
Subroutine parse_config: called CC=2 SCAR=7.2
Pod documented
32 1 ▶ if ($opts->{key} eq 'authentication_results_authserv_id') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"key"} eq "authentication_results_authserv_id") 1 0 1
33 1 my $authserv_id = quotemeta( $opts->{value} );
34 1 $self->{'authserv-id'} = $authserv_id;
35 1 return 1;
36 }
37 0 return 0;
38 }
39  
40 sub _get_authentication_results_objects {
41 8 ▶ my ( $self, $per_msg_status ) = @_;
Subroutine _get_authentication_results_objects: called CC=4 SCAR=13.9
42 8 my @objects;
43  
44 8 ▶ if ( exists ( $per_msg_status->{'_authmilter_authentication_results_objects'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $per_msg_status->{"_authmilter_authentication_results_objects"}) 6 2 8
45 6 return $per_msg_status->{'_authmilter_authentication_results_objects'};
46 }
47  
48 2 my $group = Mail::AuthenticationResults::Header::Group->new();
49  
50 2 my $headers = $per_msg_status->get('Authentication-Results');
51 2 chomp $headers;
52 2 foreach my $header ( split "\n", $headers ) {
53  
54 2 my $parsed = eval{ Mail::AuthenticationResults::Parser->new()->parse( $header ) };
55 2 ▶ next if ! $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 0 2 2
56 2 $group->add_child( $parsed );
57 }
58 2 $per_msg_status->{'_authmilter_authentication_results_objects'} = $group;
59  
60 2 return $per_msg_status->{'_authmilter_authentication_results_objects'};
61 }
62  
63 sub _get_authentication_results_objects_for_authserv_id {
64 8 ▶ my ( $self, $per_msg_status ) = @_;
Subroutine _get_authentication_results_objects_for_authserv_id: called CC=1 SCAR=0.0
65 8 my $header_objects = $self->_get_authentication_results_objects($per_msg_status);
66 8 my $authserv_id = $self->{'authserv-id'};
67 8 return $header_objects->search({ isa => 'header', authserv_id => qr{$authserv_id$} });
68 }
69  
70 sub _get_authentication_results_objects_for_key_value {
71 4 ▶ my ( $self, $per_msg_status, $key, $value ) = @_;
Subroutine _get_authentication_results_objects_for_key_value: called CC=1 SCAR=0.0
72 4 return $self->_get_authentication_results_objects_for_authserv_id($per_msg_status)->search({ isa => 'entry', key => $key, value => $value });
73 }
74  
75 sub _get_authentication_results_objects_for_key {
76 4 ▶ my ( $self, $per_msg_status, $key ) = @_;
Subroutine _get_authentication_results_objects_for_key: called CC=1 SCAR=0.0
77 4 return $self->_get_authentication_results_objects_for_authserv_id($per_msg_status)->search({ isa => 'entry', key => $key });
78 }
79  
80 sub _entry_has_key {
81 # return a count of the subentries with given key
82 0 ▶ my ( $self, $authentication_results_object, $key ) = @_;
Subroutine _entry_has_key: not called CC=1 SCAR=6.9
83 0 return scalar $authentication_results_object->search({ isa => 'subentry', key => $key })->children()->@*;
84 }
85  
86 sub _entry_has_key_value {
87 # return a count of the subentries with given key and value
88 5 ▶ my ( $self, $authentication_results_object, $key, $value ) = @_;
Subroutine _entry_has_key_value: called CC=1 SCAR=0.0
89 5 return scalar $authentication_results_object->search({ isa => 'subentry', key => $key, value => $value })->children()->@*;
90 }
91  
92  
93 sub authentication_results_has_key_value {
94 # Returns true if there was a failing sligned-from entry in the results
95 4 ▶ my ( $self, $per_msg_status, $key, $value ) = @_;
Subroutine authentication_results_has_key_value: called CC=2 SCAR=6.9
Pod documented
96 4 ▶ return 1 if ( scalar $self->_get_authentication_results_objects_for_key_value($per_msg_status,$key,$value)->children()->@* > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->_get_authentication_results_objects_for_key_value($per_msg_status, $key, $value)->children} > 0 2 2 4
97 2 return 0;
98 }
99  
100 # Aligned From x-aligned-from
101 # Possible values: error null null_smtp null_header pass domain_pass orgdomain_pass fail
102  
103 sub _authentication_results_spf_fail_sub {
104 2 ▶ my ( $self, $per_msg_status, $spf_objects, $domain ) = @_;
Subroutine _authentication_results_spf_fail_sub: called CC=5 SCAR=16.4
105 # Can we override a given domain fail with another spf result in the set.
106 # Return 0 (fail) if we cannot, or 1 (pass) if we can.
107  
108 2 ▶ if ( $domain =~ /\@/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /\@/) 2 0 2
109 2 $domain =~ s/^.*\@//;
110 }
111 2 my $domainregex = quotemeta( $domain );
112  
113 2 ▶ return 1 if scalar
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$spf_objects->search({"isa", "entry", "key", "spf", "value", qr/^(?!fail)/, "has", [{"isa", "subentry", "key", "smtp.mailfrom", "value", qr/\@$domainregex$/}]})->children} 1 1 2
114 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'smtp.mailfrom', value => qr{\@$domainregex$} }] })->children()->@*;
115 1 ▶ return 1 if scalar
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$spf_objects->search({"isa", "entry", "key", "spf", "value", qr/^(?!fail)/, "has", [{"isa", "subentry", "key", "smtp.helo", "value", $domain}]})->children} 0 1 1
116 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'smtp.helo', value => $domain }] })->children()->@*;
117 1 ▶ return 1 if scalar
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$spf_objects->search({"isa", "entry", "key", "spf", "value", qr/^(?!fail)/, "has", [{"isa", "subentry", "key", "policy.authdomain", "value", $domain}]})->children} 0 1 1
118 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'policy.authdomain', value => $domain }] })->children()->@*;
119  
120 1 return 0;
121 }
122  
123  
124 sub authentication_results_spf_fail {
125 # De we have any spf fail entries which do not have a corresponding pass entry (from for example, an arc override)
126 2 ▶ my ( $self, $per_msg_status ) = @_;
Subroutine authentication_results_spf_fail: called CC=9 SCAR=29.0
Pod documented
127 2 my $pass = 1;
128 2 my $spf_objects = $self->_get_authentication_results_objects_for_key($per_msg_status,'spf');
129 2 foreach my $header_object ( $spf_objects->children()->@* ) {
130 4 ▶ next unless $header_object->value() eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
unless $header_object->value eq "fail" 2 2 4
131 2 ▶ if ( my $authdomain = eval{ $header_object->search({ isa => 'subentry', key => 'policy.authdomain' })->children()->[0]->value() } ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $authdomain = eval { do { $header_object->search({'isa', 'subentry', 'key', 'policy.authdomain'})->children->[0]->value } }) { } 0 2 2
elsif (my $mailfrom = eval { do { $header_object->search({'isa', 'subentry', 'key', 'smtp.mailfrom'})->children->[0]->value } }) { } 2 0 2
elsif (my $helo = eval { do { $header_object->search({'isa', 'subentry', 'key', 'smtp.helo'})->children->[0]->value } }) { } 0 0 0
132 0 ▶ $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $authdomain );
Condition0%0/3
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $authdomain)
!ll&&!rl&&r
000
MC/DC0%0/2
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $authdomain)
$pass $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $authdomain)
Truth table0%0/3
A && B
A= $passB= $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $authdomain)
ABresult
0X0
100
111
133 }
134 2 elsif ( my $mailfrom = eval{ $header_object->search({ isa => 'subentry', key => 'smtp.mailfrom' })->children()->[0]->value() } ) {
135 2 ▶ $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $mailfrom );
Condition66%2/3
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $mailfrom)
!ll&&!rl&&r
011
MC/DC50%1/2
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $mailfrom)
$pass $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $mailfrom)
Truth table66%2/3
A && B
A= $passB= $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $mailfrom)
ABresult
0X0
100
111
136 }
137 0 elsif ( my $helo = eval{ $header_object->search({ isa => 'subentry', key => 'smtp.helo' })->children()->[0]->value() } ) {
138 0 ▶ $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $helo );
Condition0%0/3
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $helo)
!ll&&!rl&&r
000
MC/DC0%0/2
$pass && $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $helo)
$pass $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $helo)
Truth table0%0/3
A && B
A= $passB= $self->_authentication_results_spf_fail_sub($per_msg_status, $spf_objects, $helo)
ABresult
0X0
100
111
139 }
140 else {
141 0 $pass = 0;
142 }
143 }
144 2 return !$pass;
145 }
146  
147  
148 sub authentication_results_dmarc_list_override {
149 2 ▶ my ( $self, $per_msg_status ) = @_;
Subroutine authentication_results_dmarc_list_override: called CC=7 SCAR=19.7
Pod documented
150 2 foreach my $header_object ( $self->_get_authentication_results_objects_for_key($per_msg_status,'dmarc')->children()->@* ) {
151 3 ▶ next unless $header_object->value() eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
unless $header_object->value eq "fail" 1 2 3
152 2 ▶ next unless $self->_entry_has_key_value( $header_object, 'policy.arc-aware-result', 'fail' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_entry_has_key_value($header_object, "policy.arc-aware-result", "fail") 1 1 2
153 1 ▶ next unless $self->_entry_has_key_value( $header_object, 'policy.applied-disposition', 'none' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_entry_has_key_value($header_object, "policy.applied-disposition", "none") 0 1 1
154 1 ▶ next unless $self->_entry_has_key_value( $header_object, 'policy.override-reason', 'mailing_list' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_entry_has_key_value($header_object, "policy.override-reason", "mailing_list") 0 1 1
155 1 ▶ next if $self->_entry_has_key_value( $header_object, 'policy.evaluated-disposition', 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_entry_has_key_value($header_object, "policy.evaluated-disposition", "none") 0 1 1
156 1 return 1;
157 }
158 1 return 0;
159 }
160  
161 1 1;
162  
163 __END__
164  
165 =pod
166  
167 =encoding UTF-8
168  
169 =head1 NAME
170  
171 Mail::AuthenticationResults::SpamAssassin - SpamAssassin plugin for parsing Authentication-Results headers via Mail::AuthenticationResults
172  
173 =head1 VERSION
174  
175 version 1.20180927
176  
177 =head1 DESCRIPTION
178  
179 Process Authentication-Results in Spam Assassin
180  
181 =head1 CONSTRUCTOR
182  
183 =head2 I<new()>
184  
185 Create a SpamAssassin plugin which subclasses this class
186  
187 package AuthenticationResults;
188 use base 'Mail::AuthenticationResults::SpamAssassin';
189 1;
190  
191 Load that pluigin into SpamAssassin and set the authserv-id for the headers
192 we should be checking
193  
194 loadplugin AuthenticationResults AuthenticationResults.pm
195 authentication_results_authserv_id .example.com
196  
197 =head1 METHODS
198  
199 =head2 I<parse_config()>
200  
201 SpamAssassin plugin method to handle config entries
202  
203 =head2 I<authentication_results_has_key_value( $key, $value )>
204  
205 eval method, returns true if there is an authentication-results entry with the
206 given key and value.
207  
208 header PTR_FAIL eval:authentication_results_has_key_value('x-ptr','fail')
209 score PTR_FAIL 1
210  
211 header IPREV_FAIL eval:authentication_results_has_key_value('iprev','fail')
212 score IPREV_FAIL 1
213  
214 header RETURN_FAIL eval:authentication_results_has_key_value('x-return-mx','fail')
215 RETURN_FAIL 2
216  
217 header RETURN_WARN eval:authentication_results_has_key_value('x-return-mx','warn')
218 RETURN_WARN 1
219  
220 header __SPF_ERROR eval:authentication_results_has_key_value('spf','error')
221 header __SPF_PERMERROR eval:authentication_results_has_key_value('spf','permerror')
222 header __SPF_TEMPERROR eval:authentication_results_has_key_value('spf','temperror')
223 meta SPF_ERROR __SPF_ERROR || __SPF_PERMERROR || __SPF_TEMPERROR
224 score SPF_ERROR 1
225  
226 =head2 I<authentication_results_spf_fail()>
227  
228 eval method, returns true if there is an authentication-results entry for an spf fail
229 which does not also have a non fail entry for the same domain (as for example, would happen
230 when a trusted ARC forwarded adds a pass).
231  
232 header SPF_FAIL eval:authentication_results_spf_fail()
233 score SPF_FAIL 1
234  
235 =head2 I<authentication_results_dmarc_list_override()>
236  
237 eval method, returns true if there was a DMARC override for a failing reject/quarantine policy due
238 to detecting a list with a simple header check? (not a whitelist or arc)
239  
240 header DMARC_LIST_OVERRIDE eval:authentication_results_dmarc_list_override()
241 score DMARC_LIST_OVERRIDE 2
242  
243 DMARC Reject should be handled on the border, if not then we class this as a local policy override to quarantine.
244  
245 DMARC Quarantine is handled by the X-Disposition-Quarantine: header
246  
247 header DMARC_QUARANTINE X-Disposition-Quarantine =~ /\S/
248 score DMARC_QUARANTINE 10.0
249  
250 =head1 AUTHOR
251  
252 Marc Bradshaw <marc@marcbradshaw.net>
253  
254 =head1 COPYRIGHT AND LICENSE
255  
256 This software is copyright (c) 2018 by Marc Bradshaw.
257  
258 This is free software; you can redistribute it and/or modify it under
259 the same terms as the Perl 5 programming language system itself.
260  
261 =cut
« blib/lib/Mail/AuthenticationResults/SpamAssassin.pm ↑ Summary