Authentication/Handler/XGoogleDKIM.pm

filter: Statementstmt 72.6% 146 / 201 Branchbran 56.6% 34 / 60 Conditioncond 34.4% 10 / 29 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 83.3% 15 / 18 Podpod 11.1% 1 / 9 totaltotal 62.8% 210 / 334 CC 42 SCAR 29.5
CC
42
Cov
66%
_check_error
56.0
eom_callback
30.5
header_callback
10.2
CRAP 19.0
« Authentication/Handler/IPRev.pm ↑ Summary Authentication/Handler/ARC.pm »
1 package Mail::Milter::Authentication::Handler::XGoogleDKIM;
2 16 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for Google specific DKIM
7 16 our $VERSION = '4.20261116'; # VERSION
8 16 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Mail::DKIM 1.20200824;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Mail::DKIM::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Mail::DKIM::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
14 return {
15 0 'hide_none' => 0,
16 'early_lookup_limit' => 10,
17 };
18 }
19  
20 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
21 0 my ( $self ) = @_;
22 0 my @rows;
23 0 push @rows, $self->get_json( 'XGoogleDKIM_metrics' );
24 0 return \@rows;
25 }
26  
27 16 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
28 return {
29 16 'xgoogledkim_total' => 'The number of emails processed for X-Google-DKIM',
30 };
31 }
32  
33 67 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=1 SCAR=0.0
Pod undocumented
34 67 my ( $self, $env_from ) = @_;
35 67 $self->{'failmode'} = 0;
36 67 $self->{'headers'} = [];
37 67 $self->{'has_dkim'} = 0;
38 67 $self->{'early_lookups'} = {};
39 67 $self->{'carry'} = q{};
40 67 $self->destroy_object('xgdkim');
41 }
42  
43 831 ▶sub header_callback {
Subroutine header_callback: called CC=10 SCAR=23.2
Pod undocumented
44 831 my ( $self, $header, $value, $original ) = @_;
45 831 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 831 831
46 831 my $EOL = "\015\012";
47 831 my $dkim_chunk = $original . $EOL;
48 831 $dkim_chunk =~ s/\015?\012/$EOL/g;
49  
50 831 ▶ if ( lc($header) eq 'dkim-signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "dkim-signature") 41 790 831
51 41 $dkim_chunk = 'X-Orig-' . $dkim_chunk;
52 }
53 831 ▶ if ( lc($header) eq 'domainkey-signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq "domainkey-signature") 0 831 831
54 0 $dkim_chunk = 'X-Orig-' . $dkim_chunk;
55 }
56 831 push @{$self->{'headers'}} , $dkim_chunk;
57  
58 # Add Google signatures to the mix.
59 # Is this wise?
60 831 ▶ if ( $header eq 'X-Google-DKIM-Signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header eq "X-Google-DKIM-Signature") 66 765 831
61 66 my $x_dkim_chunk = 'DKIM-Signature: ' . $value . $EOL;
62 66 $x_dkim_chunk =~ s/\015?\012/$EOL/g;
63 66 push @{$self->{'headers'}} , $x_dkim_chunk;
64 66 $self->{'has_dkim'} = 1;
65 66 my ($domain) = $value =~ /d=([^;]*);/;
66 66 my ($selector) = $value =~ /s=([^;]*);/;
67 66 my $resolver = $self->get_object('resolver');
68 66 ▶ if ( defined $selector && defined $domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector and defined $domain) 66 0 66
Condition33%1/3
defined $selector and defined $domain
!ll&&!rl&&r
0066
MC/DC0%0/2
defined $selector and defined $domain
defined $selector defined $domain
Truth table33%1/3
A and B
A= defined $selectorB= defined $domain
ABresult
0X0
100
111
69 66 my $config = $self->handler_config();
70 66 ▶ my $lookup_limit = $config->{'early_lookup_limit'} // 10;
Condition100%2/2
$config->{"early_lookup_limit"} // 10
l!l
858
MC/DC100%1/1
$config->{"early_lookup_limit"} // 10
$config->{"early_lookup_limit"}
Truth table100%2/2
A
A= $config->{"early_lookup_limit"}
Aresult
11
00
71 66 my $lookup = $selector.'._domainkey.'.$domain;
72 66 ▶ if ( ! $self->{'early_lookups'}->{ lc $lookup } && scalar keys $self->{'early_lookups'}->%* < $lookup_limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"early_lookups"}{lc $lookup} and scalar keys %{$$self{"early_lookups"}} < $lookup_limit) 50 16 66
Condition100%3/3
not $self->{"early_lookups"}{lc $lookup} and scalar keys %{$$self{"early_lookups"}} < $lookup_limit
!ll&&!rl&&r
31350
MC/DC100%2/2
not $self->{"early_lookups"}{lc $lookup} and scalar keys %{$$self{"early_lookups"}} < $lookup_limit
not $self->{"early_lookups"}{lc $lookup} scalar keys %{$$self{"early_lookups"}} < $lookup_limit
Truth table100%3/3
not(A) and B
A= not $self->{"early_lookups"}{lc $lookup}B= scalar keys %{$$self{"early_lookups"}} < $lookup_limit
ABresult
0X0
100
111
73 50 $self->{'early_lookups'}->{ lc $lookup } = 1;
74 50 eval{ $resolver->bgsend( $lookup, 'TXT' ) };
75 50 $self->handle_exception( $@ );
76 50 $self->dbgout( 'DNSEarlyLookup', "$lookup TXT", LOG_DEBUG );
77 }
78 }
79 }
80 }
81  
82 65 ▶sub eoh_callback {
Subroutine eoh_callback: called CC=6 SCAR=19.8
Pod undocumented
83 65 my ($self) = @_;
84 65 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 65 65
85 65 my $config = $self->handler_config();
86  
87 65 ▶ if ( $self->{'has_dkim'} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'has_dkim'} == 0) { } 27 38 65
88 27 $self->metric_count( 'xgoogledkim_total', { 'result' => 'none' } );
89 27 $self->dbgout( 'XGoogleDKIMResult', 'No X-Google-DKIM headers', LOG_DEBUG );
90 27 ▶ if ( !( $config->{'hide_none'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"}) 19 8 27
91 19 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( 'none' );
92 19 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'no signatures found' ) );
93 19 $self->add_auth_header( $header );
94 }
95 27 delete $self->{'headers'};
96 }
97 else {
98  
99 38 my $dkim;
100 38 eval {
101 38 $dkim = Mail::DKIM::Verifier->new();
102 38 my $resolver = $self->get_object('resolver');
103 38 Mail::DKIM::DNS::resolver($resolver);
104 38 $self->set_object('xgdkim', $dkim, 1);
105 };
106 38 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 38 38
107 0 $self->handle_exception( $error );
108 0 $self->log_error( 'XGoogleDKIM Setup Error ' . $error );
109 0 $self->{'failmode'} = 1;
110 0 $self->_check_error( $error );
111 0 $self->metric_count( 'xgoogledkim_total', { 'result' => 'error' } );
112 0 delete $self->{'headers'};
113 0 return;
114 }
115  
116 38 eval {
117 $dkim->PRINT( join q{},
118 38 @{ $self->{'headers'} },
119 "\015\012",
120 );
121 };
122 38 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 38 38
123 0 $self->handle_exception( $error );
124 0 $self->log_error( 'XGoogleDKIM Headers Error ' . $error );
125 0 $self->{'failmode'} = 1;
126 0 $self->_check_error( $error );
127 0 $self->metric_count( 'xgoogledkim_total', { 'result' => 'error' } );
128 }
129  
130 38 delete $self->{'headers'};
131 }
132  
133 65 $self->{'carry'} = q{};
134 }
135  
136 63 ▶sub body_callback {
Subroutine body_callback: called CC=6 SCAR=21.3
Pod undocumented
137 63 my ( $self, $body_chunk ) = @_;
138 63 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 63 63
139 63 ▶ return if ( $self->{'has_dkim'} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has_dkim"} == 0 25 38 63
140 38 my $EOL = "\015\012";
141  
142 38 my $dkim_chunk;
143 38 ▶ if ( $self->{'carry'} ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'carry'} ne '') { } 0 38 38
144 0 $dkim_chunk = $self->{'carry'} . $body_chunk;
145 0 $self->{'carry'} = q{};
146 }
147 else {
148 38 $dkim_chunk = $body_chunk;
149 }
150  
151 38 ▶ if ( substr( $dkim_chunk, -1 ) eq "\015" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($dkim_chunk, -1) eq "\r") 0 38 38
152 0 $self->{'carry'} = "\015";
153 0 $dkim_chunk = substr( $dkim_chunk, 0, -1 );
154 }
155  
156 38 $dkim_chunk =~ s/\015?\012/$EOL/g;
157  
158 38 my $dkim = $self->get_object('xgdkim');
159 38 eval {
160 38 $dkim->PRINT( $dkim_chunk );
161 };
162 38 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 38 38
163 0 $self->handle_exception( $error );
164 0 $self->log_error( 'XGoogleDKIM Body Error ' . $error );
165 0 $self->{'failmode'} = 1;
166 0 $self->_check_error( $error );
167 0 $self->metric_count( 'xgoogledkim_total', { 'result' => 'error' } );
168 }
169 }
170  
171 65 ▶sub eom_callback {
Subroutine eom_callback: called CC=17 SCAR=34.2
Pod undocumented
172 65 my ($self) = @_;
173  
174 65 ▶ return if ( $self->{'has_dkim'} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"has_dkim"} == 0 27 38 65
175 38 ▶ return if ( $self->{'failmode'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 38 38
176  
177 38 my $config = $self->handler_config();
178  
179 38 my $dkim = $self->get_object('xgdkim');
180  
181 38 eval {
182 38 $dkim->PRINT( $self->{'carry'} );
183 38 $dkim->CLOSE();
184  
185 38 my $dkim_result = $dkim->result;
186 38 my $dkim_result_detail = $dkim->result_detail;
187  
188 38 $self->metric_count( 'xgoogledkim_total', { 'result' => $dkim_result } );
189  
190 38 $self->dbgout( 'XGoogleDKIMResult', $dkim_result_detail, LOG_DEBUG );
191  
192 38 ▶ if ( !$dkim->signatures() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dkim->signatures) 0 38 38
193 0 ▶ if ( !( $config->{'hide_none'} && $dkim_result eq 'none' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $dkim_result eq "none") 0 0 0
Condition0%0/3
$config->{"hide_none"} and $dkim_result eq "none"
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"hide_none"} and $dkim_result eq "none"
$config->{"hide_none"} $dkim_result eq "none"
Truth table0%0/3
A and B
A= $config->{"hide_none"}B= $dkim_result eq "none"
ABresult
0X0
100
111
194 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( $dkim_result );
195 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'no signatures found' ) );
196 0 $self->add_auth_header( $header );
197 }
198 }
199 38 foreach my $signature ( $dkim->signatures() ) {
200  
201 38 my $otype = ref $signature;
202 38 ▶ my $type =
Branch: 2 branches
BranchTrueFalseTotal
$otype eq 'Mail::DKIM::DkSignature' ? : 0 38 38
$otype eq 'Mail::DKIM::Signature' ? : 38 0 38
203 $otype eq 'Mail::DKIM::DkSignature' ? 'domainkeys'
204 : $otype eq 'Mail::DKIM::Signature' ? 'dkim'
205 : 'dkim';
206 38 $self->dbgout( 'XGoogleDKIMSignatureType', $type, LOG_DEBUG );
207  
208 38 $self->dbgout( 'XGoogleDKIMSignatureIdentity', $signature->identity, LOG_DEBUG );
209 38 $self->dbgout( 'XGoogleDKIMSignatureResult', $signature->result_detail, LOG_DEBUG );
210 38 my $signature_result = $signature->result();
211 38 my $signature_result_detail = $signature->result_detail();
212  
213 38 ▶ if ( $signature_result eq 'invalid' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature_result eq "invalid") 0 38 38
214 0 ▶ if ( $signature_result_detail =~ /DNS query timeout for (.*) at / ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature_result_detail =~ /DNS query timeout for (.*) at /u) 0 0 0
215 0 my $timeout_domain = $1;
216 0 $self->log_error( "TIMEOUT DETECTED: in XGoogleDKIM result: $timeout_domain" );
217 0 $signature_result_detail = "DNS query timeout for $timeout_domain";
218 }
219 }
220  
221 38 my $result_comment = q{};
222 38 ▶ if ( $signature_result ne 'pass' and $signature_result ne 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signature_result ne "pass" and $signature_result ne "none") 8 30 38
Condition66%2/3
$signature_result ne "pass" and $signature_result ne "none"
!ll&&!rl&&r
3008
MC/DC50%1/2
$signature_result ne "pass" and $signature_result ne "none"
$signature_result ne "pass" $signature_result ne "none"
Truth table66%2/3
A and B
A= $signature_result ne "pass"B= $signature_result ne "none"
ABresult
0X0
100
111
223 8 $signature_result_detail =~ /$signature_result \((.*)\)/;
224 8 ▶ if ( $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 8 0 8
225 8 $result_comment = $1 . ', ';
226 }
227 }
228 38 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"hide_none"} and $signature_result eq "none") 38 0 38
Condition66%2/3
$config->{"hide_none"} and $signature_result eq "none"
!ll&&!rl&&r
3260
MC/DC0%0/2
$config->{"hide_none"} and $signature_result eq "none"
$config->{"hide_none"} $signature_result eq "none"
Truth table66%2/3
A and B
A= $config->{"hide_none"}B= $signature_result eq "none"
ABresult
0X0
100
111
229 !(
230 $config->{'hide_none'} && $signature_result eq 'none'
231 )
232 )
233 {
234  
235 38 my $key_data = q{};
236 38 eval {
237 38 my $key = $signature->get_public_key();
238 38 $key_data = $key->size() . '-bit ' . $key->type() . ' key';
239 };
240  
241 38 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( $signature_result );
242 38 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $result_comment . $key_data ) );
243 38 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.d' )->safe_set_value( $signature->domain() ) );
244 38 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.i' )->safe_set_value( $signature->identity() ) );
245 38 $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'header.b' )->safe_set_value( substr( $signature->data(), 0, 8 ) ) );
246 38 $self->add_auth_header($header);
247 }
248 }
249  
250 };
251 38 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 38 38
252 0 $self->handle_exception( $error );
253 # Also in DMARC module
254 0 $self->log_error( 'XGoogleDKIM EOM Error ' . $error );
255 0 $self->{'failmode'} = 1;
256 0 $self->_check_error( $error );
257 0 $self->metric_count( 'xgoogledkim_total', { 'result' => 'error' } );
258 0 return;
259 }
260 }
261  
262 105 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
263 105 my ( $self ) = @_;
264 105 delete $self->{'failmode'};
265 105 delete $self->{'headers'};
266 105 delete $self->{'body'};
267 105 delete $self->{'carry'};
268 105 delete $self->{'has_dkim'};
269 105 delete $self->{'early_lookups'};
270 105 $self->destroy_object('xgdkim');
271 }
272  
273 0 ▶sub _check_error {
Subroutine _check_error: not called CC=7 SCAR=40.3
274 0 my ( $self, $error ) = @_;
275 0 ▶ if ( $error =~ /^DNS error: query timed out/
Branch: 2 branches
BranchTrueFalseTotal
if ($error =~ /^DNS error: query timed out/u or $error =~ /^DNS query timeout/u) { } 0 0 0
elsif ($error =~ /^no domain to fetch policy for$/u or $error =~ /^policy syntax error$/u or $error =~ /^empty domain label/u or $error =~ /^invalid name /u) { } 0 0 0
Condition0%0/3
$error =~ /^DNS error: query timed out/u or $error =~ /^DNS query timeout/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$error =~ /^DNS error: query timed out/u or $error =~ /^DNS query timeout/u
$error =~ /^DNS error: query timed out/u $error =~ /^DNS query timeout/u
Truth table0%0/3
A or B
A= $error =~ /^DNS error: query timed out/uB= $error =~ /^DNS query timeout/u
ABresult
1X1
011
000
276 or $error =~ /^DNS query timeout/
277 ){
278 0 $self->log_error( 'Temp XGoogleDKIM Error - ' . $error );
279 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( 'temperror' );
280 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'dns timeout' ) );
281 0 $self->add_auth_header( $header );
282 }
283 ▶ elsif ( $error =~ /^no domain to fetch policy for$/
Condition0%0/9
$error =~ /^no domain to fetch policy for$/u or $error =~ /^policy syntax error$/u or $error =~ /^empty domain label/u or $error =~ /^invalid name /u
l!l&&r!l&&!r
000
$error =~ /^no domain to fetch policy for$/u or $error =~ /^policy syntax error$/u or $error =~ /^empty domain label/u
l!l&&r!l&&!r
000
$error =~ /^no domain to fetch policy for$/u or $error =~ /^policy syntax error$/u
l!l&&r!l&&!r
000
MC/DC0%0/4
$error =~ /^no domain to fetch policy for$/u or $error =~ /^policy syntax error$/u or $error =~ /^empty domain label/u or $error =~ /^invalid name /u
$error =~ /^no domain to fetch policy for$/u $error =~ /^policy syntax error$/u $error =~ /^empty domain label/u $error =~ /^invalid name /u
Truth table0%0/5
A or B or C or D
A= $error =~ /^no domain to fetch policy for$/uB= $error =~ /^policy syntax error$/uC= $error =~ /^empty domain label/uD= $error =~ /^invalid name /u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
284 or $error =~ /^policy syntax error$/
285 or $error =~ /^empty domain label/
286 or $error =~ /^invalid name /
287 ){
288 0 $self->log_error( 'Perm XGoogleDKIM Error - ' . $error );
289 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( 'perlerror' );
290 0 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( 'syntax or domain error' ) );
291 0 $self->add_auth_header( $header );
292 }
293 else {
294 0 $self->exit_on_close( 'Unexpected XGoogleDKIM Error - ' . $error );
295 0 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-google-dkim' )->safe_set_value( 'temperror' );
296 0 $self->add_auth_header( $header );
297 # Fill these in as they occur, but for unknowns err on the side of caution
298 # and tempfail/exit
299 0 $self->tempfail_on_error();
300 }
301 }
302  
303 16 1;
304  
305 __END__
« Authentication/Handler/IPRev.pm ↑ Summary Authentication/Handler/ARC.pm »