lib/Mail/Milter/Authentication/Handler/DKIM2Sign.pm

filter: Statementstmt 85.6% 347 / 405 Branchbran 70.0% 105 / 150 Conditioncond 55.4% 61 / 110 MC/DCmcdc 31.1% 19 / 61 Subroutinesub 90.2% 37 / 41 Podpod 100% 10 / 10 totaltotal 74.5% 579 / 777 CC 135 SCAR 36.5
CC
135
Cov
77%
addheader_callback
85.7
_fetch_sign_config
56.0
_remove_unsigned_instances
13.5
CRAP 38.6
« lib/Mail/DKIM2/Validate.pm ↑ Summary lib/Mail/DKIM2/MessageInstance.pm »
1 package Mail::Milter::Authentication::Handler::DKIM2Sign;
2 4 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for DKIM2 signing
7 4 our $VERSION = '0.35';
8 4 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Mail::DKIM2::Common qw(extract_mi_version parse_mime DKIM2_DRAFT DKIM2_REPO DKIM2_DATE);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mail::DKIM2::Address qw(from_refusal_note);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Mail::DKIM2::Fold qw(fold_header);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Mail::DKIM2::Key qw(load_private_key parse_dkim_pubkey);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Mail::DKIM2::Gate;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Mail::DKIM2::Hop;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Mail::DKIM2::MessageStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Mail::DKIM2::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use constant DKIM2_SOFTWARE => 'authentication_milter-DKIM2Sign';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 120 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod documented
24 return {
25 # Static domain config: domain => { selector, keyfile,
26 # parent_from_domains (optional, -09 contract E3.4) }
27 120 'domains' => {},
28 # HTTP endpoint for dynamic key lookup: GET {url}?domain=X
29 # Expected response: {"selector":"sel1","key":"base64_pem_data"}
30 # or: {"selector":"sel1","keyfile":"/path/to/key.pem"}
31 'key_endpoint' => undef,
32 'key_endpoint_timeout' => 5,
33 # Only sign for authenticated/local senders
34 'sign_authenticated' => 1,
35 'sign_local' => 1,
36 # Add Message-Instance headers
37 'add_message_instance' => 1,
38 # SMTP params to record in the signature
39 'record_smtp_params' => 1,
40 # Directory for message snapshots (shared with DKIM2Verify)
41 'snapshot_directory' => undef,
42 # Body Recipe literal-line cap for a snapshot diff; over it the
43 # body Recipe is null (MessageInstance MaxRecipeLiterals)
44 'max_recipe_literals' => 1000,
45 'ignore_header_prefixes' => [], # our own fields, hashed by neither end (IgnorePrefixes)
46 # Deprecated (0.29): this hop's own null body Recipe is always
47 # signed; accepted, warned about, ignored
48 'allow_null_body_recipe' => undef,
49 # Testing only: verify the upstream chain with keys from this dns.json
50 # instead of DNS, and without the t=/expiry check
51 'dns_overrides' => undef,
52 'skip_timestamp_check' => 0,
53 };
54 }
55  
56 # The handler's configuration with default_config() filled in for every
57 # option the operator left out (or set to null). authentication_milter does
58 # not merge default_config() at runtime -- it only uses it to generate a
59 # sample config -- so without this a left-out option is undef: sign_local,
60 # sign_authenticated, add_message_instance and record_smtp_params would all
61 # silently be off.
62 120 ▶sub _config {
Subroutine _config: called CC=3 SCAR=11.0
63 120 my ($self) = @_;
64 120 ▶ my $given = $self->handler_config() || {};
Condition50%1/2
$self->handler_config || {}
l!l
1200
MC/DC0%0/1
$self->handler_config || {}
$self->handler_config
Truth table50%1/2
A
A= $self->handler_config
Aresult
11
00
65 120 my $defaults = default_config();
66 120 ▶ return { %$given, map { $_ => $given->{$_} // $defaults->{$_} } keys %$defaults };
Condition100%3/3
$given->{$_} // $defaults->{$_}
l!l&&r!l&&!r
882366312
MC/DC100%2/2
$given->{$_} // $defaults->{$_}
$given->{$_} $defaults->{$_}
Truth table100%3/3
A // B
A= $given->{$_}B= $defaults->{$_}
ABresult
1X1
011
000
67 }
68  
69 0 ▶sub register_metrics {
Subroutine register_metrics: not called CC=1 SCAR=6.9
Pod documented
70 return {
71 0 'dkim2_sign_total' => 'The number of emails signed with DKIM2',
72 };
73 }
74  
75 52 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=1 SCAR=0.0
Pod documented
76 52 my ( $self, $env_from ) = @_;
77 52 $self->{'failmode'} = 0;
78 52 $self->{'headers'} = [];
79 52 $self->{'body'} = [];
80 52 $self->{'carry'} = q{};
81 52 $self->{'env_from'} = $env_from;
82 52 $self->{'env_rcpt'} = [];
83 52 $self->{'sign_domain'} = undef;
84 }
85  
86 52 ▶sub envrcpt_callback {
Subroutine envrcpt_callback: called CC=1 SCAR=0.0
Pod documented
87 52 my ( $self, $env_to ) = @_;
88 52 push @{$self->{'env_rcpt'}}, $env_to;
89 }
90  
91 468 ▶sub header_callback {
Subroutine header_callback: called CC=2 SCAR=7.0
Pod documented
92 468 my ( $self, $header, $value, $original ) = @_;
93 468 ▶ return if $self->{'failmode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 468 468
94 468 my $EOL = "\015\012";
95 468 my $chunk = $original . $EOL;
96 468 $chunk =~ s/\015?\012/$EOL/g;
97 468 push @{$self->{'headers'}}, $chunk;
98 }
99  
100 52 ▶sub eoh_callback {
Subroutine eoh_callback: called CC=1 SCAR=0.0
Pod documented
101 52 my ($self) = @_;
102 52 $self->{'carry'} = q{};
103 }
104  
105 131 ▶sub body_callback {
Subroutine body_callback: called CC=4 SCAR=15.4
Pod documented
106 131 my ( $self, $body_chunk ) = @_;
107 131 ▶ return if $self->{'failmode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 131 131
108 131 my $EOL = "\015\012";
109  
110 131 my $chunk;
111 131 ▶ if ( $self->{'carry'} ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'carry'} ne '') { } 0 131 131
112 0 $chunk = $self->{'carry'} . $body_chunk;
113 0 $self->{'carry'} = q{};
114 }
115 else {
116 131 $chunk = $body_chunk;
117 }
118  
119 131 ▶ if ( substr( $chunk, -1 ) eq "\015" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($chunk, -1) eq "\r") 0 131 131
120 0 $self->{'carry'} = "\015";
121 0 $chunk = substr( $chunk, 0, -1 );
122 }
123  
124 131 $chunk =~ s/\015?\012/$EOL/g;
125 131 push @{$self->{'body'}}, $chunk;
126 }
127  
128 52 ▶sub eom_callback {
Subroutine eom_callback: called CC=2 SCAR=8.1
Pod documented
129 52 my ($self) = @_;
130 0 ▶ push @{$self->{'body'}}, $self->{'carry'} if $self->{'carry'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"carry"} 0 52 52
131 # Signing happens in addheader_callback
132 }
133  
134 0 ▶sub _fmtheader {
Subroutine _fmtheader: not called CC=1 SCAR=6.9
135 0 my $header = shift;
136 0 my $value = $header->{value};
137 0 $value =~ s/\015?\012/\015\012/gs;
138 0 return "$header->{field}: $value\015\012";
139 }
140  
141 61 ▶sub addheader_callback {
Subroutine addheader_callback: called CC=66 SCAR=44.5
Pod documented
142 61 my $self = shift;
143 61 my $handler = shift;
144 61 ▶ return if $self->{'failmode'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failmode"} 0 61 61
145  
146 61 my $config = $self->_config();
147  
148 # X-DKIM2-Snapshot names the snapshot DKIM2Verify took of this message on
149 # this host. It never leaves: remove it from every message, signed or
150 # not, and keep the token (one, well-formed) for the diff below.
151 61 ▶ my @tokens = map { /\AX-DKIM2-Snapshot[ \t]*:\s*(\S*)/i ? ($1) : () } @{ $self->{'headers'} || [] };
Branch: 1 branch
BranchTrueFalseTotal
/\AX-DKIM2-Snapshot[ \t]*:\s*(\S*)/iu ? : 13 543 556
Condition50%1/2
$self->{'headers'} || []
l!l
610
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
152 61 $self->change_header( 'X-DKIM2-Snapshot', $_, q{} ) for reverse 1 .. @tokens;
153 61 ▶ $self->{'_snapshot_token'} = ( @tokens == 1 && $tokens[0] =~ /\A[0-9a-f]{32}\z/ ) ? $tokens[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@tokens == 1 && $tokens[0] =~ /\A[0-9a-f]{32}\z/u ? : 13 48 61
Condition66%2/3
@tokens == 1 and $tokens[0] =~ /\A[0-9a-f]{32}\z/u
!ll&&!rl&&r
48013
MC/DC50%1/2
@tokens == 1 and $tokens[0] =~ /\A[0-9a-f]{32}\z/u
@tokens == 1 $tokens[0] =~ /\A[0-9a-f]{32}\z/u
Truth table66%2/3
A and B
A= @tokens == 1B= $tokens[0] =~ /\A[0-9a-f]{32}\z/u
ABresult
0X0
100
111
154  
155 # X-DKIM2-Exploded: a list manager on this host (Mailman, Sympa) marks
156 # each distribution copy, asking for the -08 "exploded" flag (section
157 # 12.5). Its presence, whatever the value, is the request. It never
158 # leaves either.
159 61 ▶ my $exploded = grep { /\AX-DKIM2-Exploded[ \t]*:/i } @{ $self->{'headers'} || [] };
Condition50%1/2
$self->{'headers'} || []
l!l
610
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
160 61 $self->change_header( 'X-DKIM2-Exploded', $_, q{} ) for reverse 1 .. $exploded;
161 # A List-Id says the same without being asked (a list manager that adds
162 # no hint, such as stock Mailman): a list sent this copy, to more than
163 # one address. The Signer drops the flag from a first signature.
164 61 ▶ $exploded ||= grep { /\AList-Id[ \t]*:/i } @{ $self->{'headers'} || [] };
Condition80%4/5
$exploded ||= grep({/\AList-Id[ \t]*:/iu;} @{$self->{"headers"} or [];})
l!l&&r!l&&!r
2653
$self->{'headers'} || []
l!l
590
MC/DC100%2/2
$exploded ||= grep({/\AList-Id[ \t]*:/iu;} @{$self->{"headers"} or [];})
$exploded grep({/\AList-Id[ \t]*:/iu;} @{$self->{"headers"} or [];})
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table100%3/3
A ||= B
A= $explodedB= grep({/\AList-Id[ \t]*:/iu;} @{$self->{"headers"} or [];})
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
165  
166 # Determine if we should sign this message
167 61 my $should_sign = 0;
168 61 ▶ if ( $config->{'sign_authenticated'} && $self->is_authenticated() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"sign_authenticated"} and $self->is_authenticated) 40 21 61
Condition100%3/3
$config->{"sign_authenticated"} and $self->is_authenticated
!ll&&!rl&&r
12040
MC/DC100%2/2
$config->{"sign_authenticated"} and $self->is_authenticated
$config->{"sign_authenticated"} $self->is_authenticated
Truth table100%3/3
A and B
A= $config->{"sign_authenticated"}B= $self->is_authenticated
ABresult
0X0
100
111
169 40 $should_sign = 1;
170 }
171 61 ▶ if ( $config->{'sign_local'} && $self->is_local_ip_address() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"sign_local"} and $self->is_local_ip_address) 19 42 61
Condition100%3/3
$config->{"sign_local"} and $self->is_local_ip_address
!ll&&!rl&&r
14119
MC/DC100%2/2
$config->{"sign_local"} and $self->is_local_ip_address
$config->{"sign_local"} $self->is_local_ip_address
Truth table100%3/3
A and B
A= $config->{"sign_local"}B= $self->is_local_ip_address
ABresult
0X0
100
111
172 19 $should_sign = 1;
173 }
174 61 ▶ return unless $should_sign;
Branch: 1 branch
BranchTrueFalseTotal
unless $should_sign 2 59 61
175  
176 # Determine the signing domain from the envelope sender.
177 59 ▶ my $env_from = $self->{'env_from'} || '';
Condition50%1/2
$self->{'env_from'} || ''
l!l
590
MC/DC0%0/1
$self->{'env_from'} || ''
$self->{'env_from'}
Truth table50%1/2
A
A= $self->{'env_from'}
Aresult
11
00
178 59 $env_from =~ s/^<//;
179 59 $env_from =~ s/>$//;
180 59 my $sign_domain;
181 59 ▶ if ( $env_from =~ /\@(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_from =~ /\@(.+)$/u) 57 2 59
182 57 $sign_domain = lc $1;
183 }
184 # Null sender (MAIL FROM <>) — e.g. a Postfix-generated bounce/DSN. There is
185 # no envelope domain to sign for, so fall back to the From: header domain
186 # (typically MAILER-DAEMON@<host>). We only sign if it resolves to a key,
187 # i.e. it is genuinely one of our own bounces.
188 59 ▶ if ( !$sign_domain && $env_from eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sign_domain and $env_from eq "") 2 57 59
Condition66%2/3
not $sign_domain and $env_from eq ""
!ll&&!rl&&r
5702
MC/DC50%1/2
not $sign_domain and $env_from eq ""
not $sign_domain $env_from eq ""
Truth table66%2/3
not(A) and B
A= not $sign_domainB= $env_from eq ""
ABresult
0X0
100
111
189 2 $sign_domain = $self->_from_header_domain();
190 }
191 59 ▶ return unless $sign_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $sign_domain 0 59 59
192  
193 # Look up signing config for this domain
194 59 my $sign_config = $self->_get_sign_config($sign_domain);
195 59 ▶ unless ($sign_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sign_config) 12 47 59
196 12 $self->dbgout( 'DKIM2Sign', "No signing config for $sign_domain", LOG_DEBUG );
197 12 $self->_remove_unsigned_instances($handler);
198 12 return;
199 }
200  
201 47 eval {
202 47 my $selector = $sign_config->{selector};
203 47 my $key;
204 47 ▶ if ( $sign_config->{key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sign_config->{'key'}) { } 47 0 47
elsif ($sign_config->{'keyfile'}) { } 0 0 0
205 # Inline PEM key data (may be base64-encoded)
206 47 my $pem = $sign_config->{key};
207 47 ▶ $pem = decode_base64($pem) unless $pem =~ /^-----/;
Branch: 1 branch
BranchTrueFalseTotal
unless $pem =~ /^-----/u 0 47 47
208 47 require Crypt::PK::RSA;
209 47 $key = Crypt::PK::RSA->new(\$pem);
210 }
211 elsif ( $sign_config->{keyfile} ) {
212 0 $key = load_private_key($sign_config->{keyfile});
213 }
214 else {
215 0 die "No key or keyfile in signing config for $sign_domain";
216 }
217  
218 # Build the full message (pre_headers + original headers + add_headers + body)
219 47 my $EOL = "\015\012";
220 47 my $message_data = '';
221  
222 # pre_headers from handler (reversed as they prepend in reverse)
223 47 ▶ foreach my $h (reverse @{$handler->{pre_headers} || []}) {
Condition50%1/2
$handler->{'pre_headers'} || []
l!l
470
MC/DC0%0/1
$handler->{'pre_headers'} || []
$handler->{'pre_headers'}
Truth table50%1/2
A
A= $handler->{'pre_headers'}
Aresult
11
00
224 0 $message_data .= _fmtheader($h);
225 }
226  
227 # Original headers (less X-DKIM2-Snapshot and X-DKIM2-Exploded,
228 # removed above)
229 47 ▶ foreach my $chunk (grep { !/\AX-DKIM2-(?:Snapshot|Exploded)[ \t]*:/i } @{$self->{'headers'} || []}) {
Condition50%1/2
$self->{'headers'} || []
l!l
470
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
230 427 $message_data .= $chunk;
231 }
232  
233 # post-headers from handler
234 47 ▶ foreach my $h (@{$handler->{add_headers} || []}) {
Condition50%1/2
$handler->{'add_headers'} || []
l!l
470
MC/DC0%0/1
$handler->{'add_headers'} || []
$handler->{'add_headers'}
Truth table50%1/2
A
A= $handler->{'add_headers'}
Aresult
11
00
235 0 $message_data .= _fmtheader($h);
236 }
237  
238 # End of headers + body
239 47 $message_data .= $EOL;
240 47 foreach my $chunk (@{$self->{'body'}}) {
241 108 $message_data .= $chunk;
242 }
243  
244 # Compute Message-Instance if configured. Nothing reaches the wire
245 # until the gate below has passed and the signer has signed: a
246 # refused message goes out as it came in (apart from X-DKIM2-Info),
247 # as with bin/dkim2-milter.
248 47 my @mi_removals;
249 my $mi_value;
250 47 ▶ if ( $config->{'add_message_instance'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"add_message_instance"}) 47 0 47
251 47 delete $self->{'_stripped_mi_versions'};
252 47 delete $self->{'_clean_message_data'};
253 47 delete $self->{'_restarted_mi'};
254 47 my $mi = $self->_compute_message_instance($message_data, $config);
255 47 ▶ if ( $self->{'_stripped_mi_versions'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_stripped_mi_versions"}) 6 41 47
256 # Broken intermediate MIs were stripped; use the cleaned message data
257 6 ▶ $message_data = $self->{'_clean_message_data'} // $message_data;
Condition33%1/3
$self->{"_clean_message_data"} // $message_data
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"_clean_message_data"} // $message_data
$self->{"_clean_message_data"} $message_data
Truth table33%1/3
A // B
A= $self->{"_clean_message_data"}B= $message_data
ABresult
1X1
011
000
258 # Record the stripped versions so the wire message can be
259 # cleaned up once we have signed
260 6 @mi_removals = @{$self->{'_stripped_mi_versions'}};
261 6 delete $self->{'_stripped_mi_versions'};
262 6 delete $self->{'_clean_message_data'};
263 }
264 47 ▶ if ($mi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mi) 20 27 47
265 20 $mi_value = $mi->as_header_value;
266 # Prepend MI header (will be included when we re-feed the signer)
267 20 $message_data = "Message-Instance: $mi_value$EOL" . $message_data;
268 }
269 }
270  
271 # Build SMTP params
272 my %signer_args = (
273 Domain => $sign_domain,
274 Selector => $selector,
275 Key => $key,
276 ( $exploded ? ( Exploded => 1 ) : () ),
277 # -09 contract E3.4: a d= below the From domain signs only for
278 # a parent this domain's config lists; no DNS lookup.
279 ParentFromCallback => Mail::DKIM2::Address::parent_from_matcher(
280 47 ▶ @{ $sign_config->{parent_from_domains} || [] }),
Branch: 1 branch
BranchTrueFalseTotal
$exploded ? : 5 42 47
Condition100%2/2
$sign_config->{'parent_from_domains'} || []
l!l
146
MC/DC100%1/1
$sign_config->{'parent_from_domains'} || []
$sign_config->{'parent_from_domains'}
Truth table100%2/2
A
A= $sign_config->{'parent_from_domains'}
Aresult
11
00
281 );
282  
283 # Pin t= when configured. Only the test suite sets this, so that the
284 # fixtures it writes to tests/expected/ are byte-reproducible instead
285 # of changing on every run; in production it is absent and the signer
286 # uses the current time.
287 47 ▶ if ( $config->{'signature_timestamp'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"signature_timestamp"}) 18 29 47
288 18 $signer_args{Timestamp} = $config->{'signature_timestamp'};
289 }
290  
291 47 ▶ if ( $config->{'record_smtp_params'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"record_smtp_params"}) 47 0 47
292 47 ▶ $signer_args{MailFrom} = ( $env_from ne '' ) ? $env_from : '<>';
Branch: 1 branch
BranchTrueFalseTotal
$env_from ne '' ? : 46 1 47
293 47 ▶ if ( @{$self->{'env_rcpt'} || []} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"env_rcpt"} or [];}) 47 0 47
Condition50%1/2
$self->{'env_rcpt'} || []
l!l
470
MC/DC0%0/1
$self->{'env_rcpt'} || []
$self->{'env_rcpt'}
Truth table50%1/2
A
A= $self->{'env_rcpt'}
Aresult
11
00
294 47 my @cleaned = map { my $r = $_; $r =~ s/^<//; $r =~ s/>$//; $r } @{$self->{'env_rcpt'}};
295 47 $signer_args{RcptTo} = \@cleaned;
296 }
297 }
298  
299 # The signing decision, shared with bin/dkim2-milter and bin/dkim2sign
300 # (Mail::DKIM2::Gate), made on the message exactly as we would sign it
301 # (so including our own new Message-Instance, if any): the upstream
302 # DKIM2-Signatures must verify (an unsigned Message-Instance above the
303 # top signature -- ours, or a list manager's on this host -- is
304 # allowed); the Message-Instance chain must match the content and undo
305 # cleanly (an upstream that emitted a non-reversible Recipe would give
306 # a signature that fails at every recipient); a top signature with nd=
307 # may be extended only by the domain it names. An UNSIGNED instance
308 # with a null body Recipe -- this hop's own -- is signed (0.29, -09
309 # contract E2); a null that arrived signed fails the upstream
310 # verification and is refused. A message with no upstream chain is
311 # signed as before.
312 47 $self->_deprecated_allow_null($config);
313 my $gate = Mail::DKIM2::Gate->check($message_data,
314 SigningDomain => $sign_domain,
315 SkipTimestampCheck => $config->{'skip_timestamp_check'} ? 1 : 0,
316 47 ▶ IgnorePrefixes => $config->{'ignore_header_prefixes'},
Branch: 1 branch
BranchTrueFalseTotal
$config->{'skip_timestamp_check'} ? : 47 0 47
317 $self->_gate_key_source($config));
318 47 $self->check_timeout();
319 47 ▶ unless ( $gate->{ok} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gate->{"ok"}) 17 30 47
320 17 ▶ my $reason = $gate->{reason} // 'refused';
Condition50%1/2
$gate->{"reason"} // "refused"
l!l
170
MC/DC0%0/1
$gate->{"reason"} // "refused"
$gate->{"reason"}
Truth table50%1/2
A
A= $gate->{"reason"}
Aresult
11
00
321 17 $self->metric_count( 'dkim2_sign_total', { 'result' => $reason } );
322 17 $self->dbgout( 'DKIM2Sign',
323 "Not signing for $sign_domain: $gate->{message}", LOG_INFO );
324 # -09 section 13: a message carrying an upstream DKIM2-Signature
325 # that this hop cannot sign MUST NOT be sent on. As
326 # bin/dkim2-milter: 550 5.7.1 with the reason (section 13.3).
327 17 ▶ if ( grep { /\ADKIM2-Signature[ \t]*:/i } @{ $self->{'headers'} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\ADKIM2-Signature[ \t]*:/iu;} @{$self->{"headers"} or [];}) 14 3 17
Condition50%1/2
$self->{'headers'} || []
l!l
170
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
328 14 ( my $text = "not signing: $gate->{message}" ) =~ s/[\r\n]+/ /g;
329 14 $self->reject_mail( "550 5.7.1 $text" );
330 14 return;
331 }
332 # As bin/dkim2-milter: a broken upstream chain is already reported
333 # by the verifier's Authentication-Results; the two refusals that
334 # are this hop's own business are flagged on the message.
335 3 ▶ if ( $reason ne 'upstream-chain' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reason ne "upstream-chain") 3 0 3
336 3 push @{$handler->{pre_headers}}, {
337 'field' => 'X-DKIM2-Info',
338 'value' => _dkim2_info("not-signed=$reason"),
339 };
340 }
341 3 $self->_remove_unsigned_instances($handler);
342 3 return;
343 }
344  
345 # Create the signer and feed the message
346 30 my $signer = Mail::DKIM2::Signer->new(%signer_args);
347 30 $signer->PRINT($message_data);
348 30 $signer->CLOSE();
349 30 $self->check_timeout();
350  
351 30 ▶ my $sig_result = $signer->result // 'none';
Condition50%1/2
$signer->result // "none"
l!l
300
MC/DC0%0/1
$signer->result // "none"
$signer->result
Truth table50%1/2
A
A= $signer->result
Aresult
11
00
352 30 ▶ $self->dbgout( 'DKIM2SignResult', $signer->result_detail // 'none', LOG_DEBUG );
Condition50%1/2
$signer->result_detail // "none"
l!l
300
MC/DC0%0/1
$signer->result_detail // "none"
$signer->result_detail
Truth table50%1/2
A
A= $signer->result_detail
Aresult
11
00
353  
354 30 ▶ if ( $sig_result eq 'signed' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_result eq 'signed') { } 28 2 30
355 28 $self->_delete_mi_versions(@mi_removals);
356 28 ▶ if ( @mi_removals && $self->{'_restarted_mi'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mi_removals and $self->{"_restarted_mi"}) 4 24 28
Condition100%3/3
@mi_removals and $self->{"_restarted_mi"}
!ll&&!rl&&r
2314
MC/DC100%2/2
@mi_removals and $self->{"_restarted_mi"}
@mi_removals $self->{"_restarted_mi"}
Truth table100%3/3
A and B
A= @mi_removalsB= $self->{"_restarted_mi"}
ABresult
0X0
100
111
357 4 push @{$handler->{pre_headers}}, {
358 'field' => 'X-DKIM2-Info',
359 'value' => _dkim2_info('restarted-unsigned-mi='
360 4 . join( ',', map { "m=$_" } @mi_removals )),
361 };
362 }
363 28 ▶ if ( defined $mi_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mi_value) 18 10 28
364 18 push @{$handler->{pre_headers}}, {
365 'field' => 'Message-Instance',
366 'value' => $mi_value,
367 };
368 }
369  
370 # Extract the DKIM2-Signature header
371 28 my $sig_header = $signer->as_string();
372 # Strip the "DKIM2-Signature: " prefix
373 28 $sig_header =~ s/^DKIM2-Signature:\s*//;
374  
375 28 push @{$handler->{pre_headers}}, {
376 'field' => 'DKIM2-Signature',
377 'value' => $sig_header,
378 };
379  
380 # Informational, as bin/dkim2-milter: we signed over this hop's
381 # own (unsigned) null body Recipe.
382 28 my $over_null = $gate->{unsigned_null};
383 28 ▶ if ( $over_null ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($over_null) 6 22 28
384 6 push @{$handler->{pre_headers}}, {
385 'field' => 'X-DKIM2-Info',
386 'value' => _dkim2_info('null-body-recipe'),
387 };
388 }
389  
390 28 $self->metric_count( 'dkim2_sign_total', { 'result' => 'signed' } );
391 28 ▶ $self->dbgout( 'DKIM2Sign', "Signed for $sign_domain ($selector)"
Branch: 1 branch
BranchTrueFalseTotal
$over_null ? : 6 22 28
392 . ( $over_null ? ' over a null body Recipe' : '' ), LOG_INFO );
393 }
394 else {
395 # The Signer refuses (result fail) rather than dying: a From that
396 # is not Authentic for this domain (-09 section 11), or a
397 # DKIM2-Signature it cannot key -- which the gate has already
398 # refused. Either way nothing is added to the message, and one
399 # carrying an upstream DKIM2-Signature is not sent on (section 13).
400 2 $self->metric_count( 'dkim2_sign_total', { 'result' => 'error' } );
401 2 ▶ $self->log_error( "DKIM2 signing failed: " . ($signer->result_detail // 'no result') );
Condition50%1/2
$signer->result_detail // "no result"
l!l
20
MC/DC0%0/1
$signer->result_detail // "no result"
$signer->result_detail
Truth table50%1/2
A
A= $signer->result_detail
Aresult
11
00
402 # A list posting domain that is not the signing domain (or
403 # below it) has every rewritten post refused here: say which.
404 2 ▶ if ( ( $signer->details // '' ) =~ /From header field is not Authentic/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($signer->details // "") =~ /From header field is not Authentic/u) 2 0 2
Condition50%1/2
$signer->details // ""
l!l
20
MC/DC0%0/1
$signer->details // ""
$signer->details
Truth table50%1/2
A
A= $signer->details
Aresult
11
00
405 2 ▶ my @from = map { /\AFrom[ \t]*:(.*)\z/is ? $1 : () } @{ $self->{'headers'} || [] };
Branch: 1 branch
BranchTrueFalseTotal
/\AFrom[ \t]*:(.*)\z/isu ? : 2 14 16
Condition50%1/2
$self->{'headers'} || []
l!l
20
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
406 2 $self->log_error( from_refusal_note( $sign_domain, @from ) );
407 }
408 2 ▶ if ( grep { /\ADKIM2-Signature[ \t]*:/i } @{ $self->{'headers'} || [] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\ADKIM2-Signature[ \t]*:/iu;} @{$self->{"headers"} or [];}) 0 2 2
Condition50%1/2
$self->{'headers'} || []
l!l
20
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
409 0 ▶ ( my $text = 'not signing: ' . ( $signer->details // 'signer refused' ) ) =~ s/[\r\n]+/ /g;
Condition0%0/2
$signer->details // "signer refused"
l!l
00
MC/DC0%0/1
$signer->details // "signer refused"
$signer->details
Truth table0%0/2
A
A= $signer->details
Aresult
11
00
410 0 $self->reject_mail( "550 5.7.1 $text" );
411 0 return;
412 }
413 2 $self->_remove_unsigned_instances($handler);
414 }
415 };
416 47 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 47 47
417 0 $self->handle_exception( $error );
418 0 $self->log_error( 'DKIM2 Sign Error ' . $error );
419 0 $self->metric_count( 'dkim2_sign_total', { 'result' => 'error' } );
420 }
421 }
422  
423 # Not signing a message this instance would sign: remove every
424 # Message-Instance no DKIM2-Signature on it covers (m= above every
425 # signature's m=), as bin/dkim2-milter does. Left in, every receiver reports
426 # "m=N is not signed"; without them an unsigned post has no DKIM2 at all and
427 # a signed one fails honestly against the last signature.
428 17 ▶sub _remove_unsigned_instances {
Subroutine _remove_unsigned_instances: called CC=13 SCAR=26.0
429 17 my ( $self, $handler ) = @_;
430 17 my $covered = 0;
431 17 ▶ for my $chunk ( @{ $self->{'headers'} || [] } ) {
Condition50%1/2
$self->{'headers'} || []
l!l
170
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
432 144 ▶ next unless $chunk =~ /\ADKIM2-Signature[ \t]*:(.*)\z/is;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /\ADKIM2-Signature[ \t]*:(.*)\z/isu 136 8 144
433 8 my ($m) = $1 =~ /(?:\A|;)\s*m\s*=\s*(\d+)/;
434 8 ▶ $covered = $m if defined $m && $m > $covered;
Branch: 1 branch
BranchTrueFalseTotal
if defined $m and $m > $covered 8 0 8
Condition33%1/3
defined $m and $m > $covered
!ll&&!rl&&r
008
MC/DC0%0/2
defined $m and $m > $covered
defined $m $m > $covered
Truth table33%1/3
A and B
A= defined $mB= $m > $covered
ABresult
0X0
100
111
435 }
436 17 my ( $n, @gone ) = ( 0 );
437 17 ▶ for my $chunk ( @{ $self->{'headers'} || [] } ) {
Condition50%1/2
$self->{'headers'} || []
l!l
170
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
438 144 ▶ next unless $chunk =~ /\AMessage-Instance[ \t]*:(.*)\z/is;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /\AMessage-Instance[ \t]*:(.*)\z/isu 124 20 144
439 20 $n++;
440 20 ( my $val = $1 ) =~ s/\A\s+//;
441 20 ▶ my $v = extract_mi_version($val) // 0;
Condition50%1/2
extract_mi_version($val) // 0
l!l
200
MC/DC0%0/1
extract_mi_version($val) // 0
extract_mi_version($val)
Truth table50%1/2
A
A= extract_mi_version($val)
Aresult
11
00
442 20 ▶ push @gone, [ $n, $v ] if $v > $covered;
Branch: 1 branch
BranchTrueFalseTotal
if $v > $covered 12 8 20
443 }
444 17 ▶ return unless @gone;
Branch: 1 branch
BranchTrueFalseTotal
unless @gone 9 8 17
445 8 $self->change_header( 'Message-Instance', $_->[0], q{} ) for reverse @gone;
446 8 my $list = join( ',', map { "m=$_->[1]" } sort { $a->[1] <=> $b->[1] } @gone );
447 8 push @{ $handler->{pre_headers} }, {
448 'field' => 'X-DKIM2-Info',
449 'value' => _dkim2_info("removed-unsigned-mi=$list"),
450 };
451 8 $self->dbgout( 'DKIM2MI', "Not signing: removed unsigned $list", LOG_INFO );
452 }
453  
454 # Delete the Message-Instance fields with these m= values from the message as
455 # it goes out, through the framework's change_header() (SMFIR_CHGHEADER with
456 # an empty value). The index is the field's position among the message's own
457 # Message-Instance fields, 1-based; deleting from the last one up keeps the
458 # earlier indexes valid. A version not among the received fields (another
459 # handler's) is not ours to delete.
460 28 ▶sub _delete_mi_versions {
Subroutine _delete_mi_versions: called CC=8 SCAR=20.8
461 28 my ( $self, @versions ) = @_;
462 28 ▶ return unless @versions;
Branch: 1 branch
BranchTrueFalseTotal
unless @versions 23 5 28
463 5 my %want = map { $_ => 1 } @versions;
464 5 my ( $n, @delete ) = ( 0 );
465 5 ▶ for my $chunk ( @{ $self->{'headers'} || [] } ) {
Condition50%1/2
$self->{'headers'} || []
l!l
50
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
466 49 ▶ next unless $chunk =~ /\AMessage-Instance[ \t]*:(.*)\z/is;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /\AMessage-Instance[ \t]*:(.*)\z/isu 40 9 49
467 9 $n++;
468 9 ( my $val = $1 ) =~ s/\A\s+//;
469 9 my $v = extract_mi_version($val);
470 9 ▶ push @delete, [ $n, $v ] if defined $v && $want{$v};
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $want{$v} 8 1 9
Condition66%2/3
defined $v and $want{$v}
!ll&&!rl&&r
018
MC/DC50%1/2
defined $v and $want{$v}
defined $v $want{$v}
Truth table66%2/3
A and B
A= defined $vB= $want{$v}
ABresult
0X0
100
111
471 }
472 5 for my $d ( reverse @delete ) {
473 8 $self->change_header( 'Message-Instance', $d->[0], q{} );
474 8 $self->dbgout( 'DKIM2MI',
475 "Deleted broken Message-Instance m=$d->[1] (field $d->[0]) from the message",
476 LOG_INFO );
477 }
478 }
479  
480 1 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod documented
481 1 my ( $self ) = @_;
482 1 delete $self->{'failmode'};
483 1 delete $self->{'headers'};
484 1 delete $self->{'body'};
485 1 delete $self->{'carry'};
486 1 delete $self->{'env_from'};
487 1 delete $self->{'env_rcpt'};
488 1 delete $self->{'sign_domain'};
489 }
490  
491 # The Message-Instance this hop adds before it signs, or undef when none is
492 # needed or none can be justified (Mail::DKIM2::Hop decides; its POD has the
493 # cases). The message as it arrived is the snapshot DKIM2Verify took of this
494 # very message, named by its X-DKIM2-Snapshot token -- never one found by
495 # Message-Instance value (a replayed instance over an edited body would find
496 # a different message's, and the edit would be signed as this hop's own).
497 #
498 # When instances have to go from the message (wrong ones a hop between
499 # added, or all of them for a restart as one m=1), _stripped_mi_versions
500 # lists them and _clean_message_data is the message without them.
501 47 ▶sub _compute_message_instance {
Subroutine _compute_message_instance: called CC=12 SCAR=25.1
502 47 my ( $self, $message_data, $config ) = @_;
503  
504 47 my $mi = eval {
505 47 my $snapshot;
506 47 my $token = $self->{'_snapshot_token'};
507 47 ▶ if ( $config->{'snapshot_directory'} && defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"snapshot_directory"} and defined $token) 8 39 47
Condition100%3/3
$config->{"snapshot_directory"} and defined $token
!ll&&!rl&&r
3548
MC/DC100%2/2
$config->{"snapshot_directory"} and defined $token
$config->{"snapshot_directory"} defined $token
Truth table100%3/3
A and B
A= $config->{"snapshot_directory"}B= defined $token
ABresult
0X0
100
111
508 $snapshot = Mail::DKIM2::MessageStore->new(
509 8 directory => $config->{'snapshot_directory'},
510 )->fetch("X-DKIM2-Snapshot:$token");
511 }
512  
513 my $next = Mail::DKIM2::Hop->next_instance( $message_data,
514 Outbound => 1,
515 ( defined $snapshot ? ( Snapshot => $snapshot ) : () ),
516 ( defined $config->{'ignore_header_prefixes'}
517 ? ( IgnorePrefixes => $config->{'ignore_header_prefixes'} ) : () ),
518 ( defined $config->{'max_recipe_literals'}
519 47 ▶ ? ( MaxRecipeLiterals => $config->{'max_recipe_literals'} ) : () ),
Branch: 3 branches
BranchTrueFalseTotal
defined $snapshot ? : 8 39 47
defined $config->{'ignore_header_prefixes'} ? : 47 0 47
defined $config->{'max_recipe_literals'} ? : 47 0 47
520 );
521 $self->dbgout( 'DKIM2MI', $next->{'detail'},
522 47 ▶ ( $next->{'instance'} && !@{ $next->{'removed'} } ) || $next->{'reason'} eq 'unchanged'
Branch: 1 branch
BranchTrueFalseTotal
$next->{'instance'} && !@{$next->{'removed'};} || $next->{'reason'} eq 'unchanged' ? : 35 12 47
Condition100%6/6
$next->{"instance"} and not @{$next->{"removed"};} or $next->{"reason"} eq "unchanged"
l!l&&r!l&&!r
142112
$next->{"instance"} and not @{$next->{"removed"};}
!ll&&!rl&&r
27614
MC/DC100%3/3
$next->{"instance"} and not @{$next->{"removed"};} or $next->{"reason"} eq "unchanged"
$next->{"instance"} not @{$next->{"removed"};} $next->{"reason"} eq "unchanged"
Truth table80%4/5
A and not(B) or C
A= $next->{"instance"}B= not @{$next->{"removed"};}C= $next->{"reason"} eq "unchanged"
ABCresult
11X1
0X11
1011
0X00
1000
523 ? LOG_DEBUG : LOG_INFO );
524 47 ▶ if ( @{ $next->{'removed'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$next->{"removed"};}) 6 41 47
525 6 $self->{'_stripped_mi_versions'} = $next->{'removed'};
526 6 $self->{'_clean_message_data'} = $next->{'message'};
527 6 ▶ $self->{'_restarted_mi'} = 1 if $next->{'restart'};
Branch: 1 branch
BranchTrueFalseTotal
if $next->{"restart"} 5 1 6
528 }
529 47 $next->{'instance'};
530 };
531 47 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 47 47
532 0 $self->handle_exception( $error );
533 0 $self->log_error( 'DKIM2 MI Error ' . $error );
534 0 return;
535 }
536  
537 47 return $mi;
538 }
539  
540 # X-DKIM2-Info value per draft-gondwana-dkim2-debug-header-01 (as
541 # bin/dkim2-milter): a tag-list, every tag (the last included) followed by
542 # ";", a ";" inside a value becoming ",". Folded only after a ";" or a ",",
543 # never inside a token. X-DKIM2-Info is excluded from the header hash by the
544 # x-* rule, so it can never affect a signature.
545 21 ▶sub _dkim2_info {
Subroutine _dkim2_info: called CC=2 SCAR=7.0
546 21 my ($action, %extra) = @_;
547 21 my @tags = ("draft=" . DKIM2_DRAFT, "repo=" . DKIM2_REPO,
548 "date=" . DKIM2_DATE, "sw=" . DKIM2_SOFTWARE, "action=$action");
549 0 push @tags, "$_=$extra{$_}" for grep { defined $extra{$_} } sort keys %extra;
550 21 my $val = join ' ', map { (my $t = $_) =~ s/;/,/g; "$t;" } @tags;
551 21 (my $folded = fold_header("X-DKIM2-Info: $val", undef, delimiters_only => 1))
552 =~ s/^X-DKIM2-Info:\s*//;
553 21 $folded =~ s/\015?\012\z//;
554 21 return $folded;
555 }
556  
557 # Where the gate's Verifier gets upstream public keys: dns_overrides (a
558 # dns.json, for testing) when set, else the milter's own resolver (so its
559 # timeouts and caching apply), as DKIM2Verify does.
560 # allow_null_body_recipe is retired (0.29, -09 contract E2): warn once per
561 # process when a configuration still sets it.
562 4 our $WARNED_ALLOW_NULL;
563 47 ▶sub _deprecated_allow_null {
Subroutine _deprecated_allow_null: called CC=3 SCAR=11.0
564 47 my ($self, $config) = @_;
565 47 ▶ return unless defined $config->{'allow_null_body_recipe'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->{"allow_null_body_recipe"} 44 3 47
566 3 ▶ return if $WARNED_ALLOW_NULL++;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Milter::Authentication::Handler::DKIM2Sign::WARNED_ALLOW_NULL++ 1 2 3
567 2 $self->log_error('DKIM2Sign: allow_null_body_recipe is deprecated and does nothing; '
568 . 'an unsigned null body Recipe is always signed (Mail::DKIM2 0.29)');
569 }
570  
571 47 ▶sub _gate_key_source {
Subroutine _gate_key_source: called CC=4 SCAR=16.2
572 47 my ( $self, $config ) = @_;
573 47 ▶ if ( my $file = $config->{'dns_overrides'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $config->{"dns_overrides"}) 47 0 47
574 47 require JSON;
575 47 ▶ open my $fh, '<', $file or die "dns_overrides $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "dns_overrides ${file}: $!\n" 47 0 47
576 47 my $dns_data = JSON::decode_json( do { local $/; <$fh> } );
577 18 ▶ return ( PubkeyCallback => sub {
Subroutine __ANON__: called CC=7 SCAR=23.8
578 18 my ( $signature, $idx ) = @_;
579 18 ▶ my $sel = $signature->selector( $idx // 0 );
Condition50%1/2
$idx // 0
l!l
180
MC/DC0%0/1
$idx // 0
$idx
Truth table50%1/2
A
A= $idx
Aresult
11
00
580 18 my $dom = $signature->domain;
581 my $entry = ( $dom && $sel && $dns_data->{$dom} )
582 18 ▶ ? $dns_data->{$dom}{"$sel._domainkey"} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$dom && $sel && $dns_data->{$dom} ? : 18 0 18
Condition33%2/6
$dom and $sel and $dns_data->{$dom}
!ll&&!rl&&r
0018
$dom and $sel
!ll&&!rl&&r
0018
MC/DC0%0/3
$dom and $sel and $dns_data->{$dom}
$dom $sel $dns_data->{$dom}
Truth table25%1/4
A and B and C
A= $domB= $selC= $dns_data->{$dom}
ABCresult
0XX0
10X0
1100
1111
583 18 ▶ return unless $entry && $entry->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $entry and $entry->[0] 0 18 18
Condition33%1/3
$entry and $entry->[0]
!ll&&!rl&&r
0018
MC/DC0%0/2
$entry and $entry->[0]
$entry $entry->[0]
Truth table33%1/3
A and B
A= $entryB= $entry->[0]
ABresult
0X0
100
111
584 18 return parse_dkim_pubkey( $entry->[0][1] );
585 47 }, $self->_policy_source($config) );
586 }
587 0 my $resolver = $self->get_object('resolver');
588 0 ▶ return $resolver ? ( Resolver => $resolver ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$resolver ? : 0 0 0
589 }
590  
591 # Where the Domain Policy Record (-09 section 3.8) comes from for the gate's
592 # upstream verification: dns_overrides' records for the domains it lists
593 # (testing), else the milter's own resolver. (The Signer never looks it up:
594 # parent_from_domains, contract E3.4.)
595 47 ▶sub _policy_source {
Subroutine _policy_source: called CC=4 SCAR=20.1
596 47 my ( $self, $config ) = @_;
597 47 ▶ if ( my $file = $config->{'dns_overrides'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $config->{"dns_overrides"}) 47 0 47
598 47 require JSON;
599 47 ▶ open my $fh, '<', $file or die "dns_overrides $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "dns_overrides ${file}: $!\n" 47 0 47
600 47 my $dns_data = JSON::decode_json( do { local $/; <$fh> } );
601 0 ▶ return ( PolicyCallback => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
602 0 my ( $dom, $who ) = @_;
603 0 ▶ return [ map { $_->[1] } @{ $dns_data->{$dom}{_domainkey} || [] } ]
Condition0%0/2
$dns_data->{$dom}{'_domainkey'} || []
l!l
00
MC/DC0%0/1
$dns_data->{$dom}{'_domainkey'} || []
$dns_data->{$dom}{'_domainkey'}
Truth table0%0/2
A
A= $dns_data->{$dom}{'_domainkey'}
Aresult
11
00
604 0 ▶ if $dns_data->{$dom};
Branch: 1 branch
BranchTrueFalseTotal
if $dns_data->{$dom} 0 0 0
605 0 return $who->fetch_domain_policy($dom);
606 47 } );
607 }
608 0 my $resolver = $self->get_object('resolver');
609 0 ▶ return $resolver ? ( Resolver => $resolver ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$resolver ? : 0 0 0
610 }
611  
612 # Domain of the From: header (lower-cased) from the stored raw header chunks,
613 # or undef. Used to pick a signing domain for null-sender bounces/DSNs.
614 2 ▶sub _from_header_domain {
Subroutine _from_header_domain: called CC=5 SCAR=18.6
615 2 my ($self) = @_;
616 2 ▶ for my $h ( @{ $self->{'headers'} || [] } ) {
Condition50%1/2
$self->{'headers'} || []
l!l
20
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
617 2 ▶ next unless $h =~ /^From:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $h =~ /^From:/iu 0 2 2
618 2 ▶ return lc $1 if $h =~ /\@([\w.-]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /\@([\w.-]+)/u 2 0 2
619 0 last;
620 }
621 0 return;
622 }
623  
624 # Look up signing config for a domain (static config, then HTTP endpoint)
625 59 ▶sub _get_sign_config {
Subroutine _get_sign_config: called CC=6 SCAR=18.6
626 59 my ( $self, $domain ) = @_;
627 59 my $config = $self->_config();
628  
629 # Check static config first
630 59 ▶ my $domains = $config->{'domains'} || {};
Condition50%1/2
$config->{'domains'} || {}
l!l
590
MC/DC0%0/1
$config->{'domains'} || {}
$config->{'domains'}
Truth table50%1/2
A
A= $config->{'domains'}
Aresult
11
00
631 59 ▶ if ( my $dc = $domains->{$domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dc = $domains->{$domain}) 47 12 59
632 47 return $dc;
633 }
634  
635 # Try parent domains (e.g., sub.example.com -> example.com)
636 12 my $try = $domain;
637 12 ▶ while ( $try =~ s/^[^.]+\.// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try =~ s/^[^.]+\.//u) 23 12 35
638 23 ▶ if ( my $dc = $domains->{$try} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dc = $domains->{$try}) 0 23 23
639 0 return $dc;
640 }
641 }
642  
643 # Try HTTP endpoint if configured
644 12 ▶ if ( my $endpoint = $config->{'key_endpoint'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $endpoint = $config->{"key_endpoint"}) 0 12 12
645 0 return $self->_fetch_sign_config($endpoint, $domain);
646 }
647  
648 12 return;
649 }
650  
651 # Fetch signing config from HTTP endpoint
652 0 ▶sub _fetch_sign_config {
Subroutine _fetch_sign_config: not called CC=7 SCAR=40.3
653 0 my ( $self, $endpoint, $domain ) = @_;
654 0 my $config = $self->_config();
655 0 ▶ my $timeout = $config->{'key_endpoint_timeout'} || 5;
Condition0%0/2
$config->{'key_endpoint_timeout'} || 5
l!l
00
MC/DC0%0/1
$config->{'key_endpoint_timeout'} || 5
$config->{'key_endpoint_timeout'}
Truth table0%0/2
A
A= $config->{'key_endpoint_timeout'}
Aresult
11
00
656  
657 0 my $result = eval {
658 0 require HTTP::Tiny;
659 0 my $http = HTTP::Tiny->new( timeout => $timeout );
660 0 my $url = "$endpoint?domain=$domain";
661 0 $self->dbgout( 'DKIM2SignKeyLookup', $url, LOG_DEBUG );
662 0 my $response = $http->get($url);
663  
664 0 ▶ if ( $response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 0 0
665 0 my $data = JSON::decode_json( $response->{content} );
666 0 ▶ if ( $data->{selector} && ( $data->{key} || $data->{keyfile} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'selector'} and $data->{'key'} || $data->{'keyfile'}) { } 0 0 0
Condition0%0/6
$data->{"selector"} and $data->{"key"} || $data->{"keyfile"}
!ll&&!rl&&r
000
$data->{"key"} or $data->{"keyfile"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$data->{"selector"} and $data->{"key"} || $data->{"keyfile"}
$data->{"selector"} $data->{"key"} $data->{"keyfile"}
Truth table0%0/4
A and B or C
A= $data->{"selector"}B= $data->{"key"}C= $data->{"keyfile"}
ABCresult
0XX0
1000
11X1
1011
667 0 $self->dbgout( 'DKIM2SignKeyLookup',
668 "Got config for $domain: selector=$data->{selector}", LOG_DEBUG );
669 0 $data;
670 }
671 0 else { undef }
672 }
673 else {
674 0 $self->dbgout( 'DKIM2SignKeyLookup',
675 "Failed for $domain: $response->{status} $response->{reason}", LOG_DEBUG );
676 0 undef;
677 }
678 };
679 0 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 0 0
680 0 $self->handle_exception( $error );
681 0 $self->log_error( "DKIM2 key endpoint error for $domain: $error" );
682 }
683  
684 0 return $result;
685 }
686  
687 4 1;
688  
689 __END__
« lib/Mail/DKIM2/Validate.pm ↑ Summary lib/Mail/DKIM2/MessageInstance.pm »