lib/Mail/DMARC.pm

filter: Statementstmt 98.2% 170 / 173 Branchbran 89.7% 79 / 88 Conditioncond 78.2% 18 / 23 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 27 / 27 Podpod 47.3% 9 / 19 totaltotal 90.7% 312 / 344 CC 70 SCAR 17.0
CC
70
Cov
94%
dkim_for_report
10.5
get_report_window
10.0
dkim
7.0
CRAP 5.5
« lib/Mail/DMARC/Policy.pm ↑ Summary lib/Mail/DMARC/Report/Store/SQL/Grammars/MySQL.pm »
1 package Mail::DMARC;
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶no warnings 'experimental::args_array_with_signatures'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '2.20261110';
8  
9 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 our $psl_loads = 0;
11  
12 11 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 require Mail::DMARC::Policy;
14 11 require Mail::DMARC::Result;
15 11 require Mail::DMARC::Report::Aggregate::Record;
16 11 require Mail::DMARC::Report::Aggregate::Record::Auth_Results::SPF;
17 11 require Mail::DMARC::Report::Aggregate::Record::Auth_Results::DKIM;
18  
19 87 ▶sub new( $class, @args ) {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
20 87 ▶ croak "invalid args" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 0 87 87
21 87 my %args = @args;
22 87 my $self = bless { config_file => 'mail-dmarc.ini', }, $class;
23  
24 my @keys = sort {
25 87 ▶ $a eq 'config_file' ? -1
Branch: 2 branches
BranchTrueFalseTotal
$a eq 'config_file' ? : 43 344 387
$b eq 'config_file' ? : 44 300 344
26 : $b eq 'config_file' ? 1
27 : ( $a cmp $b )
28 } keys %args;
29  
30 87 foreach my $key (@keys) {
31 212 ▶ if ( $self->can($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($key)) { } 181 31 212
32 181 $self->$key( $args{$key} );
33 }
34 else {
35 31 $self->{$key} = $args{$key};
36 }
37 }
38 87 return $self;
39 }
40  
41 99 ▶sub source_ip( $self, $val = undef ) {
Subroutine source_ip: called CC=4 SCAR=13.9
Pod documented
42 99 ▶ return $self->{source_ip} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 47 52 99
43 52 ▶ croak "invalid source_ip" if !$self->is_valid_ip($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_ip($val) 3 49 52
44 49 return $self->{source_ip} = $val;
45 }
46  
47 58 ▶sub envelope_to( $self, $val = undef ) {
Subroutine envelope_to: called CC=4 SCAR=13.9
Pod documented
48 58 ▶ return $self->{envelope_to} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 12 46 58
49 46 ▶ croak "invalid envelope_to" if !$self->is_valid_domain( lc $val );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_domain(lc $val) 1 45 46
50 45 return $self->{envelope_to} = $val;
51 }
52  
53 56 ▶sub envelope_from( $self, $val = undef ) {
Subroutine envelope_from: called CC=4 SCAR=13.9
Pod documented
54 56 ▶ return $self->{envelope_from} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 12 44 56
55 44 ▶ croak "invalid envelope_from" if !$self->is_valid_domain( lc $val );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_domain(lc $val) 0 44 44
56 44 return $self->{envelope_from} = $val;
57 }
58  
59 451 ▶sub header_from( $self, $val = undef ) {
Subroutine header_from: called CC=4 SCAR=13.9
Pod documented
60 451 ▶ return $self->{header_from} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 353 98 451
61 98 $val = $self->normalize_domain($val);
62 98 ▶ croak "invalid header_from" if !$self->is_valid_domain($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_domain($val) 3 95 98
63 95 return $self->{header_from} = $val;
64 }
65  
66 39 ▶sub header_from_raw( $self, $val = undef ) {
Subroutine header_from_raw: called CC=3 SCAR=11.0
Pod documented
67 39 ▶ return $self->{header_from_raw} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 20 19 39
68  
69 #croak "invalid header_from_raw: $val" if 'from:' ne lc substr($val, 0, 5);
70 19 return $self->{header_from_raw} = lc $val;
71 }
72  
73 1 ▶sub sender( $self, $val = undef ) {
Subroutine sender: called CC=4 SCAR=14.5
Pod undocumented
74 1 ▶ return $self->{sender} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 1 1
75 1 ▶ croak "invalid sender" if !$self->is_valid_domain( lc $val );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_domain(lc $val) 0 1 1
76 1 return $self->{sender} = lc $val;
77 }
78  
79 4 ▶sub local_policy( $self, $val = undef ) {
Subroutine local_policy: called CC=3 SCAR=11.0
Pod undocumented
80 4 ▶ return $self->{local_policy} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 2 4
81  
82 # TODO: document this, when and why it would be used
83 2 return $self->{local_policy} = $val;
84 }
85  
86 190 ▶sub dkim( $self, @args ) {
Subroutine dkim: called CC=7 SCAR=19.5
Pod documented
87 190 ▶ if ( !@args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 115 75 190
88 115 $self->_unwrap('dkim');
89 115 return $self->{dkim};
90 }
91  
92 # one shot
93 75 ▶ if ( @args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 71 4 75
94  
95 # warn "one argument\n";
96 71 ▶ if ( ref $args[0] eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "CODE") 1 70 71
97 1 return $self->{dkim} = $args[0];
98 }
99  
100 70 ▶ if ( ref $args[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "ARRAY") 58 12 70
101 58 foreach my $d ( @{ $args[0] } ) {
102 36 push @{ $self->{dkim} },
103 Mail::DMARC::Report::Aggregate::Record::Auth_Results::DKIM
104 ->new($d);
105 }
106 58 return $self->{dkim};
107 }
108  
109 12 ▶ if ( ref $args[0] eq 'Mail::DKIM::Verifier' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "Mail::DKIM::Verifier") 1 11 12
110 1 $self->_from_mail_dkim( $args[0] );
111 1 return $self->{dkim};
112 }
113 }
114  
115 #warn "iterative\n";
116 15 push @{ $self->{dkim} },
117 Mail::DMARC::Report::Aggregate::Record::Auth_Results::DKIM->new(@args);
118  
119 13 return $self->{dkim};
120 }
121  
122 1 ▶sub _from_mail_dkim( $self, $dkim ) {
Subroutine _from_mail_dkim: called CC=5 SCAR=17.6
123 1 my $signatures = 0;
124  
125 # A DKIM verifier will have result and signature methods.
126 1 foreach my $s ( $dkim->signatures ) {
127 1 ▶ next if ref $s eq 'Mail::DKIM::DkSignature';
Branch: 1 branch
BranchTrueFalseTotal
if ref $s eq "Mail::DKIM::DkSignature" 0 1 1
128 1 $signatures++;
129  
130 1 my $result = $s->result;
131  
132 1 ▶ if ( $result eq 'invalid' ) { # See GH Issue #21
Branch: 1 branch
BranchTrueFalseTotal
if ($result eq "invalid") 0 1 1
133 0 $result = 'temperror';
134 }
135  
136 1 push @{ $self->{dkim} },
137 Mail::DMARC::Report::Aggregate::Record::Auth_Results::DKIM->new(
138 domain => $s->domain,
139 selector => $s->selector,
140 result => $result,
141 human_result => $s->result_detail,
142 );
143 }
144  
145 1 ▶ if ( $signatures < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($signatures < 1) 0 1 1
146 0 push @{ $self->{dkim} },
147 Mail::DMARC::Report::Aggregate::Record::Auth_Results::DKIM->new(
148 domain => '',
149 result => 'none',
150 );
151 }
152  
153 1 return;
154 }
155  
156 257 ▶sub _unwrap( $self, $key ) {
Subroutine _unwrap: called CC=3 SCAR=11.0
157 257 ▶ if ( $self->{$key} and ref $self->{$key} eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$key} and ref $self->{$key} eq "CODE") 2 255 257
Condition100%3/3
$self->{$key} and ref $self->{$key} eq "CODE"
!ll&&!rl&&r
302252
MC/DC100%2/2
$self->{$key} and ref $self->{$key} eq "CODE"
$self->{$key} ref $self->{$key} eq "CODE"
Truth table100%3/3
A and B
A= $self->{$key}B= ref $self->{$key} eq "CODE"
ABresult
0X0
100
111
158 2 my $code = delete $self->{$key};
159 2 $self->$key( $self->$code );
160 }
161 257 return;
162 }
163  
164 215 ▶sub spf( $self, @args ) {
Subroutine spf: called CC=7 SCAR=19.5
Pod documented
165 215 ▶ if ( !@args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 142 73 215
166 142 $self->_unwrap('spf');
167 142 return $self->{spf};
168 }
169  
170 73 ▶ if ( @args == 1 && ref $args[0] eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "CODE") 1 72 73
Condition100%3/3
@args == 1 and ref $args[0] eq "CODE"
!ll&&!rl&&r
5671
MC/DC100%2/2
@args == 1 and ref $args[0] eq "CODE"
@args == 1 ref $args[0] eq "CODE"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "CODE"
ABresult
0X0
100
111
171 1 return $self->{spf} = $args[0];
172 }
173  
174 72 ▶ if ( @args == 1 && ref $args[0] eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq "ARRAY") 50 22 72
Condition100%3/3
@args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
51750
MC/DC100%2/2
@args == 1 and ref $args[0] eq "ARRAY"
@args == 1 ref $args[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
175  
176 # warn "SPF one shot";
177 50 foreach my $d ( @{ $args[0] } ) {
178 58 push @{ $self->{spf} },
179 Mail::DMARC::Report::Aggregate::Record::Auth_Results::SPF->new($d);
180 }
181 50 return $self->{spf};
182 }
183  
184 #warn "SPF iterative";
185 22 push @{ $self->{spf} },
186 Mail::DMARC::Report::Aggregate::Record::Auth_Results::SPF->new(@args);
187  
188 21 return $self->{spf};
189 }
190  
191 279 ▶sub policy( $self, @args ) {
Subroutine policy: called CC=3 SCAR=11.0
Pod undocumented
192 279 ▶ return $self->{policy} if ref $self->{policy} && !@args;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"policy"} and not @args 158 121 279
Condition100%3/3
ref $self->{"policy"} and not @args
!ll&&!rl&&r
7942158
MC/DC100%2/2
ref $self->{"policy"} and not @args
ref $self->{"policy"} not @args
Truth table100%3/3
A and not(B)
A= ref $self->{"policy"}B= not @args
ABresult
0X0
100
111
193 121 return $self->{policy} = Mail::DMARC::Policy->new(@args);
194 }
195  
196 105 ▶sub report($self) {
Subroutine report: called CC=2 SCAR=6.9
Pod undocumented
197 105 ▶ return $self->{report} if ref $self->{report};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"report"} 55 50 105
198 50 require Mail::DMARC::Report;
199 50 return $self->{report} = Mail::DMARC::Report->new();
200 }
201  
202 1032 ▶sub result($self) {
Subroutine result: called CC=2 SCAR=6.9
Pod documented
203 1032 ▶ return $self->{result} if ref $self->{result};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"result"} 952 80 1032
204 80 return $self->{result} = Mail::DMARC::Result->new();
205 }
206  
207 139 ▶sub is_subdomain( $self, $val = undef ) {
Subroutine is_subdomain: called CC=4 SCAR=13.9
Pod undocumented
208 139 ▶ return $self->{is_subdomain} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 137 139
209 137 ▶ croak "invalid boolean" if 0 == grep {/^$val$/ix} qw/ 0 1/;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({/^$val$/xi;} 0, 1) 0 137 137
210 137 return $self->{is_subdomain} = $val;
211 }
212  
213 27 ▶sub get_report_window( $self, $interval = undef, $now = undef ) {
Subroutine get_report_window: called CC=10 SCAR=23.0
Pod undocumented
214 27 my $min_interval = $self->config->{'report_sending'}{'min_interval'};
215 27 my $max_interval = $self->config->{'report_sending'}{'max_interval'};
216  
217 27 ▶ $interval = 86400 if !$interval; # Default to 1 day
Branch: 1 branch
BranchTrueFalseTotal
unless $interval 12 15 27
218 27 ▶ if ($min_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_interval) 5 22 27
219 5 ▶ $interval = $min_interval if $interval < $min_interval;
Branch: 1 branch
BranchTrueFalseTotal
if $interval < $min_interval 1 4 5
220 }
221 27 ▶ if ($max_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_interval) 5 22 27
222 5 ▶ $interval = $max_interval if $interval > $max_interval;
Branch: 1 branch
BranchTrueFalseTotal
if $interval > $max_interval 1 4 5
223 }
224  
225 27 ▶ if ( ( 86400 % $interval ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (86400 % $interval != 0) 2 25 27
226  
227 # Interval does not fit into a day nicely,
228 # So don't work out a window, just run with it.
229 2 return ( $now, $now + $interval - 1 );
230 }
231  
232 25 my $begin = $self->get_start_of_zulu_day($now);
233 25 my $end = $begin + $interval - 1;
234  
235 25 ▶ while ( $end < $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < $now) 5 25 30
236 5 $begin = $begin + $interval;
237 5 $end = $begin + $interval - 1;
238 }
239  
240 25 return ( $begin, $end );
241 }
242  
243 29 ▶sub get_start_of_zulu_day( $self, $t ) {
Subroutine get_start_of_zulu_day: called CC=1 SCAR=0.0
Pod undocumented
244 29 my $start_of_zulu_day = $t - ( $t % 86400 );
245 29 return $start_of_zulu_day;
246 }
247  
248 12 ▶sub save_aggregate($self) {
Subroutine save_aggregate: called CC=3 SCAR=11.0
Pod undocumented
249 12 my $agg = $self->report->aggregate;
250  
251 # put config information in report metadata
252 12 foreach my $f (qw/ org_name email extra_contact_info report_id /) {
253 48 $agg->metadata->$f( $self->config->{organization}{$f} );
254 }
255  
256 # ri is historic (RFC 9989 9.3)
257 12 my ( $begin, $end ) = $self->get_report_window( undef, $self->time );
258  
259 12 $agg->metadata->begin($begin);
260 12 $agg->metadata->end($end);
261  
262 12 $agg->policy_published( $self->result->published );
263  
264 12 my $rec = Mail::DMARC::Report::Aggregate::Record->new();
265 12 $rec->row->source_ip( $self->source_ip );
266  
267 12 $rec->identifiers(
268 envelope_to => $self->envelope_to,
269 envelope_from => $self->envelope_from,
270 header_from => $self->header_from,
271 );
272  
273 12 $rec->auth_results->dkim( $self->dkim_for_report );
274 12 ▶ $rec->auth_results->spf( $self->spf // [] );
Condition50%1/2
$self->spf // []
l!l
120
MC/DC0%0/1
$self->spf // []
$self->spf
Truth table50%1/2
A
A= $self->spf
Aresult
11
00
275  
276 12 $rec->row->policy_evaluated(
277 disposition => $self->result->disposition,
278 dkim => $self->result->dkim,
279 spf => $self->result->spf,
280 reason => $self->result->reason,
281 );
282  
283 12 $agg->record($rec);
284 12 return $self->report->save_aggregate;
285 }
286  
287 # RFC 9990 3.1.3
288 13 ▶sub dkim_for_report($self) {
Subroutine dkim_for_report: called CC=10 SCAR=23.5
Pod undocumented
289 13 my $from = $self->header_from;
290 13 my $from_org = $self->get_organizational_domain($from);
291  
292 13 my %rank;
293 13 ▶ my @sigs = @{ $self->dkim // [] };
Condition50%1/2
$self->dkim // []
l!l
130
MC/DC0%0/1
$self->dkim // []
$self->dkim
Truth table50%1/2
A
A= $self->dkim
Aresult
11
00
294 13 foreach my $sig (@sigs) {
295 16 ▶ my $dom = lc( $sig->{domain} // '' );
Condition50%1/2
$sig->{"domain"} // ""
l!l
160
MC/DC0%0/1
$sig->{"domain"} // ""
$sig->{"domain"}
Truth table50%1/2
A
A= $sig->{"domain"}
Aresult
11
00
296 $rank{$sig}
297 16 ▶ = ( 'pass' ne lc( $sig->{result} // '' ) || $dom eq '' ) ? 3
Branch: 3 branches
BranchTrueFalseTotal
'pass' ne lc($sig->{'result'} // '') || $dom eq '' ? : 13 3 16
$dom eq $from ? : 1 2 3
$self->get_organizational_domain($dom) eq $from_org ? : 1 1 2
Condition60%3/5
"pass" ne lc($sig->{"result"} // "") or $dom eq ""
l!l&&r!l&&!r
1303
$sig->{"result"} // ""
l!l
160
MC/DC50%1/2
"pass" ne lc($sig->{"result"} // "") or $dom eq ""
"pass" ne lc($sig->{"result"} // "") $dom eq ""
MC/DC0%0/1
$sig->{"result"} // ""
$sig->{"result"}
Truth table66%2/3
A or B
A= "pass" ne lc($sig->{"result"} // "")B= $dom eq ""
ABresult
1X1
011
000
Truth table50%1/2
A
A= $sig->{"result"}
Aresult
11
00
298 : $dom eq $from ? 0
299 : $self->get_organizational_domain($dom) eq $from_org ? 1
300 : 2;
301 }
302 13 my @ordered = sort { $rank{$a} <=> $rank{$b} } @sigs;
303 13 ▶ $#ordered = 99 if @ordered > 100;
Branch: 1 branch
BranchTrueFalseTotal
if @ordered > 100 0 13 13
304 13 return \@ordered;
305 }
306  
307 5 ▶sub init($self) {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
308  
309 # used for testing
310 5 map { delete $self->{$_} } qw/ spf spf_ar dkim dkim_ar /;
311 5 return;
312 }
313  
314 11 1;
315  
316 __END__
« lib/Mail/DMARC/Policy.pm ↑ Summary lib/Mail/DMARC/Report/Store/SQL/Grammars/MySQL.pm »