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 # The bytes as given, not Email::MIME's re-serialisation of them: that
50 # rewrites a header section it cannot parse (a first line starting with
51 # WSP vanished), and the Verifier must judge what arrived.
52 19 $verifier->PRINT($data);
53 19 $verifier->CLOSE;
54 19 ▶ unless ($verifier->result =~ /\A(?:pass|none)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($verifier->result =~ /\A(?:pass|none)\z/u) 15 4 19
55 15 ▶ say STDERR $verifier->details if $verifier->details;
Branch: 1 branch
BranchTrueFalseTotal
if $verifier->details 15 0 15
56 15 die "DKIM2: " . $verifier->result_detail() . "\n";
57 }
58 }
59  
60 # Header-level PERMERRORs, as the Verifier reports them. The walk below
61 # is driven by the i= and m= values it can read, so without this a junk
62 # DKIM2-Signature with no usable i= -- even the only one -- would simply never
63 # be visited, and an i= or m= above MAX_CHAIN_LENGTH would be a loop bound.
64 4 for my $h ($msg1->header_raw('DKIM2-Signature')) {
65 8 my $sig = eval { Mail::DKIM2::Signature->parse($h) };
66 8 ▶ my $i = $sig ? $sig->sequence : undef;
Branch: 1 branch
BranchTrueFalseTotal
$sig ? : 8 0 8
67 8 ▶ die unkeyable_signature_error($h) . "\n" unless valid_sequence($i);
Branch: 1 branch
BranchTrueFalseTotal
unless valid_sequence($i) 0 8 8
68 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
69 // chain_number_error('DKIM2-Signature', 'm', $sig->version, $i);
70 8 ▶ die "$e\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 8 8
71 }
72 4 for my $h ($msg1->header_raw('Message-Instance')) {
73 5 my $e = chain_number_error('Message-Instance', 'm', mi_version_tag($h));
74 5 ▶ die "$e\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 5 5
75 }
76  
77 4 my %map = map { _geti($_) => $_ } $msg1->header('DKIM2-Signature');
78 4 ▶my $num = %map ? max(keys %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 4 0 4
79 4 my %mimap = map { extract_mi_version($_) => $_ } $msg1->header('Message-Instance');
80 4 ▶my $instance = %mimap ? max(keys %mimap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%mimap ? : 4 0 4
81  
82 # The true top of the chain, remembered before the walk below starts stripping
83 # signatures off $msg1. Every step after the first verifies a PARTIAL view, in
84 # which the locally-highest i= is not the real top -- see mid_process below.
85 4 my $top_i = $num;
86  
87 # -09 §14: "there MUST NOT be a Message-Instance field with a higher m=
88 # value than occurs in any DKIM2-Signature field" -- reported as "PERMERROR
89 # Message-Instance m=<x> is not signed". Checked up front because the walk
90 # below happily verifies and reports "OK Message-Instance" for an instance
91 # above every signature, which is precisely the unaccountable instance the
92 # rule exists to reject. This tool is a conformance checker, so it is strict
93 # even though our own inbound path stamps an unsigned MI internally.
94 4 ▶if ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) 4 0 4
95 4 ▶ my $top_signed = %map ? max(map { _getv($_) } values %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 4 0 4
96 4 ▶ die "PERMERROR: Message-Instance m=$instance is not signed\n"
Branch: 1 branch
BranchTrueFalseTotal
if $instance > $top_signed 0 4 4
97 if $instance > $top_signed;
98 }
99  
100 # Set once past an instance with a null body Recipe: the body below it is
101 # lost, so lower levels check header hashes only, undoing header Recipes only.
102 4 my $hdr_only = 0;
103  
104 4 while (1) {
105 12 ▶ my $hi = $num ? _getv($map{$num}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$num ? : 8 4 12
106 12 ▶ while ($instance > $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance > $hi) 5 8 13
107 5 my ($check, $error) = Mail::DKIM2::MessageInstance->verify($msg1, HeadersOnly => $hdr_only);
108 5 ▶ die "ERROR: failed to verify instance $instance: $error\n" unless $check;
Branch: 1 branch
BranchTrueFalseTotal
unless $check 0 5 5
109 5 ▶ die "DIDN'T FIND TOP $instance <> $check" unless $instance == $check;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance == $check 0 5 5
110 5 say "OK Message-Instance: m=$check";
111 5 my $mi = Mail::DKIM2::MessageInstance->parse($mimap{$instance});
112 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
113 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
114 # Email::MIME keeps internal caches which get broken by replacing the body
115 5 $instance--;
116 5 ▶ last unless $instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance 4 1 5
117 1 $msg1 = parse_mime($msg1->as_string);
118 1 %mimap = map { extract_mi_version($_) => $_ } $msg1->header('Message-Instance');
119 0 %map = map { _geti($_) => $_ } $msg1->header('DKIM2-Signature');
120 1 ▶ my $newnum = %map ? max(keys %map) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%map ? : 0 1 1
121 1 ▶ my $newinstance = %mimap ? max(keys %mimap) : 0;
Branch: 1 branch
BranchTrueFalseTotal
%mimap ? : 1 0 1
122 1 ▶ die "MISMATCH TOP DKIM" unless $num == $newnum;
Branch: 1 branch
BranchTrueFalseTotal
unless $num == $newnum 0 1 1
123 1 ▶ die "MISMATCH TOP VERSION $instance <> $newinstance" unless $instance == $newinstance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance == $newinstance 0 1 1
124 1 ▶ die "NO SUCH Message-Instance m=$instance" unless $mimap{$instance};
Branch: 1 branch
BranchTrueFalseTotal
unless $mimap{$instance} 0 1 1
125 }
126 12 ▶ last unless $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $num 4 8 12
127 8 my $h = $map{$num};
128 8 ▶ die "NO SUCH DKIM2-Header i=$num" unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 8 8
129  
130 # Create a verifier for this specific signature
131 8 my $verifier = Mail::DKIM2::Verifier->new();
132 8 ▶ $verifier->skip_timestamp_check(1) if $ignore_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_ts 8 0 8
133 # After the first step this is a partial view (higher DKIM2-Signature
134 # headers have been stripped), so its locally-highest i= is not the real
135 # top of the chain and Verifier.pm's top-nd= rejection must not fire: a
136 # legitimate §12.3 nd= bridge below the top looks locally topmost here.
137 # The first step still sees the whole chain, so a true top nd= is caught.
138 8 ▶ $verifier->mid_process(1) if $num < $top_i;
Branch: 1 branch
BranchTrueFalseTotal
if $num < $top_i 4 4 8
139 8 ▶ $verifier->headers_only(1) if $hdr_only;
Branch: 1 branch
BranchTrueFalseTotal
if $hdr_only 0 8 8
140 8 ▶ $verifier->set_pubkey_callback(sub { find_key(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
141 0 ▶ $verifier->set_policy_callback(sub { find_policy(@_) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
142 8 $verifier->PRINT($msg1->as_string());
143 8 $verifier->CLOSE;
144  
145 8 ▶ if ($verifier->result eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($verifier->result eq 'pass') { } 8 0 8
146 8 say "OK DKIM2-Signature: i=$num; m=$instance";
147 } else {
148 # The primary error (contract C9) on a line of its own, first, then
149 # the result word with it.
150 0 ▶ say STDERR $verifier->details if $verifier->details;
Branch: 1 branch
BranchTrueFalseTotal
if $verifier->details 0 0 0
151 0 die "DKIM2-Signature i=$num: " . $verifier->result_detail() . "\n";
152 }
153 8 $msg1->header_raw_set('DKIM2-Signature', grep { _geti($_) < $num } $msg1->header('DKIM2-Signature'));
154 8 $num--;
155 }
156  
157 # i= and m= of a DKIM2-Signature, read with the tag-list parser so FWS around
158 # "=" (which the syntax allows) is no obstacle. Both are already known to be
159 # in range (checked above).
160 22 ▶sub _geti { return _sigtag(shift, 'sequence') }
Subroutine _geti: called CC=1 SCAR=0.0
161 16 ▶sub _getv { return _sigtag(shift, 'version') }
Subroutine _getv: called CC=1 SCAR=0.0
162  
163 38 ▶sub _sigtag {
Subroutine _sigtag: called CC=4 SCAR=15.2
164 38 my ($arg, $tag) = @_;
165 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
166 38 my $v = $sig->$tag;
167 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
168 }
169  
170 # The Domain Policy Record (-09 section 3.8) from the same dns.json: the TXT
171 # strings at _domainkey.<domain>, none for a domain it does not list.
172 0 ▶sub find_policy {
Subroutine find_policy: not called CC=3 SCAR=24.8
173 0 my ($dom) = @_;
174 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
175 }
176  
177 22 ▶sub find_key {
Subroutine find_key: called CC=6 SCAR=18.7
178 22 my ($signature, $idx) = @_;
179 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
180 22 my $sel = $signature->selector($idx);
181 22 my $dom = $signature->domain;
182 22 ▶ my $records = $dns->{$dom}{"$sel._domainkey"} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $records = $dns->{$dom}{"$sel._domainkey"} or return 22 0 22
183 # As the Verifier's own DNS fetch does (-09 sections 3.7, 14.5): records
184 # beginning v= other than DKIM1 are ignored; no record is "does not
185 # exist"; one that is there but unusable is a PERMERROR with its reason
186 # ("is malformed", "has been revoked", ...).
187 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
188 22 ▶ die "PERMERROR: has multiple records\n" if @txt > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @txt > 1 0 22 22
189 22 my ($key, $why) = parse_dkim_key_record($txt[0]);
190 22 ▶ die "PERMERROR: $why\n" unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 22 22
191 22 return $key;
192 }
« lib/Mail/DKIM2/Util.pm ↑ Summary bin/dkim2verify »