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 107 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 107 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 107 our $VERSION = '0.36';
6  
7 107 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 107 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 107 ▶use Mail::DKIM2::Common qw(parse_mime strip_mi_versions);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 107 ▶use Mail::DKIM2::Canon qw(check_ignore_prefixes);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 107 ▶use Mail::DKIM2::Chain;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 107 ▶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 142 ▶sub next_instance {
Subroutine next_instance: called CC=24 SCAR=31.8
Pod documented
19 142 my ($class, $message, %o) = @_;
20 142 Mail::DKIM2::Util::check_options("$class->next_instance", \%o,
21 qw(Snapshot Outbound IgnorePrefixes MaxRecipeLiterals Algs));
22 141 ▶ Carp::croak("next_instance needs a message") unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 1 140 141
Condition66%2/3
defined $message and length $message
!ll&&!rl&&r
01140
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 140 check_ignore_prefixes($o{IgnorePrefixes});
24 140 ▶ my @ignore = defined $o{IgnorePrefixes} ? (IgnorePrefixes => $o{IgnorePrefixes}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'IgnorePrefixes'} ? : 47 93 140
25 140 ▶ my @calc = (@ignore, map { defined $o{$_} ? ($_ => $o{$_}) : () } qw(MaxRecipeLiterals Algs));
Branch: 1 branch
BranchTrueFalseTotal
defined $o{$_} ? : 69 211 280
26  
27 140 my %r = (instance => undef, message => $message, removed => [],
28 restart => 0, matched => 0);
29 138 ▶ my $done = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 138 @r{qw(reason detail)} = @_;
31 138 return \%r;
32 140 };
33  
34 140 my $chain = Mail::DKIM2::Chain->from_message($message);
35 140 my $has_mi = $chain->count('Message-Instance');
36 140 my $has_sig = $chain->count('DKIM2-Signature');
37 140 my $msg = parse_mime($message);
38  
39 # Does the top instance still describe the message?
40 140 ▶ $r{matched} = ($has_mi && Mail::DKIM2::MessageInstance->verify($msg, @ignore)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$has_mi && 'Mail::DKIM2::MessageInstance'->verify($msg, @ignore) ? : 70 70 140
Condition100%3/3
$has_mi and "Mail::DKIM2::MessageInstance"->verify($msg, @ignore)
!ll&&!rl&&r
403070
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 140 ▶ 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
125114
$o{"Outbound"} and $has_mi and not $has_sig
!ll&&!rl&&r
735215
$o{"Outbound"} and $has_mi
!ll&&!rl&&r
561767
$r{'matched'} && $has_mi == 1
!ll&&!rl&&r
681
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 140 ▶ if $r{matched} && !$restart;
Branch: 1 branch
BranchTrueFalseTotal
if $r{"matched"} and not $restart 62 78 140
Condition100%3/3
$r{"matched"} and not $restart
!ll&&!rl&&r
70862
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 78 ▶ if (my $e = $chain->numbering_error(Signatures => 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $chain->numbering_error("Signatures", 0)) 1 77 78
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 77 ▶ unless ($has_mi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_mi) 40 37 77
58 $r{instance} = Mail::DKIM2::MessageInstance->calculate($msg, undef,
59 40 ▶ @ignore, (defined $o{Algs} ? (Algs => $o{Algs}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $o{'Algs'} ? : 10 30 40
60 38 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 37 my $previous;
69 37 ▶ if ($restart && $r{matched}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart and $r{'matched'}) { } 8 29 37
Condition100%3/3
$restart and $r{"matched"}
!ll&&!rl&&r
2368
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 8 $previous = $message;
71 } else {
72 return $done->('no-snapshot', 'top Message-Instance does not match the '
73 29 ▶ . 'content and no snapshot of ours describes it') unless defined $o{Snapshot};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o{"Snapshot"} 7 22 29
74 22 ▶ if (defined(my $m = _foreign_instance($chain, $o{Snapshot}))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $m = _foreign_instance($chain, $o{"Snapshot"}))) 1 21 22
75 1 return $done->('foreign-snapshot',
76 "snapshot does not belong to this message (m=$m differs)");
77 }
78 21 $previous = $o{Snapshot};
79 }
80  
81 29 my @numbers = $chain->instance_numbers;
82 29 ▶ if ($restart) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restart) 12 17 29
83 # Only a chain that undoes cleanly says what the message was.
84 12 my ($ok, $why) = Mail::DKIM2::MessageInstance->chain_verifies($previous, @ignore);
85 12 ▶ return $done->('bad-chain', "not restarting the unsigned instances as m=1: $why")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 10 12
86 unless $ok;
87 10 $r{instance} = Mail::DKIM2::MessageInstance->calculate_first($message, $previous, @calc);
88 10 @r{qw(removed restart)} = (\@numbers, 1);
89 10 $r{message} = strip_mi_versions($message, @numbers);
90 return $done->('restart', 'restarting unsigned '
91 10 . 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 17 ▶ 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
170
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 17 my $detail = 'the message changed since the snapshot';
100 17 ▶ if ($numbers[-1] > $snap_top) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numbers[-1] > $snap_top) 3 14 17
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 17 $r{instance} = Mail::DKIM2::MessageInstance->calculate($msg, parse_mime($previous), @calc);
109 17 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 16 ▶sub snapshot_vouches {
Subroutine snapshot_vouches: called CC=11 SCAR=25.0
Pod documented
119 16 my ($class, $message, $snapshot) = @_;
120 16 ▶ 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 16 16
Condition33%3/9
defined $message and length $message and defined $snapshot and length $snapshot
!ll&&!rl&&r
0016
defined $message and length $message and defined $snapshot
!ll&&!rl&&r
0016
defined $message and length $message
!ll&&!rl&&r
0016
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 16 my ($msg, $snap) = map { Mail::DKIM2::Chain->from_message($_) } $message, $snapshot;
123 16 for my $c ($msg, $snap) {
124 32 ▶ my $e = $c->numbering_error(Signatures => 1) // next;
Condition50%1/2
$c->numbering_error("Signatures", 1) // (next)
l!l
032
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 17 ▶ my $same = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
128 17 ▶ my ($x, $y) = map { my $v = $_ // ''; $v =~ s/\A[^:]*://; $v =~ s/\s+//g; $v } @_;
Condition100%2/2
$_ // ""
l!l
322
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
129 17 ▶ return length $x && $x eq $y;
Condition100%3/3
length $x && $x eq $y
!ll&&!rl&&r
1313
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 16 };
131 16 my %i = map { $_ => 1 } $msg->signature_numbers, $snap->signature_numbers;
132 0 for my $i (sort { $a <=> $b } keys %i) {
133 10 ▶ 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)) 3 7 10
134 unless $same->(scalar $msg->signature_field($i), scalar $snap->signature_field($i));
135 }
136 13 my $covered = $snap->covered_m;
137 13 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 12 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 22 ▶sub _foreign_instance {
Subroutine _foreign_instance: called CC=5 SCAR=16.2
148 22 my ($chain, $snapshot) = @_;
149 22 my $snap = Mail::DKIM2::Chain->from_message($snapshot);
150 22 ▶ return 0 if $snap->numbering_error(Signatures => 0);
Branch: 1 branch
BranchTrueFalseTotal
if $snap->numbering_error("Signatures", 0) 0 22 22
151 22 for my $m ($snap->instance_numbers) {
152 my ($x, $y) = map {
153 24 ▶ my $v = $_ // '';
Condition50%1/2
$_ // ""
l!l
480
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
154 48 $v =~ s/\A[^:]*://;
155 48 $v =~ s/\s+/ /g;
156 48 $v =~ s/\A | \z//g;
157 48 $v;
158 } $snap->instance_field($m), $chain->instance_field($m);
159 24 ▶ return $m if $x ne $y;
Branch: 1 branch
BranchTrueFalseTotal
if $x ne $y 1 23 24
160 }
161 21 return;
162 }
163  
164 107 1;
165  
166 __END__
« lib/Mail/DKIM2/Verifier.pm ↑ Summary lib/Mail/DKIM2/DSN.pm »