lib/Mail/DMARC/PurePerl.pm

filter: Statementstmt 93.6% 370 / 395 Branchbran 71.3% 184 / 258 Conditioncond 69.3% 77 / 111 MC/DCmcdc 43.2% 29 / 67 Subroutinesub 100% 29 / 29 Podpod 72.2% 13 / 18 totaltotal 79.9% 702 / 878 CC 186 SCAR 28.5
CC
186
Cov
83%
verify_external_reporting
35.2
discover_policy
26.9
validate
25.5
CRAP 17.3
« blib/lib/Mail/DMARC.pm ↑ Summary lib/Mail/DMARC/Report/Send/HTTP.pm »
1 package Mail::DMARC::PurePerl;
2 9 our $VERSION = '2.20260827';
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 64 ▶sub init($self) {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
14 64 $self->is_subdomain(0);
15 64 $self->{header_from} = undef;
16 64 $self->{header_from_raw} = undef;
17 64 $self->{envelope_to} = undef;
18 64 $self->{envelope_from} = undef;
19 64 $self->{source_ip} = undef;
20 64 $self->{sender} = undef;
21 64 $self->{policy} = undef;
22 64 $self->{result} = undef;
23 64 $self->{report} = undef;
24 64 $self->{spf} = undef;
25 64 $self->{dkim} = undef;
26 64 $self->{_tw_cache} = {};
27 64 return;
28 }
29  
30 47 ▶sub validate( $self, $policy = undef ) {
Subroutine validate: called CC=24 SCAR=32.4
Pod documented
31 47 $self->result->result('fail'); # set a couple
32 47 $self->result->disposition('none'); # defaults
33  
34 # 11.2.1 Extract RFC5322.From domain
35 47 ▶ my $from_dom = $self->get_from_dom() or return $self->result;
Branch: 1 branch
BranchTrueFalseTotal
my $from_dom = $self->get_from_dom or return $self->result 46 1 47
36  
37 # 9.6. reject email if the domain appears to not exist
38 46 ▶ $self->exists_in_dns() or return $self->result;
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_in_dns or return $self->result 45 1 46
39  
40 45 ▶ $policy ||= $self->discover_policy(); # 11.2.2 Query DNS for DMARC policy
Condition100%3/3
$policy ||= $self->discover_policy
l!l&&r!l&&!r
15264
MC/DC100%2/2
$policy ||= $self->discover_policy
$policy $self->discover_policy
Truth table100%3/3
A ||= B
A= $policyB= $self->discover_policy
ABresult
1X1
011
000
41 45 ▶ if ( !$policy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($policy) 4 41 45
42  
43 # RFC7489 section 4.3 step 8:
44 # If a policy is found, it is combined with the Author's domain
45 # and the SPF and DKIM results to produce a DMARC policy result (a
46 # "pass" or "fail")
47 # Hence, if no (valid) policy has been found, produce "none" instead.
48 4 $self->result->result('none');
49 4 return $self->result;
50 }
51  
52 # 3.5 Out of Scope DMARC has no "short-circuit" provision, such as
53 # specifying that a pass from one authentication test allows one
54 # to skip the other(s). All are required for reporting.
55  
56 41 try { $self->is_dkim_aligned; } catch ($e) { }; # 11.2.3. DKIM signature verification checks
57 41 try { $self->is_spf_aligned; } catch ($e) { }; # 11.2.4. SPF validation checks
58 41 my $aligned = $self->is_aligned(); # 11.2.5. identifier alignment checks
59  
60 41 ▶ if ( $self->config->{report_store}{auto_save} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"report_store"}{"auto_save"}) 0 41 41
61 0 try { $self->save_aggregate(); } catch ($e) { };
62 }
63  
64 41 ▶ return $self->result if $aligned;
Branch: 1 branch
BranchTrueFalseTotal
if $aligned 11 30 41
65  
66 # Determine if the from domain is a non-existent subdomain (for np tag)
67 30 my $is_sub = $self->is_subdomain;
68 30 ▶ my $sub_exists
Condition100%6/6
!($is_sub && defined $policy->np) || $self->_author_domain_exists($from_dom)
l!l&&r!l&&!r
2523
$is_sub && defined $policy->np
!ll&&!rl&&r
1875
MC/DC100%3/3
!($is_sub && defined $policy->np) || $self->_author_domain_exists($from_dom)
$is_sub defined $policy->np $self->_author_domain_exists($from_dom)
Truth table100%4/4
not(A && B) || C
A= $is_subB= defined $policy->npC= $self->_author_domain_exists($from_dom)
ABCresult
0XX1
10X1
1111
1100
69 = !$is_sub
70 || !defined $policy->np
71 || $self->_author_domain_exists($from_dom);
72  
73 30 my $effective_p;
74 30 ▶ if ( $is_sub && !$sub_exists && defined $policy->np ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_sub and not $sub_exists and defined $policy->np) { } 3 27 30
elsif (defined $policy->sp and $self->result->published and $self->result->published->domain ne $from_dom) { } 2 25 27
Condition83%5/6
$is_sub and not $sub_exists and defined $policy->np
!ll&&!rl&&r
2703
$is_sub and not $sub_exists
!ll&&!rl&&r
1893
MC/DC66%2/3
$is_sub and not $sub_exists and defined $policy->np
$is_sub not $sub_exists defined $policy->np
Truth table75%3/4
A and not(B) and C
A= $is_subB= not $sub_existsC= defined $policy->np
ABCresult
0XX0
10X0
1100
1111
75  
76 # RFC 9989 §4.7 np tag: policy for non-existent subdomains
77 3 $effective_p = $policy->np;
78 }
79 ▶ elsif ( defined $policy->sp
Condition66%4/6
defined $policy->sp and $self->result->published and $self->result->published->domain ne $from_dom
!ll&&!rl&&r
2502
defined $policy->sp and $self->result->published
!ll&&!rl&&r
2502
MC/DC33%1/3
defined $policy->sp and $self->result->published and $self->result->published->domain ne $from_dom
defined $policy->sp $self->result->published $self->result->published->domain ne $from_dom
Truth table50%2/4
A and B and C
A= defined $policy->spB= $self->result->publishedC= $self->result->published->domain ne $from_dom
ABCresult
0XX0
10X0
1100
1111
80 && $self->result->published
81 && $self->result->published->domain ne $from_dom )
82 {
83 2 $effective_p = $policy->sp;
84 }
85 else {
86 25 $effective_p = $policy->p;
87 }
88 30 ▶ $effective_p //= 'none';
Condition50%1/2
$effective_p //= "none"
l!l
300
MC/DC0%0/1
$effective_p //= "none"
$effective_p
Truth table50%1/2
A
A= $effective_p
Aresult
11
00
89  
90 # RFC 9989 4.7 t tag: testing mode, apply one severity level lower
91 30 ▶ if ( defined $policy->t && lc( $policy->t ) eq 'y' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $policy->t and lc $policy->t eq "y") 2 28 30
Condition66%2/3
defined $policy->t and lc $policy->t eq "y"
!ll&&!rl&&r
2802
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
92 2 ▶ my $tested
Branch: 2 branches
BranchTrueFalseTotal
lc $effective_p eq 'reject' ? : 2 0 2
lc $effective_p eq 'quarantine' ? : 0 0 0
93 = ( lc($effective_p) eq 'reject' ) ? 'quarantine'
94 : ( lc($effective_p) eq 'quarantine' ) ? 'none'
95 : $effective_p;
96 2 ▶ if ( $tested ne $effective_p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tested ne $effective_p) 2 0 2
97 2 $effective_p = $tested;
98 2 $self->result->reason(
99 type => 'other',
100 comment => 'policy testing mode (t=y)'
101 );
102 }
103 }
104  
105 # RFC 9989: pct tag is deprecated and MUST be ignored
106  
107 # Apply policy. Emails that fail the DMARC mechanism check are
108 # disposed of in accordance with the discovered DMARC policy.
109 30 ▶ if ( lc $effective_p eq 'none' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $effective_p eq "none") 7 23 30
110 7 return $self->result;
111 }
112  
113 23 ▶ return $self->result if $self->is_whitelisted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_whitelisted 1 22 23
114  
115 22 $self->result->disposition($effective_p);
116 22 return $self->result;
117 }
118  
119 13 ▶sub save_aggregate($self) {
Subroutine save_aggregate: called CC=4 SCAR=13.9
Pod undocumented
120 13 my $pol;
121 13 try {
122 13 $pol = $self->result->published;
123 } catch ($e) { };
124 13 ▶ if ( $pol && $self->has_valid_reporting_uri( $pol->rua ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pol and $self->has_valid_reporting_uri($pol->rua)) 12 1 13
Condition66%2/3
$pol and $self->has_valid_reporting_uri($pol->rua)
!ll&&!rl&&r
0112
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
125 12 my @valid_report_uris = $self->get_valid_reporting_uri( $pol->rua );
126  
127 my $filtered_report_uris = join(
128 ',',
129 map {
130 12 $_->{'uri'}
131 . (
132 12 ▶ ( $_->{'max_bytes'} > 0 ) ? ( '!' . $_->{'max_bytes'} ) : q{} )
Branch: 1 branch
BranchTrueFalseTotal
$_->{'max_bytes'} > 0 ? : 0 12 12
133 } @valid_report_uris
134 );
135  
136 12 $self->result->published->rua($filtered_report_uris);
137  
138 12 return $self->SUPER::save_aggregate();
139 }
140 1 return;
141 }
142  
143 43 ▶sub discover_policy( $self, $from_dom = undef ) {
Subroutine discover_policy: called CC=22 SCAR=32.9
Pod documented
144 43 ▶ $from_dom ||= $self->header_from;
Condition33%1/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
0430
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
145 43 ▶ croak if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 43 43
146 43 ▶ print "Header From: $from_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 43 43
147  
148 # RFC 9989 4.10: DNS Tree Walk replaces PSL-based org domain lookup
149 43 my ( $record_str, $org_dom, $at_dom ) = $self->tree_walk($from_dom);
150  
151 # Fall back to PSL when no DMARC records exist for org domain determination
152 43 ▶ $org_dom //= $self->_psl_organizational_domain($from_dom);
Condition66%2/3
$org_dom //= $self->_psl_organizational_domain($from_dom)
l!l&&r!l&&!r
4030
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
153 43 ▶ $self->is_subdomain( $org_dom eq $from_dom ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$org_dom eq $from_dom ? : 28 15 43
154  
155 43 ▶ if ( !$record_str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record_str) 3 40 43
156 3 $self->result->result('none');
157 $self->result->reason( type => 'other', comment => 'no policy' )
158 3 ▶ if !@{ $self->result->reason };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->result->reason;} 3 0 3
159 3 return;
160 }
161  
162 40 my $policy;
163 40 my $policy_str = "domain=$at_dom;" . $record_str;
164 40 try {
165 40 $policy = $self->policy($policy_str);
166 }
167 catch ($error) {
168 0 $self->result->reason(
169 type => 'other',
170 comment => "policy parse error: $error"
171 );
172 0 return;
173 }
174 40 ▶ return unless $policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy 0 40 40
175 40 $self->result->published($policy);
176  
177 # If a retrieved policy record does not contain a valid "p" tag, or
178 # contains an "sp"/"np" tag that is not valid, then:
179 40 ▶ my $p_invalid = $policy->p && !$policy->is_valid_p( $policy->p );
Condition66%2/3
$policy->p && !$policy->is_valid_p($policy->p)
!ll&&!rl&&r
1390
MC/DC0%0/2
$policy->p && !$policy->is_valid_p($policy->p)
$policy->p !$policy->is_valid_p($policy->p)
Truth table66%2/3
A && not(B)
A= $policy->pB= !$policy->is_valid_p($policy->p)
ABresult
0X0
100
111
180 40 ▶ my $sp_invalid = defined $policy->sp && !$policy->is_valid_p( $policy->sp );
Condition100%3/3
defined $policy->sp && !$policy->is_valid_p($policy->sp)
!ll&&!rl&&r
3622
MC/DC100%2/2
defined $policy->sp && !$policy->is_valid_p($policy->sp)
defined $policy->sp !$policy->is_valid_p($policy->sp)
Truth table100%3/3
A && not(B)
A= defined $policy->spB= !$policy->is_valid_p($policy->sp)
ABresult
0X0
100
111
181 40 ▶ my $np_invalid = defined $policy->np && !$policy->is_valid_p( $policy->np );
Condition66%2/3
defined $policy->np && !$policy->is_valid_p($policy->np)
!ll&&!rl&&r
3550
MC/DC0%0/2
defined $policy->np && !$policy->is_valid_p($policy->np)
defined $policy->np !$policy->is_valid_p($policy->np)
Truth table66%2/3
A && not(B)
A= defined $policy->npB= !$policy->is_valid_p($policy->np)
ABresult
0X0
100
111
182  
183 # psd=y records are not required to have p=
184 40 ▶ my $is_psd = defined $policy->psd && lc( $policy->psd ) eq 'y';
Condition66%2/3
defined $policy->psd && lc $policy->psd eq 'y'
!ll&&!rl&&r
3901
MC/DC50%1/2
defined $policy->psd && lc $policy->psd eq 'y'
defined $policy->psd lc $policy->psd eq 'y'
Truth table66%2/3
A && B
A= defined $policy->psdB= lc $policy->psd eq 'y'
ABresult
0X0
100
111
185  
186 40 ▶ if ( ( !$policy->p && !$is_psd ) || $p_invalid || $sp_invalid || $np_invalid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $policy->p || $is_psd or $p_invalid or $sp_invalid or $np_invalid) 2 38 40
Condition58%7/12
not $policy->p || $is_psd or $p_invalid or $sp_invalid or $np_invalid
l!l&&r!l&&!r
2038
not $policy->p || $is_psd or $p_invalid or $sp_invalid
l!l&&r!l&&!r
0238
not $policy->p || $is_psd or $p_invalid
l!l&&r!l&&!r
0040
$policy->p || $is_psd
l!l&&r!l&&!r
3910
MC/DC20%1/5
not $policy->p || $is_psd or $p_invalid or $sp_invalid or $np_invalid
$policy->p $is_psd $p_invalid $sp_invalid $np_invalid
Truth table33%3/9
not(A || B) or C or D or E
A= $policy->pB= $is_psdC= $p_invalidD= $sp_invalidE= $np_invalid
ABCDEresult
00XXX1
1X1XX1
011XX1
1X01X1
0101X1
1X0011
010011
1X0000
010000
187 2 ▶ if ( !$policy->rua
Branch: 1 branch
BranchTrueFalseTotal
unless ($policy->rua and $self->has_valid_reporting_uri($policy->rua)) 1 1 2
Condition66%2/3
$policy->rua and $self->has_valid_reporting_uri($policy->rua)
!ll&&!rl&&r
101
MC/DC50%1/2
$policy->rua and $self->has_valid_reporting_uri($policy->rua)
$policy->rua $self->has_valid_reporting_uri($policy->rua)
Truth table66%2/3
A and B
A= $policy->ruaB= $self->has_valid_reporting_uri($policy->rua)
ABresult
0X0
100
111
188 || !$self->has_valid_reporting_uri( $policy->rua ) )
189 {
190 1 $self->result->reason( type => 'other', comment => "no valid rua" );
191 1 return;
192 }
193 1 $policy->v('DMARC1');
194 1 $policy->p('none');
195 }
196  
197 # psd=y records reach here without p= value, default to 'none' so all
198 # downstream consumers see a concrete value.
199 39 ▶ $policy->p('none') if !$policy->p;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy->p 1 38 39
200  
201 39 return $policy;
202 }
203  
204 49 ▶sub is_aligned($self) {
Subroutine is_aligned: called CC=3 SCAR=11.0
Pod documented
205  
206 # 11.2.5 Conduct identifier alignment checks. With authentication checks
207 # and policy discovery performed, the Mail Receiver checks if
208 # Authenticated Identifiers fall into alignment as decribed in
209 # Section 4. If one or more of the Authenticated Identifiers align
210 # with the RFC5322.From domain, the message is considered to pass
211 # the DMARC mechanism check. All other conditions (authentication
212 # failures, identifier mismatches) are considered to be DMARC
213 # mechanism check failures.
214  
215 49 ▶ if ( 'pass' eq $self->result->spf
Branch: 1 branch
BranchTrueFalseTotal
if ("pass" eq $self->result->spf or "pass" eq $self->result->dkim) 14 35 49
Condition100%3/3
"pass" eq $self->result->spf or "pass" eq $self->result->dkim
l!l&&r!l&&!r
11335
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
216 || 'pass' eq $self->result->dkim )
217 {
218 14 $self->result->result('pass');
219 14 $self->result->disposition('none');
220 14 return 1;
221 }
222 35 return 0;
223 }
224  
225 52 ▶sub is_dkim_aligned($self) {
Subroutine is_dkim_aligned: called CC=11 SCAR=24.0
Pod documented
226 52 $self->result->dkim('fail'); # our 'default' result
227 52 ▶ $self->get_dkim_pass_sigs() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_dkim_pass_sigs or return 26 26 52
228  
229 # 11.2.3 Perform DKIM signature verification checks. A single email may
230 # contain multiple DKIM signatures. The results MUST include the
231 # value of the "d=" tag from all DKIM signatures that validated.
232  
233 26 ▶ 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!") 26 0 26
234 26 ▶ my $policy = $self->policy or croak "no policy!?";
Branch: 1 branch
BranchTrueFalseTotal
my $policy = $self->policy or croak("no policy!?") 26 0 26
235 26 my $from_org = $self->get_organizational_domain();
236  
237 # Required in report: DKIM-Domain, DKIM-Identity, DKIM-Selector
238 26 foreach my $dkim_ref ( $self->get_dkim_pass_sigs() ) {
239 28 my $dkim_dom = lc $dkim_ref->{domain};
240  
241 my $dkmeta = {
242 domain => $dkim_ref->{domain},
243 selector => $dkim_ref->{selector},
244 28 identity => '', # TODO, what is this?
245 };
246  
247 28 ▶ if ( $dkim_dom eq $from_dom ) { # strict alignment requires exact match
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_dom eq $from_dom) 7 21 28
248 7 $self->result->dkim('pass');
249 7 $self->result->dkim_align('strict');
250 7 $self->result->dkim_meta($dkmeta);
251 7 last;
252 }
253  
254 # don't try relaxed if policy specifies strict
255 21 ▶ next if $policy->adkim && 's' eq lc $policy->adkim;
Branch: 1 branch
BranchTrueFalseTotal
if $policy->adkim and "s" eq lc $policy->adkim 9 12 21
Condition100%3/3
$policy->adkim and "s" eq lc $policy->adkim
!ll&&!rl&&r
1119
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
256  
257 # don't try relaxed if we already got a strict match
258 12 ▶ next if 'pass' eq $self->result->dkim;
Branch: 1 branch
BranchTrueFalseTotal
if "pass" eq $self->result->dkim 0 12 12
259  
260 # relaxed policy (default): Org. Dom must match a DKIM sig
261 12 my $dkim_org = $self->get_organizational_domain($dkim_dom);
262 12 ▶ if ( $dkim_org eq $from_org ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dkim_org eq $from_org) 3 9 12
263 3 $self->result->dkim('pass');
264 3 $self->result->dkim_align('relaxed');
265 3 $self->result->dkim_meta($dkmeta);
266 }
267 }
268 26 ▶ return 1 if 'pass' eq lc $self->result->dkim;
Branch: 1 branch
BranchTrueFalseTotal
if "pass" eq lc $self->result->dkim 10 16 26
269 16 return;
270 }
271  
272 51 ▶sub is_spf_aligned( $self, $spf_dom = undef ) {
Subroutine is_spf_aligned: called CC=14 SCAR=26.6
Pod documented
273  
274 51 ▶ if ( !$spf_dom && !$self->spf ) { croak "missing SPF!"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($spf_dom or $self->spf) 4 47 51
Condition66%2/3
$spf_dom or $self->spf
l!l&&r!l&&!r
0474
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
275  
276 47 ▶ 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!") 47 0 47
277 47 my $from_org = $self->get_organizational_domain($from_dom);
278  
279 47 $self->result->spf('fail');
280  
281 47 my @spf_results;
282 47 ▶ if ($spf_dom) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf_dom) { } 0 47 47
283 0 push @spf_results,
284 { domain => lc $spf_dom, result => 'pass', scope => 'mfrom' };
285 }
286 else {
287 47 my @all_results = @{ $self->spf };
288  
289 # RFC 7489 §4.3.2: only RFC5321.MailFrom (mfrom) is used for DMARC SPF
290 # alignment. EHLO/HELO is not used. Results with no scope specified are
291 # treated as mfrom for backward compatibility.
292 47 ▶ @spf_results = grep { !defined $_->{scope} || lc $_->{scope} ne 'helo' }
Condition66%2/3
!defined($_->{'scope'}) || lc $_->{'scope'} ne 'helo'
l!l&&r!l&&!r
0516
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
293 @all_results;
294 }
295  
296 47 foreach my $res (@spf_results) {
297 51 ▶ next if lc $res->{result} ne 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if lc $res->{"result"} ne "pass" 0 51 51
298 51 my $s_dom = lc $res->{domain};
299  
300 51 ▶ if ( $s_dom eq $from_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s_dom eq $from_dom) 9 42 51
301 9 $self->result->spf('pass');
302 9 $self->result->spf_align('strict');
303 9 return 1;
304 }
305  
306 # don't try relaxed match if strict policy requested
307 42 ▶ 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 33 42
Condition100%3/3
$self->policy->aspf and "s" eq lc $self->policy->aspf
!ll&&!rl&&r
2769
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
308  
309 33 ▶ if ( $self->get_organizational_domain($s_dom) eq $from_org ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_organizational_domain($s_dom) eq $from_org) 5 28 33
310 5 $self->result->spf('pass');
311 5 $self->result->spf_align('relaxed');
312  
313 # Don't return, we might find a strict match later. DMARC just
314 # needs one, for reporting it's nice to have the best match
315 }
316 }
317  
318 38 ▶ return 1 if $self->result->spf eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
if $self->result->spf eq "pass" 4 34 38
319 34 return 0;
320 }
321  
322 27 ▶sub is_whitelisted( $self, $s_ip = undef ) {
Subroutine is_whitelisted: called CC=16 SCAR=28.4
Pod undocumented
323 27 ▶ $s_ip ||= $self->source_ip;
Condition100%3/3
$s_ip ||= $self->source_ip
l!l&&r!l&&!r
4158
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
324 27 ▶ return if !defined $s_ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_ip 8 19 27
325 19 ▶ if ( !$self->{_whitelist} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_whitelist"}) 16 3 19
326 16 ▶ my $white_file = $self->config->{smtp}{whitelist} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $white_file = $self->config->{"smtp"}{"whitelist"} or return 16 0 16
327 16 ▶ return if !-f $white_file || !-r $white_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $white_file and -r $white_file 3 13 16
Condition66%2/3
-f $white_file and -r $white_file
!ll&&!rl&&r
3013
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
328 13 foreach my $line ( split /\n/, $self->slurp($white_file) ) {
329 73 ▶ next if $line =~ /^#/; # ignore comments
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 48 25 73
330 25 my ( $lip, $reason ) = split /\s+/, $line, 2;
331 25 ▶ next if not defined $lip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lip 0 25 25
332 25 $self->{_whitelist}{$lip} = $reason;
333 }
334 }
335 16 ▶ return if !$self->{_whitelist}{$s_ip};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_whitelist"}{$s_ip} 13 3 16
336  
337 3 my ( $type, $comment ) = split /\s+/, $self->{_whitelist}{$s_ip}, 2;
338  
339 # Validate type against RFC 7489 PolicyOverrideType enum
340 my @valid_reason_types
341 3 = qw/ forwarded sampled_out trusted_forwarder mailing_list local_policy other /;
342 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
343 0 $type = 'other';
344 }
345 3 $self->result->disposition('none');
346 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
347 type => $type,
348 ( $comment && $comment =~ /\S/ ? ( 'comment' => $comment ) : () ),
349 );
350 3 return $type;
351 }
352  
353 22 ▶sub has_valid_reporting_uri( $self, $rua ) {
Subroutine has_valid_reporting_uri: called CC=1 SCAR=0.0
Pod documented
354 22 my @valid_reporting_uris = $self->get_valid_reporting_uri($rua);
355 22 return scalar @valid_reporting_uris;
356 }
357  
358 34 ▶sub get_valid_reporting_uri( $self, $rua ) {
Subroutine get_valid_reporting_uri: called CC=5 SCAR=16.1
Pod undocumented
359 34 ▶ return unless $rua;
Branch: 1 branch
BranchTrueFalseTotal
unless $rua 0 34 34
360 34 my $recips_ref = $self->report->uri->parse($rua);
361 34 my @has_permission;
362 34 foreach my $uri_ref (@$recips_ref) {
363 32 ▶ if ( !$self->external_report( $uri_ref->{uri} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->external_report($uri_ref->{"uri"})) 26 6 32
364 26 push @has_permission, $uri_ref;
365 26 next;
366 }
367 6 my $ext = $self->verify_external_reporting($uri_ref);
368 6 ▶ push @has_permission, $uri_ref if $ext;
Branch: 1 branch
BranchTrueFalseTotal
if $ext 4 2 6
369 }
370 34 return @has_permission;
371 }
372  
373 78 ▶sub get_dkim_pass_sigs($self) {
Subroutine get_dkim_pass_sigs: called CC=3 SCAR=11.3
Pod undocumented
374 78 ▶ my $dkim_sigs = $self->dkim or return (); # message not signed
Branch: 1 branch
BranchTrueFalseTotal
my $dkim_sigs = $self->dkim or return () 65 13 78
375  
376 65 ▶ if ( 'ARRAY' ne ref $dkim_sigs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne ref $dkim_sigs) 0 65 65
377 0 croak "dkim needs to be an array reference!";
378 }
379  
380 65 return grep { 'pass' eq lc $_->{result} } @$dkim_sigs;
381 }
382  
383 230 ▶sub tree_walk( $self, $from_dom ) {
Subroutine tree_walk: called CC=20 SCAR=31.5
Pod undocumented
384 230 $from_dom = lc $from_dom;
385  
386 145 return @{ $self->{_tw_cache}{$from_dom} }
387 230 ▶ if $self->{_tw_cache}{$from_dom};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tw_cache"}{$from_dom} 145 85 230
388  
389 # RFC 9989 4.10 DNS Tree Walk: walk up the DNS hierarchy from
390 # the author domain, querying _dmarc.<target> at each level.
391 # Stop when a record with psd=n (org domain anchor) or psd=y (PSD)
392 # is found, or after 8 queries, or when no labels remain.
393  
394 85 my @labels = split /\./, $from_dom;
395 85 my $target = $from_dom;
396 85 my $query_count = 0;
397  
398 # policy_record / at_dom: FIRST record found (most specific, author domain wins)
399 # org_dom: domain of the last record found, or the psd=n anchor
400 # prev_target: target queried one step before the current one (one label
401 # longer); when a psd=y PSD is found, that child is the Organizational Domain
402 85 my ( $policy_record, $at_dom, $org_dom, $prev_target );
403  
404 85 ▶ while ( $query_count < 8 && @labels > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_count < 8 and @labels > 0) 199 63 262
Condition66%2/3
$query_count < 8 and @labels > 0
!ll&&!rl&&r
063199
MC/DC50%1/2
$query_count < 8 and @labels > 0
$query_count < 8 @labels > 0
Truth table66%2/3
A and B
A= $query_count < 8B= @labels > 0
ABresult
0X0
100
111
405 199 $query_count++;
406 199 my $query = $self->get_resolver->send( "_dmarc.$target", 'TXT' );
407 199 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) 199 0 199
408 199 my @matches;
409 199 for my $rr ( $query->answer ) {
410 68 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 68 68
411 68 ▶ next if 'v=dmarc1' ne lc substr( $rr->txtdata, 0, 8 );
Branch: 1 branch
BranchTrueFalseTotal
if "v=dmarc1" ne lc substr($rr->txtdata, 0, 8) 0 68 68
412 68 push @matches, join( '', $rr->txtdata );
413 }
414 199 ▶ if ( @matches == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matches == 1) { } 68 131 199
elsif (@matches > 1) { } 0 131 131
415 68 my $rec = $matches[0];
416 68 my $pol;
417 68 try {
418 68 $pol = $self->policy->parse("domain=$target;$rec");
419 }
420 catch ($e) { };
421 68 ▶ if ($pol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pol) 68 0 68
422  
423 # Author domain takes precedence: save only the first record found
424 68 ▶ if ( !defined $policy_record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $policy_record) 62 6 68
425 62 $policy_record = $rec;
426 62 $at_dom = $target;
427 }
428 68 ▶ my $psd_val = defined $pol->psd ? lc $pol->psd : 'u';
Branch: 1 branch
BranchTrueFalseTotal
defined $pol->psd ? : 22 46 68
429 68 ▶ if ( $psd_val eq 'n' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psd_val eq "n") 19 49 68
430  
431 # Lucky anchor: this domain is the Organizational Domain
432 19 ▶ print "Tree Walk anchor (psd=n): $target\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 19 19
433 if $self->verbose;
434 19 my @result = ( $policy_record, $target, $at_dom );
435 19 $self->{_tw_cache}{$from_dom} = \@result;
436 19 return @result;
437 }
438 49 ▶ if ( $psd_val eq 'y' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psd_val eq "y") 3 46 49
439  
440 # PSD found; org domain is the child one label below it
441 3 ▶ my $below = $prev_target // $from_dom;
Condition33%1/3
$prev_target // $from_dom
l!l&&r!l&&!r
300
MC/DC0%0/2
$prev_target // $from_dom
$prev_target $from_dom
Truth table33%1/3
A // B
A= $prev_targetB= $from_dom
ABresult
1X1
011
000
442 3 ▶ print "Tree Walk PSD (psd=y): $target, org=$below\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 3 3
443 if $self->verbose;
444 3 my @result = ( $policy_record, $below, $at_dom );
445 3 $self->{_tw_cache}{$from_dom} = \@result;
446 3 return @result;
447 }
448  
449 # psd=u (default): update org_dom candidate, keep walking
450 46 $org_dom = $target;
451 }
452 }
453 elsif ( @matches > 1 ) {
454  
455 # 9.5. If the remaining set contains multiple records, processing
456 # terminates and the Mail Receiver takes no action.
457 0 $self->result->reason(
458 type => 'other',
459 comment => "too many policies"
460 );
461 0 ▶ print "Too many DMARC records\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
462 0 return;
463 }
464 }
465  
466 # Remove leftmost label; apply >=8-label reduction rule (RFC 9989 4.10)
467 177 $prev_target = $target;
468 177 my $n = @labels;
469 177 ▶ if ( $n >= 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 8) { } 0 177 177
470 0 my $trim = $n - 7;
471 0 splice @labels, 0, $trim;
472 }
473 else {
474 177 shift @labels;
475 }
476 177 $target = join( '.', @labels );
477 }
478  
479 # Walked to top without a psd=n/y anchor; use last record found as org domain
480 63 my @result = ( $policy_record, $org_dom, $at_dom );
481 63 $self->{_tw_cache}{$from_dom} = \@result;
482 63 return @result;
483 }
484  
485 242 ▶sub get_organizational_domain( $self, $from_dom = undef ) {
Subroutine get_organizational_domain: called CC=6 SCAR=18.2
Pod documented
486 242 ▶ $from_dom ||= $self->header_from;
Condition66%2/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
216260
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
487 242 ▶ croak "missing header_from!" if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 242 242
488 242 $from_dom = lc $from_dom;
489  
490 242 my ( undef, $org_dom ) = $self->tree_walk($from_dom);
491  
492 # Fallback: when no DMARC records exist in DNS for this tree, use the PSL
493 # to determine the org domain
494 242 ▶ if ( !defined $org_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $org_dom) 47 195 242
495 47 $org_dom = $self->_psl_organizational_domain($from_dom);
496 }
497  
498 242 ▶ print "Organizational Domain: $org_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 242 242
499 242 return $org_dom;
500 }
501  
502 50 ▶sub _psl_organizational_domain( $self, $from_dom ) {
Subroutine _psl_organizational_domain: called CC=5 SCAR=16.1
503 50 my @labels = reverse split /\./, lc $from_dom;
504 50 my $greatest = 0;
505 50 ▶ for ( my $i = 0; $i <= $#labels; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#labels) 110 50 160
506 110 ▶ next if !$labels[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $labels[$i] 0 110 110
507 110 my $tld = join '.', reverse( @labels[ 0 .. $i ] );
508 110 ▶ $greatest = $i + 1 if $self->is_public_suffix($tld);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_public_suffix($tld) 51 59 110
509 }
510 50 ▶ return $from_dom if $greatest == @labels;
Branch: 1 branch
BranchTrueFalseTotal
if $greatest == @labels 2 48 50
511 48 return join '.', reverse( @labels[ 0 .. $greatest ] );
512 }
513  
514 39 ▶sub _dns_name_exists( $self, $dom ) {
Subroutine _dns_name_exists: called CC=5 SCAR=20.1
515  
516 # RFC 9989 3.2.13: a name is non-existent only when DNS consistently
517 # returns NXDOMAIN. NOERROR/NODATA means the name exists but lacks that
518 # record type. Timeouts and other errors are treated conservatively as
519 # existing.
520 39 my $got_response = 0;
521 39 for my $type (qw/ A AAAA MX NS /) {
522 39 my $q = $self->get_resolver->send( $dom, $type );
523 39 ▶ next unless $q;
Branch: 1 branch
BranchTrueFalseTotal
unless $q 0 39 39
524 39 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $q->header->rcode ne "NXDOMAIN" 39 0 39
525 if $q->header->rcode ne 'NXDOMAIN'; # NOERROR/NODATA or error → exists
526 0 $got_response = 1;
527 }
528 0 ▶ return $got_response ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$got_response ? : 0 0 0
529 }
530  
531 2 ▶sub _author_domain_exists( $self, $dom ) {
Subroutine _author_domain_exists: called CC=1 SCAR=0.0
532 2 return $self->_dns_name_exists($dom);
533 }
534  
535 50 ▶sub exists_in_dns( $self, $from_dom = undef ) {
Subroutine exists_in_dns: called CC=9 SCAR=22.0
Pod documented
536 50 ▶ $from_dom ||= $self->header_from;
Condition66%2/3
$from_dom ||= $self->header_from
l!l&&r!l&&!r
4460
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
537 50 ▶ croak "no header_from!" if !$from_dom;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_dom 0 50 50
538  
539 # rfc7489 6.6.3
540 # If the set produced by the mechanism above contains no DMARC policy
541 # record (i.e., any indication that there is no such record as opposed
542 # to a transient DNS error), Mail Receivers SHOULD NOT apply the DMARC
543 # mechanism to the message.
544  
545 50 my $org_dom = $self->get_organizational_domain($from_dom);
546 50 my @todo = $from_dom;
547 50 ▶ if ( $from_dom ne $org_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_dom ne $org_dom) 20 30 50
548 20 push @todo, $org_dom;
549 20 $self->is_subdomain(1);
550 }
551 50 my $matched = 0;
552 50 foreach (@todo) {
553 70 ▶ last if $matched;
Branch: 1 branch
BranchTrueFalseTotal
if $matched 19 51 70
554 51 ▶ $matched = 1 if $self->_dns_name_exists($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dns_name_exists($_) 48 3 51
555 }
556 50 ▶ if ( !$matched ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 2 48 50
557 2 $self->result->result('none');
558 2 $self->result->disposition('none');
559 2 $self->result->reason(
560 type => 'other',
561 comment => "$from_dom not in DNS"
562 );
563 }
564 50 return $matched;
565 }
566  
567 3 ▶sub fetch_dmarc_record( $self, $zone, $org_dom = undef ) {
Subroutine fetch_dmarc_record: called CC=10 SCAR=25.9
Pod documented
568  
569 # 1. Mail Receivers MUST query the DNS for a DMARC TXT record at the
570 # DNS domain matching the one found in the RFC5322.From domain in
571 # the message. A possibly empty set of records is returned.
572 3 my @matches = ();
573 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
574 or return ( \@matches, $zone );
575  
576 3 for my $rr ( $query->answer ) {
577 1 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 1 1
578  
579 # 2. Records that do not start with a "v=" tag that identifies the
580 # current version of DMARC are discarded.
581 1 ▶ next if 'v=dmarc1' ne lc substr( $rr->txtdata, 0, 8 );
Branch: 1 branch
BranchTrueFalseTotal
if "v=dmarc1" ne lc substr($rr->txtdata, 0, 8) 0 1 1
582 1 ▶ print "\n" . $rr->txtdata . "\n\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
583 1 push @matches, join( '', $rr->txtdata ); # join long records
584 }
585 3 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 1 2 3
586 1 return \@matches, $zone; # found one! (at least)
587 }
588  
589 # 3. If the set is now empty, the Mail Receiver MUST query the DNS for
590 # a DMARC TXT record at the DNS domain matching the Organizational
591 # Domain in place of the RFC5322.From domain in the message (if
592 # different). This record can contain policy to be asserted for
593 # subdomains of the Organizational Domain.
594 2 ▶ if ( defined $org_dom ) { # <- recursion break
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org_dom) 0 2 2
595 0 ▶ if ( $org_dom ne $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_dom ne $zone) 0 0 0
596 0 return $self->fetch_dmarc_record($org_dom); # <- recursion
597 }
598 }
599  
600 2 return \@matches, $zone;
601 }
602  
603 57 ▶sub get_from_dom($self) {
Subroutine get_from_dom: called CC=8 SCAR=21.2
Pod documented
604 57 ▶ return $self->header_from if $self->header_from;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header_from 46 11 57
605  
606 11 ▶ 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 } 10 1 11
607 1 $self->result->reason( type => 'other', comment => "no header_from" );
608 1 return;
609 };
610  
611 # RFC 7489 §5.6.1: if From contains multiple addresses with different
612 # domains, use the Sender header domain. Caller should set $dmarc->sender.
613 10 my @domains;
614 10 ▶ while ( $header =~ /\@([\w][\w.-]*)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /\@([\w][\w.-]*)/g) 10 10 20
615 10 my $dom = lc $1;
616 10 $dom =~ s/[>;\s,].*$//;
617 10 ▶ push @domains, $dom if $dom;
Branch: 1 branch
BranchTrueFalseTotal
if $dom 10 0 10
618 }
619 10 my %unique_doms = map { $_ => 1 } @domains;
620 10 ▶ if ( keys %unique_doms > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %unique_doms > 1) 1 9 10
621 1 ▶ if ( $self->sender ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sender) 0 1 1
622 0 my $sender_dom = $self->to_ascii_domain( $self->sender );
623 0 return $self->header_from($sender_dom);
624 }
625 1 $self->result->result('none');
626 1 $self->result->reason(
627 type => 'other',
628 comment =>
629 'multiple RFC5322.From domains; provide Sender header via $dmarc->sender',
630 );
631 1 return;
632 }
633  
634 # Caller can pass in pre-parsed from_dom if this doesn't suit them.
635 9 my ($from_dom) = ( split /@/, $header )[-1]; # grab everything after the @
636 9 ($from_dom) = split /(\s+|>)/, lc $from_dom; # remove trailing cruft
637 9 ▶ if ( !$from_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from_dom) 0 9 9
638 0 $self->result->reason(
639 type => 'other',
640 comment => "invalid header_from: ($header)"
641 );
642 0 return;
643 }
644  
645 # RFC 8616 §6: convert U-labels to A-labels before DNS lookups
646 9 $from_dom = $self->to_ascii_domain($from_dom);
647  
648 9 return $self->header_from($from_dom);
649 }
650  
651 36 ▶sub external_report( $self, $uri ) {
Subroutine external_report: called CC=10 SCAR=23.9
Pod documented
652 36 ▶ my $dmarc_dom = $self->result->published->domain
Branch: 1 branch
BranchTrueFalseTotal
my $dmarc_dom = $self->result->published->domain or croak("published policy not tagged!") 36 0 36
653 or croak "published policy not tagged!";
654  
655 36 ▶ if ( 'mailto' eq $uri->scheme ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("mailto" eq $uri->scheme) 33 3 36
656 33 my $dest_email = lc $uri->path;
657 33 my ($dest_host) = ( split /@/, $dest_email )[-1];
658 33 ▶ 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)) 26 7 33
659 $self->get_organizational_domain($dmarc_dom) )
660 {
661 26 ▶ print "$dest_host not external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 26 26
662 26 return 0;
663 }
664 7 ▶ print "$dest_host is external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 7 7
665 7 return 1;
666 }
667  
668 3 ▶ if ( $uri->scheme =~ /^https?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme =~ /^https?$/) 3 0 3
669 3 ▶ if ( $uri->host eq $dmarc_dom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->host eq $dmarc_dom) 2 1 3
670 2 ▶ print $uri->host . " not external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
671 2 return 0;
672 }
673 1 ▶ print $uri->host . " is external for $dmarc_dom\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
674 1 return 1;
675 }
676  
677 0 return 1;
678 }
679  
680 3 ▶sub _uri_authority_host($uri) {
Subroutine _uri_authority_host: called CC=5 SCAR=20.9
681 3 ▶ my $scheme = $uri->scheme // '';
Condition50%1/2
$uri->scheme // ""
l!l
30
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
682 3 ▶ if ( $scheme eq 'mailto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'mailto') { } 3 0 3
elsif ($scheme =~ /^https?$/) { } 0 0 0
683 3 ▶ my $path = $uri->path or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $uri->path or return 3 0 3
684 3 return ( split /@/, $path )[-1];
685 }
686 elsif ( $scheme =~ /^https?$/ ) {
687 0 return $uri->host;
688 }
689 0 return;
690 }
691  
692 9 ▶sub verify_external_reporting( $self, $uri_ref = undef ) {
Subroutine verify_external_reporting: called CC=24 SCAR=35.6
Pod documented
693 9 ▶ $uri_ref or croak "missing URI";
Branch: 1 branch
BranchTrueFalseTotal
$uri_ref or croak("missing URI") 9 0 9
694  
695 # 1. Extract the host portion of the authority component of the URI.
696 # Call this the "destination host".
697 9 ▶ my $dmarc_dom = $self->result->published->domain
Branch: 1 branch
BranchTrueFalseTotal
my $dmarc_dom = $self->result->published->domain or croak("published policy not tagged!") 9 0 9
698 or croak "published policy not tagged!";
699  
700 # 1. Extract the host portion of the authority component of the URI.
701 9 my $dest_host;
702 9 ▶ my $scheme = $uri_ref->{uri}->scheme // '';
Condition50%1/2
$uri_ref->{"uri"}->scheme // ""
l!l
90
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
703 9 ▶ if ( $scheme eq 'mailto' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scheme eq 'mailto') { } 8 1 9
elsif ($scheme =~ /^https?$/) { } 1 0 1
704 8 ▶ 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") 8 0 8
705 8 ($dest_host) = ( split /@/, $path )[-1];
706 }
707 elsif ( $scheme =~ /^https?$/ ) {
708 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
709 }
710 else {
711 0 ▶ print "\tunsupported URI scheme '$scheme' for external verification\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
712 if $self->verbose;
713 0 return;
714 }
715  
716 # 2. Prepend the string "_report._dmarc".
717 # 3. Prepend the domain name from which the policy was retrieved,
718 # after conversion to an A-label if needed.
719 9 my $dest = join '.', $dmarc_dom, '_report._dmarc', $dest_host;
720  
721 # 4. Query the DNS for a TXT record at the constructed name.
722 9 ▶ 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 } 8 1 9
723 1 ▶ print "\tquery for $dest failed\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
724 1 return;
725 };
726  
727 # 5. For each record, parse the result...same overall format:
728 # "v=DMARC1" tag is mandatory and MUST appear first in the list.
729 8 my @matches;
730 8 for my $rr ( $query->answer ) {
731 7 ▶ next if $rr->type ne 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type ne "TXT" 0 7 7
732  
733 7 ▶ next if 'v=dmarc1' ne lc substr( $rr->txtdata, 0, 8 );
Branch: 1 branch
BranchTrueFalseTotal
if "v=dmarc1" ne lc substr($rr->txtdata, 0, 8) 0 7 7
734 7 my $policy = undef;
735 7 my $dmarc_str = join( '', $rr->txtdata ); # join parts
736 7 try { $policy = $self->policy->parse($dmarc_str) } catch ($e) { };
737 7 ▶ push @matches, $policy ? $policy : $dmarc_str;
Branch: 1 branch
BranchTrueFalseTotal
$policy ? : 7 0 7
738 }
739  
740 # 6. If the result includes no TXT resource records...stop
741 8 ▶ if ( !@matches ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@matches) 1 7 8
742 1 ▶ print "\tno TXT match for $dest\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
743 1 return;
744 }
745  
746 # 7. If > 1 TXT resource record remains, external reporting authorized
747 # 8. If a "rua" or "ruf" tag is discovered, replace the
748 # corresponding value with the one found in this record.
749 7 ▶ my @overrides = grep { ref $_ && $_->{rua} } @matches;
Condition66%2/3
ref $_ && $_->{'rua'}
!ll&&!rl&&r
043
MC/DC50%1/2
ref $_ && $_->{'rua'}
ref $_ $_->{'rua'}
Truth table66%2/3
A && B
A= ref $_B= $_->{'rua'}
ABresult
0X0
100
111
750 7 foreach my $or (@overrides) {
751 3 ▶ 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 3 0 3
752  
753 # the overriding URI MUST use the same destination host from step 1.
754 3 my $override_host = _uri_authority_host( $recips_ref->[0]{uri} );
755 3 ▶ next unless defined $override_host && $override_host eq $dest_host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $override_host and $override_host eq $dest_host 0 3 3
Condition33%1/3
defined $override_host and $override_host eq $dest_host
!ll&&!rl&&r
003
MC/DC0%0/2
defined $override_host and $override_host eq $dest_host
defined $override_host $override_host eq $dest_host
Truth table33%1/3
A and B
A= defined $override_hostB= $override_host eq $dest_host
ABresult
0X0
100
111
756 3 ▶ print "found override RUA: $or->{rua}\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 3 3
757 3 $self->result->published->rua( $or->{rua} );
758 }
759  
760 7 return @matches;
761 }
762  
763 9 1;
764  
765 __END__
766  
767 =pod
768  
769 =head1 NAME
770  
771 Mail::DMARC::PurePerl - Pure Perl implementation of DMARC
772  
773 =head1 VERSION
774  
775 version 2.20260827
776  
777 =head1 METHODS
778  
779 =head2 init
780  
781 Reset the Mail::DMARC object, preparing it for a fresh request.
782  
783 =head2 validate
784  
785 This method does the following:
786  
787 =over 4
788  
789 * check if the RFC5322.From domain exists (exists_in_dns)
790  
791 * query DNS for a DMARC policy (discover_policy)
792  
793 * check DKIM alignment (is_dkim_aligned)
794  
795 * check SPF alignment (is_spf_aligned)
796  
797 * determine DMARC alignment (is_aligned)
798  
799 * calculate the I<effective> DMARC policy
800  
801 * apply the DMARC policy (see L<Mail::DMARC::Result>)
802  
803 =back
804  
805 =head2 discover_policy
806  
807 Query the DNS to determine if a DMARC policy exists. When the domain name in the email From header (header_from) is not an Organizational Domain (ex: www.example.com), an attempt is made to determine the O.D. using the Mozilla Public Suffix List. When the O.D. differs from the header_from, a second DNS query is sent to _dmarc.[O.D.].
808  
809 If a DMARC DNS record is found, it is parsed as a L<Mail::DMARC::Policy> object and returned.
810  
811 =head2 is_aligned
812  
813 Determine if this message is DMARC aligned. To pass this test, the message must pass at least one of the alignment test (DKIM or SPF).
814  
815 =head2 is_dkim_aligned
816  
817 Determine if a valid DKIM signature in the message is aligned with the message's From header domain. This match can be in strict (exact match) or relaxed (subdomains match) alignment.
818  
819 =head2 is_spf_aligned
820  
821 Same as DKIM, but for SPF.
822  
823 =head2 has_valid_reporting_uri
824  
825 Check for the presence of a valid reporting URI in the rua or ruf DMARC policy tags.
826  
827 =head2 get_organizational_domain
828  
829 From the 2013 DMARC spec, section 4:
830  
831 Organizational Domain: ..is the domain that was registered with a domain
832 name registrar. Heuristics are used to determine this...
833  
834 =head2 exists_in_dns
835  
836 Determine if a domain exists, reliably. The DMARC draft says:
837  
838 9.6 If the RFC5322.From domain does not exist in the DNS, Mail Receivers
839 SHOULD direct the receiving SMTP server to reject the message {R9}.
840  
841 And in Appendix A.4:
842  
843 A common practice among MTA operators, and indeed one documented in
844 [ADSP], is a test to determine domain existence prior to any more
845 expensive processing. This is typically done by querying the DNS for
846 MX, A or AAAA resource records for the name being evaluated, and
847 assuming the domain is non-existent if it could be determined that no
848 such records were published for that domain name.
849  
850 The original pre-standardization version of this protocol included a
851 mandatory check of this nature. It was ultimately removed, as the
852 method's error rate was too high without substantial manual tuning
853 and heuristic work. There are indeed use cases this work needs to
854 address where such a method would return a negative result about a
855 domain for which reporting is desired, such as a registered domain
856 name that never sends legitimate mail and thus has none of these
857 records present in the DNS.
858  
859 I went back to the ADSP (which led me to the ietf-dkim email list where
860 some 'experts' failed to agree on The Right Way to test domain validity. They
861 pointed out: MX records aren't mandatory, and A or AAAA aren't reliable.
862  
863 Some experimentation proved both arguments in real world usage. This module
864 tests for existence by searching for a MX, NS, A, or AAAA record. Since this
865 search may be repeated for the Organizational Name, if the NS query fails,
866 there is no delegation from the TLD. That has proven very reliable.
867  
868 =head2 fetch_dmarc_record
869  
870 Query the DNS for the presence of a DMARC record at the header from domain name and the Organizational Domain name. Returns the discovered DNS record answers.
871  
872 =head2 get_from_dom
873  
874 Returns the header_from attribute, if defined.
875  
876 When header_from is not defined, crudely, and very quickly parse a From header and return the domain name (aka, the header_from domain).
877  
878 The From header format is defined in RFC 822 and is very complex. The From header can contain multiple email addresses, each with different domains. This method returns the last one. If you want to handle this differently, parse the From header yourself and set header_from.
879  
880 =head2 external_report
881  
882 Determine if a report URL is external. If the domain name portion of the URI is not the same as the domain where the DMARC record was discovered, the report address is considered external.
883  
884 =head2 verify_external_reporting
885  
886 =head3 8.2. Verifying External Destinations
887  
888 It is possible to specify destinations for the different reports that
889 are outside the domain making the request. This is enabled to allow
890 domains that do not have mail servers to request reports and have
891 them go someplace that is able to receive and process them.
892  
893 Without checks, this would allow a bad actor to publish a DMARC
894 policy record that requests reports be sent to a victim address, and
895 then send a large volume of mail that will fail both DKIM and SPF
896 checks to a wide variety of destinations, which will in turn flood
897 the victim with unwanted reports. Therefore, a verification
898 mechanism is included.
899  
900 When a Mail Receiver discovers a DMARC policy in the DNS, and the
901 domain at which that record was discovered is not identical to the
902 host part of the authority component of a [URI] specified in the
903 "rua" or "ruf" tag, the following verification steps SHOULD be taken:
904  
905 1. Extract the host portion of the authority component of the URI.
906 Call this the "destination host".
907 2. Prepend the string "_report._dmarc".
908 3. Prepend the domain name from which the policy was retrieved,
909 after conversion to an A-label if needed.
910 4. Query the DNS for a TXT record at the constructed name.
911 5. For each record, parse the result...same overall format:
912 "v=DMARC1" tag is mandatory and MUST appear first in the list.
913 6. If the result includes no TXT resource records...stop
914 7. If > 1 TXT resource record remains, external reporting authorized
915 8. If a "rua" or "ruf" tag is discovered, replace the
916 corresponding value with the one found in this record.
917  
918 The overriding URI MUST use the same destination host from the first step.
919  
920 =head1 NOT IMPLEMENTED
921  
922 =head2 RFC 9991 - DMARC Failure Reporting
923  
924 RFC 9991 defines the format and delivery of per-message DMARC failure reports
925 (forensic reports, sent via the C<ruf> tag). This module does B<not> implement
926 RFC 9991. Sending failure reports is not supported. The C<ruf> tag in a DMARC
927 policy record is parsed and stored but no failure reports are generated or
928 delivered.
929  
930 =head1 AUTHORS
931  
932 =over 4
933  
934 =item *
935  
936 Matt Simerson <msimerson@cpan.org>
937  
938 =item *
939  
940 Davide Migliavacca <shari@cpan.org>
941  
942 =item *
943  
944 Marc Bradshaw <marc@marcbradshaw.net>
945  
946 =back
947  
948 =head1 COPYRIGHT AND LICENSE
949  
950 This software is copyright (c) 2026 by Matt Simerson.
951  
952 This is free software; you can redistribute it and/or modify it under
953 the same terms as the Perl 5 programming language system itself.
954  
955 =cut
« blib/lib/Mail/DMARC.pm ↑ Summary lib/Mail/DMARC/Report/Send/HTTP.pm »