blib/lib/Mail/Milter/Authentication/Handler/BIMI.pm

filter: Statementstmt 48.9% 96 / 196 Branchbran 28.5% 24 / 84 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 89.4% 17 / 19 Podpod 12.5% 1 / 8 totaltotal 43.8% 138 / 315 CC 48 SCAR 55.6
CC
48
Cov
42%
eom_callback
450.0
header_callback
42.2
remove_bimi_header
6.0
CRAP 259.7
↑ Summary
1 package Mail::Milter::Authentication::Handler::BIMI;
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 Mail::Milter::Authentication 2.20191221;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = '2.20200102'; # VERSION
7 # ABSTRACT: BIMI handler for authentication milter
8  
9 1 ▶use English qw{ -no_match_vars };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Clone qw{ clone };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Mail::BIMI 1.20190210;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sys::Syslog qw{:standard :macros};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Mail::AuthenticationResults::Header::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Mail::AuthenticationResults::Header::SubEntry;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Mail::AuthenticationResults::Header::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub default_config {
18 return {
19 1 ▶ };
Subroutine default_config: called CC=1 SCAR=0.0
Pod undocumented
20 }
21  
22 sub register_metrics {
23 return {
24 1 ▶ 'bimi_total' => 'The number of emails processed for BIMI',
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
25 'bimi_removed_total' => 'The number BIMI-Location headers removed',
26 };
27 }
28  
29 sub remove_bimi_header {
30 0 ▶ my ( $self, $value ) = @_;
Subroutine remove_bimi_header: not called CC=2 SCAR=17.9
Pod undocumented
31 0 $self->metric_count( 'bimi_remove_total' );
32 0 ▶ if ( !exists( $self->{'remove_bimi_headers'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"remove_bimi_headers"}) 0 0 0
33 0 $self->{'remove_bimi_headers'} = [];
34 }
35 0 push @{ $self->{'remove_bimi_headers'} }, $value;
36 0 return;
37 }
38  
39 sub envfrom_callback {
40 2 ▶ my ( $self, $env_from ) = @_;
Subroutine envfrom_callback: called CC=1 SCAR=0.0
Pod undocumented
41 2 delete $self->{'bimi_header_index'};
42 2 delete $self->{'remove_bimi_headers'};
43 2 $self->{ 'header_added' } = 0;
44 2 return;
45 }
46  
47 sub header_callback {
48 6 ▶ my ( $self, $header, $value ) = @_;
Subroutine header_callback: called CC=11 SCAR=37.4
Pod undocumented
49  
50 # Not sure where this should go in the flow, so it's going here!
51 # Which is clearly, or at least probably the wrong place.
52 6 ▶ if ( lc $header eq 'bimi-location' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "bimi-location") 0 6 6
53 0 ▶ if ( !exists $self->{'bimi_header_index'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"bimi_header_index"}) 0 0 0
54 0 $self->{'bimi_header_index'} = 0;
55 }
56 $self->{'bimi_header_index'} =
57 0 $self->{'bimi_header_index'} + 1;
58 0 $self->remove_bimi_header( $self->{'bimi_header_index'} );
59 0 my $forged_header =
60 '(Received BIMI-Location header removed by '
61 . $self->get_my_hostname()
62 . ')' . "\n"
63 . ' '
64 . $value;
65 0 $self->append_header( 'X-Received-BIMI-Location',
66 $forged_header );
67 }
68  
69 6 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 0 6 6
70 6 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 0 6 6
71 6 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 6 6
72 6 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 6 6
73  
74 6 ▶ if ( lc $header eq 'bimi-selector' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "bimi-selector") 0 6 6
75 0 ▶ if ( exists $self->{'selector'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"selector"}) 0 0 0
76 0 $self->dbgout( 'BIMIFail', 'Multiple BIMI-Selector fields', LOG_INFO );
77 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'fail' );
78 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple BIMI-Selector fields in message' ) );
79 0 $self->add_auth_header( $header );
80 0 $self->metric_count( 'bimi_total', { 'result' => 'fail', 'reason' => 'bad_selector_header' } );
81 0 $self->{ 'header_added' } = 1;
82 0 $self->{'failmode'} = 1;
83 0 return;
84 }
85 0 $self->{'selector'} = $value;
86 }
87 6 ▶ if ( lc $header eq 'from' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "from") 2 4 6
88 2 ▶ if ( exists $self->{'from_header'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"from_header"}) 0 2 2
89 0 $self->dbgout( 'BIMIFail', 'Multiple RFC5322 from fields', LOG_INFO );
90 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'fail' );
91 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple RFC5322 from fields in message' ) );
92 0 $self->add_auth_header( $header );
93 0 $self->metric_count( 'bimi_total', { 'result' => 'fail', 'reason' => 'bad_from_header' } );
94 0 $self->{ 'header_added' } = 1;
95 0 $self->{'failmode'} = 1;
96 0 return;
97 }
98 2 $self->{'from_header'} = $value;
99 }
100 ## ToDo remove/rename existing headers here
101 6 return;
102 }
103  
104 sub eom_requires {
105 1 ▶ my ($self) = @_;
Subroutine eom_requires: called CC=1 SCAR=0.0
Pod undocumented
106 1 my @requires = qw{ DMARC };
107 1 return \@requires;
108 }
109  
110 sub eom_callback {
111 2 ▶ my ($self) = @_;
Subroutine eom_callback: called CC=37 SCAR=61.1
Pod undocumented
112 2 my $config = $self->handler_config();
113  
114 # Again, not sure where this should go, so it's going here.
115 2 ▶ if ( exists( $self->{'remove_bimi_headers'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"remove_bimi_headers"}) 0 2 2
116 0 foreach my $header ( reverse @{ $self->{'remove_bimi_headers'} } ) {
117 0 $self->dbgout( 'RemoveBIMILocationHeader', $header, LOG_DEBUG );
118 0 $self->change_header( 'BIMI-Location', $header, q{} );
119 }
120 }
121  
122 2 ▶ return if ( $self->{ 'header_added' } );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"header_added"} 0 2 2
123 2 ▶ return if ( $self->is_local_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_local_ip_address 0 2 2
124 2 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 0 2 2
125 2 ▶ return if ( $self->is_authenticated() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_authenticated 0 2 2
126 2 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 2 2
127 2 eval {
128 2 my $Domain = $self->get_domain_from( $self->{'from_header'} );
129  
130 # Rework this to allow for multiple dmarc_result objects as per new DMARC handler
131 2 my $DMARCResults = $self->get_object( 'dmarc_results' );
132  
133 2 ▶ if ( ! $DMARCResults ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $DMARCResults) { } 0 2 2
134  
135 0 $self->log_error( 'BIMI Error No DMARC Results object');
136 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'temperror' );
137 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'Internal DMARC error' ) );
138 0 $self->add_auth_header( $header );
139 0 $self->{ 'header_added' } = 1;
140  
141 }
142 else {
143 2 ▶ if ( scalar @$DMARCResults != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$DMARCResults != 1) { } 0 2 2
144 0 $self->dbgout( 'BIMIFail', 'Multiple DMARC Results', LOG_INFO );
145 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'fail' );
146 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'multiple DMARC results for message' ) );
147 0 $self->add_auth_header( $header );
148 0 $self->metric_count( 'bimi_total', { 'result' => 'fail', 'reason' => 'multiple_dmarc_results' } );
149 0 $self->{ 'header_added' } = 1;
150 0 $self->{'failmode'} = 1;
151 0 return;
152 }
153 else {
154 # If Multiple DMARC results is OK then... foreach my $DMARCResult ( @$DMARCResults ) {
155 2 my $DMARCResult = clone $DMARCResults->[0]; # Clone so we can modify without breaking reporting data
156  
157 ## Consider ARC
158 # We only have 1 DMARC result so we find the auth results header that it added
159 2 my $selector_arc_pass = 0;
160 2 ▶ if ( $DMARCResult->result ne 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DMARCResult->result ne "pass") 1 1 2
161 1 my $top_handler = $self->get_top_handler();
162 1 my @auth_headers;
163 1 ▶ if ( exists( $top_handler->{'auth_headers'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $top_handler->{"auth_headers"}) 1 0 1
164 1 @auth_headers = ( @auth_headers, @{ $top_handler->{'auth_headers'} } );
165 }
166 0 ▶ if (@auth_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@auth_headers) 0 0 0
167 0 foreach my $auth_header ( @auth_headers ) {
168 0 ▶ next if $auth_header->key ne 'dmarc';
Branch: 1 branch
BranchTrueFalseTotal
if $auth_header->key ne "dmarc" 0 0 0
169 0 ▶ my $arc_aware_result = eval{ $auth_header->search({key=>'policy.arc-aware-result'})->children->[0]->value } // '';
Condition0%0/2
eval { // ""
l!l
00
MC/DC0%0/1
eval { do { $auth_header->search({"key", "policy.arc-aware-result"})->children->[0]->value } } // ""
eval { do { $auth_header->search({"key", "policy.arc-aware-result"})->children->[0]->value } }
Truth table0%0/2
A
A= eval { do { $auth_header->search({"key", "policy.arc-aware-result"})->children->[0]->value } }
Aresult
11
00
170 0 $self->handle_exception( $@ );
171 0 ▶ if ( $arc_aware_result eq 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arc_aware_result eq "pass") 0 0 0
172 0 $self->log_error( 'BIMI DMARC ARC pass detected' );
173 0 $DMARCResult->{result} = $arc_aware_result; # Feels hacky, but does the right thing
174 # Note, we can't check for signness of BIMI-Selector for arc forwarded mail where DKIM context has been lost
175 # When we have a pass by arc we skip the DKIM check for BIMI-Selector
176 0 $selector_arc_pass = 1;
177 }
178 }
179 }
180 }
181  
182 1 my $Selector = $self->{ 'selector' };
183 1 ▶ if ( !$Selector ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $Selector) { } 1 0 1
elsif ($Selector =~ /^v=BIMI1;\s+s=(\w+);?/i) { } 0 0 0
184 1 $Selector = 'default';
185 }
186 elsif ( $Selector =~ m/^v=BIMI1;\s+s=(\w+);?/i ) {
187 0 $Selector = $1;
188 0 $Selector = lc $Selector;
189 # Was the BIMI-Selector header DKIM Signed?
190 0 my $selector_was_domain_signed = 0;
191 0 my $selector_was_org_domain_signed = 0;
192 0 my $selector_was_third_party_domain_signed = 0;
193 0 my $OrgDomain = eval{ $self->get_handler('DMARC')->get_dmarc_object()->get_organizational_domain( $Domain ) };
194 0 $self->handle_exception( $@ );
195 0 ▶ if ( $self->{'selector'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"selector"}) 0 0 0
196 0 my $dkim_handler = $self->get_handler('DKIM');
197 0 ▶ if ( $dkim_handler->{'has_dkim'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_handler->{"has_dkim"}) 0 0 0
198 0 my $dkim_object = $self->get_object('dkim');
199 0 ▶ if ( $dkim_object ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_object) 0 0 0
200 0 ▶ if ( $dkim_object->signatures() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_object->signatures) 0 0 0
201 0 foreach my $signature ( $dkim_object->signatures() ) {
202 0 ▶ next if $signature->result ne 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $signature->result ne "pass" 0 0 0
203 0 my @signed_headers = $signature->headerlist;
204 0 ▶ next if ! grep { lc $_ eq 'bimi-selector' } @signed_headers;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {lc $_ eq "bimi-selector";} @signed_headers 0 0 0
205 0 my $signature_domain = $signature->domain;
206 0 ▶ if ( lc $signature_domain eq lc $Domain ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $signature_domain eq lc $Domain) { } 0 0 0
elsif (lc $signature_domain eq lc $OrgDomain) { } 0 0 0
207 0 $selector_was_domain_signed = 1;
208 }
209 elsif ( lc $signature_domain eq lc $OrgDomain ) {
210 0 $selector_was_org_domain_signed = 1;
211 }
212 else {
213 0 $selector_was_third_party_domain_signed = 1;
214 }
215 }
216 }
217 }
218 }
219 }
220 0 ▶ my $Alignment = $selector_was_domain_signed ? 'domain'
Branch: 4 branches
BranchTrueFalseTotal
$selector_was_domain_signed ? : 0 0 0
$selector_was_org_domain_signed ? : 0 0 0
$selector_arc_pass ? : 0 0 0
$selector_was_third_party_domain_signed ? : 0 0 0
221 : $selector_was_org_domain_signed ? 'orgdomain'
222 : $selector_arc_pass ? 'arc'
223 : $selector_was_third_party_domain_signed ? 'thirdparty'
224 : 'unsigned';
225 0 ▶ if ( $Alignment eq 'unsigned' || $Alignment eq 'thirdparty' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Alignment eq "unsigned" or $Alignment eq "thirdparty") 0 0 0
Condition0%0/3
$Alignment eq "unsigned" or $Alignment eq "thirdparty"
l!l&&r!l&&!r
000
MC/DC0%0/2
$Alignment eq "unsigned" or $Alignment eq "thirdparty"
$Alignment eq "unsigned" $Alignment eq "thirdparty"
Truth table0%0/3
A or B
A= $Alignment eq "unsigned"B= $Alignment eq "thirdparty"
ABresult
1X1
011
000
226 0 $self->log_error( 'BIMI Header DKIM '.$Alignment.' for Selector '.$Selector.' - ignoring' );
227 0 $Selector = 'default';
228 }
229  
230 }
231 else {
232 0 $self->log_error( 'BIMI Invalid Selector Header: ' . $Selector );
233 0 $Selector = 'default';
234 }
235  
236 1 my $BIMI = Mail::BIMI->new(
237 resolver => $self->get_object( 'resolver' ),
238 dmarc_object => $DMARCResult,
239 domain => $Domain,
240 selector => $Selector,
241 );
242 1 $self->{'bimi_object'} = $BIMI; # For testing!
243  
244 1 my $Result = $BIMI->result();
245 1 my $AuthResults = $Result->get_authentication_results_object();
246 1 $self->add_auth_header( $AuthResults );
247 1 $self->{ 'header_added' } = 1;
248 1 my $Record = $BIMI->record();
249 1 my $URLList = $Record->locations->location;
250 0 ▶ if ( $Result->result() eq 'pass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Result->result eq "pass") 0 0 0
251 0 $self->prepend_header( 'BIMI-Location', join( "\n",
252 'v=BIMI1;',
253 ' l=' . join( ',', @$URLList ) ) );
254 }
255  
256 0 $self->metric_count( 'bimi_total', { 'result' => $Result->result() } );
257 }
258 }
259  
260 };
261 2 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 0 2
262 2 $self->handle_exception( $error );
263 2 $self->log_error( 'BIMI Error ' . $error );
264 2 ▶ if ( ! $self->{ 'header_added' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"header_added"}) 1 1 2
265 1 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'bimi' )->safe_set_value( 'temperror' );
266 1 $self->add_auth_header( $header );
267 1 $self->{ 'header_added' } = 1;
268 }
269 }
270  
271 2 return;
272 }
273  
274 sub close_callback {
275 0 ▶ my ( $self ) = @_;
Subroutine close_callback: not called CC=1 SCAR=6.9
Pod undocumented
276 0 delete $self->{'selector'};
277 0 delete $self->{'from_header'};
278 0 delete $self->{'failmode'};
279 0 delete $self->{'remove_bimi_headers'};
280 0 delete $self->{'bimi_object'};
281 0 delete $self->{'bimi_header_index'};
282 0 delete $self->{ 'header_added' };
283 0 return;
284 }
285  
286 1 1;
287  
288 __END__
289  
290 =head1 NAME
291  
292 Authentication Milter - BIMI Module
293  
294 =head1 DESCRIPTION
295  
296 Module implementing the BIMI standard checks.
297  
298 This handler requires the DMARC handler and its dependencies to be installed and active.
299  
300 =head1 CONFIGURATION
301  
302 "BIMI" : { | Config for the BIMI Module
303 | Requires DMARC
304 },
305  
306 =head1 SYNOPSIS
307  
308 =head1 AUTHORS
309  
310 Marc Bradshaw E<lt>marc@marcbradshaw.netE<gt>
311  
312 =head1 COPYRIGHT
313  
314 Copyright 2018
315  
316 This library is free software; you may redistribute it and/or
317 modify it under the same terms as Perl itself.
318  
↑ Summary