DKIM/Canonicalization/DkimCommon.pm

filter: Statementstmt 84.2% 48 / 57 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 25.0% 2 / 8 totaltotal 74.7% 71 / 95 CC 11 SCAR 12.5
CC
11
Cov
79%
finish_header
6.0
add_body
5.7
init
2.0
CRAP 3.5
« DKIM/Canonicalization/relaxed.pm ↑ Summary DKIM/Algorithm/dk_rsa_sha1.pm »
1 package Mail::DKIM::Canonicalization::DkimCommon;
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 our $VERSION = '1.20240923'; # VERSION
5 # ABSTRACT: common canonicalization
6  
7 # Copyright 2005-2007 Messiah College. All rights reserved.
8 # Jason Long <jlong@messiah.edu>
9  
10 # Copyright (c) 2004 Anthony D. Urso. All rights reserved.
11 # This program is free software; you can redistribute it and/or
12 # modify it under the same terms as Perl itself.
13  
14 15 ▶use base 'Mail::DKIM::Canonicalization::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 925 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
18 925 my $self = shift;
19 925 $self->SUPER::init;
20  
21 925 $self->{body_count} = 0;
22 925 $self->{body_truncated} = 0;
23  
24 # these canonicalization methods require signature to use
25 $self->{Signature}
26 925 ▶ or croak 'no signature specified';
Branch: 1 branch
BranchTrueFalseTotal
$self->{"Signature"} or croak("no signature specified") 925 0 925
27 }
28  
29 # similar to code in DkCommon.pm
30 5908 ▶sub add_header {
Subroutine add_header: called CC=1 SCAR=0.0
Pod undocumented
31  
32 #Note: canonicalization of headers is performed
33 #in finish_header()
34 }
35  
36 263 ▶sub finish_header {
Subroutine finish_header: called CC=6 SCAR=17.9
Pod documented
37 263 my $self = shift;
38 263 my %args = @_;
39  
40 # Headers are canonicalized in the order specified by the h= tag.
41 # However, in the case of multiple instances of the same header name,
42 # the headers will be canonicalized in reverse order (i.e. "from
43 # the bottom of the header field block to the top").
44 #
45 # This is described in 5.4 of RFC4871.
46  
47 # Since the bottom-most headers are to get precedence, we reverse
48 # the headers here... (now the first header matching a particular
49 # name is the header to insert)
50 263 my @mess_headers = reverse @{ $args{Headers} };
51  
52 # presence of a h= tag is mandatory...
53 263 ▶ unless ( defined $self->{Signature}->headerlist ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"Signature"}->headerlist) 0 263 263
54 0 die "Error: h= tag is required for this canonicalization\n";
55 }
56  
57 # iterate through the header field names specified in the signature
58 263 my @sig_headers = $self->{Signature}->headerlist;
59 263 foreach my $hdr_name (@sig_headers) {
60 1490 $hdr_name = lc $hdr_name;
61  
62 # find the specified header in the message
63 inner_loop:
64 1490 ▶ for ( my $i = 0 ; $i < @mess_headers ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @mess_headers) 5644 40 5684
65 5644 my $hdr = $mess_headers[$i];
66  
67 5644 ▶ if ( $hdr =~ /^([^\s:]+)\s*:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ /^([^\s:]+)\s*:/) 5642 2 5644
68 5642 my $key = lc $1;
69 5642 ▶ if ( $key eq $hdr_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $hdr_name) 1450 4192 5642
70  
71 # found it
72  
73 # remove it from our list, so if it occurs more than
74 # once, we'll get the next header in line
75 1450 splice @mess_headers, $i, 1;
76  
77 1450 $hdr =~ s/\015\012\z//s;
78 1450 $self->output(
79 $self->canonicalize_header($hdr) . "\015\012" );
80 1450 last inner_loop;
81 }
82 }
83 }
84 }
85 }
86  
87 262 ▶sub add_body {
Subroutine add_body: called CC=4 SCAR=17.3
Pod documented
88 262 my $self = shift;
89 262 my ($multiline) = @_;
90  
91 262 $multiline = $self->canonicalize_body($multiline);
92 262 ▶ if ( $self->{Signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Signature"}) 262 0 262
93 262 ▶ if ( my $limit = $self->{Signature}->body_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $limit = $self->{"Signature"}->body_count) 0 262 262
94 0 my $remaining = $limit - $self->{body_count};
95 0 ▶ if ( length($multiline) > $remaining ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $multiline > $remaining) 0 0 0
96 0 $self->{body_truncated} += length($multiline) - $remaining;
97 0 $multiline = substr( $multiline, 0, $remaining );
98 }
99 }
100 }
101  
102 262 $self->{body_count} += length($multiline);
103 262 $self->output($multiline);
104 }
105  
106 421 ▶sub finish_body {
Subroutine finish_body: called CC=1 SCAR=0.0
Pod undocumented
107 }
108  
109 1 ▶sub finish_message {
Subroutine finish_message: called CC=2 SCAR=7.0
Pod undocumented
110 1 my $self = shift;
111  
112 1 ▶ if ( $self->{Signature} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Signature"}) 1 0 1
113 1 $self->output("\015\012");
114  
115 # append the DKIM-Signature (without data)
116 1 my $line = $self->{Signature}->as_string_without_data;
117  
118 # signature is subject to same canonicalization as headers
119 1 $self->output( $self->canonicalize_header($line) );
120 }
121 }
122  
123 0 ▶sub body_count {
Subroutine body_count: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my $self = shift;
125 0 return $self->{body_count};
126 }
127  
128 0 ▶sub body_truncated {
Subroutine body_truncated: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my $self = shift;
130 0 return $self->{body_truncated};
131 }
132  
133 15 1;
134  
135 __END__
« DKIM/Canonicalization/relaxed.pm ↑ Summary DKIM/Algorithm/dk_rsa_sha1.pm »