| 1 | package Mail::DKIM2::Wrap; | ||||||||||||||||||||||||||||||||
| 2 | 67 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 67 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 67 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 67 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 67 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 67 | ▶ | use Crypt::PRNG qw(random_bytes); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 67 | our @EXPORT_OK = qw(wrap_single_part); | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 67 | my $EOL = "\015\012"; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | sub _random_boundary { '=_dkim2_' . unpack('H*', random_bytes(12)) } | ||||||||||||||||||||||||||||||
|
Subroutine
_random_boundary: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 23 | ▶ | sub wrap_single_part { | ||||||||||||||||||||||||||||||
|
Subroutine
wrap_single_part: called CC=19 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 23 | my ($message, %opt) = @_; | |||||||||||||||||||||||||||||||
| 19 | 23 | ▶ | my $gen = $opt{Boundary} // \&_random_boundary; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$opt{"Boundary"} // \&Mail::DKIM2::Wrap::_random_boundary
MC/DC100%1/1
$opt{"Boundary"} // \&Mail::DKIM2::Wrap::_random_boundary
$opt{"Boundary"}
Truth table100%2/2
A
A= $opt{"Boundary"}
| |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | # Nothing to wrap: undef, and in list context the reason. | ||||||||||||||||||||||||||||||||
| 22 | 23 | ▶ | my $none = sub { wantarray ? (undef, $_[0]) : undef }; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 23 | my $sep = index($message, "$EOL$EOL"); | |||||||||||||||||||||||||||||||
| 25 | 23 | ▶ | return $none->('no-header-end') if $sep < 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 26 | 21 | my $head = substr($message, 0, $sep + 2); | |||||||||||||||||||||||||||||||
| 27 | 21 | my $body = substr($message, $sep + 4); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 21 | my @fields = $head =~ /\G([^\r\n]*\r\n(?:[ \t][^\r\n]*\r\n)*)/g; | |||||||||||||||||||||||||||||||
| 30 | 21 | ▶ | return $none->('unparsable-header') unless join('', @fields) eq $head; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | 21 | my @parsed; | |||||||||||||||||||||||||||||||
| 32 | 21 | for my $f (@fields) { | |||||||||||||||||||||||||||||||
| 33 | 93 | ▶ | my ($name) = $f =~ /^([^:\s]+)[ \t]*:/ or return $none->('unparsable-header'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 92 | push @parsed, [$name, $f]; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 20 | my ($ct) = grep { lc $_->[0] eq 'content-type' } @parsed; | |||||||||||||||||||||||||||||||
| 38 | 20 | ▶ | return $none->('multipart') if $ct && $ct->[1] =~ /^[^:]+:\s*multipart\//i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ct and $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
MC/DC100%2/2
$ct and $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
$ct
$ct->[1] =~ m[^[^:]+:\s*multipart/]iu
Truth table100%3/3
A and B
A= $ctB= $ct->[1] =~ m[^[^:]+:\s*multipart/]iu
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 16 | my $boundary; | |||||||||||||||||||||||||||||||
| 41 | 16 | for (1 .. 10) { | |||||||||||||||||||||||||||||||
| 42 | 26 | my $c = $gen->(); | |||||||||||||||||||||||||||||||
| 43 | 26 | ▶ | if (index($body, "--$c") < 0) { $boundary = $c; last } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | 16 | ▶ | return $none->('boundary') unless defined $boundary; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 15 | my @content = grep { $_->[0] =~ /^content-/i } @parsed; | |||||||||||||||||||||||||||||||
| 48 | 15 | my $new_body = "--$boundary$EOL" . join('', map { $_->[1] } @content) | |||||||||||||||||||||||||||||||
| 49 | . $EOL . $body . "$EOL--$boundary--$EOL"; | ||||||||||||||||||||||||||||||||
| 50 | 15 | my $ct_value = qq{multipart/mixed; boundary="$boundary"}; | |||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 15 | my (%count, @deletes, @out, $changed); | |||||||||||||||||||||||||||||||
| 53 | 15 | for my $p (@parsed) { | |||||||||||||||||||||||||||||||
| 54 | 78 | my $lc = lc $p->[0]; | |||||||||||||||||||||||||||||||
| 55 | 78 | my $i = ++$count{$lc}; | |||||||||||||||||||||||||||||||
| 56 | 78 | ▶ | if ($lc eq 'content-type' && $i == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$lc eq "content-type" and $i == 1
MC/DC100%2/2
$lc eq "content-type" and $i == 1
$lc eq "content-type"
$i == 1
Truth table100%3/3
A and B
A= $lc eq "content-type"B= $i == 1
| |||||||||||||||||||||||||||||||||
| 57 | 14 | push @out, "$p->[0]: $ct_value$EOL"; | |||||||||||||||||||||||||||||||
| 58 | 14 | $changed = 1; | |||||||||||||||||||||||||||||||
| 59 | } elsif ($lc =~ /^content-/) { | ||||||||||||||||||||||||||||||||
| 60 | 3 | push @deletes, { op => 'delete', name => $p->[0], index => $i }; | |||||||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||||||
| 62 | 61 | push @out, $p->[1]; | |||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | 15 | my @edits = reverse @deletes; | |||||||||||||||||||||||||||||||
| 66 | 15 | ▶ | push @edits, { op => 'change', name => 'Content-Type', index => 1, value => $ct_value } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 67 | if $changed; | ||||||||||||||||||||||||||||||||
| 68 | 15 | my @adds; | |||||||||||||||||||||||||||||||
| 69 | 15 | ▶ | push @adds, ['Content-Type', $ct_value] unless $changed; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 15 | ▶ | push @adds, ['MIME-Version', '1.0'] unless $count{'mime-version'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 15 | for my $a (@adds) { | |||||||||||||||||||||||||||||||
| 72 | 8 | push @out, "$a->[0]: $a->[1]$EOL"; | |||||||||||||||||||||||||||||||
| 73 | 8 | push @edits, { op => 'add', name => $a->[0], value => $a->[1] }; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | return { | ||||||||||||||||||||||||||||||||
| 77 | 15 | message => join('', @out) . $EOL . $new_body, | |||||||||||||||||||||||||||||||
| 78 | body => $new_body, | ||||||||||||||||||||||||||||||||
| 79 | boundary => $boundary, | ||||||||||||||||||||||||||||||||
| 80 | edits => \@edits, | ||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 67 | 1; | |||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||