lib/Mail/DKIM2/Common.pm

filter: Statementstmt 72.3% 170 / 235 Branchbran 54.6% 59 / 108 Conditioncond 49.0% 27 / 55 MC/DCmcdc 41.1% 14 / 34 Subroutinesub 79.0% 34 / 43 Podpod 100% 19 / 19 totaltotal 65.3% 323 / 494 CC 107 SCAR 23.5
CC
107
Cov
64%
chain_number_error
25.2
chain_number_error
25.2
combine_sets
19.1
CRAP 10.5
« lib/Mail/DKIM2/HeaderParser.pm ↑ Summary lib/Mail/DKIM2/Signature.pm »
1 package Mail::DKIM2::Common;
2 223 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 223 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 223 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 223 our $VERSION = '0.35';
7  
8 223 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 223 ▶use Email::MIME::ContentType ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Everything that lived here before 0.31 and now has a module of its own is
12 # imported, so Mail::DKIM2::Common::fold_header(...) and
13 # "use Mail::DKIM2::Common qw(fold_header)" go on working.
14 223 ▶use Mail::DKIM2::Address qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 extract_domain
16 to_rfc5321_path
17 domain_of
18 to_alabel
19 from_domain
20 from_mailbox
21 from_authentic
22 from_refusal_note
23 relaxed_domain_match
24 parent_from_matcher
25 from_pair_error
26 223 );
27 223 ▶use Mail::DKIM2::Canon qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 check_ignore_prefixes
29 should_skip
30 field_name
31 header_values
32 dkim2_canonicalize_header
33 dkim2_canonicalize_sig_header
34 build_signing_input
35 223 );
36 223 ▶use Mail::DKIM2::Fold qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
37 fold_header
38 223 );
39 223 ▶use Mail::DKIM2::Key qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
40 signature_algorithm
41 signature_algorithm_for_key
42 load_private_key
43 load_private_key_data
44 parse_dkim_pubkey
45 parse_dkim_key_record
46 key_records
47 domain_policy
48 query_domain_policy
49 policy_from_source
50 223 );
51 223 ▶use Mail::DKIM2::RecipeJSON qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
52 encode_tag_json
53 decode_tag_json
54 223 );
55  
56 # Every eval in the library rethrows a reference: the library only ever dies
57 # with strings, so an object is the host's, typically a milter or MTA
58 # signalling a timeout, and swallowing it would let the caller run on past
59 # its deadline (Mail::DKIM2's CONVENTIONS).
60  
61 223 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 223 our @EXPORT_OK = qw(
63 should_skip
64 split_any_all
65 combine_sets
66 parse_mime
67 check_ignore_prefixes
68 dkim2_canonicalize_header
69 dkim2_canonicalize_sig_header
70 field_name
71 header_values
72 encode_tag_json
73 decode_tag_json
74 fold_header
75 build_signing_input
76 extract_mi_version
77 mi_version_tag
78 strip_mi_versions
79 extract_domain
80 domain_of
81 to_alabel
82 from_domain
83 from_mailbox
84 from_authentic
85 from_refusal_note
86 to_rfc5321_path
87 relaxed_domain_match
88 parse_dkim_pubkey
89 parse_dkim_key_record
90 key_records
91 domain_policy
92 load_private_key
93 load_private_key_data
94 signature_algorithm
95 signature_algorithm_for_key
96 DKIM2_DRAFT
97 DKIM2_REPO
98 DKIM2_DATE
99 MAX_CHAIN_LENGTH
100 MAX_CHAIN_NUMBER
101 MAX_SIGNATURE_ITEMS
102 FUTURE_LIMIT
103 EXPIRY
104 chain_length_error
105 valid_sequence
106 UNKEYABLE_SIGNATURE_ERROR
107 MI_MISSING_M_ERROR
108 unkeyable_signature_error
109 chain_number_error
110 duplicate_number_error
111 parent_from_matcher
112 from_pair_error
113 query_domain_policy
114 policy_from_source
115 );
116  
117 # Provenance emitted in X-DKIM2-Info headers by the milter, the reflector, and
118 # the mailman/sympa handlers. This is the single source of truth for the Perl
119 # implementation. DKIM2_DRAFT is the spec revision implemented; DKIM2_DATE is
120 # the date this implementation's DKIM2 behaviour last changed (a software
121 # version stamp, not the draft's date) -- bump it on any change to what we
122 # emit, not only on a spec bump. See ../spec/draft-gondwana-dkim2-debug-header.
123 223 ▶use constant DKIM2_DRAFT => 'ietf-dkim-dkim2-spec-09';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 223 ▶use constant DKIM2_REPO => 'github.com/dkim2wg/interop';
Subroutine BEGIN: called CC=1 SCAR=0.0
125 223 ▶use constant DKIM2_DATE => '2026-11-03';
Subroutine BEGIN: called CC=1 SCAR=0.0
126  
127 # -09 section 14.3: a DKIM2-Signature whose t= is more than a day after
128 # delivery is "from the future", and one made more than 14 days before it
129 # has "expired". Seconds.
130 223 ▶use constant FUTURE_LIMIT => 86400;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 223 ▶use constant EXPIRY => 14 * 86400;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 # Local policy, not the draft's: a message carrying more Message-Instance or
134 # DKIM2-Signature fields than this is a PERMERROR, found before any key is
135 # fetched, signature checked or recipe applied. Every hop costs a verifier a
136 # DNS lookup, a signature check and an undo of the whole message, and the
137 # sender chooses how many hops there are.
138 223 ▶use constant MAX_CHAIN_LENGTH => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 # The largest number a DKIM2-Signature i= or m=, or a Message-Instance m=, may
141 # be written as: at most two ASCII digits naming 1..99. Anything else is
142 # malformed before it is ever compared with MAX_CHAIN_LENGTH, and is never
143 # converted to a number big enough to be a loop bound.
144 223 ▶use constant MAX_CHAIN_NUMBER => 99;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 # Local policy, not the draft's: the most items one DKIM2-Signature's s= may
147 # carry. The draft sets no limit (-08 dropped it), and every item may cost a key lookup and a
148 # signature check, so a crafted s= of thousands of selectors would cost
149 # thousands of each (the -08 contract, D3). The same limit as the key
150 # prefetch's; refused before any key is fetched.
151 223 ▶use constant MAX_SIGNATURE_ITEMS => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
152  
153 # The PERMERROR for a message over MAX_CHAIN_LENGTH, or undef. Takes an
154 # Email::MIME, or a hashref of field counts keyed by lower-cased name for a
155 # caller counting as it parses.
156 4546 ▶sub chain_length_error {
Subroutine chain_length_error: called CC=5 SCAR=22.0
Pod documented
157 0 ▶ my ($msg) = @_;
Subroutine chain_length_error: not called CC=5 SCAR=22.0
Pod documented
158 0 for my $field ('Message-Instance', 'DKIM2-Signature') {
159 my $count = ref($msg) eq 'HASH'
160 0 ▶ ? ($msg->{lc $field} // 0)
Branch: 2 branches
BranchTrueFalseTotal
ref $msg eq 'HASH' ? : 9087 0 9087
ref $msg eq 'HASH' ? : 0 0 0
Condition50%2/4
$$msg{lc $field} // 0
l!l
78271260
$$msg{lc $field} // 0
l!l
00
MC/DC100%1/1
$$msg{lc $field} // 0
$$msg{lc $field}
MC/DC0%0/1
$$msg{lc $field} // 0
$$msg{lc $field}
Truth table100%2/2
A
A= $$msg{lc $field}
Aresult
11
00
Truth table0%0/2
A
A= $$msg{lc $field}
Aresult
11
00
161 : scalar(my @values = $msg->header_raw($field));
162 0 ▶ return "PERMERROR: more than " . MAX_CHAIN_LENGTH . " $field fields"
Branch: 2 branches
BranchTrueFalseTotal
if $count > 32 11 9076 9087
if $count > 32 0 0 0
163 if $count > MAX_CHAIN_LENGTH;
164 }
165 0 return;
166 }
167  
168 # A DKIM2-Signature's i= must be a positive integer (ASCII digits) for the
169 # signature to be placed in the chain and keyed. One that is not -- no i=,
170 # i=, i=0, i=abc, or a header that does not parse -- is a PERMERROR, never
171 # silently skipped: a signer gate that counts which instances are signed by
172 # m= must not be fooled by a junk signature naming an m=.
173 # The -07 C6 forms: a missing i= is "i= tag=i missing" (there is no number
174 # to name the field by); one that is not a positive integer is named as
175 # written, "i=<value> syntax error" (unkeyable_signature_error).
176 223 ▶use constant UNKEYABLE_SIGNATURE_ERROR =>
Subroutine BEGIN: called CC=1 SCAR=0.0
177 223 'PERMERROR: DKIM2-Signature i= tag=i missing';
178  
179 # The same for a Message-Instance with no m= tag: nothing to name it by.
180 223 ▶use constant MI_MISSING_M_ERROR =>
Subroutine BEGIN: called CC=1 SCAR=0.0
181 223 'PERMERROR: Message-Instance m= tag=m missing';
182  
183 # The PERMERROR for a DKIM2-Signature header value that no verifier can
184 # key: UNKEYABLE_SIGNATURE_ERROR without an i= tag, else "PERMERROR:
185 # DKIM2-Signature i=<as written> syntax error".
186 62 ▶sub unkeyable_signature_error {
Subroutine unkeyable_signature_error: called CC=2 SCAR=9.2
Pod documented
187 0 ▶ my ($contents) = @_;
Subroutine unkeyable_signature_error: not called CC=2 SCAR=9.2
Pod documented
188 0 my $i = _raw_tag($contents, 'i');
189 0 ▶ return defined $i ? "PERMERROR: DKIM2-Signature i=$i syntax error"
Branch: 2 branches
BranchTrueFalseTotal
defined $i ? : 33 29 62
defined $i ? : 0 0 0
190 : UNKEYABLE_SIGNATURE_ERROR;
191 }
192  
193 # The raw value of a tag in a header value (whitespace removed), or undef.
194 62 ▶sub _raw_tag {
Subroutine _raw_tag: called CC=4 SCAR=18.9
195 0 ▶ my ($header, $tag) = @_;
Subroutine _raw_tag: not called CC=4 SCAR=18.9
196 0 ▶ return unless defined $header;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $header 0 62 62
unless defined $header 0 0 0
197 0 for my $part (split /;/, $header) {
198 0 ▶ next unless $part =~ /\A\s*\Q$tag\E\s*=(.*)\z/si; # tag names are case insignificant
Branch: 2 branches
BranchTrueFalseTotal
unless $part =~ /\A\s*\Q$tag\E\s*=(.*)\z/isu 78 33 111
unless $part =~ /\A\s*\Q$tag\E\s*=(.*)\z/isu 0 0 0
199 0 (my $v = $1) =~ s/\s//g;
200 0 return $v;
201 }
202 0 return;
203 }
204  
205 2793 ▶sub valid_sequence {
Subroutine valid_sequence: called CC=4 SCAR=17.9
Pod documented
206 0 ▶ my ($i) = @_;
Subroutine valid_sequence: not called CC=4 SCAR=17.9
Pod documented
207 0 ▶ return (defined $i && $i =~ /\A[0-9]+\z/ && $i > 0) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
defined $i && $i =~ /\A[0-9]+\z/u && $i > 0 ? : 2721 72 2793
defined $i && $i =~ /\A[0-9]+\z/u && $i > 0 ? : 0 0 0
Condition50%6/12
defined $i and $i =~ /\A[0-9]+\z/u and $i > 0
!ll&&!rl&&r
61112721
defined $i and $i =~ /\A[0-9]+\z/u
!ll&&!rl&&r
30312732
defined $i and $i =~ /\A[0-9]+\z/u and $i > 0
!ll&&!rl&&r
000
defined $i and $i =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
MC/DC100%3/3
defined $i and $i =~ /\A[0-9]+\z/u and $i > 0
defined $i $i =~ /\A[0-9]+\z/u $i > 0
MC/DC0%0/3
defined $i and $i =~ /\A[0-9]+\z/u and $i > 0
defined $i $i =~ /\A[0-9]+\z/u $i > 0
Truth table100%4/4
A and B and C
A= defined $iB= $i =~ /\A[0-9]+\z/uC= $i > 0
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= defined $iB= $i =~ /\A[0-9]+\z/uC= $i > 0
ABCresult
0XX0
10X0
1100
1111
208 }
209  
210 # The PERMERROR for an i= or m= value that is not a chain number, or undef
211 # (also undef for a missing value, which is left to the callers). A value must
212 # be 1*DIGIT in ASCII -- "4294967297x" is malformed, never its digit prefix --
213 # with at most two digits naming 1..MAX_CHAIN_NUMBER, so "01" is 1; and since
214 # each number names one hop, no more than MAX_CHAIN_LENGTH.
215 # Checking this before anything walks 1..max keeps a value like
216 # 99999999999999999999 from being used as a loop bound.
217 #
218 # The field is named by its own number as written (a DKIM2-Signature by its
219 # i=, a Message-Instance by its m=, -07 C6): $ident, defaulting to $n. So a
220 # signature's bad m= is "DKIM2-Signature i=2 syntax error", and an
221 # out-of-range one "DKIM2-Signature i=2 m=101 exceeds ...".
222 12418 ▶sub chain_number_error {
Subroutine chain_number_error: called CC=10 SCAR=32.3
Pod documented
223 0 ▶ my ($field, $tag, $n, $ident) = @_;
Subroutine chain_number_error: not called CC=10 SCAR=32.3
Pod documented
224 0 ▶ return unless defined $n;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $n 36 12382 12418
unless defined $n 0 0 0
225 0 ▶ my $id_tag = $field eq 'Message-Instance' ? 'm' : 'i';
Branch: 2 branches
BranchTrueFalseTotal
$field eq 'Message-Instance' ? : 6980 5402 12382
$field eq 'Message-Instance' ? : 0 0 0
226 0 ▶ my $who = "$field $id_tag=" . ($ident // $n);
Condition33%2/6
$ident // $n
l!l&&r!l&&!r
267097120
$ident // $n
l!l&&r!l&&!r
000
MC/DC0%0/2
$ident // $n
$ident $n
MC/DC0%0/2
$ident // $n
$ident $n
Truth table66%2/3
A // B
A= $identB= $n
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $identB= $n
ABresult
1X1
011
000
227 0 ▶ return "PERMERROR: $who syntax error"
Branch: 2 branches
BranchTrueFalseTotal
unless $n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u 28 12354 12382
unless $n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u 0 0 0
Condition50%3/6
$n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u
!ll&&!rl&&r
25312354
$n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u
!ll&&!rl&&r
000
MC/DC100%2/2
$n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u
$n =~ /\A[0-9]+\z/u $n =~ /[1-9]/u
MC/DC0%0/2
$n =~ /\A[0-9]+\z/u and $n =~ /[1-9]/u
$n =~ /\A[0-9]+\z/u $n =~ /[1-9]/u
Truth table100%3/3
A and B
A= $n =~ /\A[0-9]+\z/uB= $n =~ /[1-9]/u
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $n =~ /\A[0-9]+\z/uB= $n =~ /[1-9]/u
ABresult
0X0
100
111
228 unless $n =~ /\A[0-9]+\z/ && $n =~ /[1-9]/;
229 0 ▶ $who .= " $tag=$n" if $tag ne $id_tag;
Branch: 2 branches
BranchTrueFalseTotal
if $tag ne $id_tag 2663 9691 12354
if $tag ne $id_tag 0 0 0
230 0 ▶ return "PERMERROR: $who exceeds the maximum chain number of "
Branch: 2 branches
BranchTrueFalseTotal
if length $n > 2 or $n > 99 34 12320 12354
if length $n > 2 or $n > 99 0 0 0
Condition33%2/6
length $n > 2 or $n > 99
l!l&&r!l&&!r
34012320
length $n > 2 or $n > 99
l!l&&r!l&&!r
000
MC/DC50%1/2
length $n > 2 or $n > 99
length $n > 2 $n > 99
MC/DC0%0/2
length $n > 2 or $n > 99
length $n > 2 $n > 99
Truth table66%2/3
A or B
A= length $n > 2B= $n > 99
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= length $n > 2B= $n > 99
ABresult
1X1
011
000
231 . MAX_CHAIN_NUMBER if length($n) > 2 || $n > MAX_CHAIN_NUMBER;
232 0 ▶ return "PERMERROR: $who exceeds the maximum chain length of "
Branch: 2 branches
BranchTrueFalseTotal
if $n > 32 8 12312 12320
if $n > 32 0 0 0
233 . MAX_CHAIN_LENGTH if $n > MAX_CHAIN_LENGTH;
234 0 return;
235 }
236  
237 # The PERMERROR for the lowest number that appears twice among a field's
238 # m= or i= values, or undef. Each number names one hop, so a second field
239 # with the same number is never a valid chain, and taking either one would
240 # let the other go unchecked.
241 6393 ▶sub duplicate_number_error {
Subroutine duplicate_number_error: called CC=3 SCAR=13.8
Pod documented
242 0 ▶ my ($field, $tag, @numbers) = @_;
Subroutine duplicate_number_error: not called CC=3 SCAR=13.8
Pod documented
243 0 my %count;
244 0 $count{$_ + 0}++ for grep { defined } @numbers;
245 # The lowest number given twice (interop contract C9), not the first met.
246 0 my ($n) = sort { $a <=> $b } grep { $count{$_} > 1 } keys %count;
247 0 ▶ return defined $n ? "PERMERROR: $field $tag=$n is duplicated" : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $n ? : 18 6375 6393
defined $n ? : 0 0 0
248 }
249  
250 # The groups of an h= or s= value (-09 sections 9.3, 10.9): any-of groups
251 # split by "|", each an all-of list split by ",", as [[item, ...], ...] with
252 # each item's text untrimmed (its own parser trims the FWS around it). Undef
253 # when a member is empty -- a leading, trailing or doubled "|" or "," -- which
254 # is a syntax error (the -08 contract, D2).
255 7037 ▶sub split_any_all {
Subroutine split_any_all: called CC=4 SCAR=17.9
Pod documented
256 0 ▶ my ($value) = @_;
Subroutine split_any_all: not called CC=4 SCAR=17.9
Pod documented
257 0 my @groups;
258 0 for my $group (split /\|/, $value, -1) {
259 0 my @items = split /,/, $group, -1;
260 0 ▶ return if !@items || grep { /\A[ \t\r\n]*\z/ } @items;
Branch: 2 branches
BranchTrueFalseTotal
if not @items or grep {/\A[ \t\r\n]*\z/u;} @items 7 15160 15167
if not @items or grep {/\A[ \t\r\n]*\z/u;} @items 0 0 0
Condition50%3/6
not @items or grep {/\A[ \t\r\n]*\z/u;} @items
l!l&&r!l&&!r
5215160
not @items or grep {/\A[ \t\r\n]*\z/u;} @items
l!l&&r!l&&!r
000
MC/DC100%2/2
not @items or grep {/\A[ \t\r\n]*\z/u;} @items
not @items grep {/\A[ \t\r\n]*\z/u;} @items
MC/DC0%0/2
not @items or grep {/\A[ \t\r\n]*\z/u;} @items
not @items grep {/\A[ \t\r\n]*\z/u;} @items
Truth table100%3/3
not(A) or B
A= not @itemsB= grep {/\A[ \t\r\n]*\z/u;} @items
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not @itemsB= grep {/\A[ \t\r\n]*\z/u;} @items
ABresult
1X1
011
000
261 0 push @groups, \@items;
262 }
263 0 return \@groups;
264 }
265  
266 # combine_sets(\@groups, $none_passed) -> ($state, $text): the result of an
267 # h= or s= value from its members' own (-09 sections 14.6, 14.7). @groups is
268 # [[[$state, $text], ...], ...] -- the "|" groups of "," lists, in the
269 # order the value gives them -- with each $state one of pass, fail,
270 # temperror, permerror and $text the member's own error. An all-of list
271 # passes if every member does; is a temperror if every member that does not
272 # is one; else fails if any member fails; else is a permerror. The any-of
273 # groups pass if one does; else are a temperror if one is; else fail if one
274 # does; else are a permerror. The text (contract D3/D4): with one group, the
275 # first member whose state is the result's; with several, $none_passed for a
276 # fail, else the first member anywhere whose state is the result's.
277 2261 ▶sub combine_sets {
Subroutine combine_sets: called CC=9 SCAR=29.5
Pod documented
278 0 ▶ my ($groups, $none_passed) = @_;
Subroutine combine_sets: not called CC=9 SCAR=29.5
Pod documented
279 0 my @all;
280 0 for my $g (@$groups) {
281 0 my @bad = grep { $_->[0] ne 'pass' } @$g;
282 push @all, !@bad ? 'pass'
283 0 ▶ : !grep({ $_->[0] ne 'temperror' } @bad) ? 'temperror'
Branch: 2 branches
BranchTrueFalseTotal
grep({$_->[0] eq 'fail';} @bad) ? : 237 59 296
grep({$_->[0] eq 'fail';} @bad) ? : 0 0 0
284 0 ▶ : grep({ $_->[0] eq 'fail' } @bad) ? 'fail'
Branch: 4 branches
BranchTrueFalseTotal
!@bad ? : 1982 310 2292
!grep({$_->[0] ne 'temperror';} @bad) ? : 14 296 310
!@bad ? : 0 0 0
!grep({$_->[0] ne 'temperror';} @bad) ? : 0 0 0
285 : 'permerror';
286 }
287 0 my %have = map { $_ => 1 } @all;
288 0 my ($state) = grep { $have{$_} } qw(pass temperror fail permerror);
289 0 ▶ return ('pass') if $state eq 'pass';
Branch: 2 branches
BranchTrueFalseTotal
if $state eq "pass" 1982 279 2261
if $state eq "pass" 0 0 0
290 0 ▶ return ($state, $none_passed) if @$groups > 1 && $state eq 'fail';
Branch: 2 branches
BranchTrueFalseTotal
if @$groups > 1 and $state eq "fail" 2 277 279
if @$groups > 1 and $state eq "fail" 0 0 0
Condition50%3/6
@$groups > 1 and $state eq "fail"
!ll&&!rl&&r
27432
@$groups > 1 and $state eq "fail"
!ll&&!rl&&r
000
MC/DC100%2/2
@$groups > 1 and $state eq "fail"
@$groups > 1 $state eq "fail"
MC/DC0%0/2
@$groups > 1 and $state eq "fail"
@$groups > 1 $state eq "fail"
Truth table100%3/3
A and B
A= @$groups > 1B= $state eq "fail"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @$groups > 1B= $state eq "fail"
ABresult
0X0
100
111
291 0 ▶ my @members = @$groups == 1 ? @{ $groups->[0] } : map { @$_ } @$groups;
Branch: 2 branches
BranchTrueFalseTotal
@$groups == 1 ? : 274 3 277
@$groups == 1 ? : 0 0 0
292 0 my ($first) = grep { $_->[0] eq $state } @members;
293 0 return ($state, $first->[1]);
294 }
295  
296 # Parse a message with Email::MIME for the Message-Instance code's purposes:
297 # raw header fields and the raw body. DKIM2 never reads MIME
298 # parameters, so a sender's broken Content-Type -- `text/plain; Windows-1252`
299 # and worse, as 2003 spam and some list software emit -- must not stop a
300 # message from being verified. Email::MIME::ContentType croaks on such a
301 # parameter by default; relax that for the duration of the parse only (it is
302 # a package variable, and the host application's own parsing keeps its
303 # setting). Found 2026-10-04 replaying the SpamAssassin corpus through a list.
304 1473 ▶sub parse_mime {
Subroutine parse_mime: called CC=1 SCAR=1.2
Pod documented
305 0 ▶ my ($raw) = @_;
Subroutine parse_mime: not called CC=1 SCAR=1.2
Pod documented
306 0 local $Email::MIME::ContentType::STRICT_PARAMS = 0;
307 0 ▶ local $SIG{__WARN__} = sub { }; # the relaxed parser warns instead
Subroutine __ANON__: called CC=1 SCAR=1.2
308 0 return Email::MIME->new($raw);
309 }
310  
311 # The raw m= value of a Message-Instance header value (whitespace removed,
312 # as MessageInstance->parse does), or undef if it has no m= tag.
313 20788 ▶sub mi_version_tag {
Subroutine mi_version_tag: called CC=6 SCAR=18.0
Pod documented
314 20788 my ($header) = @_;
315 20788 ▶ $header = $header->[0] if ref($header) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $header eq "ARRAY" 1116 19672 20788
316 20788 ▶ $header = $$header if ref($header);
Branch: 1 branch
BranchTrueFalseTotal
if ref $header 0 20788 20788
317 20788 ▶ return unless defined $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header 0 20788 20788
318 20788 for my $part (split /;/, $header) {
319 20795 ▶ next unless $part =~ /\A\s*m\s*=(.*)\z/s; # tag-names are case-sensitive (-09 ยง8)
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /\A\s*m\s*=(.*)\z/su 13 20782 20795
320 20782 (my $v = $1) =~ s/\s//g;
321 20782 return $v;
322 }
323 6 return;
324 }
325  
326 # The m= number of a Message-Instance header value, or undef when it has no
327 # m= or the m= is not all ASCII digits (never a digit prefix). Numeric, so
328 # "01" and "001" are 1. Callers check chain_number_error(mi_version_tag(...))
329 # before using it as a bound.
330 13813 ▶sub extract_mi_version {
Subroutine extract_mi_version: called CC=4 SCAR=14.2
Pod documented
331 13813 my $v = mi_version_tag(@_);
332 13813 ▶ return unless defined $v && $v =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v =~ /\A[0-9]+\z/u 1 13812 13813
Condition66%2/3
defined $v and $v =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0113812
MC/DC50%1/2
defined $v and $v =~ /\A[0-9]+\z/u
defined $v $v =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $vB= $v =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
333 13812 ▶ return length($v) <= 15 ? 0 + $v : $v;
Branch: 1 branch
BranchTrueFalseTotal
length $v <= 15 ? : 13812 0 13812
334 }
335  
336 # Strip Message-Instance headers with the given version numbers from a raw message string.
337 # The message must use CRLF line endings. Returns the modified message string.
338 19 ▶sub strip_mi_versions {
Subroutine strip_mi_versions: called CC=13 SCAR=25.9
Pod documented
339 19 my ($message, @versions) = @_;
340 19 ▶ return $message unless @versions;
Branch: 1 branch
BranchTrueFalseTotal
unless @versions 0 19 19
341 19 my %to_strip = map { $_ => 1 } @versions;
342  
343 19 my $EOL = "\015\012";
344 19 my $sep_pos = index($message, $EOL . $EOL);
345 19 ▶ return $message if $sep_pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sep_pos < 0 0 19 19
346  
347 19 my $hdrs_raw = substr($message, 0, $sep_pos);
348 19 my $rest = substr($message, $sep_pos); # includes blank line + body
349  
350 # Parse folded headers into whole-header strings
351 19 my @entries;
352 19 my $cur = '';
353 19 for my $line (split /\015\012/, $hdrs_raw, -1) {
354 377 ▶ if ($line =~ /^[ \t]/ && $cur ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]/u and $cur ne '') { } 175 202 377
Condition66%2/3
$line =~ /^[ \t]/u and $cur ne ""
!ll&&!rl&&r
2020175
MC/DC50%1/2
$line =~ /^[ \t]/u and $cur ne ""
$line =~ /^[ \t]/u $cur ne ""
Truth table66%2/3
A and B
A= $line =~ /^[ \t]/uB= $cur ne ""
ABresult
0X0
100
111
355 175 $cur .= $EOL . $line;
356 } else {
357 202 ▶ push @entries, $cur if $cur ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" 183 19 202
358 202 $cur = $line;
359 }
360 }
361 19 ▶ push @entries, $cur if $cur ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" 19 0 19
362  
363 # Keep all headers except Message-Instance ones with versions in %to_strip
364 19 my @kept;
365 19 for my $entry (@entries) {
366 202 ▶ if ($entry =~ /^Message-Instance:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /^Message-Instance:/iu) 33 169 202
367 33 my ($val) = $entry =~ /^[^:]+:\s*(.*)/s;
368 33 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 33 0 33
369 33 $val =~ s/\015?\012[ \t]+/ /g; # unfold for version extraction
370 33 my $ver = extract_mi_version($val);
371 33 ▶ next if defined $ver && $to_strip{$ver};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ver and $to_strip{$ver} 29 4 33
Condition66%2/3
defined $ver and $to_strip{$ver}
!ll&&!rl&&r
0429
MC/DC50%1/2
defined $ver and $to_strip{$ver}
defined $ver $to_strip{$ver}
Truth table66%2/3
A and B
A= defined $verB= $to_strip{$ver}
ABresult
0X0
100
111
372 }
373 }
374 173 push @kept, $entry;
375 }
376  
377 19 return join($EOL, @kept) . $rest;
378 }
379  
380 223 1;
381  
382 __END__
« lib/Mail/DKIM2/HeaderParser.pm ↑ Summary lib/Mail/DKIM2/Signature.pm »