bin/validate.pl

filter: Statementstmt 93.7% 134 / 143 Branchbran 57.6% 45 / 78 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.7% 18 / 21 Podpod n/a totaltotal 74.6% 203 / 272 CC 11 SCAR 12.8
CC
11
Cov
77%
find_policy
12.0
find_key
6.5
_sigtag
4.6
CRAP 3.6
« lib/Mail/DKIM2/Util.pm ↑ Summary bin/dkim2verify »
1 #!/usr/bin/perl -w
2  
3 19 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use Mail::DKIM2::Common qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 extract_mi_version
9 parse_mime
10 valid_sequence
11 chain_number_error
12 mi_version_tag
13 unkeyable_signature_error
14 19 );
15 19 ▶use Mail::DKIM2::Key qw(parse_dkim_key_record);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use Mail::DKIM2::Signature;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Mail::DKIM2::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 19 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 19 my $ignore_ts = 0;
24 19 my $dns_json;
25 19 ▶GetOptions('ignore-timestamps' => \$ignore_ts, 'dns-json=s' => \$dns_json)
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("ignore-timestamps", \$ignore_ts, "dns-json=s", \$dns_json) or die "usage: $0 [--ignore-timestamps] [--dns-json FILE] <file>\n" 19 0 19
26 or die "usage: $0 [--ignore-timestamps] [--dns-json FILE] <file>\n";
27 # Keys come from a dns.json (interop test fixture), never live DNS: this is the
28 # cross-implementation checker, not the production verifier. Default to the
29 # copy shipped with the tests, then the interop repository's shared file.
30 $dns_json //= $ENV{DKIM2_DNS_JSON}
31 19 ▶ // (-e 't/data/dns.json' ? 't/data/dns.json' : '../dns.json');
Branch: 1 branch
BranchTrueFalseTotal
-e 't/data/dns.json' ? : 19 0 19
Condition50%2/4
$dns_json //= $ENV{"DKIM2_DNS_JSON"} // (-e "t/data/dns.json" ? "t/data/dns.json" : "../dns.json")
l!l
019
$ENV{"DKIM2_DNS_JSON"} // (-e "t/data/dns.json" ? "t/data/dns.json" : "../dns.json")
l!l
019
MC/DC0%0/1
$dns_json //= $ENV{"DKIM2_DNS_JSON"} // (-e "t/data/dns.json" ? "t/data/dns.json" : "../dns.json")
$dns_json
Truth table50%1/2
A
A= $dns_json
Aresult
11
00
32  
33 19 my $f1 = shift;
34 19 my $data = path($f1)->slurp;
35 19 $data =~ s/\r//gs;
36 19 $data =~ s/\n/\r\n/gs;
37 19 my $msg1 = parse_mime($data);
38  
39 19 my $dns = decode_json(path($dns_json)->slurp);
40  
41 # The whole message through the Verifier first, so a message with several
42 # faults reports the primary error in the library's order (contract C9),
43 # not the order the walk below happens to meet them.
44 {
45 19 my $verifier = Mail::DKIM2::Verifier->new();
46 19 ▶ $verifier->skip_timestamp_check(1) if $ignore_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_ts 18 1 19
47 19 ▶ $verifier->set_pubkey_callback(sub { find_key(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
48 0 ▶ $verifier->set_policy_callback(sub { find_policy(@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
49 19 $verifier->PRINT($msg1->as_string());
50 19 $verifier->CLOSE;
51 19 ▶ unless ($verifier->result =~ /\A(?:pass|none)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($verifier->result =~ /\A(?:pass|none)\z/u) 15 4 19
52 15 ▶ say STDERR $verifier->details if $verifier->details;
Branch: 1 branch
BranchTrueFalseTotal
if $verifier->details 15 0 15
53 15 die "DKIM2: " . $verifier->result_detail() . "\n";
54 }
55 }
56  
57 # Header-level PERMERRORs, as the Verifier reports them. The walk below
58 # is driven by the i= and m= values it can read, so without this a junk
59 # DKIM2-Signature with no usable i= -- even the only one -- would simply never
60 # be visited, and an i= or m= above MAX_CHAIN_LENGTH would be a loop bound.
61 4 for my $h ($msg1->header_raw('DKIM2-Signature')) {
62 8 my $sig = eval { Mail::DKIM2::Signature->parse($h) };
63 8 ▶ my $i = $sig ? $sig->sequence : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 8 0 8
64 8 ▶ die unkeyable_signature_error($h) . "\n" unless valid_sequence($i);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_sequence($i) 0 8 8
65 8 ▶ my $e = chain_number_error('DKIM2-Signature', 'i', $i)
Condition33%1/3
chain_number_error("DKIM2-Signature", "i", $i) // chain_number_error("DKIM2-Signature", "m", $sig->version, $i)
l!l&&r!l&&!r
008
MC/DC0%0/2
chain_number_error("DKIM2-Signature", "i", $i) // chain_number_error("DKIM2-Signature", "m", $sig->version, $i)
chain_number_error("DKIM2-Signature", "i", $i) chain_number_error("DKIM2-Signature", "m", $sig->version, $i)
Truth table33%1/3
A // B
A= chain_number_error("DKIM2-Signature", "i", $i)B= chain_number_error("DKIM2-Signature", "m", $sig->version, $i)
ABresult
1X1
011
000
66 // chain_number_error('DKIM2-Signature', 'm', $sig->version, $i);
67 8 ▶ die "$e\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 8 8
68 }
69 4 for my $h ($msg1->header_raw('Message-Instance')) {
70 5 my $e = chain_number_error('Message-Instance', 'm', mi_version_tag($h));
71 5 ▶ die "$e\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 5 5
72 }
73  
74 4 my %map = map { _geti($_) => $_ } $msg1->header('DKIM2-Signature');
75 4 ▶my $num = %map ? max(keys %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 4 0 4
76 4 my %mimap = map { extract_mi_version($_) => $_ } $msg1->header('Message-Instance');
77 4 ▶my $instance = %mimap ? max(keys %mimap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%mimap ? : 4 0 4
78  
79 # The true top of the chain, remembered before the walk below starts stripping
80 # signatures off $msg1. Every step after the first verifies a PARTIAL view, in
81 # which the locally-highest i= is not the real top -- see mid_process below.
82 4 my $top_i = $num;
83  
84 # -09 §14: "there MUST NOT be a Message-Instance field with a higher m=
85 # value than occurs in any DKIM2-Signature field" -- reported as "PERMERROR
86 # Message-Instance m=<x> is not signed". Checked up front because the walk
87 # below happily verifies and reports "OK Message-Instance" for an instance
88 # above every signature, which is precisely the unaccountable instance the
89 # rule exists to reject. This tool is a conformance checker, so it is strict
90 # even though our own inbound path stamps an unsigned MI internally.
91 4 ▶if ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) 4 0 4
92 4 ▶ my $top_signed = %map ? max(map { _getv($_) } values %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 4 0 4
93 4 ▶ die "PERMERROR: Message-Instance m=$instance is not signed\n"
Branch: 1 branch
BranchTrueFalseTotal
if $instance > $top_signed 0 4 4
94 if $instance > $top_signed;
95 }
96  
97 # Set once past an instance with a null body Recipe: the body below it is
98 # lost, so lower levels check header hashes only, undoing header Recipes only.
99 4 my $hdr_only = 0;
100  
101 4 while (1) {
102 12 ▶ my $hi = $num ? _getv($map{$num}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$num ? : 8 4 12
103 12 ▶ while ($instance > $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance > $hi) 5 8 13
104 5 my ($check, $error) = Mail::DKIM2::MessageInstance->verify($msg1, HeadersOnly => $hdr_only);
105 5 ▶ die "ERROR: failed to verify instance $instance: $error\n" unless $check;
Branch: 1 branch
BranchTrueFalseTotal
unless $check 0 5 5
106 5 ▶ die "DIDN'T FIND TOP $instance <> $check" unless $instance == $check;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance == $check 0 5 5
107 5 say "OK Message-Instance: m=$check";
108 5 my $mi = Mail::DKIM2::MessageInstance->parse($mimap{$instance});
109 5 ▶ $hdr_only = 1 if $mi && $mi->unrecoverable;
Branch: 1 branch
BranchTrueFalseTotal
if $mi and $mi->unrecoverable 0 5 5
Condition33%1/3
$mi and $mi->unrecoverable
!ll&&!rl&&r
050
MC/DC0%0/2
$mi and $mi->unrecoverable
$mi $mi->unrecoverable
Truth table33%1/3
A and B
A= $miB= $mi->unrecoverable
ABresult
0X0
100
111
110 5 ▶ die "Failed to undo" unless Mail::DKIM2::MessageInstance->undo($msg1, HeadersOnly => $hdr_only);
Branch: 1 branch
BranchTrueFalseTotal
unless "Mail::DKIM2::MessageInstance"->undo($msg1, "HeadersOnly", $hdr_only) 0 5 5
111 # Email::MIME keeps internal caches which get broken by replacing the body
112 5 $instance--;
113 5 ▶ last unless $instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance 4 1 5
114 1 $msg1 = parse_mime($msg1->as_string);
115 1 %mimap = map { extract_mi_version($_) => $_ } $msg1->header('Message-Instance');
116 0 %map = map { _geti($_) => $_ } $msg1->header('DKIM2-Signature');
117 1 ▶ my $newnum = %map ? max(keys %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 0 1 1
118 1 ▶ my $newinstance = %mimap ? max(keys %mimap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%mimap ? : 1 0 1
119 1 ▶ die "MISMATCH TOP DKIM" unless $num == $newnum;
Branch: 1 branch
BranchTrueFalseTotal
unless $num == $newnum 0 1 1
120 1 ▶ die "MISMATCH TOP VERSION $instance <> $newinstance" unless $instance == $newinstance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance == $newinstance 0 1 1
121 1 ▶ die "NO SUCH Message-Instance m=$instance" unless $mimap{$instance};
Branch: 1 branch
BranchTrueFalseTotal
unless $mimap{$instance} 0 1 1
122 }
123 12 ▶ last unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 4 8 12
124 8 my $h = $map{$num};
125 8 ▶ die "NO SUCH DKIM2-Header i=$num" unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 8 8
126  
127 # Create a verifier for this specific signature
128 8 my $verifier = Mail::DKIM2::Verifier->new();
129 8 ▶ $verifier->skip_timestamp_check(1) if $ignore_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_ts 8 0 8
130 # After the first step this is a partial view (higher DKIM2-Signature
131 # headers have been stripped), so its locally-highest i= is not the real
132 # top of the chain and Verifier.pm's top-nd= rejection must not fire: a
133 # legitimate §12.3 nd= bridge below the top looks locally topmost here.
134 # The first step still sees the whole chain, so a true top nd= is caught.
135 8 ▶ $verifier->mid_process(1) if $num < $top_i;
Branch: 1 branch
BranchTrueFalseTotal
if $num < $top_i 4 4 8
136 8 ▶ $verifier->headers_only(1) if $hdr_only;
Branch: 1 branch
BranchTrueFalseTotal
if $hdr_only 0 8 8
137 8 ▶ $verifier->set_pubkey_callback(sub { find_key(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
138 0 ▶ $verifier->set_policy_callback(sub { find_policy(@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
139 8 $verifier->PRINT($msg1->as_string());
140 8 $verifier->CLOSE;
141  
142 8 ▶ if ($verifier->result eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($verifier->result eq 'pass') { } 8 0 8
143 8 say "OK DKIM2-Signature: i=$num; m=$instance";
144 } else {
145 # The primary error (contract C9) on a line of its own, first, then
146 # the result word with it.
147 0 ▶ say STDERR $verifier->details if $verifier->details;
Branch: 1 branch
BranchTrueFalseTotal
if $verifier->details 0 0 0
148 0 die "DKIM2-Signature i=$num: " . $verifier->result_detail() . "\n";
149 }
150 8 $msg1->header_raw_set('DKIM2-Signature', grep { _geti($_) < $num } $msg1->header('DKIM2-Signature'));
151 8 $num--;
152 }
153  
154 # i= and m= of a DKIM2-Signature, read with the tag-list parser so FWS around
155 # "=" (which the syntax allows) is no obstacle. Both are already known to be
156 # in range (checked above).
157 22 ▶sub _geti { return _sigtag(shift, 'sequence') }
Subroutine _geti: called CC=1 SCAR=0.0
158 16 ▶sub _getv { return _sigtag(shift, 'version') }
Subroutine _getv: called CC=1 SCAR=0.0
159  
160 38 ▶sub _sigtag {
Subroutine _sigtag: called CC=4 SCAR=15.2
161 38 my ($arg, $tag) = @_;
162 38 ▶ my $sig = eval { Mail::DKIM2::Signature->parse($arg) } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $sig = eval { do { "Mail::DKIM2::Signature"->parse($arg) } } or return 0 38 0 38
163 38 my $v = $sig->$tag;
164 38 ▶ return (defined $v && $v =~ /\A[0-9]+\z/) ? 0 + $v : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v && $v =~ /\A[0-9]+\z/u ? : 38 0 38
Condition33%1/3
defined $v and $v =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $v and $v =~ /\A[0-9]+\z/u
defined $v $v =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $vB= $v =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
165 }
166  
167 # The Domain Policy Record (-09 section 3.8) from the same dns.json: the TXT
168 # strings at _domainkey.<domain>, none for a domain it does not list.
169 0 ▶sub find_policy {
Subroutine find_policy: not called CC=3 SCAR=24.8
170 0 my ($dom) = @_;
171 0 ▶ return [ map { $_->[1] } @{ $dns->{$dom} && $dns->{$dom}{_domainkey} || [] } ];
Condition0%0/5
$dns->{$dom} && $dns->{$dom}{'_domainkey'} || []
l!l
00
$dns->{$dom} and $dns->{$dom}{"_domainkey"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dns->{$dom} && $dns->{$dom}{'_domainkey'} || []
$dns->{$dom} $dns->{$dom}{"_domainkey"}
Truth table0%0/3
A and B
A= $dns->{$dom}B= $dns->{$dom}{"_domainkey"}
ABresult
111
0X0
100
172 }
173  
174 22 ▶sub find_key {
Subroutine find_key: called CC=6 SCAR=18.7
175 22 my ($signature, $idx) = @_;
176 22 ▶ $idx //= 0;
Condition50%1/2
$idx //= 0
l!l
220
MC/DC0%0/1
$idx //= 0
$idx
Truth table50%1/2
A
A= $idx
Aresult
11
00
177 22 my $sel = $signature->selector($idx);
178 22 my $dom = $signature->domain;
179 22 ▶ my $records = $dns->{$dom}{"$sel._domainkey"} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $records = $dns->{$dom}{"$sel._domainkey"} or return 22 0 22
180 # As the Verifier's own DNS fetch does (-09 sections 3.7, 14.5): records
181 # beginning v= other than DKIM1 are ignored; no record is "does not
182 # exist"; one that is there but unusable is a PERMERROR with its reason
183 # ("is malformed", "has been revoked", ...).
184 22 ▶ my @txt = Mail::DKIM2::Key::key_records(map { $_->[1] } @$records) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@txt) = Mail::DKIM2::Key::key_records(map({$_->[1];} @$records)) or return 22 0 22
185 22 ▶ die "PERMERROR: has multiple records\n" if @txt > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @txt > 1 0 22 22
186 22 my ($key, $why) = parse_dkim_key_record($txt[0]);
187 22 ▶ die "PERMERROR: $why\n" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 22 22
188 22 return $key;
189 }
« lib/Mail/DKIM2/Util.pm ↑ Summary bin/dkim2verify »