lib/Mail/DKIM2/Canon.pm

filter: Statementstmt 58.4% 76 / 130 Branchbran 41.6% 20 / 48 Conditioncond 38.8% 7 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 63.6% 14 / 22 Podpod 100% 14 / 14 totaltotal 55.3% 134 / 242 CC 35 SCAR 18.1
CC
35
Cov
53%
should_skip
10.5
should_skip
10.5
_without_signature_values
9.5
CRAP 6.1
« lib/Mail/DKIM2/Signer.pm ↑ Summary bin/dkim2sign »
1 package Mail::DKIM2::Canon;
2 222 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 222 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 222 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 222 our $VERSION = '0.34';
7  
8 222 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 222 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 222 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 222 our @EXPORT_OK = qw(
13 check_ignore_prefixes
14 should_skip
15 field_name
16 header_values
17 dkim2_canonicalize_header
18 dkim2_canonicalize_sig_header
19 build_signing_input
20 );
21  
22 # Headers excluded from hashing per draft-ietf-dkim-dkim2-spec-09 Section 4.
23 # spec-05 narrowed the old /^arc-/ prefix to the three RFC 8617 field names and
24 # added a /^received-/ prefix rule so future trace fields of that form need no
25 # change here. spec-08 took auto-submitted and vbr-info off the list.
26 222 my %SKIP_EXACT = map { $_ => 1 } qw(
27 apparently-to arc-authentication-results arc-message-signature arc-seal
28 authentication-results delivered-to dkim-signature
29 dkim2-signature dl-expansion-history message-instance original-recipient
30 received return-path sio-label-history x400-received x400-trace
31 );
32  
33 # The IgnorePrefixes option as every entry point validates it: undef, or an
34 # array reference. A bare string is a configuration mistake and croaks, so it
35 # does not surface later as a 'fail' on every message.
36 2646 ▶sub check_ignore_prefixes {
Subroutine check_ignore_prefixes: called CC=3 SCAR=14.2
Pod documented
37 0 ▶ my ($prefixes) = @_;
Subroutine check_ignore_prefixes: not called CC=3 SCAR=14.2
Pod documented
38 0 ▶ return unless defined $prefixes;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $prefixes 2418 228 2646
unless defined $prefixes 0 0 0
39 0 ▶ Carp::croak("IgnorePrefixes must be an array reference of header-name prefixes")
Branch: 2 branches
BranchTrueFalseTotal
unless ref $prefixes eq "ARRAY" 3 225 228
unless ref $prefixes eq "ARRAY" 0 0 0
40 unless ref $prefixes eq 'ARRAY';
41 0 return $prefixes;
42 }
43  
44 # should_skip($name, \@prefixes)
45 #
46 # True when the field is excluded from the header hash: by the spec's list
47 # above, or by one of the caller's prefixes. The prefixes are an operator's
48 # own field names, put on the wrong side of the signature by its own systems:
49 # added at its border on the way in, stripped at its border on the way out.
50 # A signer that hashed one would sign a message no recipient ever sees; a
51 # verifier that hashed one would fail the operator's own forwards. The spec
52 # knows nothing of them and neither does a remote verifier, so the operator
53 # also has to make sure no field with one of these names ever leaves its
54 # network. They are local policy, so they travel as an argument -- never as
55 # state shared by every Signer and Verifier in the process.
56 22679 ▶sub should_skip {
Subroutine should_skip: called CC=6 SCAR=23.5
Pod documented
57 0 ▶ my ($name, $prefixes) = @_;
Subroutine should_skip: not called CC=6 SCAR=23.5
Pod documented
58 0 my $hname = lc $name;
59 0 ▶ return 1 if $SKIP_EXACT{$hname};
Branch: 2 branches
BranchTrueFalseTotal
if $SKIP_EXACT{$hname} 2976 19703 22679
if $SKIP_EXACT{$hname} 0 0 0
60 0 ▶ return 1 if $hname =~ m/^x-/;
Branch: 2 branches
BranchTrueFalseTotal
if $hname =~ /^x-/u 4157 15546 19703
if $hname =~ /^x-/u 0 0 0
61 0 ▶ return 1 if $hname =~ m/^received-/;
Branch: 2 branches
BranchTrueFalseTotal
if $hname =~ /^received-/u 89 15457 15546
if $hname =~ /^received-/u 0 0 0
62 0 ▶ return 1 if $prefixes && grep { index($hname, lc $_) == 0 } @$prefixes;
Branch: 2 branches
BranchTrueFalseTotal
if $prefixes and grep {index($hname, lc $_) == 0;} @$prefixes 15 15442 15457
if $prefixes and grep {index($hname, lc $_) == 0;} @$prefixes 0 0 0
Condition50%3/6
$prefixes and grep {index($hname, lc $_) == 0;} @$prefixes
!ll&&!rl&&r
14222122015
$prefixes and grep {index($hname, lc $_) == 0;} @$prefixes
!ll&&!rl&&r
000
MC/DC100%2/2
$prefixes and grep {index($hname, lc $_) == 0;} @$prefixes
$prefixes grep {index($hname, lc $_) == 0;} @$prefixes
MC/DC0%0/2
$prefixes and grep {index($hname, lc $_) == 0;} @$prefixes
$prefixes grep {index($hname, lc $_) == 0;} @$prefixes
Truth table100%3/3
A and B
A= $prefixesB= grep {index($hname, lc $_) == 0;} @$prefixes
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $prefixesB= grep {index($hname, lc $_) == 0;} @$prefixes
ABresult
0X0
100
111
63 0 return 0;
64 }
65  
66 # field_name($name): a header field's name as DKIM2 compares it: lowercased,
67 # without the WSP RFC 5322's obsolete syntax (obs-optional) allows between it
68 # and the colon. "From :" is a From field (-09 section 6.2 deletes that WSP);
69 # taking the WSP as part of the name hid such a field from the From check.
70 126249 ▶sub field_name {
Subroutine field_name: called CC=2 SCAR=10.5
Pod documented
71 0 ▶ my ($name) = @_;
Subroutine field_name: not called CC=2 SCAR=10.5
Pod documented
72 0 ▶ ($name = lc($name // '')) =~ s/[ \t]+\z//;
Condition25%1/4
$name // ""
l!l
1262490
$name // ""
l!l
00
MC/DC0%0/1
$name // ""
$name
MC/DC0%0/1
$name // ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
Truth table0%0/2
A
A= $name
Aresult
11
00
73 0 return $name;
74 }
75  
76 # header_values($msg, $name): the values of every field of an Email::MIME
77 # message whose field_name is $name's, in order -- header_raw, but finding
78 # "From :" (which Email::Simple files under "From ") as well.
79 3142 ▶sub header_values {
Subroutine header_values: called CC=1 SCAR=1.2
Pod documented
80 0 ▶ my ($msg, $name) = @_;
Subroutine header_values: not called CC=1 SCAR=1.2
Pod documented
81 0 my $want = field_name($name);
82 0 my @pairs = $msg->header_obj->header_raw_pairs;
83 0 return map { $pairs[2 * $_ + 1] }
84 0 grep { field_name($pairs[2 * $_]) eq $want } 0 .. $#pairs / 2;
85 }
86  
87 # DKIM2 header canonicalization for the header hash, -09 section 6.2:
88 # 1. Lowercase the field name
89 # 2. Unfold continuation lines (remove CRLF before WSP)
90 # 3. Collapse each run of WSP to a single SP
91 # 4. Strip trailing WSP before the CRLF
92 # 5. Remove WSP around the colon
93 18835 ▶sub dkim2_canonicalize_header {
Subroutine dkim2_canonicalize_header: called CC=2 SCAR=9.2
Pod documented
94 0 ▶ my ($line) = @_;
Subroutine dkim2_canonicalize_header: not called CC=2 SCAR=9.2
Pod documented
95 # Unfold: remove CRLF followed by WSP
96 0 $line =~ s/\r?\n[ \t]/ /g;
97 # Split on colon
98 0 my ($name, $value) = split(/:/, $line, 2);
99 0 ▶ return $line unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 2910 15925 18835
unless defined $value 0 0 0
100 # Lowercase name, without the WSP RFC 5322's obsolete syntax allows
101 # before the colon (-09 section 6.2 deletes it)
102 0 $name = field_name($name);
103 # Collapse WSP runs to single SP
104 0 $value =~ s/[ \t]+/ /g;
105 # Strip leading and trailing WSP from value
106 0 $value =~ s/^[ \t]+//;
107 0 $value =~ s/[ \t]*\r?\n?$//;
108 0 return "$name:$value\r\n";
109 }
110  
111 # DKIM2 header canonicalization for the signing input, -09 section 12.8:
112 # as for the header hash, except that ALL WSP is deleted rather than each
113 # run collapsed to one SP -- so where a Message-Instance or DKIM2-Signature
114 # field is folded never matters to a signature.
115 6920 ▶sub dkim2_canonicalize_sig_header {
Subroutine dkim2_canonicalize_sig_header: called CC=2 SCAR=9.8
Pod documented
116 0 ▶ my ($line) = @_;
Subroutine dkim2_canonicalize_sig_header: not called CC=2 SCAR=9.8
Pod documented
117 # Unfold: remove CRLF followed by WSP
118 0 $line =~ s/\r?\n[ \t]//g;
119 # Split on colon
120 0 my ($name, $value) = split(/:/, $line, 2);
121 0 ▶ return $line unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 0 6920 6920
unless defined $value 0 0 0
122 # Lowercase name, without any WSP before the colon
123 0 $name = field_name($name);
124 # Delete ALL WSP characters
125 0 $value =~ s/[ \t]+//g;
126 # Strip trailing CRLF/LF
127 0 $value =~ s/\r?\n?$//;
128 0 return "$name:$value\r\n";
129 }
130  
131 # build_signing_input(%args): the octets a DKIM2-Signature signs (-09
132 # section 12.8), for the Signer and the Verifier alike. Each field is given
133 # whole, name and value, as it is in the message (or as it will be): folded
134 # or not, with or without its line break.
135 # instances => the Message-Instance fields the signature covers (m= up to
136 # its own m=), in ascending m= order
137 # signatures => the DKIM2-Signature fields below it, in ascending i= order
138 # signing => the DKIM2-Signature being made or checked; its s= values
139 # are emptied here, so it may be given signed or unsigned
140 1722 ▶sub build_signing_input {
Subroutine build_signing_input: called CC=4 SCAR=19.6
Pod documented
141 0 ▶ my (%args) = @_;
Subroutine build_signing_input: not called CC=4 SCAR=19.6
Pod documented
142 0 Mail::DKIM2::Util::check_options('build_signing_input', \%args, qw(instances signatures signing));
143 Carp::croak("build_signing_input needs the field being signed (signing)")
144 0 ▶ unless defined $args{signing};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $args{"signing"} 0 1722 1722
unless defined $args{"signing"} 0 0 0
145 return join '',
146 0 (map { dkim2_canonicalize_sig_header($_) }
147 0 ▶ @{ $args{instances} || [] }, @{ $args{signatures} || [] }),
Condition37%3/8
$args{'instances'} || []
l!l
17220
$args{'signatures'} || []
l!l
1589133
$args{'instances'} || []
l!l
00
$args{'signatures'} || []
l!l
00
MC/DC0%0/1
$args{'instances'} || []
$args{'instances'}
MC/DC100%1/1
$args{'signatures'} || []
$args{'signatures'}
MC/DC0%0/1
$args{'instances'} || []
$args{'instances'}
MC/DC0%0/1
$args{'signatures'} || []
$args{'signatures'}
Truth table50%1/2
A
A= $args{'instances'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'signatures'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'instances'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'signatures'}
Aresult
11
00
148 0 _without_signature_values(dkim2_canonicalize_sig_header($args{signing}));
149 }
150  
151 # A canonicalized DKIM2-Signature field with each s= item's signature value
152 # emptied (-09 section 12.8): the selectors, the algorithm names and the
153 # ":" "," "|" all stay. Only the tag named exactly "s": tag-names are
154 # case-sensitive (section 8), so an "S=" is an unknown tag, signed as it is.
155 1722 ▶sub _without_signature_values {
Subroutine _without_signature_values: called CC=5 SCAR=22.6
156 0 ▶ my ($canon) = @_;
Subroutine _without_signature_values: not called CC=5 SCAR=22.6
157 0 my ($name, $value) = split /:/, $canon, 2;
158 0 ▶ return $canon unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 0 1722 1722
unless defined $value 0 0 0
159 0 ▶ my $eol = $value =~ s/(\r\n)\z// ? $1 : '';
Branch: 2 branches
BranchTrueFalseTotal
$value =~ s/(\r\n)\z//u ? : 1722 0 1722
$value =~ s/(\r\n)\z//u ? : 0 0 0
160 0 my @specs = split /;/, $value, -1;
161 0 for (@specs) {
162 0 ▶ next unless /\As=/;
Branch: 2 branches
BranchTrueFalseTotal
unless /\As=/u 12003 1722 13725
unless /\As=/u 0 0 0
163 0 s/([^,|:=]+:[^,|:]+):[^,|]*/$1:/g;
164 }
165 0 return "$name:" . join(';', @specs) . $eol;
166 }
167  
168 222 1;
169  
170 __END__
« lib/Mail/DKIM2/Signer.pm ↑ Summary bin/dkim2sign »