lib/Mail/DKIM2/Hop.pm

filter: Statementstmt 98.2% 110 / 112 Branchbran 94.7% 36 / 38 Conditioncond 77.2% 34 / 44 MC/DCmcdc 65.2% 15 / 23 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 90.5% 210 / 232 CC 40 SCAR 26.7
CC
40
Cov
93%
next_instance
24.0
snapshot_vouches
12.2
_foreign_instance
5.0
CRAP 14.5
« lib/Mail/DKIM2/Verifier.pm ↑ Summary lib/Mail/DKIM2/DSN.pm »
1 package Mail::DKIM2::Hop;
2 111 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 111 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 111 our $VERSION = '0.34';
6  
7 111 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 111 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 111 ▶use Mail::DKIM2::Common qw(parse_mime strip_mi_versions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 111 ▶use Mail::DKIM2::Canon qw(check_ignore_prefixes);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 111 ▶use Mail::DKIM2::Chain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 111 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # What one hop does to a message's Message-Instance fields before it signs:
15 # the decision the milter, the authentication_milter handler and dkim2sign
16 # each used to make for themselves.
17  
18 147 ▶sub next_instance {
Subroutine next_instance: called CC=24 SCAR=31.8
Pod documented
19 147 my ($class, $message, %o) = @_;
20 147 Mail::DKIM2::Util::check_options("$class->next_instance", \%o,
21 qw(Snapshot Outbound IgnorePrefixes MaxRecipeLiterals Algs));
22 146 ▶ Carp::croak("next_instance needs a message") unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 1 145 146
Condition66%2/3
defined $message and length $message
!ll&&!rl&&r
01145
MC/DC50%1/2
defined $message and length $message
defined $message length $message
Truth table66%2/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
23 145 check_ignore_prefixes($o{IgnorePrefixes});
24 145 ▶ my @ignore = defined $o{IgnorePrefixes} ? (IgnorePrefixes => $o{IgnorePrefixes}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'IgnorePrefixes'} ? : 47 98 145
25 145 ▶ my @calc = (@ignore, map { defined $o{$_} ? ($_ => $o{$_}) : () } qw(MaxRecipeLiterals Algs));
Branch: 1 branch
BranchTrueFalseTotal
defined $o{$_} ? : 69 221 290
26  
27 145 my %r = (instance => undef, message => $message, removed => [],
28 restart => 0, matched => 0);
29 143 ▶ my $done = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 143 @r{qw(reason detail)} = @_;
31 143 return \%r;
32 145 };
33  
34 145 my $chain = Mail::DKIM2::Chain->from_message($message);
35 145 my $has_mi = $chain->count('Message-Instance');
36 145 my $has_sig = $chain->count('DKIM2-Signature');
37 145 my $msg = parse_mime($message);
38  
39 # Does the top instance still describe the message?
40 145 ▶ $r{matched} = ($has_mi && Mail::DKIM2::MessageInstance->verify($msg, @ignore)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_mi && 'Mail::DKIM2::MessageInstance'->verify($msg, @ignore) ? : 73 72 145
Condition100%3/3
$has_mi and "Mail::DKIM2::MessageInstance"->verify($msg, @ignore)
!ll&&!rl&&r
413173
MC/DC100%2/2
$has_mi and "Mail::DKIM2::MessageInstance"->verify($msg, @ignore)
$has_mi "Mail::DKIM2::MessageInstance"->verify($msg, @ignore)
Truth table100%3/3
A and B
A= $has_miB= "Mail::DKIM2::MessageInstance"->verify($msg, @ignore)
ABresult
0X0
100
111
41  
42 # Outbound, with instances and no signature: every instance is this
43 # host's own and unsigned, and the message leaves with ONE m=1 (-09
44 # section 12.1) -- unless that is what it already has.
45 145 ▶ my $restart = $o{Outbound} && $has_mi && !$has_sig && !($r{matched} && $has_mi == 1);
Condition100%12/12
$o{'Outbound'} && $has_mi && !$has_sig && !($r{'matched'} && $has_mi == 1)
!ll&&!rl&&r
129115
$o{"Outbound"} and $has_mi and not $has_sig
!ll&&!rl&&r
775216
$o{"Outbound"} and $has_mi
!ll&&!rl&&r
601768
$r{'matched'} && $has_mi == 1
!ll&&!rl&&r
691
MC/DC100%5/5
$o{'Outbound'} && $has_mi && !$has_sig && !($r{'matched'} && $has_mi == 1)
$o{"Outbound"} $has_mi not $has_sig $r{'matched'} $has_mi == 1
Truth table100%6/6
A and B and not(C) && not(D && E)
A= $o{"Outbound"}B= $has_miC= not $has_sigD= $r{'matched'}E= $has_mi == 1
ABCDEresult
0XXXX0
10XXX0
110XX0
111110
1110X1
111101
46  
47 return $done->('unchanged', 'the top Message-Instance describes the message')
48 145 ▶ if $r{matched} && !$restart;
Branch: 1 branch
BranchTrueFalseTotal
if $r{"matched"} and not $restart 64 81 145
Condition100%3/3
$r{"matched"} and not $restart
!ll&&!rl&&r
72964
MC/DC100%2/2
$r{"matched"} and not $restart
$r{"matched"} not $restart
Truth table100%3/3
A and not(B)
A= $r{"matched"}B= not $restart
ABresult
0X0
100
111
49  
50 # A chain no verifier can number (the Gate refuses it): add nothing, and
51 # never use one of its numbers as a bound.
52 81 ▶ if (my $e = $chain->numbering_error(Signatures => 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $chain->numbering_error("Signatures", 0)) 1 80 81
53 1 return $done->('bad-chain', "not computing a Message-Instance: $e");
54 }
55  
56 # No instance yet: this hop starts the chain, m=1 over everything.
57 80 ▶ unless ($has_mi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_mi) 41 39 80
58 $r{instance} = Mail::DKIM2::MessageInstance->calculate($msg, undef,
59 41 ▶ @ignore, (defined $o{Algs} ? (Algs => $o{Algs}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'Algs'} ? : 10 31 41
60 39 return $done->('first', 'no Message-Instance yet: m=1');
61 }
62  
63 # What the message was before this hop changed it: the message itself
64 # when its instances already say (a list manager that recorded its own
65 # change), else the snapshot this host took of this very message. Only
66 # that can say what changed; anything else would book someone else's
67 # change as this hop's.
68 39 my $previous;
69 39 ▶ if ($restart && $r{matched}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart and $r{'matched'}) { } 9 30 39
Condition100%3/3
$restart and $r{"matched"}
!ll&&!rl&&r
2469
MC/DC100%2/2
$restart and $r{"matched"}
$restart $r{"matched"}
Truth table100%3/3
A and B
A= $restartB= $r{"matched"}
ABresult
0X0
100
111
70 9 $previous = $message;
71 } else {
72 return $done->('no-snapshot', 'top Message-Instance does not match the '
73 30 ▶ . 'content and no snapshot of ours describes it') unless defined $o{Snapshot};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o{"Snapshot"} 7 23 30
74 23 ▶ if (defined(my $m = _foreign_instance($chain, $o{Snapshot}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $m = _foreign_instance($chain, $o{"Snapshot"}))) 1 22 23
75 1 return $done->('foreign-snapshot',
76 "snapshot does not belong to this message (m=$m differs)");
77 }
78 22 $previous = $o{Snapshot};
79 }
80  
81 31 my @numbers = $chain->instance_numbers;
82 31 ▶ if ($restart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart) 13 18 31
83 # Only a chain that undoes cleanly says what the message was.
84 13 my ($ok, $why) = Mail::DKIM2::MessageInstance->chain_verifies($previous, @ignore);
85 13 ▶ return $done->('bad-chain', "not restarting the unsigned instances as m=1: $why")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 11 13
86 unless $ok;
87 11 $r{instance} = Mail::DKIM2::MessageInstance->calculate_first($message, $previous, @calc);
88 11 @r{qw(removed restart)} = (\@numbers, 1);
89 11 $r{message} = strip_mi_versions($message, @numbers);
90 return $done->('restart', 'restarting unsigned '
91 11 . join(',', map { "m=$_" } @numbers) . ' as one m=1');
92 }
93  
94 # The message has instances the snapshot does not: a hop between added
95 # them, and since the top one does not match they are wrong. Drop them
96 # and describe the change from the snapshot, rather than build on a
97 # broken chain.
98 18 ▶ my $snap_top = Mail::DKIM2::Chain->from_message($previous)->top_instance // 0;
Condition50%1/2
"Mail::DKIM2::Chain"->from_message($previous)->top_instance // 0
l!l
180
MC/DC0%0/1
"Mail::DKIM2::Chain"->from_message($previous)->top_instance // 0
"Mail::DKIM2::Chain"->from_message($previous)->top_instance
Truth table50%1/2
A
A= "Mail::DKIM2::Chain"->from_message($previous)->top_instance
Aresult
11
00
99 18 my $detail = 'the message changed since the snapshot';
100 18 ▶ if ($numbers[-1] > $snap_top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numbers[-1] > $snap_top) 3 15 18
101 3 my @strip = ($snap_top + 1 .. $numbers[-1]);
102 3 $r{removed} = \@strip;
103 3 $r{message} = strip_mi_versions($message, @strip);
104 3 $msg = parse_mime($r{message});
105 3 $detail = 'stripping broken MI versions ' . join(',', map { "m=$_" } @strip)
106 . " (snapshot at m=$snap_top): broken chain detected";
107 }
108 18 $r{instance} = Mail::DKIM2::MessageInstance->calculate($msg, parse_mime($previous), @calc);
109 18 return $done->('changed', $detail);
110 }
111  
112 # snapshot_vouches($message, $snapshot): is the snapshot one of this very
113 # message? True when the message has exactly the snapshot's DKIM2-Signature
114 # fields and, under them, the Message-Instance fields they cover (white space
115 # apart: neither is hashed with it). What a signature was verified over on
116 # arrival is then what is on the message now. Instances above the signatures
117 # are this host's own, and next_instance and the Gate judge those.
118 18 ▶sub snapshot_vouches {
Subroutine snapshot_vouches: called CC=11 SCAR=25.0
Pod documented
119 18 my ($class, $message, $snapshot) = @_;
120 18 ▶ Carp::croak("snapshot_vouches needs a message and a snapshot")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message and defined $snapshot and length $snapshot 0 18 18
Condition33%3/9
defined $message and length $message and defined $snapshot and length $snapshot
!ll&&!rl&&r
0018
defined $message and length $message and defined $snapshot
!ll&&!rl&&r
0018
defined $message and length $message
!ll&&!rl&&r
0018
MC/DC0%0/4
defined $message and length $message and defined $snapshot and length $snapshot
defined $message length $message defined $snapshot length $snapshot
Truth table20%1/5
A and B and C and D
A= defined $messageB= length $messageC= defined $snapshotD= length $snapshot
ABCDresult
0XXX0
10XX0
110X0
11100
11111
121 unless defined $message && length $message && defined $snapshot && length $snapshot;
122 18 my ($msg, $snap) = map { Mail::DKIM2::Chain->from_message($_) } $message, $snapshot;
123 18 for my $c ($msg, $snap) {
124 36 ▶ my $e = $c->numbering_error(Signatures => 1) // next;
Condition50%1/2
$c->numbering_error("Signatures", 1) // (next)
l!l
036
MC/DC0%0/1
$c->numbering_error("Signatures", 1) // (next)
$c->numbering_error("Signatures", 1)
Truth table50%1/2
A
A= $c->numbering_error("Signatures", 1)
Aresult
11
00
125 0 return (0, "the snapshot is not of this message: $e");
126 }
127 18 ▶ my $same = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
128 18 ▶ my ($x, $y) = map { my $v = $_ // ''; $v =~ s/\A[^:]*://; $v =~ s/\s+//g; $v } @_;
Condition100%2/2
$_ // ""
l!l
342
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
129 18 ▶ return length $x && $x eq $y;
Condition100%3/3
length $x && $x eq $y
!ll&&!rl&&r
1413
MC/DC100%2/2
length $x && $x eq $y
length $x $x eq $y
Truth table100%3/3
A && B
A= length $xB= $x eq $y
ABresult
0X0
100
111
130 18 };
131 18 my %i = map { $_ => 1 } $msg->signature_numbers, $snap->signature_numbers;
132 0 for my $i (sort { $a <=> $b } keys %i) {
133 11 ▶ return (0, "the snapshot is not of this message: DKIM2-Signature i=$i differs")
Branch: 1 branch
BranchTrueFalseTotal
unless &$same(scalar $msg->signature_field($i), scalar $snap->signature_field($i)) 4 7 11
134 unless $same->(scalar $msg->signature_field($i), scalar $snap->signature_field($i));
135 }
136 14 my $covered = $snap->covered_m;
137 14 for my $m (grep { $_ <= $covered } $snap->instance_numbers) {
138 7 ▶ return (0, "the snapshot is not of this message: Message-Instance m=$m differs")
Branch: 1 branch
BranchTrueFalseTotal
unless &$same(scalar $msg->instance_field($m), scalar $snap->instance_field($m)) 1 6 7
139 unless $same->(scalar $msg->instance_field($m), scalar $snap->instance_field($m));
140 }
141 13 return (1, undef);
142 }
143  
144 # The lowest m= at which a snapshot's Message-Instance fields are not the
145 # message's own (white space apart), or undef when they all are. A snapshot
146 # whose own fields cannot be numbered is nobody's.
147 23 ▶sub _foreign_instance {
Subroutine _foreign_instance: called CC=5 SCAR=16.2
148 23 my ($chain, $snapshot) = @_;
149 23 my $snap = Mail::DKIM2::Chain->from_message($snapshot);
150 23 ▶ return 0 if $snap->numbering_error(Signatures => 0);
Branch: 1 branch
BranchTrueFalseTotal
if $snap->numbering_error("Signatures", 0) 0 23 23
151 23 for my $m ($snap->instance_numbers) {
152 my ($x, $y) = map {
153 25 ▶ my $v = $_ // '';
Condition50%1/2
$_ // ""
l!l
500
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
154 50 $v =~ s/\A[^:]*://;
155 50 $v =~ s/\s+/ /g;
156 50 $v =~ s/\A | \z//g;
157 50 $v;
158 } $snap->instance_field($m), $chain->instance_field($m);
159 25 ▶ return $m if $x ne $y;
Branch: 1 branch
BranchTrueFalseTotal
if $x ne $y 1 24 25
160 }
161 22 return;
162 }
163  
164 111 1;
165  
166 __END__
« lib/Mail/DKIM2/Verifier.pm ↑ Summary lib/Mail/DKIM2/DSN.pm »