Authentication/Resolver.pm

filter: Statementstmt 28.2% 26 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 50.0% 8 / 16 Podpod 80.0% 4 / 5 totaltotal 21.4% 38 / 177 CC 22 SCAR 49.8
CC
22
Cov
18%
_do
306.0
_get_public_suffix_object
30.0
new
6.0
CRAP 145.4
« Authentication/App/Blocker/App/Command/add.pm ↑ Summary Authentication/Handler/DMARC.pm »
1 package Mail::Milter::Authentication::Resolver;
2 143 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 143 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 143 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 143 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: DNS Recolver methods
7 143 our $VERSION = '4.20261116'; # VERSION
8 143 ▶use base 'Net::DNS::Resolver';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 143 ▶use Domain::PublicSuffix;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 143 ▶use Scalar::Util qw{ weaken };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 143 ▶use Time::HiRes qw{ ualarm gettimeofday };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 {
15 0 ▶ sub new { ## no critic
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
16 0 my $class = shift;
17 0 my %args = @_;
18 0 my $self = $class->SUPER::new( @_ );
19 0 weaken($args{_handler});
20 0 $self->{ _handler } = $args{_handler};
21 0 $self->{ _timedout } = {};
22 0 ▶ $self->{cache_dns_timeouts} = $args{cache_dns_timeouts} // 1;
Condition0%0/2
$args{"cache_dns_timeouts"} // 1
l!l
00
MC/DC0%0/1
$args{"cache_dns_timeouts"} // 1
$args{"cache_dns_timeouts"}
Truth table0%0/2
A
A= $args{"cache_dns_timeouts"}
Aresult
11
00
23 0 return $self;
24 }
25 }
26  
27 143 ▶sub clear_error_cache {
Subroutine clear_error_cache: not called CC=1 SCAR=2.6
Pod undocumented
28 0 my $self = shift;
29 0 $self->{ _timedout } = {};
30 }
31  
32 0 ▶sub _get_microseconds {
Subroutine _get_microseconds: not called CC=1 SCAR=6.9
33 0 my ( $self ) = @_;
34 0 my ($seconds, $microseconds) = gettimeofday;
35 0 return ( ( $seconds * 1000000 ) + $microseconds );
36 }
37  
38 0 ▶sub _get_public_suffix_object {
Subroutine _get_public_suffix_object: not called CC=5 SCAR=34.0
39 0 my ( $self ) = @_;
40 # Parsing the suffix list is expensive, build it once per resolver.
41 0 ▶ return $self->{ _public_suffix } //= do {
Condition0%0/3
$self->{"_public_suffix"} //= do { my $handler = $self->{"_handler"}; my $config = $handler ? $handler->config : {}; my $data_file = $config->{"public_suffix_list"}; "Domain::PublicSuffix"->new($data_file && -r $data_file ? {"data_file", $data_file} : ()) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_public_suffix"} //= do { my $handler = $self->{"_handler"}; my $config = $handler ? $handler->config : {}; my $data_file = $config->{"public_suffix_list"}; "Domain::PublicSuffix"->new($data_file && -r $data_file ? {"data_file", $data_file} : ()) }
$self->{"_public_suffix"} do { my $handler = $self->{"_handler"}; my $config = $handler ? $handler->config : {}; my $data_file = $config->{"public_suffix_list"}; "Domain::PublicSuffix"->new($data_file && -r $data_file ? {"data_file", $data_file} : ()) }
Truth table0%0/3
A //= B
A= $self->{"_public_suffix"}B= do { my $handler = $self->{"_handler"}; my $config = $handler ? $handler->config : {}; my $data_file = $config->{"public_suffix_list"}; "Domain::PublicSuffix"->new($data_file && -r $data_file ? {"data_file", $data_file} : ()) }
ABresult
1X1
011
000
42 0 my $handler = $self->{_handler};
43 0 ▶ my $config = $handler ? $handler->config() : {};
Branch: 1 branch
BranchTrueFalseTotal
$handler ? : 0 0 0
44 # Prefer a locally maintained suffix list when one is configured,
45 # otherwise fall back to the copy bundled with Domain::PublicSuffix.
46 0 my $data_file = $config->{'public_suffix_list'};
47 0 ▶ Domain::PublicSuffix->new(
Branch: 1 branch
BranchTrueFalseTotal
$data_file && -r $data_file ? : 0 0 0
Condition0%0/3
$data_file and -r $data_file
!ll&&!rl&&r
000
MC/DC0%0/2
$data_file and -r $data_file
$data_file -r $data_file
Truth table0%0/3
A and B
A= $data_fileB= -r $data_file
ABresult
0X0
100
111
48 $data_file && -r $data_file ? { data_file => $data_file } : ()
49 );
50 };
51 }
52  
53 0 ▶sub _do { ## no critic
Subroutine _do: not called CC=17 SCAR=57.2
54 0 my $self = shift;
55 0 my $what = shift;
56  
57 0 my $handler = $self->{_handler};
58 0 my $config = $handler->config();
59 0 my $timeout = $config->{'dns_timeout'};
60  
61 0 my $return;
62 0 my $domain = $_[0];
63 0 my $org_domain = $_[0];
64 0 my $query = $_[1];
65 # The timeout cache is keyed on the organizational domain so that one
66 # timeout suppresses lookups for every name beneath it. Resolve that from
67 # the public suffix list rather than via Mail::DMARC: 2.x resolves the
68 # organizational domain with a DNS tree walk (RFC 9989), and since this
69 # method wraps every DNS query, that call recursed into itself without
70 # bound. A public suffix lookup is pure computation and cannot recurse.
71 0 $org_domain = eval{ $self->_get_public_suffix_object->get_root_domain( $domain ) };
72 0 $handler->handle_exception( $@ );
73 # get_root_domain returns undef for bare TLDs and unlisted suffixes; fall
74 # back to the queried domain so the cache below is not keyed on undef.
75 0 ▶ $org_domain //= $domain;
Condition0%0/3
$org_domain //= $domain
l!l&&r!l&&!r
000
MC/DC0%0/2
$org_domain //= $domain
$org_domain $domain
Truth table0%0/3
A //= B
A= $org_domainB= $domain
ABresult
1X1
011
000
76  
77 # If we have a 'cached' timeout for this org domain then return
78 0 ▶ if ( $self->{ _timedout }->{ $org_domain } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_timedout"}{$org_domain}) 0 0 0
79 0 $handler->log_error( "Lookup $query $domain aborted due to previous DNS Lookup timeout on $org_domain" );
80 0 $self->errorstring('query timed out');
81 0 return;
82 }
83  
84 0 my $start_time = $self->_get_microseconds;
85  
86 0 eval {
87 0 $handler->set_handler_alarm( ( $timeout + 0.2 ) * 1000000 ); # 0.2 seconds over that passed to Net::DNS::Resolver
88 0 ▶ $return = $self->SUPER::send( @_ ) if $what eq 'send';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "send" 0 0 0
89 0 ▶ $return = $self->SUPER::query( @_ ) if $what eq 'query';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "query" 0 0 0
90 0 ▶ $return = $self->SUPER::search( @_ ) if $what eq 'search';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq "search" 0 0 0
91 0 $handler->reset_alarm();
92 };
93  
94 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
95 0 $handler->reset_alarm();
96 0 my $type = $handler->is_exception_type( $error );
97 0 ▶ if ( $type && $type eq 'Timeout' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $type eq "Timeout") 0 0 0
Condition0%0/3
$type and $type eq "Timeout"
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type eq "Timeout"
$type $type eq "Timeout"
Truth table0%0/3
A and B
A= $typeB= $type eq "Timeout"
ABresult
0X0
100
111
98 # We have a timeout, is it global or is it ours?
99 0 ▶ if ( $handler->get_time_remaining() > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->get_time_remaining > 0) 0 0 0
100 # We have time left, but the lookup timed out
101 # Log this and move on!
102 0 ▶ if ($self->{cache_dns_timeouts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_dns_timeouts"}) 0 0 0
103 0 $handler->log_error( "DNS Lookup $query $domain error, hold set on $org_domain : Timeout calling Net::DNS::Resolver" );
104 0 $self->{ _timedout }->{ $org_domain } = 1;
105 }
106 0 $self->errorstring('query timed out');
107 0 return;
108 }
109 }
110 0 $handler->handle_exception( $error );
111 }
112  
113 0 my $time_taken = $self->_get_microseconds - $start_time;
114 0 ▶ my $servfail_timeout = exists $config->{'dns_servfail_timeout'} ? $config->{'dns_servfail_timeout'} : 1000000; # Consider a servfail as a timeout after (default) 1 second;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'dns_servfail_timeout'} ? : 0 0 0
115  
116 # Timeouts or SERVFAIL are unlikely to recover within the lifetime of this transaction,
117 # when we encounter them, don't lookup this org domain again.
118 0 ▶ if ( $self->{cache_dns_timeouts} && (( $self->errorstring =~ /timeout/i ) || ( $self->errorstring eq 'query timed out' ) || ( $self->errorstring eq 'SERVFAIL' && $time_taken > $servfail_timeout )) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_dns_timeouts"} and $self->errorstring =~ /timeout/iu || $self->errorstring eq "query timed out" || $self->errorstring eq "SERVFAIL" && $time_taken > $servfail_timeout) 0 0 0
Condition0%0/12
$self->{"cache_dns_timeouts"} and $self->errorstring =~ /timeout/iu || $self->errorstring eq "query timed out" || $self->errorstring eq "SERVFAIL" && $time_taken > $servfail_timeout
!ll&&!rl&&r
000
$self->errorstring =~ /timeout/iu or $self->errorstring eq "query timed out" or $self->errorstring eq "SERVFAIL" and $time_taken > $servfail_timeout
l!l&&r!l&&!r
000
$self->errorstring =~ /timeout/iu or $self->errorstring eq "query timed out"
l!l&&r!l&&!r
000
$self->errorstring eq "SERVFAIL" and $time_taken > $servfail_timeout
!ll&&!rl&&r
000
MC/DC0%0/5
$self->{"cache_dns_timeouts"} and $self->errorstring =~ /timeout/iu || $self->errorstring eq "query timed out" || $self->errorstring eq "SERVFAIL" && $time_taken > $servfail_timeout
$self->{"cache_dns_timeouts"} $self->errorstring =~ /timeout/iu $self->errorstring eq "query timed out" $self->errorstring eq "SERVFAIL" $time_taken > $servfail_timeout
Truth table0%0/6
A and B or C or D and E
A= $self->{"cache_dns_timeouts"}B= $self->errorstring =~ /timeout/iuC= $self->errorstring eq "query timed out"D= $self->errorstring eq "SERVFAIL"E= $time_taken > $servfail_timeout
ABCDEresult
0XXXX0
1000X0
100100
11XXX1
101XX1
100111
119 0 $self->{ _timedout }->{ $org_domain } = 1;
120 0 $handler->log_error( "DNS Lookup $query $domain error, hold set on $org_domain : ".$self->errorstring );
121 }
122  
123 0 return $return;
124 }
125  
126 0 ▶sub query { ## no critic
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self = shift;
128 0 return $self->_do( 'query', @_ );
129 }
130  
131 0 ▶sub search { ## no critic
Subroutine search: not called CC=1 SCAR=6.9
Pod documented
132 0 my $self = shift;
133 0 return $self->_do( 'search', @_ );
134 }
135  
136 0 ▶sub send { ## no critic
Subroutine send: not called CC=1 SCAR=6.9
Pod documented
137 0 my $self = shift;
138 0 return $self->_do( 'send', @_ );
139 }
140  
141 143 1;
142  
143 __END__
« Authentication/App/Blocker/App/Command/add.pm ↑ Summary Authentication/Handler/DMARC.pm »