| 1 | package Mail::DKIM2::Split; | ||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 4 | our $VERSION = '0.33'; | |||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use Email::MIME; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | ▶ | use Mail::DKIM2::Common qw(parse_mime); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | our @EXPORT_OK = qw(plan_copies disclosed_addresses); | |||||||||
| 11 | |||||||||||
| 12 | # Mail::DKIM2::Split - Bcc-safe recipient grouping for DKIM2 origination. | ||||||||||
| 13 | # | ||||||||||
| 14 | # DKIM2 records the RCPT TO of each hop in the signature's rt= tag. If a single | ||||||||||
| 15 | # submitted message with undisclosed (Bcc) recipients is signed once, all those | ||||||||||
| 16 | # recipients end up in one rt=, visible to everyone -- a Bcc leak. To avoid it, | ||||||||||
| 17 | # the message must be split into instances before signing: disclosed recipients | ||||||||||
| 18 | # (those already named in To:/Cc:) can share one copy; each Bcc recipient must | ||||||||||
| 19 | # get its own. The signing step then records only that copy's recipients in rt=. | ||||||||||
| 20 | # | ||||||||||
| 21 | # This module is the (network-free, testable) grouping logic. The LMTP daemon | ||||||||||
| 22 | # (bin/dkim2-split-lmtp) uses it to fan a message out into per-copy | ||||||||||
| 23 | # re-injections. See deploy/SERVER.md "Bcc-safe origination". | ||||||||||
| 24 | |||||||||||
| 25 | # An address as a key: the domain lowercased, the local-part exactly as written. | ||||||||||
| 26 | # A local-part may be case-sensitive (RFC 5321 2.4), so User@ and user@ can be | ||||||||||
| 27 | # two mailboxes; folding them together would put a Bcc to one in the same copy, | ||||||||||
| 28 | # and so the same rt=, as the other. Telling them apart when they are in fact | ||||||||||
| 29 | # one mailbox costs only an extra copy. | ||||||||||
| 30 | 30 | ▶ | sub _addr_key { | ||||||||
|
Subroutine
_addr_key: called CC=2 SCAR=7.0
| |||||||||||
| 31 | 30 | my ($a) = @_; | |||||||||
| 32 | 30 | my $at = rindex($a, '@'); | |||||||||
| 33 | 30 | ▶ | return $a if $at < 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 30 | return substr($a, 0, $at) . lc substr($a, $at); | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | # disclosed_addresses($message_bytes) -> hashref { address => 1 } of every | ||||||||||
| 38 | # address named in the message's To: and Cc: header fields, keyed as _addr_key | ||||||||||
| 39 | # makes them (domain lowercased, local-part as written). | ||||||||||
| 40 | 8 | ▶ | sub disclosed_addresses { | ||||||||
|
Subroutine
disclosed_addresses: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||
| 41 | 8 | my ($msg_bytes) = @_; | |||||||||
| 42 | 8 | my %seen; | |||||||||
| 43 | 8 | my $em = eval { parse_mime($msg_bytes) }; | |||||||||
| 44 | 8 | ▶ | die $@ if ref $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 8 | ▶ | return \%seen unless $em; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 8 | for my $hdr (qw(To Cc)) { | |||||||||
| 47 | 16 | for my $val ($em->header($hdr)) { | |||||||||
| 48 | 10 | ▶ | next unless defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | # addresses inside angle brackets: To: "Name" <a@b>, ... | ||||||||||
| 50 | 10 | ▶ | while ($val =~ /<([^>]+)>/g) { $seen{ _addr_key($1) } = 1; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | # bare addr-specs (comma-separated tokens with @ and no <>) | ||||||||||
| 52 | 10 | for my $tok (split /,/, $val) { | |||||||||
| 53 | 14 | $tok =~ s/^\s+|\s+$//g; | |||||||||
| 54 | 14 | ▶ | next if $tok =~ /</; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 9 | ▶ | $seen{ _addr_key($1) } = 1 if $tok =~ /([^\s<>,]+\@[^\s<>,]+)/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | } | ||||||||||
| 57 | } | ||||||||||
| 58 | } | ||||||||||
| 59 | 8 | return \%seen; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | # Strip optional angle brackets from an envelope path and key it as above. | ||||||||||
| 63 | 16 | ▶ | sub _addr { | ||||||||
|
Subroutine
_addr: called CC=1 SCAR=0.0
| |||||||||||
| 64 | 16 | my ($r) = @_; | |||||||||
| 65 | 16 | $r =~ s/^<//; $r =~ s/>$//; | |||||||||
| 66 | 16 | return _addr_key($r); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # plan_copies($message_bytes, \@envelope_rcpts) -> \@copies | ||||||||||
| 70 | # | ||||||||||
| 71 | # Returns the set of copies to generate so that no copy's rt= ever lists a Bcc | ||||||||||
| 72 | # recipient alongside anyone else. Each copy is { rcpts => [envelope rcpts] }, | ||||||||||
| 73 | # preserving the original (possibly bracketed) recipient strings: | ||||||||||
| 74 | # - all disclosed recipients (address present in To:/Cc:) share ONE copy; | ||||||||||
| 75 | # - each undisclosed (Bcc) recipient gets its OWN copy. | ||||||||||
| 76 | # Recipients whose address is not in To:/Cc: are treated as Bcc (so a message | ||||||||||
| 77 | # with no To:/Cc: at all yields one copy per recipient -- always safe). | ||||||||||
| 78 | 6 | ▶ | sub plan_copies { | ||||||||
|
Subroutine
plan_copies: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 79 | 6 | my ($msg_bytes, $envelope_rcpts) = @_; | |||||||||
| 80 | 6 | my $disclosed = disclosed_addresses($msg_bytes); | |||||||||
| 81 | 6 | my (@disc, @bcc); | |||||||||
| 82 | 6 | for my $r (@$envelope_rcpts) { | |||||||||
| 83 | 16 | ▶ | if ($disclosed->{ _addr($r) }) { push @disc, $r } else { push @bcc, $r } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | } | ||||||||||
| 85 | 6 | my @copies; | |||||||||
| 86 | 6 | ▶ | push @copies, { rcpts => [@disc] } if @disc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 6 | push @copies, { rcpts => [$_] } for @bcc; | |||||||||
| 88 | 6 | return \@copies; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 4 | 1; | |||||||||
| 92 | |||||||||||
| 93 | __END__ | ||||||||||