Authentication/Handler/SPF.pm

filter: Statementstmt 80.2% 211 / 263 Branchbran 63.9% 78 / 122 Conditioncond 76.1% 16 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 88.8% 16 / 18 Podpod 9.0% 1 / 11 totaltotal 73.8% 330 / 447 CC 73 SCAR 39.9
CC
73
Cov
75%
envfrom_callback
115.3
spfu_checks
27.1
header_callback
9.0
CRAP 54.1
« Authentication/Protocol/SMTP.pm ↑ Summary Authentication/Config.pm »
1 package Mail::Milter::Authentication::Handler::SPF;
2 42 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 42 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 42 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for SPF
7 42 our $VERSION = '4.20261116'; # VERSION
8 42 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use Mail::SPF;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
12 return {
13 0 'hide_received-spf_header' => 0,
14 'hide_none' => 0,
15 'best_guess' => 0,
16 'spfu_detection' => 0,
17 'helo_check' => 0,
18 'log_dns_record' => 0,
19 };
20 }
21  
22 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my ( $self ) = @_;
24 0 my @rows;
25 0 push @rows, $self->get_json( 'SPF_metrics' );
26 0 return \@rows;
27 }
28  
29 172 ▶sub setup_callback {
Subroutine setup_callback: called CC=1 SCAR=0.1
Pod undocumented
30 172 my ( $self ) = @_;
31  
32 108 ▶ $self->set_object_maker( 'spf_server' , sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
33 108 my ( $self, $name ) = @_;
34 108 my $thischild = $self->{'thischild'};
35 108 $self->dbgout( 'Object created', $name, LOG_DEBUG );
36 108 my $object;
37 108 eval {
38 108 my $resolver = $self->get_object('resolver');
39 108 $object = Mail::SPF::Server->new(
40 'hostname' => $self->get_my_hostname(),
41 'dns_resolver' => $resolver,
42 );
43 };
44 108 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 108 108
45 0 $self->handle_exception( $error );
46 0 $self->log_error( 'SPF Object Setup Error ' . $error );
47 }
48 108 $thischild->{'object'}->{$name} = {
49 'object' => $object,
50 'destroy' => 0,
51 };
52 172 });
53 }
54  
55 64 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
56 return {
57 64 'spf_total' => 'The number of emails processed for SPF',
58 };
59 }
60  
61 152 ▶sub wrap_header {
Subroutine wrap_header: called CC=1 SCAR=0.0
Pod undocumented
62 152 my ( $self, $value ) = @_;
63 152 $value =~ s/ /\n /;
64 152 $value =~ s/\) /\)\n /;
65 152 $value =~ s/; /;\n /g;
66 152 return $value;
67 }
68  
69 166 ▶sub helo_callback {
Subroutine helo_callback: called CC=1 SCAR=0.0
Pod undocumented
70 166 my ( $self, $helo_host ) = @_;
71 166 $self->{'failmode'} = 0;
72 166 $self->{'helo_name'} = $helo_host;
73 }
74  
75 166 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=35 SCAR=47.5
Pod undocumented
76  
77 # On MAILFROM
78 #...
79 166 my ( $self, $env_from ) = @_;
80 166 my $config = $self->handler_config();
81 166 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 8 158 166
82 158 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 2 156 158
83 156 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 156 156
84  
85 156 ▶ if ( $config->{'spfu_detection'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"spfu_detection"}) 8 148 156
86 8 $self->{'spfu_from_domain'} = '';
87 8 $self->{'spfu_chain'} = [];
88 }
89 156 delete $self->{'spf_header'};
90 156 delete $self->{'spf_helo_header'};
91 156 delete $self->{'spf_metric'};
92 156 delete $self->{'spf_helo_metric'};
93  
94 156 my $spf_server = $self->get_object('spf_server');
95 156 ▶ if ( ! $spf_server ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spf_server) 0 156 156
96 0 $self->log_error( 'SPF Setup Error' );
97 0 $self->{ 'spf_metric' } = 'error';
98 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'spf' )->safe_set_value( 'temperror' );
99 0 $self->{'spf_header'} = $header;
100 0 return;
101 }
102  
103 156 my $scope = 'mfrom';
104  
105 156 ▶ $env_from = q{} if $env_from eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $env_from eq "<>" 1 155 156
106  
107 156 my $identity;
108 my $domain;
109 156 ▶ if ( !$env_from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $env_from) { } 5 151 156
110 5 $identity = $self->{'helo_name'};
111 5 $domain = $identity;
112 5 $scope = 'helo';
113 }
114 else {
115 151 $identity = $self->get_address_from($env_from);
116 151 $domain = $self->get_domain_from($identity);
117 }
118  
119 156 ▶ if ( !$identity ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($identity) 6 150 156
120 6 $identity = $self->{'helo_name'};
121 6 $domain = $identity;
122 6 $scope = 'helo';
123 }
124  
125 156 eval {
126 my $spf_request = Mail::SPF::Request->new(
127 'versions' => [1],
128 'scope' => $scope,
129 'identity' => $identity,
130 'ip_address' => $self->ip_address(),
131 156 'helo_identity' => $self->{'helo_name'},
132 );
133  
134 156 my $spf_result = $spf_server->process($spf_request);
135 156 my $spf_results = $self->get_object('spf_results');
136 156 ▶ $spf_results = [] if ! $spf_results;
Branch: 1 branch
BranchTrueFalseTotal
unless $spf_results 156 0 156
137 156 push @$spf_results, $spf_result;
138 156 $self->set_object('spf_results',$spf_results,1);
139  
140 156 my $result_code = $spf_result->code();
141  
142 # Best Guess SPF based on org domain
143 # ToDo report this in both metrics and AR header
144 156 my $auth_domain;
145 156 ▶ if ( $result_code eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_code eq "none") 41 115 156
146 41 ▶ if ( $config->{'best_guess'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"best_guess"}) 0 41 41
147 0 ▶ if ( $self->is_handler_loaded( 'DMARC' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("DMARC")) 0 0 0
148 0 my $dmarc_handler = $self->get_handler('DMARC');
149 0 my $dmarc_object = $dmarc_handler->get_dmarc_object();
150 0 ▶ if ( $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain) 0 0 0
151 0 my $org_domain = eval{ $dmarc_object->get_organizational_domain( $domain ); };
152 0 $self->handle_exception( $@ );
153 0 ▶ if ( $org_domain ne $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_domain ne $domain) 0 0 0
154 0 $auth_domain = $org_domain;
155 $spf_request = Mail::SPF::Request->new(
156 'versions' => [1],
157 'scope' => $scope,
158 'identity' => $identity,
159 'authority_domain' => $org_domain,
160 'ip_address' => $self->ip_address(),
161 0 'helo_identity' => $self->{'helo_name'},
162 );
163 0 $spf_result = $spf_server->process($spf_request);
164 0 my $spf_results = $self->get_object('spf_results');
165 0 ▶ $spf_results = [] if ! $spf_results;
Branch: 1 branch
BranchTrueFalseTotal
unless $spf_results 0 0 0
166 0 push @$spf_results, $spf_result;
167 0 $self->set_object('spf_results',$spf_results,1);
168 0 $result_code = $spf_result->code();
169 }
170 }
171 }
172 }
173 }
174  
175 156 $self->{ 'spf_metric' } = $result_code;
176  
177 156 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'spf' )->safe_set_value( $result_code );
178 156 ▶ if ( $auth_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_domain) 0 156 156
179 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'policy.authdomain' )->safe_set_value( $auth_domain ) );
180 }
181 156 ▶ if ( $config->{'helo_check'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'helo_check'}) { } 0 156 156
182 # Add single property for this scope
183 0 ▶ if ( $scope eq 'mfrom' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope eq 'mfrom') { } 0 0 0
184 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.mailfrom' )->safe_set_value( $self->get_address_from( $env_from ) ) );
185 }
186 else {
187 0 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.helo' )->safe_set_value( $self->{ 'helo_name' } ) );
188 }
189 } else {
190 # Add properties for both scopes
191 156 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.mailfrom' )->safe_set_value( $self->get_address_from( $env_from ) ) );
192 156 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.helo' )->safe_set_value( $self->{ 'helo_name' } ) );
193 }
194 156 ▶ if ( $config->{'log_dns_record'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"log_dns_record"}) 2 154 156
195 2 eval {
196 2 my $resolver = $self->get_object('resolver');
197 2 my $reply = $resolver->query( $domain, 'TXT' );
198 2 ▶ if ( $reply ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply) 2 0 2
199 2 for my $rr ( $reply->answer ) {
200 2 ▶ next unless $rr->type eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "TXT" 0 2 2
201 2 my $txt = join( '', $rr->txtdata );
202 2 ▶ if ( $txt =~ /^v=spf1/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /^v=spf1/iu) 2 0 2
203 2 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'x-dns-record=' . $self->escape_non_printable_ascii( $txt ) ) );
204 2 last;
205 }
206 }
207 }
208 };
209 2 $self->handle_exception( $@ );
210 }
211 156 ▶ if ( !( $config->{'hide_none'} && $result_code eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $result_code eq "none") 152 4 156
Condition100%3/3
$config->{"hide_none"} and $result_code eq "none"
!ll&&!rl&&r
142104
MC/DC100%2/2
$config->{"hide_none"} and $result_code eq "none"
$config->{"hide_none"} $result_code eq "none"
Truth table100%3/3
A and B
A= $config->{"hide_none"}B= $result_code eq "none"
ABresult
0X0
100
111
212 152 $self->{'spf_header'} = $header;
213 }
214  
215 # Set for DMARC
216 # Note, these are the SPF results to be processed
217 # BY the DMARC handler, they are not DMARC results.
218 156 $self->{'dmarc_domain'} = $domain;
219 156 $self->{'dmarc_scope'} = $scope;
220 156 $self->{'dmarc_result'} = $result_code;
221 156 $self->{'dmarc_spfu_downgrade'} = 0;
222  
223 156 $self->dbgout( 'SPFCode', $result_code, LOG_DEBUG );
224  
225 156 ▶ if ( !( $config->{'hide_received-spf_header'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_received-spf_header"}) 156 0 156
226 156 ▶ if ( !( $config->{'hide_none'} && $result_code eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $result_code eq "none") 152 4 156
Condition100%3/3
$config->{"hide_none"} and $result_code eq "none"
!ll&&!rl&&r
142104
MC/DC100%2/2
$config->{"hide_none"} and $result_code eq "none"
$config->{"hide_none"} $result_code eq "none"
Truth table100%3/3
A and B
A= $config->{"hide_none"}B= $result_code eq "none"
ABresult
0X0
100
111
227 152 my $result_header = $spf_result->received_spf_header();
228 152 my ( $header, $value ) = split( ': ', $result_header, 2 );
229 152 $value = $self->wrap_header( $value );
230 152 $self->prepend_header( $header, $value );
231 152 $self->dbgout( 'SPFHeader', $result_header, LOG_DEBUG );
232 }
233 }
234  
235 # Separate HELO SPF check (only when the primary check was mfrom)
236 156 ▶ if ( $config->{'helo_check'} && $scope eq 'mfrom' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"helo_check"} and $scope eq "mfrom") 0 156 156
Condition33%1/3
$config->{"helo_check"} and $scope eq "mfrom"
!ll&&!rl&&r
15600
MC/DC0%0/2
$config->{"helo_check"} and $scope eq "mfrom"
$config->{"helo_check"} $scope eq "mfrom"
Truth table33%1/3
A and B
A= $config->{"helo_check"}B= $scope eq "mfrom"
ABresult
0X0
100
111
237 0 eval {
238 my $helo_spf_request = Mail::SPF::Request->new(
239 'versions' => [1],
240 'scope' => 'helo',
241 'identity' => $self->{'helo_name'},
242 'ip_address' => $self->ip_address(),
243 0 'helo_identity' => $self->{'helo_name'},
244 );
245 0 my $helo_spf_result = $spf_server->process($helo_spf_request);
246 0 my $helo_result_code = $helo_spf_result->code();
247 0 $self->{'spf_helo_metric'} = $helo_result_code;
248 0 my $helo_header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'spf' )->safe_set_value( $helo_result_code );
249 0 $helo_header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.helo' )->safe_set_value( $self->{'helo_name'} ) );
250 0 ▶ if ( !( $config->{'hide_none'} && $helo_result_code eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $helo_result_code eq "none") 0 0 0
Condition0%0/3
$config->{"hide_none"} and $helo_result_code eq "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"hide_none"} and $helo_result_code eq "none"
$config->{"hide_none"} $helo_result_code eq "none"
Truth table0%0/3
A and B
A= $config->{"hide_none"}B= $helo_result_code eq "none"
ABresult
0X0
100
111
251 0 $self->{'spf_helo_header'} = $helo_header;
252 }
253 0 $self->dbgout( 'SPFHeloCode', $helo_result_code, LOG_DEBUG );
254 };
255 0 ▶ if ( my $helo_error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $helo_error = $@) 0 0 0
256 0 $self->handle_exception( $helo_error );
257 0 $self->log_error( 'SPF HELO Error ' . $helo_error );
258 }
259 }
260 };
261 156 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 156 156
262 0 $self->handle_exception( $error );
263 0 $self->log_error( 'SPF Error ' . $error );
264 0 $self->{'failmode'} = 1;
265 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'spf' )->safe_set_value( 'temperror' );
266 0 $self->{'spf_header'} = $header;
267 0 $self->{'spf_metric'} = 'error';
268 }
269 }
270  
271 1359 ▶sub header_callback {
Subroutine header_callback: called CC=9 SCAR=22.0
Pod undocumented
272 1359 my ( $self, $header, $value ) = @_;
273  
274 1359 ▶ return unless exists $self->{'spfu_chain'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"spfu_chain"} 1308 51 1359
275 51 ▶ return unless $self->{'dmarc_result'} eq 'pass'; # Did we have an SPF pass for DMARC
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dmarc_result"} eq "pass" 0 51 51
276  
277 51 my $lc_header = lc $header;
278  
279 51 ▶ if ( $lc_header eq 'from') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_header eq "from") 8 43 51
280 8 my $spfu_from_domain = lc $self->get_address_from($value);
281 8 ▶ $spfu_from_domain = $self->get_domain_from($spfu_from_domain) if $spfu_from_domain =~ /\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $spfu_from_domain =~ /\@/u 8 0 8
282 8 $self->{'spfu_from_domain'} = $spfu_from_domain;
283  
284 8 return;
285 }
286  
287 43 ▶ if ( $lc_header eq 'received-spf' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($lc_header eq "received-spf" or $lc_header eq "x-ms-exchange-authentication-results" or $lc_header eq "arc-authentication-results" or $lc_header =~ /authentication-results$/u) 18 25 43
Condition100%9/9
$lc_header eq "received-spf" or $lc_header eq "x-ms-exchange-authentication-results" or $lc_header eq "arc-authentication-results" or $lc_header =~ /authentication-results$/u
l!l&&r!l&&!r
12625
$lc_header eq "received-spf" or $lc_header eq "x-ms-exchange-authentication-results" or $lc_header eq "arc-authentication-results"
l!l&&r!l&&!r
8431
$lc_header eq "received-spf" or $lc_header eq "x-ms-exchange-authentication-results"
l!l&&r!l&&!r
4435
MC/DC100%4/4
$lc_header eq "received-spf" or $lc_header eq "x-ms-exchange-authentication-results" or $lc_header eq "arc-authentication-results" or $lc_header =~ /authentication-results$/u
$lc_header eq "received-spf" $lc_header eq "x-ms-exchange-authentication-results" $lc_header eq "arc-authentication-results" $lc_header =~ /authentication-results$/u
Truth table100%5/5
A or B or C or D
A= $lc_header eq "received-spf"B= $lc_header eq "x-ms-exchange-authentication-results"C= $lc_header eq "arc-authentication-results"D= $lc_header =~ /authentication-results$/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
288 $lc_header eq 'x-ms-exchange-authentication-results' ||
289 $lc_header eq 'arc-authentication-results' ||
290 $lc_header =~ 'authentication-results$'
291 ) {
292 18 push $self->{'spfu_chain'}->@*, { header => $header, value => $value };
293 }
294 }
295  
296 166 ▶sub eoh_callback {
Subroutine eoh_callback: called CC=5 SCAR=16.3
Pod undocumented
297 166 my ($self) = @_;
298 166 ▶ if ( $self->{'spf_header'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spf_header"}) 152 14 166
299 152 eval {
300 152 $self->spfu_checks();
301 };
302 152 $self->handle_exception( $@ );
303 152 $self->add_auth_header($self->{'spf_header'});
304 }
305 166 ▶ if ( $self->{'spf_helo_header'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spf_helo_header"}) 0 166 166
306 0 $self->add_auth_header($self->{'spf_helo_header'});
307 }
308 166 ▶ $self->metric_count( 'spf_total', { 'result' => $self->{'spf_metric'} } ) if $self->{'spf_metric'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"spf_metric"} 156 10 166
309 166 ▶ $self->metric_count( 'spf_total', { 'result' => $self->{'spf_helo_metric'} } ) if $self->{'spf_helo_metric'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"spf_helo_metric"} 0 166 166
310 }
311  
312 152 ▶sub spfu_checks {
Subroutine spfu_checks: called CC=26 SCAR=33.0
Pod undocumented
313 152 my ($self) = @_;
314  
315 152 ▶ return unless exists $self->{'spfu_chain'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"spfu_chain"} 144 8 152
316 8 ▶ return unless exists $self->{'spfu_from_domain'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"spfu_from_domain"} 0 8 8
317 8 ▶ return unless $self->{'dmarc_result'} eq 'pass'; # Did we have an SPF pass for DMARC
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dmarc_result"} eq "pass" 0 8 8
318 8 my $dmarc_object;
319 8 ▶ if ( $self->is_handler_loaded( 'DMARC' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded("DMARC")) 8 0 8
320 8 my $dmarc_handler = $self->get_handler('DMARC');
321 8 $dmarc_object = $dmarc_handler->get_dmarc_object();
322 }
323  
324 8 my $spfu_from_domain = $self->{'spfu_from_domain'};
325 8 my $dmarc_domain = $self->{'dmarc_domain'};
326 8 ▶ if ($dmarc_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dmarc_object) 8 0 8
327 # Work with org domain if possible
328 8 $dmarc_domain = $dmarc_object->get_organizational_domain( $dmarc_domain );
329 8 $spfu_from_domain = $dmarc_object->get_organizational_domain( $spfu_from_domain );
330 }
331  
332 8 ▶ return unless lc $dmarc_domain eq $spfu_from_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless lc $dmarc_domain eq $spfu_from_domain 0 8 8
333  
334 ENTRY:
335 8 for my $chain_entry ( reverse $self->{'spfu_chain'}->@* ) {
336 12 ▶ last ENTRY if $self->{'spfu_detected'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"spfu_detected"} 2 10 12
337 10 my $header = lc $chain_entry->{'header'};
338 10 my $value = $chain_entry->{'value'};
339  
340 # Check for a Received-SPF we can parse
341 10 ▶ if ( $header eq 'received-spf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header eq "received-spf") 4 6 10
342 # We can parse the domain from the comment in most cases
343 # Received-SPF: Fail (protection.outlook.com: domain of ups.com does not designate 23.26.253.8 as permitted sender) receiver=protection.outlook.com; client-ip=23.26.253.8; helo=fa83.windbound.org.uk;
344 4 my $lc_value = lc $value;
345 4 ▶ next ENTRY unless $lc_value =~ /^fail /;
Branch: 1 branch
BranchTrueFalseTotal
unless $lc_value =~ /^fail /u 0 4 4
346 4 my ($for_value) = $lc_value =~ /^.*: domain of (\S+) .*/;
347 4 ▶ next ENTRY unless $for_value;
Branch: 1 branch
BranchTrueFalseTotal
unless $for_value 0 4 4
348 4 my $failed_domain = lc $for_value;
349 4 ▶ $failed_domain = $self->get_domain_from($failed_domain) if $failed_domain =~ /\@/;
Branch: 1 branch
BranchTrueFalseTotal
if $failed_domain =~ /\@/u 0 4 4
350 4 ▶ $failed_domain = $dmarc_object->get_organizational_domain( $failed_domain ) if $dmarc_object;
Branch: 1 branch
BranchTrueFalseTotal
if $dmarc_object 4 0 4
351 4 ▶ if ( $failed_domain eq $spfu_from_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed_domain eq $spfu_from_domain) 3 1 4
352 3 $self->{'spfu_detected'} = 1; # suspicious...
353 }
354  
355 4 next ENTRY;
356 }
357  
358 # Check for Authentication-Results style headers
359 # NOTE, We look for ARC-Authentication-Results but do
360 # not verify ARC here, this is used as a negative signal
361 # so forgery will not be of benefit
362 6 my $ar_object;
363 6 ▶ if ( $header eq 'x-ms-exchange-authentication-results' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($header eq 'x-ms-exchange-authentication-results') { } 2 4 6
elsif ($header eq 'arc-authentication-results') { } 2 2 4
elsif ($header =~ /authentication-results$/u) { } 2 0 2
364 # We can parse this slightly nonstandard format into an object
365 2 $ar_object = eval{ Mail::AuthenticationResults->parser()->parse( $value ) };
366 2 $self->handle_exception( $@ );
367 2 ▶ unless ( $ar_object ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ar_object) 2 0 2
368 # Try again with a synthesized authserv id (this is often missing)
369 2 $ar_object = eval{ Mail::AuthenticationResults->parser()->parse( "authserv.example.com; $value" ) };
370 2 $self->handle_exception( $@ );
371 }
372 } elsif ( $header eq 'arc-authentication-results' ) {
373 # We can parse this into an object, remove the instance
374 2 my ($null, $arc_value) = split ';', $value, 2;
375 2 $arc_value =~ s/^ +//;
376 2 $ar_object = eval{ Mail::AuthenticationResults->parser()->parse( $arc_value ) };
377 2 $self->handle_exception( $@ );
378 } elsif ( $header =~ 'authentication-results$' ) {
379 # We can parse this into an object, best effort with subtypes
380 2 $ar_object = eval{ Mail::AuthenticationResults->parser()->parse( $value ) };
381 2 $self->handle_exception( $@ );
382 }
383 6 ▶ next ENTRY unless $ar_object; # We didn't find one we could parse
Branch: 1 branch
BranchTrueFalseTotal
unless $ar_object 0 6 6
384  
385 6 eval {
386 6 my $spf_fail_entries = $ar_object->search({ 'isa' => 'entry', 'key' => 'spf', 'value' => 'fail' })->children();
387 6 for my $spf_fail_entry ($spf_fail_entries->@*) {
388 6 my $mailfrom_domain_entries = $spf_fail_entry->search({ 'isa' => 'subentry', 'key' => 'smtp.mailfrom'})->children();
389 # should be only 1, but let's iterate anyway
390 6 for my $mailfrom_domain_entry ($mailfrom_domain_entries->@*) {
391 6 my $mailfrom_domain = $mailfrom_domain_entry->value();
392 6 ▶ $mailfrom_domain = $dmarc_object->get_organizational_domain( $mailfrom_domain ) if $dmarc_object;
Branch: 1 branch
BranchTrueFalseTotal
if $dmarc_object 6 0 6
393 6 ▶ if ( lc $mailfrom_domain eq $spfu_from_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $mailfrom_domain eq $spfu_from_domain) 4 2 6
394 4 $self->{'spfu_detected'} = 1; # suspicious...
395 }
396 }
397 }
398  
399 };
400 6 $self->handle_exception( $@ );
401  
402 }
403  
404 8 ▶ if ( $self->{'spfu_detected'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"spfu_detected"}) 7 1 8
405 7 my $config = $self->handler_config();
406 7 $self->{'spf_metric'} = 'spf_upgrade';
407 7 ▶ if ( $config->{'spfu_detection'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'spfu_detection'} == 1) { } 6 1 7
408 6 $self->{'dmarc_spfu_downgrade'} = 1;
409 6 $self->{'spf_header'}->safe_set_value( 'fail' );
410 6 $self->{'spf_header'}->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'spf pass downgraded due to suspicious path' ) );
411 }
412 else {
413 1 $self->{'spf_header'}->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'warning: aligned spf fail in history' ) );
414 }
415 }
416 }
417  
418 120 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
419 120 my ( $self ) = @_;
420 120 delete $self->{'spfu_from_domain'};
421 120 delete $self->{'spfu_chain'};
422 120 delete $self->{'spfu_detected'};
423 120 delete $self->{'spf_header'};
424 120 delete $self->{'spf_helo_header'};
425 120 delete $self->{'spf_metric'};
426 120 delete $self->{'spf_helo_metric'};
427 120 delete $self->{'dmarc_domain'};
428 120 delete $self->{'dmarc_scope'};
429 120 delete $self->{'dmarc_result'};
430 120 delete $self->{'dmarc_spfu_downgrade'};
431 120 delete $self->{'failmode'};
432 120 delete $self->{'helo_name'};
433 120 $self->destroy_object('spf_results');
434 }
435  
436 42 1;
437  
438 __END__
« Authentication/Protocol/SMTP.pm ↑ Summary Authentication/Config.pm »