Authentication/Handler/Sanitize.pm

filter: Statementstmt 89.1% 115 / 129 Branchbran 77.2% 34 / 44 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 84.2% 16 / 19 Podpod 15.3% 2 / 13 totaltotal 82.1% 175 / 213 CC 32 SCAR 20.4
CC
32
Cov
87%
header_callback
16.0
eom_callback
8.0
is_hostname_mine
8.0
CRAP 7.7
« Authentication/Handler.pm ↑ Summary Authentication/Handler/AbusixDataFeed.pm »
1 package Mail::Milter::Authentication::Handler::Sanitize;
2 35 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for Removing headers
7 35 our $VERSION = '4.20261116'; # VERSION
8 35 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use List::MoreUtils qw{ uniq };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub default_config {
Subroutine default_config: not called CC=1 SCAR=6.9
Pod undocumented
12 return {
13 0 'hosts_to_remove' => [ 'example.com', 'example.net' ],
14 'remove_headers' => 'yes',
15 'extra_auth_results_types' => ['X-Authentication-Results'],
16 };
17 }
18  
19 0 ▶sub grafana_rows {
Subroutine grafana_rows: not called CC=1 SCAR=6.9
Pod undocumented
20 0 my ( $self ) = @_;
21 0 my @rows;
22 0 push @rows, $self->get_json( 'Sanitize_metrics' );
23 0 return \@rows;
24 }
25  
26 38 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
27 return {
28 38 'sanitize_remove_total' => 'The number Authentication Results headers removed',
29 };
30 }
31  
32 52 ▶sub is_hostname_mine {
Subroutine is_hostname_mine: called CC=8 SCAR=20.8
Pod undocumented
33 52 my ( $self, $check_hostname ) = @_;
34 52 my $config = $self->handler_config();
35  
36 52 ▶ return 0 if ! defined $check_hostname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $check_hostname 0 52 52
37  
38 # A fully qualified name with trailing dot(s) is the same host, compare
39 # without them so "host.example.com." is not treated as foreign.
40 52 $check_hostname =~ s/\.+$//;
41  
42 52 ▶ if ( exists( $config->{'hosts_to_remove'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"hosts_to_remove"}) 51 1 52
43 51 foreach my $remove_hostname ( @{ $config->{'hosts_to_remove'} } ) {
44 53 ( my $remove_for = $remove_hostname ) =~ s/\.+$//;
45 53 ▶ if ( $check_hostname =~ m/^(.*\.)?\Q${remove_for}\E$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_hostname =~ /^(.*\.)?\Q$remove_for\E$/iu) 13 40 53
46 13 return 1;
47 }
48 }
49 }
50  
51 39 my $hostname = $self->get_my_hostname();
52 39 $hostname =~ s/\.+$//;
53 39 my ($check_for) = $hostname =~ /^[^\.]+\.(.*)/;
54 39 ▶ if ( defined $check_for && $check_hostname =~ m/^(.*\.)?\Q${check_for}\E$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $check_for and $check_hostname =~ /^(.*\.)?\Q$check_for\E$/iu) 8 31 39
Condition100%3/3
defined $check_for and $check_hostname =~ /^(.*\.)?\Q$check_for\E$/iu
!ll&&!rl&&r
15168
MC/DC100%2/2
defined $check_for and $check_hostname =~ /^(.*\.)?\Q$check_for\E$/iu
defined $check_for $check_hostname =~ /^(.*\.)?\Q$check_for\E$/iu
Truth table100%3/3
A and B
A= defined $check_forB= $check_hostname =~ /^(.*\.)?\Q$check_for\E$/iu
ABresult
0X0
100
111
55 8 return 1;
56 }
57  
58 31 my $authserv_id = $self->get_my_authserv_id();
59 31 $authserv_id =~ s/\.+$//;
60 31 ▶ if ( fc( $check_hostname ) eq fc( $authserv_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (fc $check_hostname eq fc $authserv_id) 11 20 31
61 11 return 1;
62 }
63  
64 20 return 0;
65 }
66  
67 23 ▶sub remove_auth_header {
Subroutine remove_auth_header: called CC=2 SCAR=6.9
Pod undocumented
68 23 my ( $self, $index, $type ) = @_;
69 23 $self->metric_count( 'sanitize_remove_total', {'header'=>$type} );
70 23 ▶ if ( !exists( $self->{'remove_auth_headers'}->{$type} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"remove_auth_headers"}{$type}) 8 15 23
71 8 $self->{'remove_auth_headers'}->{$type} = [];
72 }
73 23 push @{ $self->{'remove_auth_headers'}->{$type} }, $index;
74 }
75  
76 # Which headers of $type have been marked for removal, as 1-based occurrence
77 # numbers within that type. Exposed so the ARC handler can exclude them from
78 # the seal rather than attesting to results we have just stripped.
79 10 ▶sub get_removed_auth_header_indexes {
Subroutine get_removed_auth_header_indexes: called CC=3 SCAR=11.1
Pod undocumented
80 10 my ( $self, $type ) = @_;
81 10 ▶ return [] if ref $self->{'remove_auth_headers'} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"remove_auth_headers"} ne "HASH" 0 10 10
82 10 ▶ return $self->{'remove_auth_headers'}->{$type} // [];
Condition100%2/2
$self->{"remove_auth_headers"}{$type} // []
l!l
82
MC/DC100%1/1
$self->{"remove_auth_headers"}{$type} // []
$self->{"remove_auth_headers"}{$type}
Truth table100%2/2
A
A= $self->{"remove_auth_headers"}{$type}
Aresult
11
00
83 }
84  
85 {
86 35 my $headers_to_remove = {
87 'x-disposition-quarantine' => { silent => 1 },
88 };
89  
90 0 ▶ sub add_header_to_sanitize_list {
Subroutine add_header_to_sanitize_list: not called CC=1 SCAR=6.9
Pod documented
91 0 my ( $self, $header, $silent ) = @_;
92 0 $headers_to_remove->{lc $header} = { silent => $silent };
93 }
94  
95 1033 ▶ sub get_headers_to_remove {
Subroutine get_headers_to_remove: called CC=1 SCAR=0.0
Pod undocumented
96 1033 my ( $self ) = @_;
97 1033 my @headers = sort keys $headers_to_remove->%*;
98 1033 return \@headers;
99 }
100  
101 80 ▶ sub get_remove_header_settings {
Subroutine get_remove_header_settings: called CC=1 SCAR=0.0
Pod undocumented
102 80 my ($self, $key) = @_;
103 80 return $headers_to_remove->{lc $key};
104 }
105 }
106  
107 35 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=2 SCAR=6.9
Pod undocumented
108 80 my ( $self, $env_from ) = @_;
109 80 $self->{'auth_result_header_index'} = {};
110 80 $self->{'remove_auth_headers'} = {};
111  
112 80 my $headers = {};
113 80 foreach my $header ( sort @{ $self->get_headers_to_remove() } ) {
114 $headers->{ lc $header } = {
115 'index' => 0,
116 'silent' => $self->get_remove_header_settings($header)->{silent},
117 80 };
118 }
119 80 $self->{'header_hash'} = $headers;
120 }
121  
122 903 ▶sub header_callback {
Subroutine header_callback: called CC=14 SCAR=27.7
Pod undocumented
123 903 my ( $self, $header, $value ) = @_;
124 903 my $config = $self->handler_config();
125  
126 903 ▶ return if ( $self->is_trusted_ip_address() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_trusted_ip_address 30 873 903
127 873 ▶ return if ( lc $config->{'remove_headers'} eq 'no' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $config->{"remove_headers"} eq "no" 0 873 873
128  
129 873 my @types = ('Authentication-Results');
130 873 ▶ if ( exists $config->{extra_auth_results_types} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config->{"extra_auth_results_types"}) 0 873 873
131 0 push @types, $config->{extra_auth_results_types}->@*;
132 }
133 873 for my $type (uniq sort @types) {
134  
135 # Sanitize Authentication-Results headers
136 873 ▶ if ( lc $header eq lc $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $header eq lc $type) 40 833 873
137 40 ▶ if ( !exists $self->{'auth_result_header_index'}->{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"auth_result_header_index"}{$type}) 10 30 40
138 10 $self->{'auth_result_header_index'}->{$type} = 0;
139 }
140 $self->{'auth_result_header_index'}->{$type} =
141 40 $self->{'auth_result_header_index'}->{$type} + 1;
142  
143 40 my $authserv_id = '';
144 40 eval {
145 40 my $parsed = Mail::AuthenticationResults::Parser->new()->parse($value);
146 40 $authserv_id = $parsed->value()->value();
147 };
148 40 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 40 40
149 0 $self->handle_exception($error);
150 0 $self->log_error("Error parsing existing Authentication-Results header: $value");
151 }
152  
153 40 my $remove = 0;
154 40 my $silent = lc $config->{'remove_headers'} eq 'silent';
155 40 ▶ if ( $authserv_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authserv_id) { } 40 0 40
156 40 $remove = $self->is_hostname_mine($authserv_id);
157 }
158 else {
159 # We couldn't parse the authserv_id, removing this header is the safest option
160 # Add to X-Received headers for analysis later
161 0 $remove = 1;
162 0 $silent = 0;
163 }
164  
165 40 ▶ if ( $remove ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove) 23 17 40
166 23 $self->remove_auth_header( $self->{'auth_result_header_index'}->{$type}, $type );
167 23 ▶ if ( ! $silent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 23 0 23
168 23 my $forged_header =
169 '(Received '.$type.' header removed by '
170 . $self->get_my_hostname()
171 . ')' . "\n"
172 . ' '
173 . $value;
174 23 $self->append_header( 'X-Received-'.$type,
175 $forged_header );
176 }
177 }
178 }
179 }
180  
181 # Sanitize other headers
182 873 foreach my $remove_header ( sort @{ $self->get_headers_to_remove() } ) {
183 873 ▶ next if ( lc $remove_header ne lc $header );
Branch: 1 branch
BranchTrueFalseTotal
if lc $remove_header ne lc $header 865 8 873
184 8 $self->{'header_hash'}->{ lc $header }->{'index'} = $self->{'header_hash'}->{ lc $header }->{'index'} + 1;
185 8 $self->metric_count( 'sanitize_remove_total', {'header'=> lc $header} );
186  
187 8 ▶ if ( ! $self->{'header_hash'}->{ lc $header }->{'silent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"header_hash"}{lc $header}{"silent"}) 0 8 8
188 0 my $forged_header =
189 '(Received ' . $remove_header . ' header removed by '
190 . $self->get_my_hostname()
191 . ')' . "\n"
192 . ' '
193 . $value;
194 0 $self->append_header( 'X-Received-' . $remove_header,
195 $forged_header );
196 }
197 }
198 }
199  
200 80 ▶sub eom_callback {
Subroutine eom_callback: called CC=8 SCAR=20.8
Pod undocumented
201 80 my ($self) = @_;
202 80 my $config = $self->handler_config();
203 80 ▶ return if ( lc $config->{'remove_headers'} eq 'no' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $config->{"remove_headers"} eq "no" 0 80 80
204  
205 80 ▶ if ( exists( $self->{'remove_auth_headers'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"remove_auth_headers"}) 80 0 80
206 80 foreach my $type ( sort keys $self->{'remove_auth_headers'}->%* ) {
207 8 foreach my $index ( reverse @{ $self->{'remove_auth_headers'}->{$type} } ) {
208 23 $self->dbgout( 'RemoveAuthHeader', "$type $index", LOG_DEBUG );
209 23 $self->change_header( $type, $index, q{} );
210 }
211 }
212 }
213  
214 80 foreach my $remove_header ( sort @{ $self->get_headers_to_remove() } ) {
215 80 my $max_index = $self->{'header_hash'}->{ lc $remove_header }->{'index'};
216 80 ▶ if ( $max_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_index) 4 76 80
217 4 ▶ for ( my $index = $max_index; $index > 0; $index-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) 8 4 12
218 8 $self->dbgout( 'RemoveHeader', "$remove_header $index", LOG_DEBUG );
219 8 $self->change_header( $remove_header, $index, q{} );
220 }
221 }
222 }
223 }
224  
225 124 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
226 124 my ( $self ) = @_;
227 124 delete $self->{'remove_auth_headers'};
228 124 delete $self->{'auth_result_header_index'};
229 124 delete $self->{'header_hash'};
230 }
231  
232 35 1;
233  
234 __END__
« Authentication/Handler.pm ↑ Summary Authentication/Handler/AbusixDataFeed.pm »