blib/lib/Mail/AuthenticationResults/SpamAssassin.pm untested

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 196 CC 18 SCAR 28.5
CC
18
Cov
0%
authentication_results_dmarc_list_override
42.0
_authentication_results_spf_fail_sub
30.0
authentication_results_spf_fail
30.0
CRAP 17.3
1 package Mail::AuthenticationResults::SpamAssassin;
2 use strict;
3 use warnings;
4 use feature qw(postderef);
5 no warnings qw(experimental::postderef); ## no critic
6 # ABSTRACT: SpamAssassin plugin for parsing Authentication-Results headers via Mail::AuthenticationResults
7 our $VERSION = '1.20180927'; # VERSION
8  
9  
10 use Mail::SpamAssassin::Plugin;
11 our @ISA = qw(Mail::SpamAssassin::Plugin);
12  
13 use Mail::AuthenticationResults 1.20180923;
14 use Mail::AuthenticationResults::Parser;
15 use Mail::AuthenticationResults::Header::Group;
16  
17  
18 sub new {
19 my ($class, $mailsa) = @_;
20 $class = ref($class) || $class;
21 my $self = $class->SUPER::new($mailsa);
22 bless ($self, $class);
23 $self->register_eval_rule('authentication_results_dmarc_list_override');
24 $self->register_eval_rule('authentication_results_has_key_value');
25 $self->register_eval_rule('authentication_results_spf_fail');
26 return $self;
27 }
28  
29  
30 sub parse_config {
31 my ( $self, $opts ) = @_;
32 if ($opts->{key} eq 'authentication_results_authserv_id') {
33 my $authserv_id = quotemeta( $opts->{value} );
34 $self->{'authserv-id'} = $authserv_id;
35 return 1;
36 }
37 return 0;
38 }
39  
40 sub _get_authentication_results_objects {
41 my ( $self, $per_msg_status ) = @_;
42 my @objects;
43  
44 if ( exists ( $per_msg_status->{'_authmilter_authentication_results_objects'} ) ) {
45 return $per_msg_status->{'_authmilter_authentication_results_objects'};
46 }
47  
48 my $group = Mail::AuthenticationResults::Header::Group->new();
49  
50 my $headers = $per_msg_status->get('Authentication-Results');
51 chomp $headers;
52 foreach my $header ( split "\n", $headers ) {
53  
54 my $parsed = eval{ Mail::AuthenticationResults::Parser->new()->parse( $header ) };
55 next if ! $parsed;
56 $group->add_child( $parsed );
57 }
58 $per_msg_status->{'_authmilter_authentication_results_objects'} = $group;
59  
60 return $per_msg_status->{'_authmilter_authentication_results_objects'};
61 }
62  
63 sub _get_authentication_results_objects_for_authserv_id {
64 my ( $self, $per_msg_status ) = @_;
65 my $header_objects = $self->_get_authentication_results_objects($per_msg_status);
66 my $authserv_id = $self->{'authserv-id'};
67 return $header_objects->search({ isa => 'header', authserv_id => qr{$authserv_id$} });
68 }
69  
70 sub _get_authentication_results_objects_for_key_value {
71 my ( $self, $per_msg_status, $key, $value ) = @_;
72 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 my ( $self, $per_msg_status, $key ) = @_;
77 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 my ( $self, $authentication_results_object, $key ) = @_;
83 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 my ( $self, $authentication_results_object, $key, $value ) = @_;
89 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 my ( $self, $per_msg_status, $key, $value ) = @_;
96 return 1 if ( scalar $self->_get_authentication_results_objects_for_key_value($per_msg_status,$key,$value)->children()->@* > 0 );
97 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 my ( $self, $per_msg_status, $spf_objects, $domain ) = @_;
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 if ( $domain =~ /\@/ ) {
109 $domain =~ s/^.*\@//;
110 }
111 my $domainregex = quotemeta( $domain );
112  
113 return 1 if scalar
114 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'smtp.mailfrom', value => qr{\@$domainregex$} }] })->children()->@*;
115 return 1 if scalar
116 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'smtp.helo', value => $domain }] })->children()->@*;
117 return 1 if scalar
118 $spf_objects->search({ isa => 'entry', key => 'spf', value => qr{^(?!fail)}, has => [{ isa => 'subentry', key => 'policy.authdomain', value => $domain }] })->children()->@*;
119  
120 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 my ( $self, $per_msg_status ) = @_;
127 my $pass = 1;
128 my $spf_objects = $self->_get_authentication_results_objects_for_key($per_msg_status,'spf');
129 foreach my $header_object ( $spf_objects->children()->@* ) {
130 next unless $header_object->value() eq 'fail';
131 if ( my $authdomain = eval{ $header_object->search({ isa => 'subentry', key => 'policy.authdomain' })->children()->[0]->value() } ) {
132 $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $authdomain );
133 }
134 elsif ( my $mailfrom = eval{ $header_object->search({ isa => 'subentry', key => 'smtp.mailfrom' })->children()->[0]->value() } ) {
135 $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $mailfrom );
136 }
137 elsif ( my $helo = eval{ $header_object->search({ isa => 'subentry', key => 'smtp.helo' })->children()->[0]->value() } ) {
138 $pass = $pass && $self->_authentication_results_spf_fail_sub( $per_msg_status, $spf_objects, $helo );
139 }
140 else {
141 $pass = 0;
142 }
143 }
144 return !$pass;
145 }
146  
147  
148 sub authentication_results_dmarc_list_override {
149 my ( $self, $per_msg_status ) = @_;
150 foreach my $header_object ( $self->_get_authentication_results_objects_for_key($per_msg_status,'dmarc')->children()->@* ) {
151 next unless $header_object->value() eq 'fail';
152 next unless $self->_entry_has_key_value( $header_object, 'policy.arc-aware-result', 'fail' );
153 next unless $self->_entry_has_key_value( $header_object, 'policy.applied-disposition', 'none' );
154 next unless $self->_entry_has_key_value( $header_object, 'policy.override-reason', 'mailing_list' );
155 next if $self->_entry_has_key_value( $header_object, 'policy.evaluated-disposition', 'none' );
156 return 1;
157 }
158 return 0;
159 }
160  
161 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