lib/Mail/DKIM2/Reflector.pm

filter: Statementstmt 96.7% 323 / 334 Branchbran 72.8% 121 / 166 Conditioncond 54.8% 68 / 124 MC/DCmcdc 26.7% 19 / 71 Subroutinesub 97.5% 39 / 40 Podpod 100% 5 / 5 totaltotal 77.7% 575 / 740 CC 140 SCAR 25.8
CC
140
Cov
82%
_dkim2test_cname_ok
30.0
reflect
28.3
generate_brand
20.2
CRAP 13.2
« lib/Mail/DKIM2/Wrap.pm ↑ Summary bin/dkim2-split-lmtp »
1 package Mail::DKIM2::Reflector;
2 6 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 our $VERSION = '0.33';
5 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Mail::DKIM2::Verifier;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Mail::DKIM2::Signer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Mail::DKIM2::DSN;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Mail::DKIM2::Common qw(parse_mime DKIM2_DRAFT DKIM2_REPO DKIM2_DATE);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Mail::DKIM2::Canon qw(should_skip);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Mail::DKIM2::Fold qw(fold_header);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 our $SUBJECT_PREFIX = '[DKIM2] ';
19 6 our $FOOTER = "-- \r\nReflected and signed by the DKIM2 reflector at dkim2.com\r\n";
20 6 our $DAMAGE_LINE = "damage line, breaks the signature\r\n";
21  
22 # X-DKIM2-Info provenance, per ../spec/draft-gondwana-dkim2-debug-header-01:
23 # one field per action (verify, mi-m=<N>, sign), each directly above the
24 # header it describes. The spec version constants come from Mail::DKIM2::Common.
25 6 ▶use constant DKIM2_SOFTWARE => 'dkim2-reflector.pl';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # The value is a tag-list in the DKIM2 syntax: every tag, the last included,
28 # is followed by ";". A ";" has no escape and ends a tag, so one inside a
29 # value becomes ",".
30 89 ▶sub _dkim2_info {
Subroutine _dkim2_info: called CC=2 SCAR=6.9
31 89 my ($action, %extra) = @_;
32 89 my @tags = ("draft=" . DKIM2_DRAFT, "repo=" . DKIM2_REPO,
33 "date=" . DKIM2_DATE, "sw=" . DKIM2_SOFTWARE, "action=$action");
34 89 push @tags, "$_=$extra{$_}" for grep { defined $extra{$_} } sort keys %extra;
35 89 return join ' ', map { (my $t = $_) =~ s/;/,/g; "$t;" } @tags;
36 }
37  
38 # A complete, folded "X-DKIM2-Info: ..." line with trailing CRLF, ready to
39 # prepend directly above the header field the action added. Folded only after
40 # a ";" or a "," (Section 5), never inside a token.
41 89 ▶sub _info_line {
Subroutine _info_line: called CC=1 SCAR=0.0
42 89 my ($action, %extra) = @_;
43 89 (my $xi = fold_header("X-DKIM2-Info: " . _dkim2_info($action, %extra), undef, delimiters_only => 1))
44 =~ s/\r?\n\z//;
45 89 return "$xi\r\n";
46 }
47  
48 # "sign d=<domain> a=<algorithm>" for a DKIM2-Signature made with %sa.
49 31 ▶sub _sign_info_line {
Subroutine _sign_info_line: called CC=2 SCAR=7.2
50 31 my (%sa) = @_;
51 31 ▶ return _info_line("sign d=$sa{Domain} a=" . ($sa{Algorithm} // 'rsa-sha256'));
Condition50%1/2
$sa{"Algorithm"} // "rsa-sha256"
l!l
031
MC/DC0%0/1
$sa{"Algorithm"} // "rsa-sha256"
$sa{"Algorithm"}
Truth table50%1/2
A
A= $sa{"Algorithm"}
Aresult
11
00
52 }
53  
54 # (count, comma-separated-names) of the headers a Message-Instance hash covers,
55 # matching dkim2-milter's hc=/hn= fields.
56 30 ▶sub _header_list_for_hash {
Subroutine _header_list_for_hash: called CC=3 SCAR=11.0
57 30 my ($msg) = @_;
58 30 my @fields;
59 30 for my $h (sort { lc($a) cmp lc($b) } $msg->header_names) {
60 174 ▶ next if should_skip($h);
Branch: 1 branch
BranchTrueFalseTotal
if should_skip($h) 40 134 174
61 134 my @vals = $msg->header_raw($h);
62 134 push @fields, (lc $h) x scalar(@vals);
63 }
64 30 return (scalar(@fields), join(',', @fields));
65 }
66  
67 # RFC 5322 date string (always UTC) for the given epoch.
68 8 ▶sub _rfc2822_date {
Subroutine _rfc2822_date: called CC=1 SCAR=0.0
69 8 my ($epoch) = @_;
70 8 return POSIX::strftime('%a, %d %b %Y %H:%M:%S +0000', gmtime($epoch));
71 }
72  
73 # Low-level: sign $text with explicit Signer args, return the DKIM2-Signature
74 # header (folded, no trailing CRLF). i= is auto-assigned from existing sigs.
75 9 ▶sub _sign_with {
Subroutine _sign_with: called CC=4 SCAR=15.2
76 9 my ($text, %sa) = @_;
77 9 my $signer = Mail::DKIM2::Signer->new(%sa);
78 9 $signer->PRINT($text); $signer->CLOSE;
79 9 ▶ croak "signing failed: " . ($signer->result_detail // 'no result')
Branch: 1 branch
BranchTrueFalseTotal
unless ($signer->result // "") eq "signed" 0 9 9
Condition25%1/4
$signer->result // ""
l!l
90
$signer->result_detail // "no result"
l!l
00
MC/DC0%0/1
$signer->result // ""
$signer->result
MC/DC0%0/1
$signer->result_detail // "no result"
$signer->result_detail
Truth table50%1/2
A
A= $signer->result
Aresult
11
00
Truth table0%0/2
A
A= $signer->result_detail
Aresult
11
00
80 unless ($signer->result // '') eq 'signed';
81 9 return $signer->as_string; # "DKIM2-Signature: ..."
82 }
83  
84 # Build a fresh message (headers + body) and prepend its m=1 Message-Instance.
85 # No signature. Used by generate() and generate_brand().
86 6 ▶sub _fresh_message_text {
Subroutine _fresh_message_text: called CC=3 SCAR=11.0
87 6 my (%a) = @_;
88 6 ▶ my $now = $a{now} // time();
Condition50%1/2
$a{"now"} // time
l!l
60
MC/DC0%0/1
$a{"now"} // time
$a{"now"}
Truth table50%1/2
A
A= $a{"now"}
Aresult
11
00
89 6 ▶ my $mid = $a{message_id} // sprintf('<fresh-%d-%d@%s>', $now, $$, $a{domain});
Condition100%2/2
$a{"message_id"} // sprintf("<fresh-%d-%d\@%s>", $now, $$, $a{"domain"})
l!l
51
MC/DC100%1/1
$a{"message_id"} // sprintf("<fresh-%d-%d\@%s>", $now, $$, $a{"domain"})
$a{"message_id"}
Truth table100%2/2
A
A= $a{"message_id"}
Aresult
11
00
90 6 my $date = _rfc2822_date($now);
91 my $text =
92 "From: $a{from}\r\n"
93 . "To: $a{to}\r\n"
94 . "Subject: $a{subject}\r\n"
95 . "Date: $date\r\n"
96 . "Message-ID: $mid\r\n"
97 . "MIME-Version: 1.0\r\n"
98 . "Content-Type: text/plain; charset=utf-8\r\n"
99 . "\r\n"
100 6 . $a{body};
101 6 my $mi = Mail::DKIM2::MessageInstance->calculate(parse_mime($text));
102 6 my $miv = $mi->as_header_value;
103 6 my ($hc, $hn) = _header_list_for_hash(parse_mime($text));
104 6 return _info_line('mi-m=1', hc => $hc, hn => $hn)
105 . "Message-Instance: $miv\r\n" . $text;
106 }
107  
108 # Default explainer body for the fresh generator.
109 2 ▶sub _fresh_body {
Subroutine _fresh_body: called CC=1 SCAR=0.0
110 2 my ($domain, $sender, $date) = @_;
111 return
112 2 "Hello,\r\n\r\n"
113 . "This is a freshly-originated DKIM2 message from $domain, generated\r\n"
114 . "because you sent mail to reflector-fresh\@$domain.\r\n\r\n"
115 . "Unlike the other reflector addresses, this is NOT a forward of your\r\n"
116 . "message: it is a brand-new message with a single Message-Instance (m=1)\r\n"
117 . "and a single DKIM2-Signature (i=1), and no forwarding chain.\r\n\r\n"
118 . "Paste it into https://$domain/validate/ to see it verify.\r\n\r\n"
119 . "Requested by: $sender\r\n"
120 . "Generated at: $date\r\n\r\n"
121 . "-- \r\n"
122 . "The DKIM2 reflector at $domain\r\n";
123 }
124  
125 # generate(%args) — ORIGINATE a brand-new DKIM2 message back to the sender:
126 # a single Message-Instance (m=1) and a single DKIM2-Signature (i=1), no chain.
127 # Unlike reflect(), the incoming message is not used (the caller passes only the
128 # reply target as `sender`). From is a <domain> identity so DMARC aligns and the
129 # message lands in the inbox. An optional `body` overrides the default explainer
130 # (used by generate_brand()'s not-delegated path). See
131 # docs/superpowers/specs/2026-06-20-dkim2-reflector-fresh-design.md.
132 3 ▶sub generate {
Subroutine generate: called CC=11 SCAR=27.2
Pod documented
133 3 my (%a) = @_;
134 3 ▶ croak "need a sender" unless $a{sender};
Branch: 1 branch
BranchTrueFalseTotal
unless $a{"sender"} 0 3 3
135 3 ▶ $a{domain} //= 'dkim2.com';
Condition50%1/2
$a{"domain"} //= "dkim2.com"
l!l
30
MC/DC0%0/1
$a{"domain"} //= "dkim2.com"
$a{"domain"}
Truth table50%1/2
A
A= $a{"domain"}
Aresult
11
00
136 3 ▶ $a{selector} //= 'sel1';
Condition50%1/2
$a{"selector"} //= "sel1"
l!l
30
MC/DC0%0/1
$a{"selector"} //= "sel1"
$a{"selector"}
Truth table50%1/2
A
A= $a{"selector"}
Aresult
11
00
137 3 ▶ $a{mailfrom} //= "reflector-bounces\@$a{domain}";
Condition50%1/2
$a{"mailfrom"} //= qq[reflector-bounces\@$a{"domain"}]
l!l
30
MC/DC0%0/1
$a{"mailfrom"} //= qq[reflector-bounces\@$a{"domain"}]
$a{"mailfrom"}
Truth table50%1/2
A
A= $a{"mailfrom"}
Aresult
11
00
138 3 ▶ my $now = $a{now} // time();
Condition50%1/2
$a{"now"} // time
l!l
30
MC/DC0%0/1
$a{"now"} // time
$a{"now"}
Truth table50%1/2
A
A= $a{"now"}
Aresult
11
00
139 3 ▶ $a{timestamp} //= $now;
Condition33%1/3
$a{"timestamp"} //= $now
l!l&&r!l&&!r
030
MC/DC0%0/2
$a{"timestamp"} //= $now
$a{"timestamp"} $now
Truth table33%1/3
A //= B
A= $a{"timestamp"}B= $now
ABresult
1X1
011
000
140 3 ▶ my $body = $a{body} // _fresh_body($a{domain}, $a{sender}, _rfc2822_date($now));
Condition66%2/3
$a{"body"} // _fresh_body($a{"domain"}, $a{"sender"}, _rfc2822_date($now))
l!l&&r!l&&!r
120
MC/DC0%0/2
$a{"body"} // _fresh_body($a{"domain"}, $a{"sender"}, _rfc2822_date($now))
$a{"body"} _fresh_body($a{"domain"}, $a{"sender"}, _rfc2822_date($now))
Truth table66%2/3
A // B
A= $a{"body"}B= _fresh_body($a{"domain"}, $a{"sender"}, _rfc2822_date($now))
ABresult
1X1
011
000
141  
142 my $text = _fresh_message_text(
143 from => "\"DKIM2 Generator\" <fresh\@$a{domain}>",
144 to => $a{sender}, subject => 'Freshly generated DKIM2 message',
145 body => $body, now => $now, message_id => $a{message_id}, domain => $a{domain},
146 3 );
147  
148 # i=1 DKIM2-Signature (mf= relaxed-matches d=; rt = [sender]; no predecessor).
149 my %sa = (Domain => $a{domain}, Selector => $a{selector},
150 3 MailFrom => $a{mailfrom}, RcptTo => [ $a{sender} ], Timestamp => $a{timestamp});
151 3 ▶ $sa{Key} = $a{key} if $a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"key"} 3 0 3
152 3 ▶ $sa{KeyFile} = $a{keyfile} if $a{keyfile} && !$a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"keyfile"} and not $a{"key"} 0 3 3
Condition33%1/3
$a{"keyfile"} and not $a{"key"}
!ll&&!rl&&r
300
MC/DC0%0/2
$a{"keyfile"} and not $a{"key"}
$a{"keyfile"} not $a{"key"}
Truth table33%1/3
A and not(B)
A= $a{"keyfile"}B= not $a{"key"}
ABresult
0X0
100
111
153 3 $text = _sign_with($text, %sa) . "\r\n" . $text;
154 3 return _sign_info_line(%sa) . $text;
155 }
156  
157 # generate_dsn(%args) — return a Delivery Status Notification for the incoming
158 # message, addressed back to the sender. Used by the reflector-dsn address.
159 # The DSN is a fresh DKIM2 message signed as new: MAIL FROM <>, one
160 # Message-Instance and one DKIM2-Signature on the top message, embedding the
161 # original as a message/rfc822 part. -09 section 16.2: only for a message
162 # that is Provisionally Verified, whose top mf= is not <>, and whose top rt=
163 # names a recipient in our domain; otherwise returns undef, and in list
164 # context (undef, $why). Keys from pubkey_cb, else DNS.
165 3 ▶sub generate_dsn {
Subroutine generate_dsn: called CC=14 SCAR=28.6
Pod documented
166 3 my (%a) = @_;
167 3 ▶ croak "need a sender" unless $a{sender};
Branch: 1 branch
BranchTrueFalseTotal
unless $a{"sender"} 0 3 3
168 3 ▶ croak "need the incoming message" unless defined $a{message};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"message"} 0 3 3
169 3 ▶ $a{domain} //= 'dkim2.com';
Condition50%1/2
$a{"domain"} //= "dkim2.com"
l!l
30
MC/DC0%0/1
$a{"domain"} //= "dkim2.com"
$a{"domain"}
Truth table50%1/2
A
A= $a{"domain"}
Aresult
11
00
170 3 ▶ $a{selector} //= 'sel1';
Condition50%1/2
$a{"selector"} //= "sel1"
l!l
30
MC/DC0%0/1
$a{"selector"} //= "sel1"
$a{"selector"}
Truth table50%1/2
A
A= $a{"selector"}
Aresult
11
00
171 3 ▶ my $now = $a{now} // time();
Condition100%2/2
$a{"now"} // time
l!l
21
MC/DC100%1/1
$a{"now"} // time
$a{"now"}
Truth table100%2/2
A
A= $a{"now"}
Aresult
11
00
172  
173 my %keys = (($a{pubkey_cb} ? (PubkeyCallback => $a{pubkey_cb}) : ()),
174 3 ▶ ($a{skip_timestamp_check} ? (SkipTimestampCheck => 1) : ()));
Branch: 2 branches
BranchTrueFalseTotal
$a{'pubkey_cb'} ? : 3 0 3
$a{'skip_timestamp_check'} ? : 2 1 3
175 my ($may, $why) = Mail::DKIM2::DSN->may_generate(Message => $a{message},
176 3 Domain => $a{domain}, %keys);
177 3 ▶ return wantarray ? (undef, $why) : undef unless $may;
Branch: 2 branches
BranchTrueFalseTotal
unless $may 1 2 3
wantarray ? : 1 0 1
178  
179 2 my %sa = (Domain => $a{domain}, Selector => $a{selector}, MailFrom => '<>',
180 Timestamp => $now);
181 2 ▶ $sa{Key} = $a{key} if $a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"key"} 2 0 2
182 2 ▶ $sa{KeyFile} = $a{keyfile} if $a{keyfile} && !$a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"keyfile"} and not $a{"key"} 0 2 2
Condition33%1/3
$a{"keyfile"} and not $a{"key"}
!ll&&!rl&&r
200
MC/DC0%0/2
$a{"keyfile"} and not $a{"key"}
$a{"keyfile"} not $a{"key"}
Truth table33%1/3
A and not(B)
A= $a{"keyfile"}B= not $a{"key"}
ABresult
0X0
100
111
183 2 my $signer = Mail::DKIM2::Signer->new(%sa);
184  
185 my $out = Mail::DKIM2::DSN->generate(
186 Message => $a{message},
187 Signer => $signer,
188 To => $a{sender},
189 ReportingMTA => $a{domain},
190 Reason => $a{reason}
191 2 ▶ // 'message accepted then returned by the reflector-dsn demo address',
Condition50%1/2
$a{"reason"} // "message accepted then returned by the reflector-dsn demo address"
l!l
02
MC/DC0%0/1
$a{"reason"} // "message accepted then returned by the reflector-dsn demo address"
$a{"reason"}
Truth table50%1/2
A
A= $a{"reason"}
Aresult
11
00
192 %keys,
193 );
194 2 return $out->{raw};
195 }
196  
197 # Domain part of an email address.
198 4 ▶sub _addr_domain { my ($a) = @_; $a =~ /\@([^>]+?)>?\s*$/ ? $1 : $a }
Subroutine _addr_domain: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /\@([^>]+?)>?\s*$/u ? : 4 0 4
199  
200 # True iff dkim2test._domainkey.$domain is a CNAME to dkim2test._domainkey.dkim2.com.
201 # Live DNS (kept out of generate_brand so the message logic is testable offline).
202 0 ▶sub _dkim2test_cname_ok {
Subroutine _dkim2test_cname_ok: not called CC=5 SCAR=34.0
203 0 my ($domain) = @_;
204 0 require Net::DNS::Resolver;
205 0 my $r = Net::DNS::Resolver->new;
206 0 ▶ my $q = $r->query("dkim2test._domainkey.$domain", 'CNAME') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $q = $r->query("dkim2test._domainkey.$domain", "CNAME") or return 0 0 0 0
207 0 for my $rr ($q->answer) {
208 0 ▶ next unless $rr->type eq 'CNAME';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "CNAME" 0 0 0
209 0 (my $t = $rr->cname) =~ s/\.\z//;
210 0 ▶ return 1 if lc($t) eq 'dkim2test._domainkey.dkim2.com';
Branch: 1 branch
BranchTrueFalseTotal
if lc $t eq "dkim2test._domainkey.dkim2.com" 0 0 0
211 }
212 0 return 0;
213 }
214  
215 # sign_dkim1($text, @specs) — prepend a classic DKIM1 DKIM-Signature per spec.
216 # Each spec: { domain, selector, keyfile } or { domain, selector, key => <PEM> }.
217 # rsa-sha256 / relaxed-relaxed. Composable post-step: only prepends headers,
218 # never touches the body or the DKIM2/MI headers. See
219 # docs/superpowers/specs/2026-06-21-dkim2-reflector-dkim1-signing-design.md.
220 2 ▶sub sign_dkim1 {
Subroutine sign_dkim1: called CC=3 SCAR=11.0
Pod documented
221 2 my ($text, @specs) = @_;
222 2 require Mail::DKIM::Signer;
223 2 require Mail::DKIM::PrivateKey;
224 2 for my $s (@specs) {
225 # Mail::DKIM::PrivateKey's Data=> path does not parse a PEM string; only
226 # File=> does. So an in-memory PEM (key=>) goes via a temp file.
227 3 my ($key, $tmp);
228 3 ▶ if (defined $s->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{'key'}) { } 3 0 3
229 3 require File::Temp;
230 3 $tmp = File::Temp->new(SUFFIX => '.pem');
231 3 print {$tmp} $s->{key};
232 3 close $tmp;
233 3 $key = Mail::DKIM::PrivateKey->load(File => "$tmp");
234 } else {
235 0 $key = Mail::DKIM::PrivateKey->load(File => $s->{keyfile});
236 }
237 my $signer = Mail::DKIM::Signer->new(
238 Algorithm => 'rsa-sha256',
239 Method => 'relaxed/relaxed',
240 Domain => $s->{domain},
241 Selector => $s->{selector},
242 3 Key => $key,
243 );
244 3 $signer->PRINT($text);
245 3 $signer->CLOSE;
246 3 (my $sig = $signer->signature->as_string) =~ s/\r?\n/\r\n/g;
247 3 $sig =~ s/\r\n\z//;
248 3 $text = "$sig\r\n" . $text;
249 }
250 2 return $text;
251 }
252  
253 # generate_brand(%args) — the reflector-brand behaviour. With delegated=1, build a
254 # fresh message From the brand and sign it twice (i=1 as the brand via the
255 # delegated key, i=2 as <domain>), on one Message-Instance. With delegated=0, fall
256 # back to the fresh generator carrying a CNAME-setup error body. See
257 # docs/superpowers/specs/2026-06-20-dkim2-reflector-brand-design.md.
258 4 ▶sub generate_brand {
Subroutine generate_brand: called CC=17 SCAR=30.0
Pod documented
259 4 my (%a) = @_;
260 4 ▶ croak "need a sender" unless $a{sender};
Branch: 1 branch
BranchTrueFalseTotal
unless $a{"sender"} 0 4 4
261 4 ▶ $a{domain} //= 'dkim2.com';
Condition50%1/2
$a{"domain"} //= "dkim2.com"
l!l
40
MC/DC0%0/1
$a{"domain"} //= "dkim2.com"
$a{"domain"}
Truth table50%1/2
A
A= $a{"domain"}
Aresult
11
00
262 4 ▶ $a{selector} //= 'sel1';
Condition50%1/2
$a{"selector"} //= "sel1"
l!l
40
MC/DC0%0/1
$a{"selector"} //= "sel1"
$a{"selector"}
Truth table50%1/2
A
A= $a{"selector"}
Aresult
11
00
263 4 ▶ $a{mailfrom} //= "reflector-bounces\@$a{domain}";
Condition50%1/2
$a{"mailfrom"} //= qq[reflector-bounces\@$a{"domain"}]
l!l
40
MC/DC0%0/1
$a{"mailfrom"} //= qq[reflector-bounces\@$a{"domain"}]
$a{"mailfrom"}
Truth table50%1/2
A
A= $a{"mailfrom"}
Aresult
11
00
264 4 ▶ $a{brand_selector} //= 'dkim2test';
Condition50%1/2
$a{"brand_selector"} //= "dkim2test"
l!l
40
MC/DC0%0/1
$a{"brand_selector"} //= "dkim2test"
$a{"brand_selector"}
Truth table50%1/2
A
A= $a{"brand_selector"}
Aresult
11
00
265 4 ▶ my $now = $a{now} // time();
Condition50%1/2
$a{"now"} // time
l!l
40
MC/DC0%0/1
$a{"now"} // time
$a{"now"}
Truth table50%1/2
A
A= $a{"now"}
Aresult
11
00
266 4 my $bd = _addr_domain($a{sender});
267  
268 4 ▶ unless ($a{delegated}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($a{"delegated"}) 1 3 4
269 1 my $err =
270 "Hello,\r\n\r\n"
271 . "You asked for the DKIM2 brand demo, but dkim2test._domainkey.$bd is not\r\n"
272 . "a CNAME to dkim2test._domainkey.$a{domain}. Publish that CNAME and try\r\n"
273 . "again to get a brand-signed (two-signature) message.\r\n\r\n"
274 . "In the meantime, here is a plain freshly-generated DKIM2 message.\r\n\r\n"
275 . "-- \r\n"
276 . "The DKIM2 reflector at $a{domain}\r\n";
277 return generate(sender => $a{sender}, domain => $a{domain}, selector => $a{selector},
278 key => $a{key}, keyfile => $a{keyfile}, mailfrom => $a{mailfrom},
279 1 now => $now, message_id => $a{message_id}, body => $err);
280 }
281  
282 3 my $rcpt = "reflector-brand\@$a{domain}";
283 3 my $from = "dkim2demo\@$bd";
284 my $i1_desc = $a{nd}
285 3 ▶ ? " i=1 d=$bd (the brand hop; instead of mf=/rt= it carries\r\n"
Branch: 1 branch
BranchTrueFalseTotal
$a{'nd'} ? : 2 1 3
286 . " nd=$a{domain}, naming the domain that signs the next hop --\r\n"
287 . " the \"imaginary hop\" encoding from -08 Section 12.3)\r\n"
288 : " i=1 d=$bd (signed with the key you delegated via the\r\n"
289 . " dkim2test._domainkey.$bd CNAME to dkim2test._domainkey.$a{domain})\r\n";
290 3 my $body =
291 "Hello,\r\n\r\n"
292 . "This is a brand-signed DKIM2 message, sent for $bd by an ESP that does\r\n"
293 . "not show its own identity in the visible headers. It is freshly\r\n"
294 . "originated (a single Message-Instance, m=1) but carries TWO DKIM2-Signatures:\r\n\r\n"
295 . $i1_desc
296 . " i=2 d=$a{domain} (the ESP/platform hop out to you)\r\n\r\n"
297 . "Paste it into https://$a{domain}/validate/ to see both signatures verify.\r\n\r\n"
298 . "-- \r\n"
299 . "The DKIM2 reflector at $a{domain}\r\n";
300  
301 my $subject = $a{nd}
302 3 ▶ ? 'Brand-signed DKIM2 message (nd= imaginary hop)'
Branch: 1 branch
BranchTrueFalseTotal
$a{'nd'} ? : 2 1 3
303 : 'Brand-signed DKIM2 message';
304 my $text = _fresh_message_text(
305 from => $from, to => $a{sender}, subject => $subject,
306 body => $body, now => $now, message_id => $a{message_id}, domain => $a{domain},
307 3 );
308  
309 # i=1: sign AS the brand using the delegated key. With nd=>1 this hop uses
310 # the nd= "imaginary hop" encoding (nd=<platform domain>) instead of mf=/rt=
311 # (-09 §12.3); the chain still validates because nd= must equal the d=
312 # of the next signature (i=2).
313 3 my %b = (Domain => $bd, Selector => $a{brand_selector}, Timestamp => $now);
314 3 ▶ if ($a{nd}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a{'nd'}) { } 2 1 3
315 2 $b{NextDomain} = $a{domain};
316 } else {
317 1 $b{MailFrom} = $from;
318 1 $b{RcptTo} = [ $rcpt ];
319 }
320 3 ▶ $b{Key} = $a{brand_key} if $a{brand_key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"brand_key"} 3 0 3
321 3 ▶ $b{KeyFile} = $a{brand_keyfile} if $a{brand_keyfile} && !$a{brand_key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"brand_keyfile"} and not $a{"brand_key"} 0 3 3
Condition33%1/3
$a{"brand_keyfile"} and not $a{"brand_key"}
!ll&&!rl&&r
300
MC/DC0%0/2
$a{"brand_keyfile"} and not $a{"brand_key"}
$a{"brand_keyfile"} not $a{"brand_key"}
Truth table33%1/3
A and not(B)
A= $a{"brand_keyfile"}B= not $a{"brand_key"}
ABresult
0X0
100
111
322 3 $text = _sign_info_line(%b) . _sign_with($text, %b) . "\r\n" . $text;
323  
324 # i=2: the dkim2.com hop out to the sender.
325 my %d = (Domain => $a{domain}, Selector => $a{selector},
326 3 MailFrom => $a{mailfrom}, RcptTo => [ $a{sender} ], Timestamp => $now);
327 3 ▶ $d{Key} = $a{key} if $a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"key"} 3 0 3
328 3 ▶ $d{KeyFile} = $a{keyfile} if $a{keyfile} && !$a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"keyfile"} and not $a{"key"} 0 3 3
Condition33%1/3
$a{"keyfile"} and not $a{"key"}
!ll&&!rl&&r
300
MC/DC0%0/2
$a{"keyfile"} and not $a{"key"}
$a{"keyfile"} not $a{"key"}
Truth table33%1/3
A and not(B)
A= $a{"keyfile"}B= not $a{"key"}
ABresult
0X0
100
111
329 3 return _sign_info_line(%d) . _sign_with($text, %d) . "\r\n" . $text;
330 }
331  
332 6 my %VALID = map { $_ => 1 } qw(raw subject body both redacted damage);
333  
334 # reflect(%args) — verify an incoming message, transform it per mode, and
335 # return the message to send back to the sender (signed only if the incoming
336 # DKIM2 chain verified). Works on raw text throughout (Email::MIME is used
337 # only to compute hashes/Recipes, where body_raw/header_raw preserve the
338 # original bytes) so upstream signatures are never invalidated by reserialising.
339 28 ▶sub reflect {
Subroutine reflect: called CC=27 SCAR=33.4
Pod documented
340 28 my (%a) = @_;
341 28 ▶ croak "unknown mode " . ($a{mode} // '(undef)') unless $VALID{ $a{mode} // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless $VALID{$a{"mode"} // ""} 0 28 28
Condition25%1/4
$a{"mode"} // ""
l!l
280
$a{"mode"} // "(undef)"
l!l
00
MC/DC0%0/1
$a{"mode"} // ""
$a{"mode"}
MC/DC0%0/1
$a{"mode"} // "(undef)"
$a{"mode"}
Truth table50%1/2
A
A= $a{"mode"}
Aresult
11
00
Truth table0%0/2
A
A= $a{"mode"}
Aresult
11
00
342 28 my $mode = $a{mode};
343 28 ▶ $a{domain} //= 'dkim2.com';
Condition50%1/2
$a{"domain"} //= "dkim2.com"
l!l
280
MC/DC0%0/1
$a{"domain"} //= "dkim2.com"
$a{"domain"}
Truth table50%1/2
A
A= $a{"domain"}
Aresult
11
00
344 28 ▶ $a{selector} //= 'sel1';
Condition50%1/2
$a{"selector"} //= "sel1"
l!l
280
MC/DC0%0/1
$a{"selector"} //= "sel1"
$a{"selector"}
Truth table50%1/2
A
A= $a{"selector"}
Aresult
11
00
345 28 ▶ $a{mailfrom} //= 'reflector-bounces@dkim2.com';
Condition50%1/2
$a{"mailfrom"} //= "reflector-bounces\@dkim2.com"
l!l
280
MC/DC0%0/1
$a{"mailfrom"} //= "reflector-bounces\@dkim2.com"
$a{"mailfrom"}
Truth table50%1/2
A
A= $a{"mailfrom"}
Aresult
11
00
346  
347 28 (my $incoming = $a{message}) =~ s/\r?\n/\r\n/g;
348  
349 # Strip the Unix mailbox "From sender timestamp" envelope line that Postfix
350 # local(8) prepends when piping a message to an alias command. It has no
351 # colon, so if it survived into the reflected message it would prematurely
352 # terminate the header block when the reply is re-injected (RFC 5322),
353 # dumping every real header into the body. A genuine header is "From:";
354 # only the mbox line is "From " followed by a space.
355 28 $incoming =~ s/\AFrom [^\r\n]*\r\n//;
356  
357 # Strip any Delivered-To: header Postfix local(8) prepends when piping the
358 # message to this alias command. It is a per-hop delivery header (renamed to
359 # X-Remote-Delivered-To / dropped before the reply is delivered), so it is
360 # not seen by verifiers — but it is NOT an IANA trace header, so it is not in
361 # should_skip(). If we left it in, _build_mi would hash it into our
362 # Message-Instance and the resulting header hash could never be verified.
363 # Remove it from the header block only, before we hash or sign anything.
364 {
365 28 my $hend = index($incoming, "\r\n\r\n");
366 28 ▶ $hend = length($incoming) if $hend < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hend < 0 0 28 28
367 28 my $head = substr($incoming, 0, $hend);
368 28 my $tail = substr($incoming, $hend);
369 28 $head =~ s/^Delivered-To:[^\r\n]*(?:\r\n[ \t][^\r\n]*)*(?:\r\n|\z)//img;
370 28 $incoming = $head . $tail;
371 }
372  
373 # 1. DKIM2 verdict (computed here) + DKIM1 verdict (read from A-R).
374 28 my ($auth, $auth_detail) = _verify($incoming, $a{pubkey_cb}, $a{skip_timestamp_check});
375 28 my $from_domain = _from_domain($incoming);
376 28 my $dkim1_d = _dkim1_aligned($incoming, $from_domain, $a{authserv_id});
377 28 ▶ my $dkim1 = $dkim1_d ? 'pass' : 'none';
Branch: 1 branch
BranchTrueFalseTotal
$dkim1_d ? : 4 24 28
378  
379 # 2. Three-tier signing basis: DKIM2 chain, else DKIM1 bridge (only when no
380 # chain present — a broken chain, dkim2=fail, does NOT fall back).
381 28 ▶ my $basis = ($auth eq 'pass') ? 'dkim2'
Branch: 2 branches
BranchTrueFalseTotal
$auth eq 'pass' ? : 19 9 28
$auth eq 'none' && $dkim1_d ? : 3 6 9
Condition100%3/3
$auth eq "none" and $dkim1_d
!ll&&!rl&&r
243
MC/DC100%2/2
$auth eq "none" and $dkim1_d
$auth eq "none" $dkim1_d
Truth table100%3/3
A and B
A= $auth eq "none"B= $dkim1_d
ABresult
0X0
100
111
382 : ($auth eq 'none' && $dkim1_d) ? 'dkim1'
383 : 'none';
384 28 my $will_sign = ($basis ne 'none');
385  
386 # 3. Transform (always, except damage which mutates after signing).
387 # Originating (no DKIM2 chain to continue, so our signature is i=1):
388 # with no DKIM2-Signature on it, the message starts again at m=1
389 # (-09 section 13) -- the inbound milter's m=1 holds the sender's From,
390 # which can never be Authentic for us.
391 28 my $originate = $basis ne 'dkim2';
392 28 ▶ my $prev_text = ($originate && !_has_field($incoming, 'DKIM2-Signature'))
Branch: 1 branch
BranchTrueFalseTotal
$originate && !_has_field($incoming, 'DKIM2-Signature') ? : 7 21 28
Condition100%3/3
$originate and not _has_field($incoming, "DKIM2-Signature")
!ll&&!rl&&r
1927
MC/DC100%2/2
$originate and not _has_field($incoming, "DKIM2-Signature")
$originate not _has_field($incoming, "DKIM2-Signature")
Truth table100%3/3
A and not(B)
A= $originateB= not _has_field($incoming, "DKIM2-Signature")
ABresult
0X0
100
111
393 ? _without_field($incoming, 'Message-Instance') : $incoming;
394 28 ▶ my $cur_text = ($mode eq 'damage') ? $prev_text : _transform_text($prev_text, $mode);
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'damage' ? : 2 26 28
395  
396 # 3a. From (-09 section 11, the -07 contract's decision 3): a reflector that
397 # originates the message or changes it sends it From its own domain,
398 # the sender's From kept as Reply-To. Only raw mode continuing a
399 # DKIM2 chain sends the message on as it came.
400 $cur_text = _rewrite_from($cur_text, $mode, $a{domain})
401 28 ▶ if $originate || $mode ne 'raw';
Branch: 1 branch
BranchTrueFalseTotal
if $originate or $mode ne "raw" 24 4 28
Condition100%3/3
$originate or $mode ne "raw"
l!l&&r!l&&!r
9154
MC/DC100%2/2
$originate or $mode ne "raw"
$originate $mode ne "raw"
Truth table100%3/3
A or B
A= $originateB= $mode ne "raw"
ABresult
1X1
011
000
402  
403 # 4. Message-Instance for our change — whenever the message changed (raw
404 # continuing a chain reuses the top m=). Emitted whether or not we sign.
405 28 my $mi = _build_mi($cur_text, $prev_text, $mode);
406 28 ▶ if ($mi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mi) 24 4 28
407 24 my $val = $mi->as_header_value;
408 24 my ($hc, $hn) = _header_list_for_hash(parse_mime($cur_text));
409 24 $cur_text = _info_line("mi-m=" . $mi->get_tag('m'), hc => $hc, hn => $hn)
410 . "Message-Instance: $val\r\n" . $cur_text;
411 }
412  
413 # 5. Sign when we have a basis; for damage, break the body AFTER signing.
414 28 my $signed = 0;
415 28 ▶ if ($will_sign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($will_sign) 22 6 28
416 22 my $sig = _sign($cur_text, %a);
417 22 $cur_text = _sign_info_line(Domain => $a{domain}) . "$sig\r\n" . $cur_text;
418 22 $signed = 1;
419 22 ▶ $cur_text .= $DAMAGE_LINE if $mode eq 'damage';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "damage" 2 20 22
420 }
421  
422 # 6. Explanation headers (excluded from the DKIM2 header hash by
423 # should_skip(): ^x- and authentication-results). Safe to prepend last.
424 # Flatten the verifier's explanation to one line with no parens or
425 # backslashes, so it can sit in an RFC 8601 comment and in X-DKIM2-Info.
426 28 my $why = '';
427 28 ▶ if (defined $auth_detail && length $auth_detail) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $auth_detail and length $auth_detail) 28 0 28
Condition33%1/3
defined $auth_detail and length $auth_detail
!ll&&!rl&&r
0028
MC/DC0%0/2
defined $auth_detail and length $auth_detail
defined $auth_detail length $auth_detail
Truth table33%1/3
A and B
A= defined $auth_detailB= length $auth_detail
ABresult
0X0
100
111
428 28 ($why = $auth_detail) =~ s/[()\\\r\n]+/ /g;
429 28 $why =~ s/\s+/ /g;
430 28 $why =~ s/\A\s+|\s+\z//g;
431 }
432 28 my $ar = "Authentication-Results: $a{domain}; dkim2=$auth";
433 28 ▶ $ar .= " ($why)" if $auth ne 'pass' && length $why;
Branch: 1 branch
BranchTrueFalseTotal
if $auth ne "pass" and length $why 9 19 28
Condition66%2/3
$auth ne "pass" and length $why
!ll&&!rl&&r
1909
MC/DC50%1/2
$auth ne "pass" and length $why
$auth ne "pass" length $why
Truth table66%2/3
A and B
A= $auth ne "pass"B= length $why
ABresult
0X0
100
111
434 28 ▶ $ar .= "; dkim=pass header.d=$dkim1_d" if $dkim1_d;
Branch: 1 branch
BranchTrueFalseTotal
if $dkim1_d 4 24 28
435 28 $ar .= "\r\n";
436 28 ▶ my $xr = "X-DKIM2-Reflector: mode=$mode; auth=$auth; dkim1=$dkim1; "
Branch: 1 branch
BranchTrueFalseTotal
$signed ? : 22 6 28
437 . "basis=$basis; signed=" . ($signed ? 'yes' : 'no')
438 . "; note=reflected-to-sender\r\n";
439  
440 # X-DKIM2-Info for the verification, directly above Authentication-Results.
441 # The mi-m=<N> and sign fields were added above their own headers in steps
442 # 4 and 5.
443 28 ▶ my $xi = _info_line("verify=$auth" . (length $why ? " ($why)" : ''));
Branch: 1 branch
BranchTrueFalseTotal
length $why ? : 28 0 28
444  
445 28 $cur_text = $xi . $ar . $xr . $cur_text;
446  
447 return {
448 28 message => $cur_text, auth => $auth, dkim1 => $dkim1,
449 basis => $basis, signed => $signed, mode => $mode,
450 };
451 }
452  
453 # Returns ($result, $detail). The detail explains a non-pass result and is
454 # echoed to the sender in Authentication-Results -- for a temperror that is the
455 # only way they can tell an unfetchable key from a bad signature.
456 28 ▶sub _verify {
Subroutine _verify: called CC=6 SCAR=18.3
457 28 my ($text, $cb, $skip_ts) = @_;
458 28 my $v = Mail::DKIM2::Verifier->new;
459 28 ▶ $v->skip_timestamp_check(1) if $skip_ts;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_ts 28 0 28
460 # The inbound milter stamps a Message-Instance on non-DKIM2 mail, so a
461 # message reaching us can legitimately carry an MI with no signature over
462 # it. Verified mail from outside would make that -09 §14's "is not
463 # signed" PERMERROR, but here it is our own header on our own side of the
464 # trust boundary, and the whole point of this path is to then bridge-sign
465 # it -- which is what makes the instance legal on the wire again. Without
466 # this the result would be permerror instead of 'none' and we would refuse
467 # to sign the very mail we are here to sign.
468 28 $v->allow_unsigned_mi(1);
469 28 ▶ $v->set_pubkey_callback($cb) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 28 0 28
470 28 $v->PRINT($text); $v->CLOSE;
471 # The -08 string, and for the sender the resolver's reason behind a key
472 # that could not be fetched (Verifier::diagnostic).
473 28 my $detail = $v->details;
474 28 ▶ $detail .= " - " . $v->diagnostic if defined $detail && defined $v->diagnostic;
Branch: 1 branch
BranchTrueFalseTotal
if defined $detail and defined $v->diagnostic 1 27 28
Condition66%2/3
defined $detail and defined $v->diagnostic
!ll&&!rl&&r
0271
MC/DC50%1/2
defined $detail and defined $v->diagnostic
defined $detail defined $v->diagnostic
Truth table66%2/3
A and B
A= defined $detailB= defined $v->diagnostic
ABresult
0X0
100
111
475 28 ▶ return ($v->result // 'none', $detail);
Condition50%1/2
$v->result // "none"
l!l
280
MC/DC0%0/1
$v->result // "none"
$v->result
Truth table50%1/2
A
A= $v->result
Aresult
11
00
476 }
477  
478 # Split a message into (headers-without-trailing-CRLF, body).
479 54 ▶sub _split {
Subroutine _split: called CC=2 SCAR=7.0
480 54 my ($text) = @_;
481 54 my $i = index($text, "\r\n\r\n");
482 54 ▶ return ($text, '') if $i < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 54 54
483 54 return (substr($text, 0, $i), substr($text, $i + 4));
484 }
485  
486 # Relaxed, PSL-free domain alignment: equal, or one a subdomain of the other.
487 16 ▶sub _domains_align {
Subroutine _domains_align: called CC=8 SCAR=21.4
488 16 my ($f, $d) = @_;
489 16 ▶ return 0 unless defined $f && defined $d && length $f && length $d;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f and defined $d and length $f and length $d 1 15 16
Condition44%4/9
defined $f and defined $d and length $f and length $d
!ll&&!rl&&r
0115
defined $f and defined $d and length $f
!ll&&!rl&&r
0016
defined $f and defined $d
!ll&&!rl&&r
0016
MC/DC25%1/4
defined $f and defined $d and length $f and length $d
defined $f defined $d length $f length $d
Truth table40%2/5
A and B and C and D
A= defined $fB= defined $dC= length $fD= length $d
ABCDresult
0XXX0
10XX0
110X0
11100
11111
490 15 $f = lc $f; $d = lc $d;
491 15 ▶ return 1 if $f eq $d;
Branch: 1 branch
BranchTrueFalseTotal
if $f eq $d 8 7 15
492 7 ▶ return 1 if $f =~ /\.\Q$d\E\z/; # f is a subdomain of d
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /\.\Q$d\E\z/u 2 5 7
493 5 ▶ return 1 if $d =~ /\.\Q$f\E\z/; # d is a subdomain of f
Branch: 1 branch
BranchTrueFalseTotal
if $d =~ /\.\Q$f\E\z/u 1 4 5
494 4 return 0;
495 }
496  
497 # Lowercased domain of the message's From: header, or undef.
498 31 ▶sub _from_domain {
Subroutine _from_domain: called CC=6 SCAR=18.2
499 31 my ($text) = @_;
500 31 my $from = eval { parse_mime($text)->header('From') };
501 31 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 31 31
502 31 ▶ return undef unless defined $from && length $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from and length $from 1 30 31
Condition66%2/3
defined $from and length $from
!ll&&!rl&&r
1030
MC/DC50%1/2
defined $from and length $from
defined $from length $from
Truth table66%2/3
A and B
A= defined $fromB= length $from
ABresult
0X0
100
111
503 30 ▶ my $addr = ($from =~ /<([^>]+)>/) ? $1 : $from;
Branch: 1 branch
BranchTrueFalseTotal
$from =~ /<([^>]+)>/u ? : 3 27 30
504 30 my ($dom) = $addr =~ /\@([A-Za-z0-9.\-]+)/;
505 30 ▶ return defined $dom ? lc $dom : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $dom ? : 30 0 30
506 }
507  
508 # The aligned header.d of a dkim=pass result in our authserv-id's
509 # Authentication-Results, or undef. Only A-R bearing $authserv_id are trusted.
510 35 ▶sub _dkim1_aligned {
Subroutine _dkim1_aligned: called CC=12 SCAR=25.0
511 35 my ($text, $from_domain, $authserv_id) = @_;
512 35 ▶ return undef unless defined $from_domain && defined $authserv_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_domain and defined $authserv_id 21 14 35
Condition66%2/3
defined $from_domain and defined $authserv_id
!ll&&!rl&&r
02114
MC/DC50%1/2
defined $from_domain and defined $authserv_id
defined $from_domain defined $authserv_id
Truth table66%2/3
A and B
A= defined $from_domainB= defined $authserv_id
ABresult
0X0
100
111
513 14 my @ar = eval { parse_mime($text)->header_raw('Authentication-Results') };
514 14 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 14 14
515 14 for my $ar (@ar) {
516 12 $ar =~ s/\r?\n[ \t]+/ /g; # unfold
517 12 ▶ 1 while $ar =~ s/\([^()]*\)//g; # strip CFWS comments (may hold ';')
Branch: 1 branch
BranchTrueFalseTotal
$ar =~ s/\([^()]*\)//gu and do { '???' } 1 12 13
518 12 my ($id, $rest) = split /;/, $ar, 2;
519 12 ▶ next unless defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest 0 12 12
520 12 $id =~ s/^\s+|\s+$//g;
521 12 $id =~ s/\s.*\z//; # drop optional version after authserv-id
522 12 ▶ next unless lc($id) eq lc($authserv_id);
Branch: 1 branch
BranchTrueFalseTotal
unless lc $id eq lc $authserv_id 2 10 12
523 # The first A-R bearing our authserv-id is OpenDKIM's genuine result
524 # (it prepends on top of any sender-forged copy). Trust ONLY this one.
525 10 for my $chunk (split /;/, $rest) { # one resinfo per chunk
526 11 ▶ next unless $chunk =~ /\bdkim\s*=\s*pass\b/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /\bdkim\s*=\s*pass\b/iu 2 9 11
527 9 ▶ next unless $chunk =~ /header\.d\s*=\s*([A-Za-z0-9.\-]+)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $chunk =~ /header\.d\s*=\s*([A-Za-z0-9.\-]+)/iu 0 9 9
528 9 my $d = lc $1;
529 9 ▶ return $d if _domains_align($from_domain, $d);
Branch: 1 branch
BranchTrueFalseTotal
if _domains_align($from_domain, $d) 7 2 9
530 }
531 3 return undef; # ignore any lower A-R headers
532 }
533 4 return undef;
534 }
535  
536 26 ▶sub _transform_text {
Subroutine _transform_text: called CC=9 SCAR=22.1
537 26 my ($text, $mode) = @_;
538 26 ▶ return $text if $mode eq 'raw';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "raw" 12 14 26
539 14 my ($head, $body) = _split($text);
540 14 ▶ if ($mode eq 'subject' || $mode eq 'both') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "subject" or $mode eq "both") 6 8 14
Condition100%3/3
$mode eq "subject" or $mode eq "both"
l!l&&r!l&&!r
338
MC/DC100%2/2
$mode eq "subject" or $mode eq "both"
$mode eq "subject" $mode eq "both"
Truth table100%3/3
A or B
A= $mode eq "subject"B= $mode eq "both"
ABresult
1X1
011
000
541 6 $head =~ s/^(Subject:[ \t]*)/$1$SUBJECT_PREFIX/mi;
542 }
543 14 ▶ if ($mode eq 'body' || $mode eq 'both' || $mode eq 'redacted') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "body" or $mode eq "both" or $mode eq "redacted") 11 3 14
Condition100%6/6
$mode eq "body" or $mode eq "both" or $mode eq "redacted"
l!l&&r!l&&!r
923
$mode eq "body" or $mode eq "both"
l!l&&r!l&&!r
635
MC/DC100%3/3
$mode eq "body" or $mode eq "both" or $mode eq "redacted"
$mode eq "body" $mode eq "both" $mode eq "redacted"
Truth table100%4/4
A or B or C
A= $mode eq "body"B= $mode eq "both"C= $mode eq "redacted"
ABCresult
1XX1
01X1
0011
0000
544 11 ▶ $body .= "\r\n" if length($body) && $body !~ /\r\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if length $body and not $body =~ /\r\n\z/u 0 11 11
Condition33%1/3
length $body and not $body =~ /\r\n\z/u
!ll&&!rl&&r
0110
MC/DC0%0/2
length $body and not $body =~ /\r\n\z/u
length $body not $body =~ /\r\n\z/u
Truth table33%1/3
A and not(B)
A= length $bodyB= not $body =~ /\r\n\z/u
ABresult
0X0
100
111
545 11 $body .= $FOOTER;
546 }
547 14 return "$head\r\n\r\n$body";
548 }
549  
550 # Returns a MessageInstance object (new MI) or undef (reuse top m=): none
551 # when the message did not change, m=1 when it carries no instance (an
552 # originating reflector started it again), else the next m= with Recipes
553 # back to $prev_text -- for redacted, a null body Recipe.
554 28 ▶sub _build_mi {
Subroutine _build_mi: called CC=4 SCAR=13.9
555 28 my ($cur_text, $prev_text, $mode) = @_;
556 28 ▶ return undef if $cur_text eq $prev_text;
Branch: 1 branch
BranchTrueFalseTotal
if $cur_text eq $prev_text 4 24 28
557 24 my $prev = parse_mime($prev_text);
558 24 ▶ return Mail::DKIM2::MessageInstance->calculate(parse_mime($cur_text))
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->header_raw("Message-Instance") 7 17 24
559 unless $prev->header_raw('Message-Instance');
560 17 my $mi = Mail::DKIM2::MessageInstance->calculate(parse_mime($cur_text), $prev);
561 17 ▶ $mi->set_null_body_recipe if $mode eq 'redacted';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "redacted" 2 15 17
562 17 return $mi;
563 }
564  
565 # True if the header block of $text has a $name field.
566 9 ▶sub _has_field {
Subroutine _has_field: called CC=2 SCAR=6.9
567 9 my ($text, $name) = @_;
568 9 my ($head) = _split($text);
569 9 ▶ return $head =~ /^\Q$name\E[ \t]*:/mi ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$head =~ /^\Q$name\E[ \t]*:/imu ? : 2 7 9
570 }
571  
572 # $text without any $name field in its header block.
573 7 ▶sub _without_field {
Subroutine _without_field: called CC=1 SCAR=0.0
574 7 my ($text, $name) = @_;
575 7 my ($head, $body) = _split($text);
576 7 $head =~ s/^\Q$name\E[ \t]*:[^\r\n]*(?:\r\n[ \t][^\r\n]*)*(?:\r\n|\z)//mig;
577 7 $head =~ s/\r\n\z//;
578 7 return "$head\r\n\r\n$body";
579 }
580  
581 # $text with its From replaced by
582 # "<sender's display name, or address> via DKIM2 reflector" <reflector-<mode>@<domain>>
583 # and, when it has no Reply-To, a Reply-To holding the sender's From as it
584 # was. The display name is quoted if ASCII, RFC 2047 encoded otherwise.
585 24 ▶sub _rewrite_from {
Subroutine _rewrite_from: called CC=11 SCAR=24.7
586 24 my ($text, $mode, $domain) = @_;
587 24 my ($head, $body) = _split($text);
588 24 my ($old) = $head =~ /^From[ \t]*:[ \t]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/mi;
589 24 my $name = 'unknown sender';
590 24 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) 24 0 24
591 24 ▶ my $decoded = eval { parse_mime("From: $old\r\n\r\n")->header_str('From') } // $old;
Condition33%1/3
eval { do { parse_mime("From: $old\r\n\r\n")->header_str("From") } } // $old
l!l&&r!l&&!r
2400
MC/DC0%0/2
eval { do { parse_mime("From: $old\r\n\r\n")->header_str("From") } } // $old
eval { do { parse_mime("From: $old\r\n\r\n")->header_str("From") } } $old
Truth table33%1/3
A // B
A= eval { do { parse_mime("From: $old\r\n\r\n")->header_str("From") } }B= $old
ABresult
1X1
011
000
592 24 ▶ die $@ if ref $@;
Branch: 1 branch
BranchTrueFalseTotal
if ref $@ 0 24 24
593 24 $decoded =~ s/\s+/ /g;
594 24 ▶ if ($decoded =~ /\A\s*(.*?)\s*<([^<>]*)>\s*\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($decoded =~ /\A\s*(.*?)\s*<([^<>]*)>\s*\z/u) { } 2 22 24
elsif ($decoded =~ /\S/u) { } 22 0 22
595 2 my ($phrase, $addr) = ($1, $2);
596 2 $phrase =~ s/\A"(.*)"\z/$1/;
597 2 $phrase =~ s/\\(.)/$1/g;
598 2 ▶ $name = length $phrase ? $phrase : $addr;
Branch: 1 branch
BranchTrueFalseTotal
length $phrase ? : 2 0 2
599 } elsif ($decoded =~ /\S/) {
600 22 ($name = $decoded) =~ s/\A\s+|\s+\z//g;
601 }
602 }
603 24 my $phrase = "$name via DKIM2 reflector";
604 24 ▶ if ($phrase =~ /[^\x20-\x7E]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phrase =~ /[^\x20-\x7E]/u) { } 1 23 24
605 1 require Encode;
606 1 $phrase = Encode::encode('MIME-Q', $phrase) =~ s/\r?\n/\r\n/gr;
607 } else {
608 23 $phrase = '"' . ($phrase =~ s/(["\\])/\\$1/gr) . '"';
609 }
610 24 my $from = "From: $phrase <reflector-$mode\@$domain>";
611 24 ▶ my $reply = (defined $old && $head !~ /^Reply-To[ \t]*:/mi) ? "\r\nReply-To: $old" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $old && !($head =~ /^Reply-To[ \t]*:/imu) ? : 23 1 24
Condition66%2/3
defined $old and not $head =~ /^Reply-To[ \t]*:/imu
!ll&&!rl&&r
0123
MC/DC50%1/2
defined $old and not $head =~ /^Reply-To[ \t]*:/imu
defined $old not $head =~ /^Reply-To[ \t]*:/imu
Truth table66%2/3
A and not(B)
A= defined $oldB= not $head =~ /^Reply-To[ \t]*:/imu
ABresult
0X0
100
111
612 24 ▶ unless ($head =~ s/^From[ \t]*:[^\r\n]*(?:\r\n[ \t][^\r\n]*)*/$from$reply/mi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($head =~ s/^From[ \t]*:[^\r\n]*(?:\r\n[ \t][^\r\n]*)*/$from$reply/imu) 0 24 24
613 0 $head .= "\r\n$from";
614 }
615 24 return "$head\r\n\r\n$body";
616 }
617  
618 22 ▶sub _sign {
Subroutine _sign: called CC=8 SCAR=24.0
619 22 my ($text, %a) = @_;
620 my %sa = (
621 Domain => $a{domain},
622 Selector => $a{selector},
623 MailFrom => $a{mailfrom},
624 22 RcptTo => [ $a{sender} ],
625 );
626 22 ▶ $sa{Key} = $a{key} if $a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"key"} 22 0 22
627 22 ▶ $sa{KeyFile} = $a{keyfile} if $a{keyfile} && !$a{key};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"keyfile"} and not $a{"key"} 0 22 22
Condition33%1/3
$a{"keyfile"} and not $a{"key"}
!ll&&!rl&&r
2200
MC/DC0%0/2
$a{"keyfile"} and not $a{"key"}
$a{"keyfile"} not $a{"key"}
Truth table33%1/3
A and not(B)
A= $a{"keyfile"}B= not $a{"key"}
ABresult
0X0
100
111
628 22 ▶ $sa{Timestamp} = $a{timestamp} if $a{timestamp};
Branch: 1 branch
BranchTrueFalseTotal
if $a{"timestamp"} 0 22 22
629 22 my $signer = Mail::DKIM2::Signer->new(%sa);
630 22 $signer->PRINT($text); $signer->CLOSE;
631 22 ▶ croak "signing failed: " . ($signer->result_detail // 'no result')
Branch: 1 branch
BranchTrueFalseTotal
unless ($signer->result // "") eq "signed" 0 22 22
Condition25%1/4
$signer->result // ""
l!l
220
$signer->result_detail // "no result"
l!l
00
MC/DC0%0/1
$signer->result // ""
$signer->result
MC/DC0%0/1
$signer->result_detail // "no result"
$signer->result_detail
Truth table50%1/2
A
A= $signer->result
Aresult
11
00
Truth table0%0/2
A
A= $signer->result_detail
Aresult
11
00
632 unless ($signer->result // '') eq 'signed';
633 22 return $signer->as_string; # "DKIM2-Signature: ..."
634 }
635  
636 6 1;
637  
638 __END__
« lib/Mail/DKIM2/Wrap.pm ↑ Summary bin/dkim2-split-lmtp »