lib/Mail/DMARC/PurePerl.pm

filter: Statementstmt 96.7% 414 / 428 Branchbran 76.0% 216 / 284 Conditioncond 69.0% 67 / 97 MC/DCmcdc 40.9% 25 / 61 Subroutinesub 100% 41 / 41 Podpod 72.2% 13 / 18 totaltotal 83.5% 776 / 929 CC 198 SCAR 25.2
CC
198
Cov
86%
verify_external_reporting
33.7
validate
19.0
is_whitelisted
17.0
CRAP 12.5
« lib/Mail/DMARC/Report/Send/HTTP.pm ↑ Summary blib/lib/Mail/DMARC/ConfigData.pm »
1 package Mail::DMARC::PurePerl;
2 9 our $VERSION = '2.20261110';
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use parent 'Mail::DMARC';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 72 ▶sub init($self) {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
14 72 $self->is_subdomain(0);
15 72 $self->{header_from} = undef;
16 72 $self->{header_from_raw} = undef;
17 72 $self->{envelope_to} = undef;
18 72 $self->{envelope_from} = undef;
19 72 $self->{source_ip} = undef;
20 72 $self->{sender} = undef;
21 72 $self->{policy} = undef;
22 72 $self->{result} = undef;
23 72 $self->{report} = undef;
24 72 $self->{spf} = undef;
25 72 $self->{dkim} = undef;
26 72 $self->{_tw_cache} = {};
27 72 $self->{_tw_error} = {};
28 72 $self->{_author_domains} = undef;
29 72 return;
30 }
31  
32 73 ▶sub validate( $self, $policy = undef ) {
Subroutine validate: called CC=18 SCAR=29.5
Pod documented
33 73 $self->result->result('fail'); # set a couple
34 73 $self->result->disposition('none'); # defaults
35  
36 # 11.2.1 Extract RFC5322.From domain
37 73 my $from_dom = $self->get_from_dom();
38 73 ▶ if ( !$from_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_dom) 6 67 73
39 6 ▶ my $authors = delete $self->{_author_domains} or return $self->result;
Branch: 1 branch
BranchTrueFalseTotal
my $authors = delete $self->{"_author_domains"} or return $self->result 5 1 6
40 5 return $self->_validate_each_author( $policy, @$authors );
41 }
42  
43 67 ▶ if ($policy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($policy) { } 18 49 67
44 18 $self->{policy} = $policy;
45 18 ▶ $self->result->published($policy) if $policy->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $policy->domain 18 0 18
46 }
47 else {
48 49 $policy = $self->discover_policy(); # 11.2.2 Query DNS for DMARC policy
49 }
50 67 ▶ return $self->_dns_temperror($from_dom) if $self->{_tw_error}{$from_dom};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tw_error"}{$from_dom} 1 66 67
51 66 ▶ if ( !$policy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($policy) 11 55 66
52  
53 # RFC7489 section 4.3 step 8:
54 # If a policy is found, it is combined with the Author's domain
55 # and the SPF and DKIM results to produce a DMARC policy result (a
56 # "pass" or "fail")
57 # Hence, if no (valid) policy has been found, produce "none" instead.
58 11 $self->result->result('none');
59 11 return $self->result;
60 }
61  
62 # 3.5 Out of Scope DMARC has no "short-circuit" provision, such as
63 # specifying that a pass from one authentication test allows one
64 # to skip the other(s). All are required for reporting.
65  
66 55 try { $self->is_dkim_aligned; } catch ($e) { }; # 11.2.3. DKIM signature verification checks
67 55 try { $self->is_spf_aligned; } catch ($e) { }; # 11.2.4. SPF validation checks
68 55 my $aligned = $self->is_aligned(); # 11.2.5. identifier alignment checks
69  
70 55 ▶ if ( $self->config->{report_store}{auto_save} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"report_store"}{"auto_save"}) 0 55 55
71 0 try { $self->save_aggregate(); } catch ($e) { };
72 }
73  
74 55 ▶ return $self->result if $aligned;
Branch: 1 branch
BranchTrueFalseTotal
if $aligned 12 43 55
75  
76 43 ▶ my $effective_p = $self->_assessment_policy( $policy, $from_dom ) // 'none';
Condition50%1/2
$self->_assessment_policy($policy, $from_dom) // "none"
l!l
430
MC/DC0%0/1
$self->_assessment_policy($policy, $from_dom) // "none"
$self->_assessment_policy($policy, $from_dom)
Truth table50%1/2
A
A= $self->_assessment_policy($policy, $from_dom)
Aresult
11
00
77  
78 # RFC 9989 4.7 t tag: testing mode, apply one severity level lower
79 43 ▶ if ( defined $policy->t && lc( $policy->t ) eq 'y' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $policy->t and lc $policy->t eq "y") 2 41 43
Condition66%2/3
defined $policy->t and lc $policy->t eq "y"
!ll&&!rl&&r
4102
MC/DC50%1/2
defined $policy->t and lc $policy->t eq "y"
defined $policy->t lc $policy->t eq "y"
Truth table66%2/3
A and B
A= defined $policy->tB= lc $policy->t eq "y"
ABresult
0X0
100
111
80 2 ▶ my $tested
Branch: 2 branches
BranchTrueFalseTotal
lc $effective_p eq 'reject' ? : 2 0 2
lc $effective_p eq 'quarantine' ? : 0 0 0
81 = ( lc($effective_p) eq 'reject' ) ? 'quarantine'
82 : ( lc($effective_p) eq 'quarantine' ) ? 'none'
83 : $effective_p;
84 2 ▶ if ( $tested ne $effective_p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tested ne $effective_p) 2 0 2
85 2 $effective_p = $tested;
86 2 $self->result->reason( type => 'policy_test_mode' );
87 }
88 }
89  
90 # RFC 9989: pct tag is deprecated and MUST be ignored
91  
92 # Apply policy. Emails that fail the DMARC mechanism check are
93 # disposed of in accordance with the discovered DMARC policy.
94 43 ▶ if ( lc $effective_p eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $effective_p eq "none") 8 35 43
95 8 return $self->result;
96 }
97  
98 35 ▶ return $self->result if $self->is_whitelisted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_whitelisted 1 34 35
99  
100 34 $self->result->disposition($effective_p);
101 34 return $self->result;
102 }
103  
104 # RFC 9989 4.10.1
105 43 ▶sub _assessment_policy( $self, $policy, $from_dom ) {
Subroutine _assessment_policy: called CC=6 SCAR=18.2
106 43 ▶ my $policy_dom = $policy->domain // $from_dom;
Condition33%1/3
$policy->domain // $from_dom
l!l&&r!l&&!r
4300
MC/DC0%0/2
$policy->domain // $from_dom
$policy->domain $from_dom
Truth table33%1/3
A // B
A= $policy->domainB= $from_dom
ABresult
1X1
011
000
107 43 ▶ return $policy->p if $policy_dom eq $from_dom;
Branch: 1 branch
BranchTrueFalseTotal
if $policy_dom eq $from_dom 28 15 43
108 15 ▶ return $policy->np
Branch: 1 branch
BranchTrueFalseTotal
if defined $policy->np and not $self->_author_domain_exists($from_dom) 4 11 15
Condition100%3/3
defined $policy->np and not $self->_author_domain_exists($from_dom)
!ll&&!rl&&r
924
MC/DC100%2/2
defined $policy->np and not $self->_author_domain_exists($from_dom)
defined $policy->np not $self->_author_domain_exists($from_dom)
Truth table100%3/3
A and not(B)
A= defined $policy->npB= not $self->_author_domain_exists($from_dom)
ABresult
0X0
100
111
109 if defined $policy->np && !$self->_author_domain_exists($from_dom);
110 11 ▶ return $policy->sp // $policy->p;
Condition66%2/3
$policy->sp // $policy->p
l!l&&r!l&&!r
290
MC/DC0%0/2
$policy->sp // $policy->p
$policy->sp $policy->p
Truth table66%2/3
A // B
A= $policy->spB= $policy->p
ABresult
1X1
011
000
111 }
112  
113 # RFC 9989 5.3.6
114 1 ▶sub _dns_temperror( $self, $from_dom ) {
Subroutine _dns_temperror: called CC=1 SCAR=0.0
115 1 $self->result->result('temperror');
116 1 $self->result->disposition('none');
117 1 $self->result->reason(
118 type => 'other',
119 comment => "DNS error: $self->{_tw_error}{$from_dom}"
120 );
121 1 return $self->result;
122 }
123  
124 9 my $max_author_domains = 5;
125 9 my %disposition_rank = ( none => 0, quarantine => 1, reject => 2 );
126 9 my %result_rank = ( pass => 0, none => 1, temperror => 2, fail => 3 );
127  
128 # RFC 9989 11.5; the cap bounds the DNS work a crafted From can cause
129 5 ▶sub _validate_each_author( $self, $policy, @authors ) {
Subroutine _validate_each_author: called CC=5 SCAR=16.1
130  
131 # skipping some would let a sender bury the protected domain among others
132 5 ▶ if ( @authors > $max_author_domains ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@authors > $max_author_domains) 1 4 5
133 1 $self->result->result('permerror');
134 1 $self->result->disposition('none');
135 1 $self->result->reason(
136 type => 'other',
137 comment => scalar(@authors)
138 . " RFC5322.From domains, over the limit of $max_author_domains"
139 );
140 1 return $self->result;
141 }
142  
143 4 $self->get_resolver; # built lazily, so build it once for every clone to share
144  
145 4 my $strictest;
146 4 for my $author (@authors) {
147 8 my $one = bless { %$self, map { $_ => undef } qw/ policy result report / },
148 ref $self;
149 8 $one->header_from($author);
150 8 $one->validate($policy);
151 8 ▶ $strictest = $one
Branch: 1 branch
BranchTrueFalseTotal
if not $strictest or _is_stricter($one->result, $strictest->result) 4 4 8
Condition66%2/3
not $strictest or _is_stricter($one->result, $strictest->result)
l!l&&r!l&&!r
404
MC/DC50%1/2
not $strictest or _is_stricter($one->result, $strictest->result)
not $strictest _is_stricter($one->result, $strictest->result)
Truth table66%2/3
not(A) or B
A= not $strictestB= _is_stricter($one->result, $strictest->result)
ABresult
1X1
011
000
152 if !$strictest || _is_stricter( $one->result, $strictest->result );
153 }
154 4 %$self = %$strictest;
155 4 return $self->result;
156 }
157  
158 4 ▶sub _is_stricter( $candidate, $current ) {
Subroutine _is_stricter: called CC=2 SCAR=7.0
159 my @rank = map {
160 4 [ $disposition_rank{ lc $_->disposition }, $result_rank{ lc $_->result } ]
161 } $candidate, $current;
162 4 ▶ return ( $rank[0][0] <=> $rank[1][0] || $rank[0][1] <=> $rank[1][1] ) > 0;
Condition66%2/3
$rank[0][0] <=> $rank[1][0] || $rank[0][1] <=> $rank[1][1]
l!l&&r!l&&!r
301
MC/DC50%1/2
$rank[0][0] <=> $rank[1][0] || $rank[0][1] <=> $rank[1][1]
$rank[0][0] <=> $rank[1][0] $rank[0][1] <=> $rank[1][1]
Truth table66%2/3
A || B
A= $rank[0][0] <=> $rank[1][0]B= $rank[0][1] <=> $rank[1][1]
ABresult
1X1
011
000
163 }
164  
165 14 ▶sub save_aggregate($self) {
Subroutine save_aggregate: called CC=4 SCAR=13.9
Pod undocumented
166 14 my $pol;
167 14 try {
168 14 $pol = $self->result->published;
169 } catch ($e) { };
170 14 ▶ if ( $pol && $self->has_valid_reporting_uri( $pol->rua ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pol and $self->has_valid_reporting_uri($pol->rua)) 13 1 14
Condition66%2/3
$pol and $self->has_valid_reporting_uri($pol->rua)
!ll&&!rl&&r
0113
MC/DC50%1/2
$pol and $self->has_valid_reporting_uri($pol->rua)
$pol $self->has_valid_reporting_uri($pol->rua)
Truth table66%2/3
A and B
A= $polB= $self->has_valid_reporting_uri($pol->rua)
ABresult
0X0
100
111
171 13 my @valid_report_uris = $self->get_valid_reporting_uri( $pol->rua );
172  
173 my $filtered_report_uris = join(
174 ',',
175 map {
176 13 $_->{'uri'}
177 . (
178 13 ▶ ( $_->{'max_bytes'} > 0 ) ? ( '!' . $_->{'max_bytes'} ) : q{} )
Branch: 1 branch
BranchTrueFalseTotal
$_->{'max_bytes'} > 0 ? : 0 13 13
179 } @valid_report_uris
180 );
181  
182 13 $self->result->published->rua($filtered_report_uris);
183  
184 13 return $self->SUPER::save_aggregate();
185 }
186 1 return;
187 }
188  
189 63 ▶sub discover_policy( $self, $from_dom = undef ) {
Subroutine discover_policy: called CC=11 SCAR=24.4
Pod documented
190 63 ▶ $from_dom ||= $self->header_from;
Condition33%1/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
0630
MC/DC0%0/2
$from_dom ||= $self->header_from
$from_dom $self->header_from
Truth table33%1/3
A ||= B
A= $from_domB= $self->header_from
ABresult
1X1
011
000
191 63 ▶ croak if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 63 63
192 63 ▶ print "Header From: $from_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 63 63
193  
194 # RFC 9989 4.10: DNS Tree Walk replaces PSL-based org domain lookup
195 63 my ( $record_str, $org_dom, $at_dom ) = $self->tree_walk($from_dom);
196  
197 # Fall back to PSL when no DMARC records exist for org domain determination
198 63 ▶ $org_dom //= $self->_psl_organizational_domain($from_dom);
Condition66%2/3
$org_dom //= $self->_psl_organizational_domain($from_dom)
l!l&&r!l&&!r
52110
MC/DC0%0/2
$org_dom //= $self->_psl_organizational_domain($from_dom)
$org_dom $self->_psl_organizational_domain($from_dom)
Truth table66%2/3
A //= B
A= $org_domB= $self->_psl_organizational_domain($from_dom)
ABresult
1X1
011
000
199 63 ▶ $self->is_subdomain( $org_dom eq $from_dom ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$org_dom eq $from_dom ? : 38 25 63
200  
201 63 ▶ if ( !$record_str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record_str) 11 52 63
202 11 $self->result->result('none');
203 $self->result->reason( type => 'other', comment => 'no policy' )
204 11 ▶ if !@{ $self->result->reason };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->result->reason;} 11 0 11
205 11 return;
206 }
207  
208 52 my $policy;
209 52 my $policy_str = "domain=$at_dom;" . $record_str;
210 52 try {
211 52 $policy = $self->policy($policy_str);
212 }
213 catch ($error) {
214 1 $self->result->reason(
215 type => 'other',
216 comment => "policy parse error: $error"
217 );
218 1 return;
219 }
220 51 ▶ return unless $policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy 0 51 51
221 51 $policy->discovery_method('treewalk');
222 51 $self->result->published($policy);
223  
224 # psd=y records reach here without p= value, default to 'none' so all
225 # downstream consumers see a concrete value.
226 51 ▶ $policy->p('none') if !$policy->p;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy->p 1 50 51
227  
228 51 return $policy;
229 }
230  
231 63 ▶sub is_aligned($self) {
Subroutine is_aligned: called CC=3 SCAR=11.0
Pod documented
232  
233 # 11.2.5 Conduct identifier alignment checks. With authentication checks
234 # and policy discovery performed, the Mail Receiver checks if
235 # Authenticated Identifiers fall into alignment as decribed in
236 # Section 4. If one or more of the Authenticated Identifiers align
237 # with the RFC5322.From domain, the message is considered to pass
238 # the DMARC mechanism check. All other conditions (authentication
239 # failures, identifier mismatches) are considered to be DMARC
240 # mechanism check failures.
241  
242 63 ▶ if ( 'pass' eq $self->result->spf
Branch: 1 branch
BranchTrueFalseTotal
if ("pass" eq $self->result->spf or "pass" eq $self->result->dkim) 15 48 63
Condition100%3/3
"pass" eq $self->result->spf or "pass" eq $self->result->dkim
l!l&&r!l&&!r
12348
MC/DC100%2/2
"pass" eq $self->result->spf or "pass" eq $self->result->dkim
"pass" eq $self->result->spf "pass" eq $self->result->dkim
Truth table100%3/3
A or B
A= "pass" eq $self->result->spfB= "pass" eq $self->result->dkim
ABresult
1X1
011
000
243 || 'pass' eq $self->result->dkim )
244 {
245 15 $self->result->result('pass');
246 15 $self->result->disposition('none');
247 15 return 1;
248 }
249 48 return 0;
250 }
251  
252 66 ▶sub is_dkim_aligned($self) {
Subroutine is_dkim_aligned: called CC=11 SCAR=24.0
Pod documented
253 66 $self->result->dkim('fail'); # our 'default' result
254 66 ▶ $self->get_dkim_pass_sigs() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_dkim_pass_sigs or return 28 38 66
255  
256 # 11.2.3 Perform DKIM signature verification checks. A single email may
257 # contain multiple DKIM signatures. The results MUST include the
258 # value of the "d=" tag from all DKIM signatures that validated.
259  
260 28 ▶ my $from_dom = $self->header_from or croak "header_from not set!";
Branch: 1 branch
BranchTrueFalseTotal
my $from_dom = $self->header_from or croak("header_from not set!") 28 0 28
261 28 ▶ my $policy = $self->policy or croak "no policy!?";
Branch: 1 branch
BranchTrueFalseTotal
my $policy = $self->policy or croak("no policy!?") 28 0 28
262 28 my $from_org = $self->get_organizational_domain();
263  
264 # Required in report: DKIM-Domain, DKIM-Identity, DKIM-Selector
265 28 foreach my $dkim_ref ( $self->get_dkim_pass_sigs() ) {
266 30 my $dkim_dom = lc $dkim_ref->{domain};
267  
268 my $dkmeta = {
269 domain => $dkim_ref->{domain},
270 selector => $dkim_ref->{selector},
271 30 identity => '', # TODO, what is this?
272 };
273  
274 30 ▶ if ( $dkim_dom eq $from_dom ) { # strict alignment requires exact match
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_dom eq $from_dom) 7 23 30
275 7 $self->result->dkim('pass');
276 7 $self->result->dkim_align('strict');
277 7 $self->result->dkim_meta($dkmeta);
278 7 last;
279 }
280  
281 # don't try relaxed if policy specifies strict
282 23 ▶ next if $policy->adkim && 's' eq lc $policy->adkim;
Branch: 1 branch
BranchTrueFalseTotal
if $policy->adkim and "s" eq lc $policy->adkim 11 12 23
Condition100%3/3
$policy->adkim and "s" eq lc $policy->adkim
!ll&&!rl&&r
11111
MC/DC100%2/2
$policy->adkim and "s" eq lc $policy->adkim
$policy->adkim "s" eq lc $policy->adkim
Truth table100%3/3
A and B
A= $policy->adkimB= "s" eq lc $policy->adkim
ABresult
0X0
100
111
283  
284 # don't try relaxed if we already got a strict match
285 12 ▶ next if 'pass' eq $self->result->dkim;
Branch: 1 branch
BranchTrueFalseTotal
if "pass" eq $self->result->dkim 0 12 12
286  
287 # relaxed policy (default): Org. Dom must match a DKIM sig
288 12 my $dkim_org = $self->get_organizational_domain($dkim_dom);
289 12 ▶ if ( $dkim_org eq $from_org ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_org eq $from_org) 3 9 12
290 3 $self->result->dkim('pass');
291 3 $self->result->dkim_align('relaxed');
292 3 $self->result->dkim_meta($dkmeta);
293 }
294 }
295 28 ▶ return 1 if 'pass' eq lc $self->result->dkim;
Branch: 1 branch
BranchTrueFalseTotal
if "pass" eq lc $self->result->dkim 10 18 28
296 18 return;
297 }
298  
299 65 ▶sub is_spf_aligned( $self, $spf_dom = undef ) {
Subroutine is_spf_aligned: called CC=14 SCAR=26.6
Pod documented
300  
301 65 ▶ if ( !$spf_dom && !$self->spf ) { croak "missing SPF!"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($spf_dom or $self->spf) 6 59 65
Condition66%2/3
$spf_dom or $self->spf
l!l&&r!l&&!r
0596
MC/DC50%1/2
$spf_dom or $self->spf
$spf_dom $self->spf
Truth table66%2/3
A or B
A= $spf_domB= $self->spf
ABresult
1X1
011
000
302  
303 59 ▶ my $from_dom = lc $self->header_from or croak "header_from not set!";
Branch: 1 branch
BranchTrueFalseTotal
my $from_dom = lc $self->header_from or croak("header_from not set!") 59 0 59
304 59 my $from_org = $self->get_organizational_domain($from_dom);
305  
306 59 $self->result->spf('fail');
307  
308 59 my @spf_results;
309 59 ▶ if ($spf_dom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf_dom) { } 0 59 59
310 0 push @spf_results,
311 { domain => lc $spf_dom, result => 'pass', scope => 'mfrom' };
312 }
313 else {
314 59 my @all_results = @{ $self->spf };
315  
316 # RFC 7489 §4.3.2: only RFC5321.MailFrom (mfrom) is used for DMARC SPF
317 # alignment. EHLO/HELO is not used. Results with no scope specified are
318 # treated as mfrom for backward compatibility.
319 59 ▶ @spf_results = grep { !defined $_->{scope} || lc $_->{scope} ne 'helo' }
Condition66%2/3
!defined($_->{'scope'}) || lc $_->{'scope'} ne 'helo'
l!l&&r!l&&!r
0637
MC/DC50%1/2
!defined($_->{'scope'}) || lc $_->{'scope'} ne 'helo'
!defined($_->{'scope'}) lc $_->{'scope'} ne 'helo'
Truth table66%2/3
not(A) || B
A= !defined($_->{'scope'})B= lc $_->{'scope'} ne 'helo'
ABresult
1X1
011
000
320 @all_results;
321 }
322  
323 59 foreach my $res (@spf_results) {
324 63 ▶ next if lc $res->{result} ne 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if lc $res->{"result"} ne "pass" 0 63 63
325 63 my $s_dom = lc $res->{domain};
326  
327 63 ▶ if ( $s_dom eq $from_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_dom eq $from_dom) 9 54 63
328 9 $self->result->spf('pass');
329 9 $self->result->spf_align('strict');
330 9 return 1;
331 }
332  
333 # don't try relaxed match if strict policy requested
334 54 ▶ next if $self->policy->aspf && 's' eq lc $self->policy->aspf;
Branch: 1 branch
BranchTrueFalseTotal
if $self->policy->aspf and "s" eq lc $self->policy->aspf 9 45 54
Condition100%3/3
$self->policy->aspf and "s" eq lc $self->policy->aspf
!ll&&!rl&&r
3969
MC/DC100%2/2
$self->policy->aspf and "s" eq lc $self->policy->aspf
$self->policy->aspf "s" eq lc $self->policy->aspf
Truth table100%3/3
A and B
A= $self->policy->aspfB= "s" eq lc $self->policy->aspf
ABresult
0X0
100
111
335  
336 45 ▶ if ( $self->get_organizational_domain($s_dom) eq $from_org ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_organizational_domain($s_dom) eq $from_org) 6 39 45
337 6 $self->result->spf('pass');
338 6 $self->result->spf_align('relaxed');
339  
340 # Don't return, we might find a strict match later. DMARC just
341 # needs one, for reporting it's nice to have the best match
342 }
343 }
344  
345 50 ▶ return 1 if $self->result->spf eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $self->result->spf eq "pass" 5 45 50
346 45 return 0;
347 }
348  
349 39 ▶sub is_whitelisted( $self, $s_ip = undef ) {
Subroutine is_whitelisted: called CC=16 SCAR=28.4
Pod undocumented
350 39 ▶ $s_ip ||= $self->source_ip;
Condition100%3/3
$s_ip ||= $self->source_ip
l!l&&r!l&&!r
4278
MC/DC100%2/2
$s_ip ||= $self->source_ip
$s_ip $self->source_ip
Truth table100%3/3
A ||= B
A= $s_ipB= $self->source_ip
ABresult
1X1
011
000
351 39 ▶ return if !defined $s_ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_ip 8 31 39
352 31 ▶ if ( !$self->{_whitelist} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_whitelist"}) 28 3 31
353 28 ▶ my $white_file = $self->config->{smtp}{whitelist} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $white_file = $self->config->{"smtp"}{"whitelist"} or return 28 0 28
354 28 ▶ return if !-f $white_file || !-r $white_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $white_file and -r $white_file 15 13 28
Condition66%2/3
-f $white_file and -r $white_file
!ll&&!rl&&r
15013
MC/DC50%1/2
-f $white_file and -r $white_file
-f $white_file -r $white_file
Truth table66%2/3
A and B
A= -f $white_fileB= -r $white_file
ABresult
0X0
100
111
355 13 foreach my $line ( split /\n/, $self->slurp($white_file) ) {
356 73 ▶ next if $line =~ /^#/; # ignore comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 48 25 73
357 25 my ( $lip, $reason ) = split /\s+/, $line, 2;
358 25 ▶ next if not defined $lip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lip 0 25 25
359 25 $self->{_whitelist}{$lip} = $reason;
360 }
361 }
362 16 ▶ return if !$self->{_whitelist}{$s_ip};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_whitelist"}{$s_ip} 13 3 16
363  
364 3 my ( $type, $comment ) = split /\s+/, $self->{_whitelist}{$s_ip}, 2;
365  
366 my @valid_reason_types
367 3 = qw/ forwarded sampled_out trusted_forwarder mailing_list local_policy other /;
368 3 ▶ if ( !$type || !grep { $_ eq lc $type } @valid_reason_types ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type and grep {$_ eq lc $type;} @valid_reason_types) 0 3 3
Condition33%1/3
$type and grep {$_ eq lc $type;} @valid_reason_types
!ll&&!rl&&r
003
MC/DC0%0/2
$type and grep {$_ eq lc $type;} @valid_reason_types
$type grep {$_ eq lc $type;} @valid_reason_types
Truth table33%1/3
A and B
A= $typeB= grep {$_ eq lc $type;} @valid_reason_types
ABresult
0X0
100
111
369 0 $type = 'other';
370 }
371 3 $self->result->disposition('none');
372 3 ▶ $self->result->reason(
Branch: 1 branch
BranchTrueFalseTotal
$comment && $comment =~ /\S/ ? : 2 1 3
Condition66%2/3
$comment and $comment =~ /\S/
!ll&&!rl&&r
102
MC/DC50%1/2
$comment and $comment =~ /\S/
$comment $comment =~ /\S/
Truth table66%2/3
A and B
A= $commentB= $comment =~ /\S/
ABresult
0X0
100
111
373 type => $type,
374 ( $comment && $comment =~ /\S/ ? ( 'comment' => $comment ) : () ),
375 );
376 3 return $type;
377 }
378  
379 22 ▶sub has_valid_reporting_uri( $self, $rua ) {
Subroutine has_valid_reporting_uri: called CC=1 SCAR=0.0
Pod documented
380 22 my @valid_reporting_uris = $self->get_valid_reporting_uri($rua);
381 22 return scalar @valid_reporting_uris;
382 }
383  
384 35 ▶sub get_valid_reporting_uri( $self, $rua ) {
Subroutine get_valid_reporting_uri: called CC=5 SCAR=16.1
Pod undocumented
385 35 ▶ return unless $rua;
Branch: 1 branch
BranchTrueFalseTotal
unless $rua 0 35 35
386 35 my $recips_ref = $self->report->uri->parse($rua);
387 35 my @has_permission;
388 35 foreach my $uri_ref (@$recips_ref) {
389 33 ▶ if ( !$self->external_report( $uri_ref->{uri} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->external_report($uri_ref->{"uri"})) 25 8 33
390 25 push @has_permission, $uri_ref;
391 25 next;
392 }
393 8 my $ext = $self->verify_external_reporting($uri_ref);
394 8 ▶ push @has_permission, $uri_ref if $ext;
Branch: 1 branch
BranchTrueFalseTotal
if $ext 6 2 8
395 }
396 35 return @has_permission;
397 }
398  
399 94 ▶sub get_dkim_pass_sigs($self) {
Subroutine get_dkim_pass_sigs: called CC=3 SCAR=11.3
Pod undocumented
400 94 ▶ my $dkim_sigs = $self->dkim or return (); # message not signed
Branch: 1 branch
BranchTrueFalseTotal
my $dkim_sigs = $self->dkim or return () 70 24 94
401  
402 70 ▶ if ( 'ARRAY' ne ref $dkim_sigs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $dkim_sigs) 0 70 70
403 0 croak "dkim needs to be an array reference!";
404 }
405  
406 70 return grep { 'pass' eq lc $_->{result} } @$dkim_sigs;
407 }
408  
409 265 ▶sub tree_walk( $self, $from_dom ) {
Subroutine tree_walk: called CC=2 SCAR=6.9
Pod undocumented
410 265 $from_dom = $self->normalize_domain($from_dom);
411  
412 265 ▶ $self->{_tw_cache}{$from_dom} //= [
Condition100%2/2
$self->{"_tw_cache"}{$from_dom} //= [$self->_select_from_tree_walk($from_dom, $self->_tree_walk_records($from_dom))]
l!l
137128
MC/DC100%1/1
$self->{"_tw_cache"}{$from_dom} //= [$self->_select_from_tree_walk($from_dom, $self->_tree_walk_records($from_dom))]
$self->{"_tw_cache"}{$from_dom}
Truth table100%2/2
A
A= $self->{"_tw_cache"}{$from_dom}
Aresult
11
00
413 $self->_select_from_tree_walk(
414 $from_dom, $self->_tree_walk_records($from_dom)
415 )
416 ];
417 265 return @{ $self->{_tw_cache}{$from_dom} };
418 }
419  
420 # RFC 9989 4.10, longest name first
421 128 ▶sub _tree_walk_records( $self, $from_dom ) {
Subroutine _tree_walk_records: called CC=7 SCAR=19.5
422 128 my @labels = split /\./, $from_dom;
423 128 my @found;
424  
425 128 for ( 1 .. 8 ) {
426 416 ▶ last if !@labels;
Branch: 1 branch
BranchTrueFalseTotal
unless @labels 91 325 416
427 325 my $target = join '.', @labels;
428 325 ▶ splice @labels, 0, ( @labels >= 8 ? @labels - 7 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
@labels >= 8 ? : 1 324 325
429  
430 325 my $found = $self->_tree_walk_query( $target, $from_dom );
431 325 ▶ return if $self->{_tw_error}{$from_dom};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tw_error"}{$from_dom} 1 324 325
432 324 ▶ next if !$found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 218 106 324
433 106 push @found, $found;
434 106 ▶ last if $found->{psd} ne 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $found->{"psd"} ne "u" 36 70 106
435 }
436 127 return @found;
437 }
438  
439 326 ▶sub _tree_walk_query( $self, $target, $from_dom ) {
Subroutine _tree_walk_query: called CC=11 SCAR=24.1
440 326 my $resolver = $self->get_resolver;
441 326 my $query = $resolver->send( "_dmarc.$target", 'TXT' );
442 326 ▶ my $rcode = $query ? $query->header->rcode : $resolver->errorstring;
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 325 1 326
443 326 ▶ if ( !$query || $rcode !~ /^(?:NOERROR|NXDOMAIN)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query and $rcode =~ /^(?:NOERROR|NXDOMAIN)$/) 1 325 326
Condition66%2/3
$query and $rcode =~ /^(?:NOERROR|NXDOMAIN)$/
!ll&&!rl&&r
10325
MC/DC50%1/2
$query and $rcode =~ /^(?:NOERROR|NXDOMAIN)$/
$query $rcode =~ /^(?:NOERROR|NXDOMAIN)$/
Truth table66%2/3
A and B
A= $queryB= $rcode =~ /^(?:NOERROR|NXDOMAIN)$/
ABresult
0X0
100
111
444 1 $self->{_tw_error}{$from_dom} = "_dmarc.$target: $rcode";
445 1 return;
446 }
447  
448 325 my @matches;
449 325 for my $rr ( $query->answer ) {
450 115 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 115 115
451 115 my $txt = join '', $rr->txtdata;
452 115 ▶ push @matches, $txt if _is_dmarc_record($txt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_dmarc_record($txt) 115 0 115
453 }
454  
455 # RFC 9989 4.10 step 2
456 325 ▶ if ( @matches > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches > 1) 4 321 325
457 4 ▶ print "Ignoring multiple DMARC records at $target\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
458 4 return;
459 }
460 321 ▶ return if !@matches;
Branch: 1 branch
BranchTrueFalseTotal
unless @matches 214 107 321
461  
462 107 my $pol = $self->policy->parse("domain=$target;$matches[0]");
463 return {
464 107 ▶ domain => $target,
Branch: 1 branch
BranchTrueFalseTotal
defined $pol->psd ? : 36 71 107
465 record => $matches[0],
466 psd => defined $pol->psd ? lc $pol->psd : 'u',
467 };
468 }
469  
470 128 ▶sub _select_from_tree_walk( $self, $from_dom, @found ) {
Subroutine _select_from_tree_walk: called CC=11 SCAR=24.1
471 128 ▶ return ( undef, undef, undef ) if !@found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 35 93 128
472  
473 # RFC 9989 4.10.2: the walk stops at psd=n/y, so only $last can carry one
474 93 my $last = $found[-1];
475 93 my $org_dom = $last->{domain};
476 93 ▶ if ( $last->{psd} eq 'y' && $last->{domain} ne $from_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last->{"psd"} eq "y" and $last->{"domain"} ne $from_dom) 6 87 93
Condition66%2/3
$last->{"psd"} eq "y" and $last->{"domain"} ne $from_dom
!ll&&!rl&&r
8706
MC/DC50%1/2
$last->{"psd"} eq "y" and $last->{"domain"} ne $from_dom
$last->{"psd"} eq "y" $last->{"domain"} ne $from_dom
Truth table66%2/3
A and B
A= $last->{"psd"} eq "y"B= $last->{"domain"} ne $from_dom
ABresult
0X0
100
111
477 6 $org_dom = _one_label_below( $from_dom, $last->{domain} );
478 }
479 93 ▶ print "Tree Walk Organizational Domain: $org_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 93 93
480  
481 93 my %by_domain = map { $_->{domain} => $_ } @found;
482  
483 # the >=8-label shortcut can skip the Organizational Domain
484 93 ▶ if ( !$by_domain{$org_dom}
Branch: 1 branch
BranchTrueFalseTotal
if (not $by_domain{$org_dom} and $org_dom ne $from_dom and _label_count($org_dom) > 7) 1 92 93
Condition100%6/6
not $by_domain{$org_dom} and $org_dom ne $from_dom and _label_count($org_dom) > 7
!ll&&!rl&&r
9021
not $by_domain{$org_dom} and $org_dom ne $from_dom
!ll&&!rl&&r
8733
MC/DC100%3/3
not $by_domain{$org_dom} and $org_dom ne $from_dom and _label_count($org_dom) > 7
not $by_domain{$org_dom} $org_dom ne $from_dom _label_count($org_dom) > 7
Truth table100%4/4
not(A) and B and C
A= not $by_domain{$org_dom}B= $org_dom ne $from_domC= _label_count($org_dom) > 7
ABCresult
0XX0
10X0
1100
1111
485 && $org_dom ne $from_dom
486 && _label_count($org_dom) > 7 )
487 {
488 1 my $found = $self->_tree_walk_query( $org_dom, $from_dom );
489 1 ▶ $by_domain{$org_dom} = $found if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 1 0 1
490 }
491  
492 # RFC 9989 4.10.1
493 93 ▶ my $policy = $by_domain{$from_dom} // $by_domain{$org_dom} // $last;
Condition83%5/6
($by_domain{$from_dom} // $by_domain{$org_dom}) // $last
l!l&&r!l&&!r
8850
$by_domain{$from_dom} // $by_domain{$org_dom}
l!l&&r!l&&!r
45435
MC/DC0%0/3
($by_domain{$from_dom} // $by_domain{$org_dom}) // $last
$by_domain{$from_dom} $by_domain{$org_dom} $last
Truth table75%3/4
A // B // C
A= $by_domain{$from_dom}B= $by_domain{$org_dom}C= $last
ABCresult
1XX1
01X1
0011
0000
494  
495 93 return ( $policy->{record}, $org_dom, $policy->{domain} );
496 }
497  
498 # laxer than RFC 9989 4.7 about case, quotes, and whitespace (#39)
499 135 ▶sub _is_dmarc_record($txt) {
Subroutine _is_dmarc_record: called CC=1 SCAR=0.0
500 135 return $txt =~ /^\s*"?\s*v\s*=\s*DMARC1\s*(?:[;,"]|$)/i;
501 }
502  
503 9 ▶sub _label_count($dom) {
Subroutine _label_count: called CC=1 SCAR=0.0
504 9 return 1 + ( $dom =~ tr/.// );
505 }
506  
507 6 ▶sub _one_label_below( $from_dom, $ancestor ) {
Subroutine _one_label_below: called CC=1 SCAR=0.0
508 6 my $depth = 1 + _label_count($ancestor);
509 6 my @labels = split /\./, $from_dom;
510 6 return join '.', @labels[ -$depth .. -1 ];
511 }
512  
513 239 ▶sub get_organizational_domain( $self, $from_dom = undef ) {
Subroutine get_organizational_domain: called CC=6 SCAR=18.2
Pod documented
514 239 ▶ $from_dom ||= $self->header_from;
Condition66%2/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
211280
MC/DC0%0/2
$from_dom ||= $self->header_from
$from_dom $self->header_from
Truth table66%2/3
A ||= B
A= $from_domB= $self->header_from
ABresult
1X1
011
000
515 239 ▶ croak "missing header_from!" if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 239 239
516 239 $from_dom = lc $from_dom;
517  
518 239 my ( undef, $org_dom ) = $self->tree_walk($from_dom);
519  
520 # Fallback: when no DMARC records exist in DNS for this tree, use the PSL
521 # to determine the org domain
522 239 ▶ if ( !defined $org_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $org_dom) 49 190 239
523 49 $org_dom = $self->_psl_organizational_domain($from_dom);
524 }
525  
526 239 ▶ print "Organizational Domain: $org_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 239 239
527 239 return $org_dom;
528 }
529  
530 60 ▶sub _psl_organizational_domain( $self, $from_dom ) {
Subroutine _psl_organizational_domain: called CC=5 SCAR=16.1
531 60 my @labels = reverse split /\./, lc $from_dom;
532 60 my $greatest = 0;
533 60 ▶ for ( my $i = 0; $i <= $#labels; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#labels) 139 60 199
534 139 ▶ next if !$labels[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $labels[$i] 0 139 139
535 139 my $tld = join '.', reverse( @labels[ 0 .. $i ] );
536 139 ▶ $greatest = $i + 1 if $self->is_public_suffix($tld);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_public_suffix($tld) 61 78 139
537 }
538 60 ▶ return $from_dom if $greatest == @labels;
Branch: 1 branch
BranchTrueFalseTotal
if $greatest == @labels 2 58 60
539 58 return join '.', reverse( @labels[ 0 .. $greatest ] );
540 }
541  
542 5 ▶sub _dns_name_exists( $self, $dom ) {
Subroutine _dns_name_exists: called CC=5 SCAR=20.1
543  
544 # RFC 9989 3.2.13: a name is non-existent only when DNS consistently
545 # returns NXDOMAIN. NOERROR/NODATA means the name exists but lacks that
546 # record type. Timeouts and other errors are treated conservatively as
547 # existing.
548 5 my $got_response = 0;
549 5 for my $type (qw/ A AAAA MX NS /) {
550 5 my $q = $self->get_resolver->send( $dom, $type );
551 5 ▶ next unless $q;
Branch: 1 branch
BranchTrueFalseTotal
unless $q 0 5 5
552 5 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $q->header->rcode ne "NXDOMAIN" 5 0 5
553 if $q->header->rcode ne 'NXDOMAIN'; # NOERROR/NODATA or error → exists
554 0 $got_response = 1;
555 }
556 0 ▶ return $got_response ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$got_response ? : 0 0 0
557 }
558  
559 2 ▶sub _author_domain_exists( $self, $dom ) {
Subroutine _author_domain_exists: called CC=1 SCAR=0.0
560 2 return $self->_dns_name_exists($dom);
561 }
562  
563 4 ▶sub exists_in_dns( $self, $from_dom = undef ) {
Subroutine exists_in_dns: called CC=9 SCAR=22.1
Pod documented
564 4 ▶ $from_dom ||= $self->header_from;
Condition33%1/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
400
MC/DC0%0/2
$from_dom ||= $self->header_from
$from_dom $self->header_from
Truth table33%1/3
A ||= B
A= $from_domB= $self->header_from
ABresult
1X1
011
000
565 4 ▶ croak "no header_from!" if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 4 4
566  
567 # rfc7489 6.6.3
568 # If the set produced by the mechanism above contains no DMARC policy
569 # record (i.e., any indication that there is no such record as opposed
570 # to a transient DNS error), Mail Receivers SHOULD NOT apply the DMARC
571 # mechanism to the message.
572  
573 4 my $org_dom = $self->get_organizational_domain($from_dom);
574 4 my @todo = $from_dom;
575 4 ▶ if ( $from_dom ne $org_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_dom ne $org_dom) 2 2 4
576 2 push @todo, $org_dom;
577 2 $self->is_subdomain(1);
578 }
579 4 my $matched = 0;
580 4 foreach (@todo) {
581 6 ▶ last if $matched;
Branch: 1 branch
BranchTrueFalseTotal
if $matched 1 5 6
582 5 ▶ $matched = 1 if $self->_dns_name_exists($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dns_name_exists($_) 3 2 5
583 }
584 4 ▶ if ( !$matched ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 1 3 4
585 1 $self->result->result('none');
586 1 $self->result->disposition('none');
587 1 $self->result->reason(
588 type => 'other',
589 comment => "$from_dom not in DNS"
590 );
591 }
592 4 return $matched;
593 }
594  
595 3 ▶sub fetch_dmarc_record( $self, $zone, $org_dom = undef ) {
Subroutine fetch_dmarc_record: called CC=10 SCAR=25.6
Pod documented
596  
597 # 1. Mail Receivers MUST query the DNS for a DMARC TXT record at the
598 # DNS domain matching the one found in the RFC5322.From domain in
599 # the message. A possibly empty set of records is returned.
600 3 my @matches = ();
601 3 ▶ my $query = $self->get_resolver->send( "_dmarc.$zone", 'TXT' )
Branch: 1 branch
BranchTrueFalseTotal
my $query = $self->get_resolver->send("_dmarc.$zone", "TXT") or return \@matches, $zone 3 0 3
602 or return ( \@matches, $zone );
603  
604 3 for my $rr ( $query->answer ) {
605 1 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 1 1
606  
607 # 2. Records that do not start with a "v=" tag that identifies the
608 # current version of DMARC are discarded.
609 1 my $txt = join '', $rr->txtdata;
610 1 ▶ next if !_is_dmarc_record($txt);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_dmarc_record($txt) 0 1 1
611 1 ▶ print "\n$txt\n\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
612 1 push @matches, $txt;
613 }
614 3 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 1 2 3
615 1 return \@matches, $zone; # found one! (at least)
616 }
617  
618 # 3. If the set is now empty, the Mail Receiver MUST query the DNS for
619 # a DMARC TXT record at the DNS domain matching the Organizational
620 # Domain in place of the RFC5322.From domain in the message (if
621 # different). This record can contain policy to be asserted for
622 # subdomains of the Organizational Domain.
623 2 ▶ if ( defined $org_dom ) { # <- recursion break
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org_dom) 0 2 2
624 0 ▶ if ( $org_dom ne $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_dom ne $zone) 0 0 0
625 0 return $self->fetch_dmarc_record($org_dom); # <- recursion
626 }
627 }
628  
629 2 return \@matches, $zone;
630 }
631  
632 83 ▶sub get_from_dom($self) {
Subroutine get_from_dom: called CC=6 SCAR=17.9
Pod documented
633 83 ▶ return $self->header_from if $self->header_from;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header_from 63 20 83
634  
635 20 ▶ my $header = $self->header_from_raw or do {
Branch: 1 branch
BranchTrueFalseTotal
my $header = $self->header_from_raw or do { $self->result->reason("type", "other", "comment", "no header_from"); return } 19 1 20
636 1 $self->result->reason( type => 'other', comment => "no header_from" );
637 1 return;
638 };
639  
640 # RFC 8616 §6: convert U-labels to A-labels before DNS lookups
641 19 my %seen;
642 28 ▶ my @domains = sort grep { !$seen{$_}++ && $self->is_valid_domain($_) }
Condition33%1/3
!$seen{$_}++ && $self->is_valid_domain($_)
!ll&&!rl&&r
0028
MC/DC0%0/2
!$seen{$_}++ && $self->is_valid_domain($_)
!$seen{$_}++ $self->is_valid_domain($_)
Truth table33%1/3
not(A) && B
A= !$seen{$_}++B= $self->is_valid_domain($_)
ABresult
0X0
100
111
643 19 map { $self->to_ascii_domain( $self->normalize_domain($_) ) }
644 _mailbox_domains($header);
645  
646 19 ▶ if ( !@domains ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@domains) 1 18 19
647 1 $self->result->reason(
648 type => 'other',
649 comment => "invalid header_from: ($header)"
650 );
651 1 return;
652 }
653  
654 # RFC 9989 A.3: the Sender header is forgeable, so never substitute it
655 18 ▶ if ( @domains > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@domains > 1) 6 12 18
656 6 $self->{_author_domains} = \@domains;
657 6 return;
658 }
659 12 return $self->header_from( $domains[0] );
660 }
661  
662 # RFC 5322 3.4: a mailbox's domain is in its addr-spec, never in a display
663 # name or comment, where list rewrites put the original author
664 19 ▶sub _mailbox_domains($header) {
Subroutine _mailbox_domains: called CC=3 SCAR=11.0
665 19 ( my $mailboxes = $header ) =~ s/^\s*from\s*:\s*//i;
666 19 $mailboxes = _strip_comments_and_quotes($mailboxes);
667  
668 19 my @domains;
669 19 foreach my $mailbox ( split /,/, $mailboxes ) {
670 29 my ($addr) = $mailbox =~ /<([^>]*)>/;
671 29 ▶ $addr //= $mailbox;
Condition66%2/3
$addr //= $mailbox
l!l&&r!l&&!r
11180
MC/DC0%0/2
$addr //= $mailbox
$addr $mailbox
Truth table66%2/3
A //= B
A= $addrB= $mailbox
ABresult
1X1
011
000
672 29 push @domains, $addr =~ /\@\s*([^\s@<>;,]+)\s*$/;
673 }
674 19 return @domains;
675 }
676  
677 # RFC 5322 3.2: one pass, since a quote in a comment and a parenthesis in a
678 # quoted string are literal, and a quoted-pair escapes either delimiter
679 19 ▶sub _strip_comments_and_quotes($text) {
Subroutine _strip_comments_and_quotes: called CC=11 SCAR=24.0
680 19 my ( $kept, $comment_depth, $in_quote ) = ( '', 0, 0 );
681 19 ▶ while ( $text =~ /\G(\\.|.)/gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G(\\.|.)/gs) 814 19 833
682 814 my $char = $1;
683 814 ▶ if ($in_quote) {
Branch: 4 branches
BranchTrueFalseTotal
if ($in_quote) { } 85 729 814
elsif ($comment_depth) { } 65 664 729
elsif ($char eq '"') { } 5 659 664
elsif ($char eq '(') { } 5 654 659
684 85 ▶ $in_quote = 0 if $char eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\"" 5 80 85
685 }
686 elsif ($comment_depth) {
687 65 ▶ $comment_depth++ if $char eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "(" 0 65 65
688 65 ▶ $comment_depth-- if $char eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $char eq ")" 4 61 65
689 }
690 5 elsif ( $char eq '"' ) { $in_quote = 1 }
691 5 elsif ( $char eq '(' ) { $comment_depth = 1 }
692 654 else { $kept .= $char }
693 }
694  
695 # unbalanced, so evaluate every address-like domain rather than lose one
696 19 ▶ return $text if $in_quote || $comment_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $in_quote or $comment_depth 1 18 19
Condition66%2/3
$in_quote or $comment_depth
l!l&&r!l&&!r
0118
MC/DC50%1/2
$in_quote or $comment_depth
$in_quote $comment_depth
Truth table66%2/3
A or B
A= $in_quoteB= $comment_depth
ABresult
1X1
011
000
697 18 return $kept;
698 }
699  
700 37 ▶sub external_report( $self, $uri ) {
Subroutine external_report: called CC=10 SCAR=23.9
Pod documented
701 37 ▶ my $dmarc_dom = $self->result->published->domain
Branch: 1 branch
BranchTrueFalseTotal
my $dmarc_dom = $self->result->published->domain or croak("published policy not tagged!") 37 0 37
702 or croak "published policy not tagged!";
703  
704 37 ▶ if ( 'mailto' eq $uri->scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("mailto" eq $uri->scheme) 34 3 37
705 34 my $dest_email = lc $uri->path;
706 34 my ($dest_host) = ( split /@/, $dest_email )[-1];
707 34 ▶ if ( $self->get_organizational_domain($dest_host) eq
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_organizational_domain($dest_host) eq $self->get_organizational_domain($dmarc_dom)) 25 9 34
708 $self->get_organizational_domain($dmarc_dom) )
709 {
710 25 ▶ print "$dest_host not external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 25 25
711 25 return 0;
712 }
713 9 ▶ print "$dest_host is external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 9 9
714 9 return 1;
715 }
716  
717 3 ▶ if ( $uri->scheme =~ /^https?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme =~ /^https?$/) 3 0 3
718 3 ▶ if ( $uri->host eq $dmarc_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->host eq $dmarc_dom) 2 1 3
719 2 ▶ print $uri->host . " not external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
720 2 return 0;
721 }
722 1 ▶ print $uri->host . " is external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
723 1 return 1;
724 }
725  
726 0 return 1;
727 }
728  
729 6 ▶sub _uri_authority_host($uri) {
Subroutine _uri_authority_host: called CC=5 SCAR=20.9
730 6 ▶ my $scheme = $uri->scheme // '';
Condition50%1/2
$uri->scheme // ""
l!l
60
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
731 6 ▶ if ( $scheme eq 'mailto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'mailto') { } 6 0 6
elsif ($scheme =~ /^https?$/) { } 0 0 0
732 6 ▶ my $path = $uri->path or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $uri->path or return 6 0 6
733 6 return ( split /@/, $path )[-1];
734 }
735 elsif ( $scheme =~ /^https?$/ ) {
736 0 return $uri->host;
737 }
738 0 return;
739 }
740  
741 13 ▶sub verify_external_reporting( $self, $uri_ref = undef ) {
Subroutine verify_external_reporting: called CC=25 SCAR=35.2
Pod documented
742 13 ▶ $uri_ref or croak "missing URI";
Branch: 1 branch
BranchTrueFalseTotal
$uri_ref or croak("missing URI") 13 0 13
743  
744 # 1. Extract the host portion of the authority component of the URI.
745 # Call this the "destination host".
746 13 ▶ my $dmarc_dom = $self->result->published->domain
Branch: 1 branch
BranchTrueFalseTotal
my $dmarc_dom = $self->result->published->domain or croak("published policy not tagged!") 13 0 13
747 or croak "published policy not tagged!";
748  
749 # 1. Extract the host portion of the authority component of the URI.
750 13 my $dest_host;
751 13 ▶ my $scheme = $uri_ref->{uri}->scheme // '';
Condition50%1/2
$uri_ref->{"uri"}->scheme // ""
l!l
130
MC/DC0%0/1
$uri_ref->{"uri"}->scheme // ""
$uri_ref->{"uri"}->scheme
Truth table50%1/2
A
A= $uri_ref->{"uri"}->scheme
Aresult
11
00
752 13 ▶ if ( $scheme eq 'mailto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'mailto') { } 12 1 13
elsif ($scheme =~ /^https?$/) { } 1 0 1
753 12 ▶ my $path = $uri_ref->{uri}->path or croak "invalid mailto URI";
Branch: 1 branch
BranchTrueFalseTotal
my $path = $uri_ref->{"uri"}->path or croak("invalid mailto URI") 12 0 12
754 12 ($dest_host) = ( split /@/, $path )[-1];
755 }
756 elsif ( $scheme =~ /^https?$/ ) {
757 1 ▶ $dest_host = $uri_ref->{uri}->host or croak "invalid $scheme URI";
Branch: 1 branch
BranchTrueFalseTotal
$dest_host = $uri_ref->{"uri"}->host or croak("invalid $scheme URI") 1 0 1
758 }
759 else {
760 0 ▶ print "\tunsupported URI scheme '$scheme' for external verification\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
761 if $self->verbose;
762 0 return;
763 }
764  
765 # 2. Prepend the string "_report._dmarc".
766 # 3. Prepend the domain name from which the policy was retrieved,
767 # after conversion to an A-label if needed.
768 13 my $dest = join '.', $dmarc_dom, '_report._dmarc', $dest_host;
769  
770 # 4. If the length of the constructed name exceeds DNS limits, stop.
771 13 ▶ if ( length $dest > 253 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dest > 253) 1 12 13
772 1 ▶ print "\t$dest exceeds DNS name limits\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
773 1 return;
774 }
775  
776 # 5. Query the DNS for a TXT record at the constructed name.
777 12 ▶ my $query = $self->get_resolver->send( $dest, 'TXT' ) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $query = $self->get_resolver->send($dest, "TXT") or do { print "\tquery for $dest failed\n" if $self->verbose; return } 11 1 12
778 1 ▶ print "\tquery for $dest failed\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
779 1 return;
780 };
781  
782 # 6. For each record, parse the result...same overall format:
783 # "v=DMARC1" tag is mandatory and MUST appear first in the list.
784 11 my @matches;
785 11 for my $rr ( $query->answer ) {
786 10 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 10 10
787 10 my $txt = join '', $rr->txtdata;
788 10 ▶ push @matches, $self->policy->parse($txt) if _is_dmarc_record($txt);
Branch: 1 branch
BranchTrueFalseTotal
if _is_dmarc_record($txt) 10 0 10
789 }
790  
791 # 7. If the result includes no TXT resource records...stop
792 11 ▶ if ( !@matches ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@matches) 1 10 11
793 1 ▶ print "\tno TXT match for $dest\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
794 1 return;
795 }
796  
797 # 8. If a TXT resource record remains, external reporting is authorized
798 # 9. If a "rua" or "ruf" tag is discovered, replace the
799 # corresponding value with the one found in this record.
800 10 my @overrides = grep { $_->{rua} } @matches;
801 10 foreach my $or (@overrides) {
802 6 ▶ my $recips_ref = $self->report->uri->parse( $or->{rua} ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $recips_ref = $self->report->uri->parse($or->{"rua"}) or next 6 0 6
803  
804 # an override to another host voids both destinations
805 6 ▶ if ( grep { lc( _uri_authority_host( $_->{uri} ) // '' ) ne lc $dest_host }
Branch: 1 branch
BranchTrueFalseTotal
if (grep {lc(_uri_authority_host($_->{"uri"}) // "") ne lc $dest_host;} @$recips_ref) 1 5 6
Condition50%1/2
_uri_authority_host($_->{"uri"}) // ""
l!l
60
MC/DC0%0/1
_uri_authority_host($_->{"uri"}) // ""
_uri_authority_host($_->{"uri"})
Truth table50%1/2
A
A= _uri_authority_host($_->{"uri"})
Aresult
11
00
806 @$recips_ref )
807 {
808 1 ▶ print "\toverride RUA $or->{rua} leaves $dest_host\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
809 1 return;
810 }
811 5 ▶ print "found override RUA: $or->{rua}\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 5 5
812 5 $self->result->published->rua( $or->{rua} );
813 }
814  
815 9 return @matches;
816 }
817  
818 9 1;
819  
820 __END__
« lib/Mail/DMARC/Report/Send/HTTP.pm ↑ Summary blib/lib/Mail/DMARC/ConfigData.pm »