lib/Mail/DKIM2/Validate.pm

filter: Statementstmt 94.1% 303 / 322 Branchbran 73.6% 140 / 190 Conditioncond 54.3% 106 / 195 MC/DCmcdc 24.5% 27 / 110 Subroutinesub 96.3% 26 / 27 Podpod 100% 1 / 1 totaltotal 71.3% 603 / 845 CC 178 SCAR 37.9
CC
178
Cov
78%
_sig_level
90.0
_report_once
54.0
_mi_level
38.2
CRAP 44.4
« bin/dkim2-milter ↑ Summary lib/Mail/Milter/Authentication/Handler/DKIM2Sign.pm »
1 package Mail::DKIM2::Validate;
2 9 ▶use strict; use warnings; use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 our $VERSION = '0.35';
5  
6 9 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Mail::DKIM2::Common qw(extract_mi_version parse_mime);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Mail::DKIM2::Address qw(domain_of to_alabel relaxed_domain_match);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Mail::DKIM2::Key qw(parse_dkim_pubkey);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Mail::DKIM2::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Mail::DKIM2::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # i= / m= of a field value; tag-names are case-sensitive (-09 §8, D10).
17 # Only a chain number (one or two digits, 1..MAX_CHAIN_NUMBER) counts; 0
18 # for anything else, as the Chain treats it.
19 155 ▶sub _i { my $h = shift // ''; $h =~ /(?:\A|;)\s*i\s*=\s*(\d{1,2})\s*(?:;|\z)/ ? 0 + $1 : 0 }
Subroutine _i: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$h =~ /(?:\A|;)\s*i\s*=\s*(\d{1,2})\s*(?:;|\z)/u ? : 154 1 155
Condition50%1/2
shift() // ""
l!l
1550
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
20 81 ▶sub _m { my $h = shift // ''; $h =~ /(?:\A|;)\s*m\s*=\s*(\d{1,2})\s*(?:;|\z)/ ? 0 + $1 : 0 }
Subroutine _m: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$h =~ /(?:\A|;)\s*m\s*=\s*(\d{1,2})\s*(?:;|\z)/u ? : 80 1 81
Condition50%1/2
shift() // ""
l!l
810
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
21  
22 # --- parsed-tag breakdown for the validator UI ---------------------------
23 # A compact, human-readable rendering of every tag in a DKIM2-Signature or
24 # Message-Instance, so the validator can show the full parsed structure.
25  
26 # Unix timestamp -> "<n> (YYYY-MM-DD HH:MM:SSZ)".
27 35 ▶sub _fmt_ts {
Subroutine _fmt_ts: called CC=4 SCAR=15.2
28 35 my $t = shift;
29 35 ▶ return '' unless defined $t && $t ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t ne "" 0 35 35
Condition33%1/3
defined $t and $t ne ""
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $t and $t ne ""
defined $t $t ne ""
Truth table33%1/3
A and B
A= defined $tB= $t ne ""
ABresult
0X0
100
111
30 35 ▶ return $t unless $t =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $t =~ /^\d+$/u 0 35 35
31 35 my @g = gmtime($t);
32 35 return sprintf('%s (%04d-%02d-%02d %02d:%02d:%02dZ)',
33 $t, $g[5]+1900, $g[4]+1, $g[3], $g[2], $g[1], $g[0]);
34 }
35  
36 # Middle-truncate a long base64 blob to keep the structure compact.
37 35 ▶sub _trunc {
Subroutine _trunc: called CC=4 SCAR=14.6
38 35 my ($s, $keep) = @_;
39 35 ▶ $keep //= 16;
Condition50%1/2
$keep //= 16
l!l
035
MC/DC0%0/1
$keep //= 16
$keep
Truth table50%1/2
A
A= $keep
Aresult
11
00
40 35 ▶ $s //= '';
Condition50%1/2
$s //= ""
l!l
350
MC/DC0%0/1
$s //= ""
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
41 35 ▶ return $s if length($s) <= 2*$keep + 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $s <= 2 * $keep + 1 0 35 35
42 35 return substr($s, 0, $keep) . '…' . substr($s, -$keep);
43 }
44  
45 # Ordered [{tag,label,value}] for every tag present in a DKIM2-Signature.
46 35 ▶sub _sig_tags {
Subroutine _sig_tags: called CC=19 SCAR=32.7
47 35 my ($sig) = @_;
48 35 ▶ return [] unless $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig 0 35 35
49 35 my @t;
50 35 ▶ push @t, { tag => 'i', label => 'sequence', value => ($sig->sequence // '') };
Condition50%1/2
$sig->sequence // ""
l!l
350
MC/DC0%0/1
$sig->sequence // ""
$sig->sequence
Truth table50%1/2
A
A= $sig->sequence
Aresult
11
00
51 35 ▶ push @t, { tag => 'm', label => 'covers MI', value => ($sig->version // '') };
Condition50%1/2
$sig->version // ""
l!l
350
MC/DC0%0/1
$sig->version // ""
$sig->version
Truth table50%1/2
A
A= $sig->version
Aresult
11
00
52 35 push @t, { tag => 't', label => 'timestamp', value => _fmt_ts($sig->timestamp) };
53 35 ▶ push @t, { tag => 'd', label => 'domain', value => ($sig->domain // '') };
Condition50%1/2
$sig->domain // ""
l!l
350
MC/DC0%0/1
$sig->domain // ""
$sig->domain
Truth table50%1/2
A
A= $sig->domain
Aresult
11
00
54 35 my $nd = $sig->next_domain;
55 35 ▶ push @t, { tag => 'nd', label => 'next-domain', value => $nd } if defined $nd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $nd 5 30 35
56 35 my $mf = $sig->mail_from;
57 35 ▶ push @t, { tag => 'mf', label => 'MAIL FROM', value => ($mf // ''), decoded => 1 } if defined $mf;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mf 30 5 35
Condition50%1/2
$mf // ""
l!l
300
MC/DC0%0/1
$mf // ""
$mf
Truth table50%1/2
A
A= $mf
Aresult
11
00
58 35 my $rt = $sig->rcpt_to;
59 35 ▶ push @t, { tag => 'rt', label => 'RCPT TO', value => join(', ', @$rt), decoded => 1 } if $rt && @$rt;
Branch: 1 branch
BranchTrueFalseTotal
if $rt and @$rt 30 5 35
Condition66%2/3
$rt and @$rt
!ll&&!rl&&r
5030
MC/DC50%1/2
$rt and @$rt
$rt @$rt
Truth table66%2/3
A and B
A= $rtB= @$rt
ABresult
0X0
100
111
60 35 my $n = $sig->nonce;
61 35 ▶ push @t, { tag => 'n', label => 'nonce', value => $n } if defined $n && length $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n and length $n 0 35 35
Condition33%1/3
defined $n and length $n
!ll&&!rl&&r
3500
MC/DC0%0/2
defined $n and length $n
defined $n length $n
Truth table33%1/3
A and B
A= defined $nB= length $n
ABresult
0X0
100
111
62 35 my $f = $sig->flags;
63 35 ▶ push @t, { tag => 'f', label => 'flags', value => join(', ', @$f) } if $f && @$f;
Branch: 1 branch
BranchTrueFalseTotal
if $f and @$f 0 35 35
Condition33%1/3
$f and @$f
!ll&&!rl&&r
3500
MC/DC0%0/2
$f and @$f
$f @$f
Truth table33%1/3
A and B
A= $fB= @$f
ABresult
0X0
100
111
64 35 ▶ my $cnt = $sig->sig_count || 0;
Condition50%1/2
$sig->sig_count || 0
l!l
350
MC/DC0%0/1
$sig->sig_count || 0
$sig->sig_count
Truth table50%1/2
A
A= $sig->sig_count
Aresult
11
00
65 35 for my $idx (0 .. $cnt - 1) {
66 35 ▶ my $v = $sig->signature_value($idx) // '';
Condition50%1/2
$sig->signature_value($idx) // ""
l!l
350
MC/DC0%0/1
$sig->signature_value($idx) // ""
$sig->signature_value($idx)
Truth table50%1/2
A
A= $sig->signature_value($idx)
Aresult
11
00
67 35 ▶ push @t, { tag => 's', label => 'signature',
Condition50%2/4
$sig->selector($idx) // ""
l!l
350
$sig->algorithm($idx) // ""
l!l
350
MC/DC0%0/1
$sig->selector($idx) // ""
$sig->selector($idx)
MC/DC0%0/1
$sig->algorithm($idx) // ""
$sig->algorithm($idx)
Truth table50%1/2
A
A= $sig->selector($idx)
Aresult
11
00
Truth table50%1/2
A
A= $sig->algorithm($idx)
Aresult
11
00
68 value => sprintf('%s : %s : %s (%d bytes b64)',
69 $sig->selector($idx) // '', $sig->algorithm($idx) // '',
70 _trunc($v), length $v) };
71 }
72 35 return \@t;
73 }
74  
75 # Ordered [{tag,label,value}] for a Message-Instance.
76 27 ▶sub _mi_tags {
Subroutine _mi_tags: called CC=14 SCAR=27.8
77 27 my ($mi) = @_;
78 27 ▶ return [] unless $mi;
Branch: 1 branch
BranchTrueFalseTotal
unless $mi 0 27 27
79 27 my @t;
80 27 ▶ push @t, { tag => 'm', label => 'instance', value => ($mi->get_tag('m') // '') };
Condition50%1/2
$mi->get_tag("m") // ""
l!l
270
MC/DC0%0/1
$mi->get_tag("m") // ""
$mi->get_tag("m")
Truth table50%1/2
A
A= $mi->get_tag("m")
Aresult
11
00
81 # -09 §9.3: h= may carry a hash-set per algorithm -- show each one
82 # under its own name rather than assuming sha256.
83 27 ▶ my $hashes = $mi->get_tag('hashes') || {};
Condition50%1/2
$mi->get_tag('hashes') || {}
l!l
270
MC/DC0%0/1
$mi->get_tag('hashes') || {}
$mi->get_tag('hashes')
Truth table50%1/2
A
A= $mi->get_tag('hashes')
Aresult
11
00
84 27 for my $alg (sort keys %$hashes) {
85 27 my ($hh, $bh) = @{ $hashes->{$alg} };
86 27 ▶ push @t, { tag => 'h', label => 'header hash', value => "$alg:" . ($hh // '') };
Condition50%1/2
$hh // ""
l!l
270
MC/DC0%0/1
$hh // ""
$hh
Truth table50%1/2
A
A= $hh
Aresult
11
00
87 27 ▶ push @t, { tag => 'h', label => 'body hash', value => "$alg:" . ($bh // '') };
Condition50%1/2
$bh // ""
l!l
270
MC/DC0%0/1
$bh // ""
$bh
Truth table50%1/2
A
A= $bh
Aresult
11
00
88 }
89 27 my $rh = $mi->get_tag('rh');
90 27 my $rb = $mi->get_tag('rb');
91 27 ▶ if ($mi->unrecoverable) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mi->unrecoverable) { } 3 24 27
elsif ($rh or $rb) { } 6 18 24
92 3 push @t, { tag => 'r', label => 'recipe', value => 'null (previous state not recoverable)' };
93 ▶ } elsif ($rh || $rb) {
Condition66%2/3
$rh or $rb
l!l&&r!l&&!r
6018
MC/DC50%1/2
$rh or $rb
$rh $rb
Truth table66%2/3
A or B
A= $rhB= $rb
ABresult
1X1
011
000
94 6 my @parts;
95 6 ▶ push @parts, 'headers: ' . join(', ', sort keys %$rh) if $rh && %$rh;
Branch: 1 branch
BranchTrueFalseTotal
if $rh and %$rh 6 0 6
Condition33%1/3
$rh and %$rh
!ll&&!rl&&r
006
MC/DC0%0/2
$rh and %$rh
$rh %$rh
Truth table33%1/3
A and B
A= $rhB= %$rh
ABresult
0X0
100
111
96 6 ▶ push @parts, 'body: ' . (ref $rb eq 'ARRAY' ? scalar(@$rb) . ' step(s)' : 'changed') if $rb;
Branch: 2 branches
BranchTrueFalseTotal
if $rb 5 1 6
ref $rb eq 'ARRAY' ? : 5 0 5
97 6 push @t, { tag => 'r', label => 'recipe', value => join('; ', @parts) };
98 }
99 27 return \@t;
100 }
101  
102 # Default pubkey callback: LIVE DNS. A dns.json override applies only when a
103 # readable $dns_path is passed in (offline testing) — production passes none,
104 # so the validator uses real DNS and never masks a broken key record.
105 9 our $POLICY_CB;
106  
107 # The records of a dns.json file (util's interop key format), or undef.
108 25 ▶sub _dns_json {
Subroutine _dns_json: called CC=5 SCAR=16.3
109 25 my ($dns_path) = @_;
110 25 ▶ return unless $dns_path && -r $dns_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $dns_path and -r $dns_path 22 3 25
Condition66%2/3
$dns_path and -r $dns_path
!ll&&!rl&&r
2203
MC/DC50%1/2
$dns_path and -r $dns_path
$dns_path -r $dns_path
Truth table66%2/3
A and B
A= $dns_pathB= -r $dns_path
ABresult
0X0
100
111
111 3 require JSON;
112 3 ▶ open my $fh, '<', $dns_path or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $dns_path or return 3 0 3
113 3 my $dns = eval { JSON::decode_json(do { local $/; <$fh> }) };
114 3 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 3 3
115 3 return $dns;
116 }
117  
118 # A PolicyCallback answering from DnsPath's records for the domains it
119 # lists, else from DNS.
120 22 ▶sub _default_policy_cb {
Subroutine _default_policy_cb: called CC=1 SCAR=4.9
121 22 my $dns = _dns_json(@_);
122 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
123 0 my ($dom, $verifier) = @_;
124 0 ▶ return [ map { $_->[1] } @{ $dns->{$dom}{_domainkey} || [] } ]
Condition0%0/2
$dns->{$dom}{'_domainkey'} || []
l!l
00
MC/DC0%0/1
$dns->{$dom}{'_domainkey'} || []
$dns->{$dom}{'_domainkey'}
Truth table0%0/2
A
A= $dns->{$dom}{'_domainkey'}
Aresult
11
00
125 0 ▶ if $dns && $dns->{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $dns and $dns->{$dom} 0 0 0
Condition0%0/3
$dns and $dns->{$dom}
!ll&&!rl&&r
000
MC/DC0%0/2
$dns and $dns->{$dom}
$dns $dns->{$dom}
Truth table0%0/3
A and B
A= $dnsB= $dns->{$dom}
ABresult
0X0
100
111
126 0 return $verifier->fetch_domain_policy($dom);
127 22 };
128 }
129  
130 3 ▶sub _default_cb {
Subroutine _default_cb: called CC=1 SCAR=0.1
131 3 my $dns = _dns_json(@_);
132 2 ▶ return sub {
Subroutine __ANON__: called CC=9 SCAR=22.9
133 2 ▶ my ($sig, $idx, $verifier) = @_; $idx //= 0;
Condition50%1/2
$idx //= 0
l!l
20
MC/DC0%0/1
$idx //= 0
$idx
Truth table50%1/2
A
A= $idx
Aresult
11
00
134 2 my $sel = $sig->selector($idx); my $dom = $sig->domain;
135 2 ▶ if ($dns && $dom && $sel && (my $rr = $dns->{$dom}{"$sel._domainkey"})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dns and $dom and $sel and my $rr = $dns->{$dom}{"$sel._domainkey"}) 1 1 2
Condition66%6/9
$dns and $dom and $sel and my $rr = $dns->{$dom}{"$sel._domainkey"}
!ll&&!rl&&r
101
$dns and $dom and $sel
!ll&&!rl&&r
101
$dns and $dom
!ll&&!rl&&r
101
MC/DC25%1/4
$dns and $dom and $sel and my $rr = $dns->{$dom}{"$sel._domainkey"}
$dns $dom $sel my $rr = $dns->{$dom}{"$sel._domainkey"}
Truth table40%2/5
A and B and C and D
A= $dnsB= $domC= $selD= my $rr = $dns->{$dom}{"$sel._domainkey"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
136 # As the Verifier's own fetch: non-DKIM1 records ignored, then
137 # one record or an error (-09 section 3.7).
138 1 my @t = Mail::DKIM2::Key::key_records(map { $_->[1] } @$rr);
139 1 ▶ die "PERMERROR: has multiple records\n" if @t > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @t > 1 0 1 1
140 1 ▶ if (@t) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 1 0 1
141 1 my ($key, $why) = Mail::DKIM2::Key::parse_dkim_key_record($t[0]);
142 1 ▶ die "PERMERROR: $why\n" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 1 1
143 1 return $key;
144 }
145 }
146 # Real DNS. Not eval'd: a transient failure must stay a TEMPERROR,
147 # which the verifier classifies; swallowing it here read as "no key".
148 1 return $verifier->fetch_public_key($sig, $idx);
149 3 };
150 }
151  
152 # report($text, %opts) — verify a DKIM2 message and return a structured report.
153 #
154 # Prior to spec-06, a receiving MTA's Received-SPF header (Fastmail adds one)
155 # was covered by the Message-Instance header hash and could break
156 # verification; this used to retry once with it stripped. -09 §4 excludes
157 # Received-SPF from the hash via the "received-" prefix rule (should_skip()),
158 # so stripping it can no longer change h_digest()/b_digest() or any verdict —
159 # the retry was provably a no-op. Removed rather than kept as a "safety net":
160 # it was hardcoded to the literal name Received-SPF, not a general mechanism,
161 # so it covered nothing else anyway. Mirrored removal in
162 # deploy/www/verify/verify.js's verifyMessage().
163 23 ▶sub report {
Subroutine report: called CC=2 SCAR=7.0
Pod documented
164 23 my ($text, %opts) = @_;
165 23 Mail::DKIM2::Util::check_options('Mail::DKIM2::Validate::report', \%opts,
166 qw(DnsPath PubkeyCallback PolicyCallback SkipTimestampCheck));
167 22 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
220
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
168  
169 22 return _report_once($text, %opts);
170 }
171  
172 22 ▶sub _report_once {
Subroutine _report_once: called CC=36 SCAR=39.9
173 22 my ($text, %opts) = @_;
174 22 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
220
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
175 22 $text =~ s/\r?\n/\r\n/g;
176 22 ▶ my $cb = $opts{PubkeyCallback} || _default_cb($opts{DnsPath});
Condition66%2/3
$opts{'PubkeyCallback'} || _default_cb($opts{'DnsPath'})
l!l&&r!l&&!r
2110
MC/DC0%0/2
$opts{'PubkeyCallback'} || _default_cb($opts{'DnsPath'})
$opts{'PubkeyCallback'} _default_cb($opts{'DnsPath'})
Truth table66%2/3
A || B
A= $opts{'PubkeyCallback'}B= _default_cb($opts{'DnsPath'})
ABresult
1X1
011
000
177 # The Domain Policy Record source every Verifier here uses (-09 section
178 # 3.8): the caller's, else DnsPath's records, else DNS.
179 22 ▶ local $POLICY_CB = $opts{PolicyCallback} || _default_policy_cb($opts{DnsPath});
Condition33%1/3
$opts{'PolicyCallback'} || _default_policy_cb($opts{'DnsPath'})
l!l&&r!l&&!r
0220
MC/DC0%0/2
$opts{'PolicyCallback'} || _default_policy_cb($opts{'DnsPath'})
$opts{'PolicyCallback'} _default_policy_cb($opts{'DnsPath'})
Truth table33%1/3
A || B
A= $opts{'PolicyCallback'}B= _default_policy_cb($opts{'DnsPath'})
ABresult
1X1
011
000
180 22 ▶ my $skip_ts = $opts{SkipTimestampCheck} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'SkipTimestampCheck'} ? : 18 4 22
181  
182 22 my %res = (overall => 'none', summary => '',
183 counts => { signatures => 0, instances => 0 }, levels => []);
184  
185 22 my $msg = eval { parse_mime($text) };
186 22 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 22 22
187 22 ▶ return { %res, overall => 'fail', summary => "could not parse message: $@" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $msg 0 22 22
Condition33%1/3
$@ or not $msg
l!l&&r!l&&!r
0022
MC/DC0%0/2
$@ or not $msg
$@ not $msg
Truth table33%1/3
A or not(B)
A= $@B= not $msg
ABresult
1X1
011
000
188 if $@ || !$msg;
189  
190 22 my @sig_hdrs = $msg->header('DKIM2-Signature');
191 22 my @mi_hdrs = $msg->header('Message-Instance');
192 22 $res{counts} = { signatures => scalar @sig_hdrs, instances => scalar @mi_hdrs };
193 # A Message-Instance with no signature anywhere is -09 §14's
194 # "PERMERROR: Message-Instance m=<x> is not signed", not an absence of
195 # DKIM2. Reporting it as 'none' is exactly the answer someone pasting such
196 # a message here does not need: it says "nothing to see" about a message
197 # that a conforming receiver will reject.
198 22 ▶ if (!@sig_hdrs && @mi_hdrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @sig_hdrs and @mi_hdrs) 0 22 22
Condition66%2/3
not @sig_hdrs and @mi_hdrs
!ll&&!rl&&r
2020
MC/DC0%0/2
not @sig_hdrs and @mi_hdrs
not @sig_hdrs @mi_hdrs
Truth table66%2/3
not(A) and B
A= not @sig_hdrsB= @mi_hdrs
ABresult
0X0
100
111
199 0 my ($top_mi) = sort { $b <=> $a }
200 0 grep { defined }
201 0 map { extract_mi_version($_) } @mi_hdrs;
202 0 ▶ $top_mi //= '?';
Condition0%0/2
$top_mi //= "?"
l!l
00
MC/DC0%0/1
$top_mi //= "?"
$top_mi
Truth table0%0/2
A
A= $top_mi
Aresult
11
00
203 0 return { %res, overall => 'fail',
204 summary => "PERMERROR: Message-Instance m=$top_mi is not signed" };
205 }
206  
207 22 ▶ return { %res, overall => 'none', summary => 'NONE: No DKIM2 header fields found' }
Branch: 1 branch
BranchTrueFalseTotal
unless @sig_hdrs 2 20 22
208 unless @sig_hdrs;
209  
210 # Overall verdict from the full verifier (includes the §14.7 deep MI walk).
211 # Timestamp age is graded per-signature as a soft 'warn' below, so it must
212 # not hard-fail the crypto verdict here.
213 20 my $v = Mail::DKIM2::Verifier->new(PolicyCallback => $POLICY_CB);
214 20 $v->skip_timestamp_check(1);
215 20 $v->set_pubkey_callback($cb);
216 20 eval { $v->PRINT($text); $v->CLOSE; 1 };
217 20 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 1 19 20
218 19 ▶ my $ov = $v->result // 'fail';
Condition50%1/2
$v->result // "fail"
l!l
190
MC/DC0%0/1
$v->result // "fail"
$v->result
Truth table50%1/2
A
A= $v->result
Aresult
11
00
219 19 ▶ $res{summary} = $v->result_detail // '';
Condition50%1/2
$v->result_detail // ""
l!l
190
MC/DC0%0/1
$v->result_detail // ""
$v->result_detail
Truth table50%1/2
A
A= $v->result_detail
Aresult
11
00
220 19 ▶ $res{overall} = $ov eq 'pass' ? 'pass' : ($ov eq 'none' ? 'none' : 'fail');
Branch: 2 branches
BranchTrueFalseTotal
$ov eq 'pass' ? : 7 12 19
$ov eq 'none' ? : 0 12 12
221 # Deprecated (0.29): a null body Recipe is now a PERMERROR (-09 contract
222 # E2), so this is true exactly for a pass.
223 19 $res{fully_verified} = $v->fully_verified;
224  
225 # The walk below parses the message again for every level, so it is
226 # bounded by the chain cap like everything else: over it, the
227 # Verifier's PERMERROR is the whole report (reported against 0.34,
228 # CVE-2026-129849).
229 19 ▶ if (Mail::DKIM2::Common::chain_length_error(
Branch: 1 branch
BranchTrueFalseTotal
if (Mail::DKIM2::Common::chain_length_error({"message-instance", scalar @mi_hdrs, "dkim2-signature", scalar @sig_hdrs})) 1 18 19
230 { 'message-instance' => scalar @mi_hdrs,
231 'dkim2-signature' => scalar @sig_hdrs })) {
232 1 $res{levels} = [];
233 1 return \%res;
234 }
235  
236 18 my %sig_by_i = map { _i($_) => $_ } @sig_hdrs;
237  
238 # Descend the chain top-down. At each step: verify every signature whose
239 # m= equals the current top Message-Instance (content is at the right level
240 # for them), then record that MI level and undo one step. Records a level
241 # for every signature and every MI, in chain order (top hop first).
242 18 my @levels;
243 18 my $work = parse_mime($text);
244 18 my $stopped;
245 # Set once the walk has crossed an instance with a null body Recipe: the
246 # body below it is lost, so lower levels are judged on their header
247 # hashes alone, undoing header Recipes only (as Verifier.pm does).
248 18 my $hdr_only = 0;
249  
250 18 while (1) {
251 27 my %sigmap = map { _i($_) => $_ } $work->header('DKIM2-Signature');
252 27 my %mimap = map { extract_mi_version($_) => $_ } $work->header('Message-Instance');
253 27 ▶ my $num = %sigmap ? max(keys %sigmap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%sigmap ? : 27 0 27
254 27 ▶ my $inst = %mimap ? max(keys %mimap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%mimap ? : 27 0 27
255 27 ▶ last if !$num && !$inst;
Branch: 1 branch
BranchTrueFalseTotal
unless $num or $inst 0 27 27
Condition33%1/3
$num or $inst
l!l&&r!l&&!r
2700
MC/DC0%0/2
$num or $inst
$num $inst
Truth table33%1/3
A or B
A= $numB= $inst
ABresult
1X1
011
000
256  
257 # Signatures covering the current top MI (highest i first).
258 27 ▶ while ($num > 0 && _m($sigmap{$num}) == $inst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0 and _m($sigmap{$num}) == $inst) 35 27 62
Condition100%3/3
$num > 0 and _m($sigmap{$num}) == $inst
!ll&&!rl&&r
18935
MC/DC100%2/2
$num > 0 and _m($sigmap{$num}) == $inst
$num > 0 _m($sigmap{$num}) == $inst
Truth table100%3/3
A and B
A= $num > 0B= _m($sigmap{$num}) == $inst
ABresult
0X0
100
111
259 35 push @levels, _sig_level($work, $num, \%sig_by_i, $cb, $skip_ts, $hdr_only);
260 $work->header_raw_set('DKIM2-Signature',
261 35 grep { _i($_) < $num } $work->header('DKIM2-Signature'));
262 35 %sigmap = map { _i($_) => $_ } $work->header('DKIM2-Signature');
263 35 ▶ $num = %sigmap ? max(keys %sigmap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%sigmap ? : 17 18 35
264 }
265  
266 27 ▶ last unless $inst; # no more MIs to record
Branch: 1 branch
BranchTrueFalseTotal
unless $inst 0 27 27
267  
268 27 my $lvl = _mi_level($work, $inst, $mimap{$inst}, $hdr_only);
269 27 push @levels, $lvl;
270  
271 27 ▶ last if $inst <= 1; # base instance recorded; done
Branch: 1 branch
BranchTrueFalseTotal
if $inst <= 1 18 9 27
272 9 ▶ if ($lvl->{undo} eq 'unrecoverable') {
Branch: 2 branches
BranchTrueFalseTotal
if ($lvl->{'undo'} eq 'unrecoverable') { } 3 6 9
elsif ($lvl->{'undo'} eq 'clean') { } 6 0 6
273 # Null body Recipe: keep going on the header history only. A
274 # header Recipe that will not apply stops the walk here.
275 3 $hdr_only = 1;
276 3 my $prev = eval { Mail::DKIM2::MessageInstance->undo($work, HeadersOnly => 1) };
277 3 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 3 3
278 3 ▶ unless ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev) 0 3 3
279 0 $stopped = "stopped below m=$inst (header history did not undo)";
280 0 last;
281 }
282 3 $work = parse_mime($work->as_string);
283 } elsif ($lvl->{undo} eq 'clean') {
284 6 Mail::DKIM2::MessageInstance->undo($work, HeadersOnly => $hdr_only);
285 6 $work = parse_mime($work->as_string); # reset Email::MIME caches
286 } else {
287 0 $stopped = "stopped below m=$inst ($lvl->{undo})";
288 0 last;
289 }
290 }
291  
292 18 ▶ $res{summary} = ($res{summary} ? "$res{summary}; " : '') . $stopped if $stopped;
Branch: 2 branches
BranchTrueFalseTotal
if $stopped 0 18 18
$res{'summary'} ? : 0 0 0
293  
294 # An old/future timestamp is a soft warning, not a hard fail: if the chain
295 # otherwise verified, surface it as an amber 'warn' overall.
296 18 ▶ if ($res{overall} eq 'pass'
Branch: 1 branch
BranchTrueFalseTotal
if ($res{"overall"} eq "pass" and grep {($_->{"result"} // "") eq "warn";} @levels) 1 17 18
Condition100%3/3
$res{"overall"} eq "pass" and grep {($_->{"result"} // "") eq "warn";} @levels
!ll&&!rl&&r
1161
MC/DC100%2/2
$res{"overall"} eq "pass" and grep {($_->{"result"} // "") eq "warn";} @levels
$res{"overall"} eq "pass" grep {($_->{"result"} // "") eq "warn";} @levels
Truth table100%3/3
A and B
A= $res{"overall"} eq "pass"B= grep {($_->{"result"} // "") eq "warn";} @levels
ABresult
0X0
100
111
297 25 ▶ && grep { ($_->{result} // '') eq 'warn' } @levels) {
Condition50%1/2
$_->{"result"} // ""
l!l
250
MC/DC0%0/1
$_->{"result"} // ""
$_->{"result"}
Truth table50%1/2
A
A= $_->{"result"}
Aresult
11
00
298 1 $res{overall} = 'warn';
299 }
300  
301 18 $res{levels} = \@levels;
302 18 return \%res;
303 }
304  
305 # Level hashref for the top Message-Instance m=$inst of $msg.
306 # $hdr_only: below a null body Recipe, so the body is not checkable.
307 27 ▶sub _mi_level {
Subroutine _mi_level: called CC=33 SCAR=36.4
308 27 my ($msg, $inst, $mi_raw, $hdr_only) = @_;
309 27 my %lvl = (kind => 'mi', m => $inst, result => 'fail',
310 header_hash => 'mismatch', body_hash => 'mismatch',
311 recipe => 'none', undo => 'n/a', detail => '',
312 header_recipes => [], body_recipe => 'none');
313 27 my $mi = eval { Mail::DKIM2::MessageInstance->parse($mi_raw) };
314 27 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 27 27
315 0 ▶ unless ($mi) { $lvl{detail} = 'unparseable Message-Instance'; return \%lvl; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($mi) 0 27 27
316  
317 27 $lvl{tags} = _mi_tags($mi);
318  
319 # -09 §3.5/§9.3: mirror MessageInstance::verify()'s semantics here --
320 # every implemented hash-set must match; an MI naming no implemented
321 # algorithm displays as a mismatch (fail-closed), not silently as a
322 # sha256-only "no hash".
323 27 ▶ my $hashes = $mi->get_tag('hashes') || {};
Condition50%1/2
$mi->get_tag('hashes') || {}
l!l
270
MC/DC0%0/1
$mi->get_tag('hashes') || {}
$mi->get_tag('hashes')
Truth table50%1/2
A
A= $mi->get_tag('hashes')
Aresult
11
00
324 27 my $impl = Mail::DKIM2::MessageInstance::hash_algs();
325 27 my @usable = sort grep { $impl->{$_} } keys %$hashes;
326 27 my ($h_match, $b_match) = (0, 0);
327 27 ▶ if (@usable) {
Branch: 1 branch
BranchTrueFalseTotal
if (@usable) 27 0 27
328 27 $h_match = $b_match = 1;
329 27 for my $alg (@usable) {
330 27 my ($h1, $b1) = @{ $hashes->{$alg} };
331 27 my $hd = Mail::DKIM2::MessageInstance::h_digest($msg, $alg);
332 27 my $bd = Mail::DKIM2::MessageInstance::b_digest($msg, $alg);
333 27 ▶ $h_match = 0 unless defined $h1 && $h1 eq $hd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h1 and $h1 eq $hd 1 26 27
Condition66%2/3
defined $h1 and $h1 eq $hd
!ll&&!rl&&r
0126
MC/DC50%1/2
defined $h1 and $h1 eq $hd
defined $h1 $h1 eq $hd
Truth table66%2/3
A and B
A= defined $h1B= $h1 eq $hd
ABresult
0X0
100
111
334 27 ▶ $b_match = 0 unless defined $b1 && $b1 eq $bd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b1 and $b1 eq $bd 7 20 27
Condition66%2/3
defined $b1 and $b1 eq $bd
!ll&&!rl&&r
0720
MC/DC50%1/2
defined $b1 and $b1 eq $bd
defined $b1 $b1 eq $bd
Truth table66%2/3
A and B
A= defined $b1B= $b1 eq $bd
ABresult
0X0
100
111
335 }
336 }
337 27 ▶ $lvl{header_hash} = $h_match ? 'match' : 'mismatch';
Branch: 1 branch
BranchTrueFalseTotal
$h_match ? : 26 1 27
338 27 ▶ $lvl{body_hash} = $hdr_only ? 'not-checked' : $b_match ? 'match' : 'mismatch';
Branch: 2 branches
BranchTrueFalseTotal
$hdr_only ? : 3 24 27
$b_match ? : 20 4 24
339 27 my $rh = $mi->get_tag('rh');
340 27 my $rb = $mi->get_tag('rb');
341 27 ▶ $lvl{recipe} = $mi->unrecoverable ? 'null' : ($rb || $rh) ? 'diff' : 'none';
Branch: 2 branches
BranchTrueFalseTotal
$mi->unrecoverable ? : 3 24 27
$rb || $rh ? : 6 18 24
Condition100%3/3
$rb or $rh
l!l&&r!l&&!r
5118
MC/DC100%2/2
$rb or $rh
$rb $rh
Truth table100%3/3
A or B
A= $rbB= $rh
ABresult
1X1
011
000
342 27 ▶ $lvl{body_recipe} = $mi->unrecoverable ? 'null' : ($rb ? 'diff' : 'none');
Branch: 2 branches
BranchTrueFalseTotal
$mi->unrecoverable ? : 3 24 27
$rb ? : 5 19 24
343  
344 27 ▶ if ($inst <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inst <= 1) { } 18 9 27
elsif ($mi->unrecoverable) { } 3 6 9
345 18 $lvl{undo} = 'n/a';
346 } elsif ($mi->unrecoverable) {
347 3 $lvl{undo} = 'unrecoverable';
348 } else {
349 6 my $clone = parse_mime($msg->as_string);
350 6 ▶ my $ok = eval { Mail::DKIM2::MessageInstance->undo($clone, HeadersOnly => $hdr_only ? 1 : 0) };
Branch: 1 branch
BranchTrueFalseTotal
$hdr_only ? : 0 6 6
351 6 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 6 6
352 6 ▶ $lvl{undo} = ($ok && !$@) ? 'clean' : 'failed';
Branch: 1 branch
BranchTrueFalseTotal
$ok && !$@ ? : 6 0 6
Condition33%1/3
$ok and not $@
!ll&&!rl&&r
006
MC/DC0%0/2
$ok and not $@
$ok not $@
Truth table33%1/3
A and not(B)
A= $okB= not $@
ABresult
0X0
100
111
353 6 ▶ if ($ok && !$@ && $rh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and not $@ and $rh) 6 0 6
Condition33%2/6
$ok and not $@ and $rh
!ll&&!rl&&r
006
$ok and not $@
!ll&&!rl&&r
006
MC/DC0%0/3
$ok and not $@ and $rh
$ok not $@ $rh
Truth table25%1/4
A and not(B) and C
A= $okB= not $@C= $rh
ABCresult
0XX0
10X0
1100
1111
354 6 for my $h (sort keys %$rh) {
355 13 my $cur = join(' / ', $msg->header($h));
356 13 my $prev = join(' / ', $clone->header($h));
357 13 ▶ push @{$lvl{header_recipes}}, {
Branch: 2 branches
BranchTrueFalseTotal
length $cur ? : 13 0 13
length $prev ? : 7 6 13
358 name => $h,
359 current => (length $cur ? $cur : '(absent)'),
360 previous => (length $prev ? $prev : '(absent)'),
361 };
362 }
363 }
364 }
365  
366 $lvl{result} = ($lvl{header_hash} eq 'match'
367 27 ▶ && ($hdr_only || $lvl{body_hash} eq 'match'))
Branch: 1 branch
BranchTrueFalseTotal
$lvl{'header_hash'} eq 'match' && ($hdr_only || $lvl{'body_hash'} eq 'match') ? : 22 5 27
Condition100%6/6
$lvl{"header_hash"} eq "match" and $hdr_only || $lvl{"body_hash"} eq "match"
!ll&&!rl&&r
1422
$hdr_only or $lvl{"body_hash"} eq "match"
l!l&&r!l&&!r
2204
MC/DC100%3/3
$lvl{"header_hash"} eq "match" and $hdr_only || $lvl{"body_hash"} eq "match"
$lvl{"header_hash"} eq "match" $hdr_only $lvl{"body_hash"} eq "match"
Truth table100%4/4
A and B or C
A= $lvl{"header_hash"} eq "match"B= $hdr_onlyC= $lvl{"body_hash"} eq "match"
ABCresult
0XX0
1000
11X1
1011
368 ? 'pass' : 'fail';
369 27 return \%lvl;
370 }
371  
372 # Signature-level hashref for i=$num, verifying the chain prefix on $work.
373 35 ▶sub _sig_level {
Subroutine _sig_level: called CC=54 SCAR=45.0
374 35 my ($work, $num, $sig_by_i, $cb, $skip_ts, $hdr_only) = @_;
375 # $sig_by_i values come from Email::MIME->header() — already bare values.
376 35 my $sig = eval { Mail::DKIM2::Signature->parse($sig_by_i->{$num}) };
377 35 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 35 35
378 35 ▶ my %lvl = (kind => 'signature', i => $num, m => _m($sig_by_i->{$num}),
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 35 0 35
Condition50%1/2
$sig->domain // ""
l!l
350
MC/DC0%0/1
$sig->domain // ""
$sig->domain
Truth table50%1/2
A
A= $sig->domain
Aresult
11
00
379 domain => ($sig ? ($sig->domain // '') : ''),
380 mail_from => '', rcpt_to => [],
381 items => [], timestamp => { ok => 1, detail => '' },
382 custody => { ok => 1, detail => '' }, result => 'fail', detail => '');
383  
384 35 ▶ if ($sig) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig) { } 35 0 35
385 35 $lvl{tags} = _sig_tags($sig);
386 35 ▶ $lvl{mail_from} = $sig->mail_from // '';
Condition100%2/2
$sig->mail_from // ""
l!l
305
MC/DC100%1/1
$sig->mail_from // ""
$sig->mail_from
Truth table100%2/2
A
A= $sig->mail_from
Aresult
11
00
387 35 my $rt = $sig->rcpt_to;
388 35 ▶ $lvl{rcpt_to} = [ ref $rt eq 'ARRAY' ? @$rt : (defined $rt ? ($rt) : ()) ];
Branch: 2 branches
BranchTrueFalseTotal
ref $rt eq 'ARRAY' ? : 30 5 35
defined $rt ? : 0 5 5
389 35 ▶ my $n = $sig->sig_count || 0;
Condition50%1/2
$sig->sig_count || 0
l!l
350
MC/DC0%0/1
$sig->sig_count || 0
$sig->sig_count
Truth table50%1/2
A
A= $sig->sig_count
Aresult
11
00
390 35 for my $idx (0 .. $n - 1) {
391 35 ▶ push @{$lvl{items}}, {
Condition50%2/4
$sig->selector($idx) // ""
l!l
350
$sig->algorithm($idx) // ""
l!l
350
MC/DC0%0/1
$sig->selector($idx) // ""
$sig->selector($idx)
MC/DC0%0/1
$sig->algorithm($idx) // ""
$sig->algorithm($idx)
Truth table50%1/2
A
A= $sig->selector($idx)
Aresult
11
00
Truth table50%1/2
A
A= $sig->algorithm($idx)
Aresult
11
00
392 selector => ($sig->selector($idx) // ''),
393 algorithm => ($sig->algorithm($idx) // ''),
394 };
395 }
396 35 ▶ unless ($skip_ts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_ts) 1 34 35
397 1 my $ts = $sig->timestamp;
398 1 ▶ if (defined $ts && $ts > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ts and $ts > 0) 1 0 1
Condition33%1/3
defined $ts and $ts > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $ts and $ts > 0
defined $ts $ts > 0
Truth table33%1/3
A and B
A= defined $tsB= $ts > 0
ABresult
0X0
100
111
399 1 my $now = time();
400 0 ▶ if ($ts > $now + 86400) { $lvl{timestamp} = { ok => 0, status => 'future', detail => 'timestamp in the future' }; }
Branch: 2 branches
BranchTrueFalseTotal
if ($ts > $now + 86400) { } 0 1 1
elsif ($now > $ts + 1209600) { } 1 0 1
401 1 elsif ($now > $ts + 14*24*3600) { $lvl{timestamp} = { ok => 0, status => 'old', detail => 'older than 14 days' }; }
402 }
403 }
404 35 ▶ if ($num > 1 && $sig_by_i->{$num - 1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 1 and $$sig_by_i{$num - 1}) 17 18 35
Condition66%2/3
$num > 1 and $$sig_by_i{$num - 1}
!ll&&!rl&&r
18017
MC/DC50%1/2
$num > 1 and $$sig_by_i{$num - 1}
$num > 1 $$sig_by_i{$num - 1}
Truth table66%2/3
A and B
A= $num > 1B= $$sig_by_i{$num - 1}
ABresult
0X0
100
111
405 17 my $prev = eval { Mail::DKIM2::Signature->parse($sig_by_i->{$num-1}) };
406 17 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 17 17
407 17 ▶ if ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 17 0 17
408 # -09 section 7.1 item 1, as Verifier.pm checks it, so the
409 # report and the verdict cannot disagree.
410 17 my $p = $num - 1;
411 17 ▶ my $cur_d = $sig->domain // '';
Condition50%1/2
$sig->domain // ""
l!l
170
MC/DC0%0/1
$sig->domain // ""
$sig->domain
Truth table50%1/2
A
A= $sig->domain
Aresult
11
00
412 17 my $prev_nd = $prev->next_domain;
413 17 my $cur_nd = $sig->next_domain;
414 17 my $mf = $sig->mail_from;
415 17 ▶ my @rts = map { [domain_of($_)] } @{ $prev->rcpt_to || [] };
Condition100%2/2
$prev->rcpt_to || []
l!l
134
MC/DC100%1/1
$prev->rcpt_to || []
$prev->rcpt_to
Truth table100%2/2
A
A= $prev->rcpt_to
Aresult
11
00
416 17 ▶ if (defined $prev_nd && length $prev_nd) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $prev_nd and length $prev_nd) { } 4 13 17
elsif (defined $cur_nd and length $cur_nd or defined $mf and $mf =~ /\A<\s*>\z/u) { } 2 11 13
elsif (defined $mf) { } 11 0 11
Condition66%2/3
defined $prev_nd and length $prev_nd
!ll&&!rl&&r
1304
MC/DC50%1/2
defined $prev_nd and length $prev_nd
defined $prev_nd length $prev_nd
Truth table66%2/3
A and B
A= defined $prev_ndB= length $prev_nd
ABresult
0X0
100
111
417 # An nd= hop names the domain that signs the next signature.
418 4 ▶ $lvl{custody} = ((to_alabel($prev_nd) =~ tr/A-Z/a-z/r) eq (to_alabel($cur_d) =~ tr/A-Z/a-z/r))
Branch: 1 branch
BranchTrueFalseTotal
to_alabel($prev_nd) =~ tr/A-Z/a-z/r eq to_alabel($cur_d) =~ tr/A-Z/a-z/r ? : 3 1 4
419 ? { ok => 1, detail => "nd=$prev_nd matches d= of i=$num" }
420 : { ok => 0, detail => "PERMERROR: DKIM2-Signature i=$p nd= does not match" };
421 ▶ } elsif ((defined $cur_nd && length $cur_nd) || (defined $mf && $mf =~ /\A<\s*>\z/)) {
Condition55%5/9
defined $cur_nd and length $cur_nd or defined $mf and $mf =~ /\A<\s*>\z/u
l!l&&r!l&&!r
2011
defined $cur_nd and length $cur_nd
!ll&&!rl&&r
1102
defined $mf and $mf =~ /\A<\s*>\z/u
!ll&&!rl&&r
0110
MC/DC25%1/4
defined $cur_nd and length $cur_nd or defined $mf and $mf =~ /\A<\s*>\z/u
defined $cur_nd length $cur_nd defined $mf $mf =~ /\A<\s*>\z/u
Truth table28%2/7
A and B or C and D
A= defined $cur_ndB= length $cur_ndC= defined $mfD= $mf =~ /\A<\s*>\z/u
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
422 # An nd= hop, or a null mf=: its d= against the rt= domains.
423 2 ▶ my $ok = grep { $_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $cur_d) } @rts;
Condition50%3/6
$_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $cur_d)
!ll&&!rl&&r
011
$_->[0] and not $_->[1]
!ll&&!rl&&r
002
MC/DC33%1/3
$_->[0] && !$_->[1] && relaxed_domain_match($_->[0], $cur_d)
$_->[0] not $_->[1] relaxed_domain_match($_->[0], $cur_d)
Truth table50%2/4
A and not(B) && C
A= $_->[0]B= not $_->[1]C= relaxed_domain_match($_->[0], $cur_d)
ABCresult
0XX0
10X0
1100
1111
424 2 ▶ $lvl{custody} = $ok
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 1 1 2
defined $cur_nd && length $cur_nd ? : 1 0 1
Condition33%1/3
defined $cur_nd and length $cur_nd
!ll&&!rl&&r
001
MC/DC0%0/2
defined $cur_nd and length $cur_nd
defined $cur_nd length $cur_nd
Truth table33%1/3
A and B
A= defined $cur_ndB= length $cur_nd
ABresult
0X0
100
111
425 ? { ok => 1, detail => ((defined $cur_nd && length $cur_nd) ? "nd= hop" : "mf=<> hop") . " d=$cur_d matches rt= of i=$p" }
426 : { ok => 0, detail => "PERMERROR: DKIM2-Signature i=$num d= does not match the rt= of i=$p" };
427 } elsif (defined $mf) {
428 11 my ($mfd, $lit) = domain_of($mf);
429 my $ok = $mfd && !$lit
430 11 ▶ && grep { $_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0]) } @rts;
Condition50%6/12
$mfd && !$lit && grep({$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rts)
!ll&&!rl&&r
0110
$mfd and not $lit
!ll&&!rl&&r
0011
$_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0])
!ll&&!rl&&r
0110
$_->[0] and not $_->[1]
!ll&&!rl&&r
0011
MC/DC33%1/3
$mfd && !$lit && grep({$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rts)
$mfd not $lit grep({$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rts)
MC/DC33%1/3
$_->[0] && !$_->[1] && relaxed_domain_match($mfd, $_->[0])
$_->[0] not $_->[1] relaxed_domain_match($mfd, $_->[0])
Truth table50%2/4
A and not(B) && C
A= $mfdB= not $litC= grep({$_->[0] and not $_->[1] and relaxed_domain_match($mfd, $_->[0]);} @rts)
ABCresult
0XX0
10X0
1100
1111
Truth table50%2/4
A and not(B) && C
A= $_->[0]B= not $_->[1]C= relaxed_domain_match($mfd, $_->[0])
ABCresult
0XX0
10X0
1100
1111
431 11 ▶ $lvl{custody} = $ok ? { ok => 1, detail => '' }
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 10 1 11
432 : { ok => 0, detail => "PERMERROR: DKIM2-Signature i=$num mf= does not match the rt= of i=$p" };
433 }
434 }
435 }
436 } else {
437 0 $lvl{detail} = 'unparseable DKIM2-Signature';
438 }
439  
440 # Timestamp age never hard-fails here; it is graded as a soft warn above.
441 # mid_process: $work is a partial view (higher DKIM2-Signature headers
442 # already stripped for this top-down walk), so its locally-highest i= is
443 # not necessarily the true top of the whole chain. Verifier.pm's top-nd=
444 # rejection must not fire here; the real top-of-chain check is done
445 # separately below against $sig_by_i (the full, original signature set).
446 35 my $vv = Mail::DKIM2::Verifier->new(PolicyCallback => $POLICY_CB);
447 35 $vv->skip_timestamp_check(1);
448 35 $vv->mid_process(1);
449 35 ▶ $vv->headers_only(1) if $hdr_only; # body lost below a null Recipe
Branch: 1 branch
BranchTrueFalseTotal
if $hdr_only 3 32 35
450 35 $vv->set_pubkey_callback($cb);
451 35 eval { $vv->PRINT($work->as_string); $vv->CLOSE; 1 };
452 35 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 35 35
453 35 ▶ my $r = $vv->result // 'fail';
Condition50%1/2
$vv->result // "fail"
l!l
350
MC/DC0%0/1
$vv->result // "fail"
$vv->result
Truth table50%1/2
A
A= $vv->result
Aresult
11
00
454 35 ▶ my $crypto = ($r eq 'pass') ? 'pass' : 'fail';
Branch: 1 branch
BranchTrueFalseTotal
$r eq 'pass' ? : 20 15 35
455 35 $lvl{result} = $crypto;
456 35 ▶ $lvl{detail} = ($vv->result_detail // $lvl{detail}) unless $crypto eq 'pass';
Branch: 1 branch
BranchTrueFalseTotal
unless $crypto eq "pass" 15 20 35
Condition33%1/3
$vv->result_detail // $lvl{"detail"}
l!l&&r!l&&!r
1500
MC/DC0%0/2
$vv->result_detail // $lvl{"detail"}
$vv->result_detail $lvl{"detail"}
Truth table33%1/3
A // B
A= $vv->result_detailB= $lvl{"detail"}
ABresult
1X1
011
000
457 # Crypto verified but timestamp old/future -> soft amber warning.
458 35 ▶ $lvl{result} = 'warn' if $crypto eq 'pass' && !$lvl{timestamp}{ok};
Branch: 1 branch
BranchTrueFalseTotal
if $crypto eq "pass" and not $lvl{"timestamp"}{"ok"} 1 34 35
Condition100%3/3
$crypto eq "pass" and not $lvl{"timestamp"}{"ok"}
!ll&&!rl&&r
15191
MC/DC100%2/2
$crypto eq "pass" and not $lvl{"timestamp"}{"ok"}
$crypto eq "pass" not $lvl{"timestamp"}{"ok"}
Truth table100%3/3
A and not(B)
A= $crypto eq "pass"B= not $lvl{"timestamp"}{"ok"}
ABresult
0X0
100
111
459 35 $_->{result} = $crypto for @{$lvl{items}};
460  
461 # Local policy (-09 section 14.4 allows a top nd= only by arrangement): the
462 # highest-numbered DKIM2-Signature in the *whole* chain MUST NOT carry
463 # nd= (mirrors the Verifier.pm permerror from Task 2.1). $sig_by_i is the
464 # original, unmodified full signature set for every call in this walk,
465 # so max(keys %$sig_by_i) is the true top-of-chain i=, not merely the
466 # top of this step's partial $work view (a lower, legitimately-nd=
467 # hop can look locally "topmost" once higher signatures are stripped
468 # for the walk; that must not trip this check). Checked last so it
469 # always wins over the partial-view crypto verdict above.
470 35 ▶ if ($sig && $num == max(keys %$sig_by_i)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig and $num == &max(keys %$sig_by_i)) 18 17 35
Condition66%2/3
$sig and $num == &max(keys %$sig_by_i)
!ll&&!rl&&r
01718
MC/DC50%1/2
$sig and $num == &max(keys %$sig_by_i)
$sig $num == &max(keys %$sig_by_i)
Truth table66%2/3
A and B
A= $sigB= $num == &max(keys %$sig_by_i)
ABresult
0X0
100
111
471 18 my $nd = $sig->next_domain;
472 18 ▶ if (defined $nd && length $nd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nd and length $nd) 1 17 18
Condition66%2/3
defined $nd and length $nd
!ll&&!rl&&r
1701
MC/DC50%1/2
defined $nd and length $nd
defined $nd length $nd
Truth table66%2/3
A and B
A= defined $ndB= length $nd
ABresult
0X0
100
111
473 1 $lvl{result} = 'fail';
474 1 $lvl{detail} = "DKIM2-Signature i=$num unexpected nd= tag";
475 }
476 }
477  
478 35 return \%lvl;
479 }
480  
481 9 1;
482  
483 __END__
« bin/dkim2-milter ↑ Summary lib/Mail/Milter/Authentication/Handler/DKIM2Sign.pm »