Authentication/Handler/AlignedFrom.pm

filter: Statementstmt 97.8% 93 / 95 Branchbran 89.2% 25 / 28 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 14.2% 1 / 7 totaltotal 92.5% 137 / 148 CC 19 SCAR 18.4
CC
19
Cov
96%
eom_callback
12.0
header_callback
5.0
envfrom_callback
4.0
CRAP 6.3
« Authentication/Handler/AbusixDataFeed.pm ↑ Summary Authentication/Handler/PTR.pm »
1 package Mail::Milter::Authentication::Handler::AlignedFrom;
2 4 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mail::Milter::Authentication::Pragmas;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Handler class for Address alignment
7 4 our $VERSION = '4.20261116'; # VERSION
8 4 ▶use base 'Mail::Milter::Authentication::Handler';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶sub default_config {
Subroutine default_config: called CC=1 SCAR=0.0
Pod undocumented
12 1 return {};
13 }
14  
15 1 ▶sub grafana_rows {
Subroutine grafana_rows: called CC=1 SCAR=0.0
Pod undocumented
16 1 my ( $self ) = @_;
17 1 my @rows;
18 1 push @rows, $self->get_json( 'AlignedFrom_metrics' );
19 1 return \@rows;
20 }
21  
22 3 ▶sub register_metrics {
Subroutine register_metrics: called CC=1 SCAR=0.0
Pod documented
23 return {
24 3 'alignedfrom_total' => 'The number of emails processed for AlignedFrom',
25 };
26 }
27  
28 45 ▶sub envfrom_callback {
Subroutine envfrom_callback: called CC=4 SCAR=13.9
Pod undocumented
29 45 my ( $self, $env_from ) = @_;
30  
31 45 ▶ $env_from = q{} if $env_from eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $env_from eq "<>" 2 43 45
32  
33 # Defaults
34 45 $self->{ 'from_header_count' } = 0;
35 45 $self->{ 'envfrom_count' } = 0;
36 45 $self->{ 'smtp_address' } = q{};
37 45 $self->{ 'smtp_domain' } = q{};
38 45 $self->{ 'header_address' } = q{};
39 45 $self->{ 'header_domain' } = q{};
40  
41 45 my $emails = $self->get_addresses_from( $env_from );
42 45 foreach my $email ( @$emails ) {
43 33 ▶ next if ! $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 33 33
44 33 $self->{ 'envfrom_count' } = $self->{ 'envfrom_count' } + 1;
45 # More than 1 here? we set to error in eom callback.!
46 33 $self->{ 'smtp_address'} = lc $email;
47 33 $self->{ 'smtp_domain'} = lc $self->get_domain_from( $email );
48 }
49 }
50  
51 142 ▶sub header_callback {
Subroutine header_callback: called CC=5 SCAR=16.1
Pod undocumented
52 142 my ( $self, $header, $value ) = @_;
53  
54 142 ▶ return if lc $header ne 'from';
Branch: 1 branch
BranchTrueFalseTotal
if lc $header ne "from" 97 45 142
55  
56 45 my $emails = $self->get_addresses_from( $value );
57  
58 45 my $found_domains = {};
59  
60  
61 45 foreach my $email ( @$emails ) {
62 37 ▶ next if ! $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 37 37
63 37 $self->{ 'header_address'} = lc $email;
64 37 my $domain = lc $self->get_domain_from( $email );
65 37 $self->{ 'header_domain'} = $domain;
66 37 $found_domains->{ $domain } = $1;
67 }
68  
69 # We don't consider finding 2 addresses at the same domain in a header to be 2 separate entries
70 # for alignment checking, only count them as one.
71 45 foreach my $domain ( sort keys %$found_domains ) {
72 37 $self->{ 'from_header_count' } = $self->{ 'from_header_count' } + 1;
73 # If there are more than 1 then the result will be set to error in the eom callback
74 # Multiple from headers should always set the result to error.
75 }
76 }
77  
78 3 ▶sub close_callback {
Subroutine close_callback: called CC=1 SCAR=0.0
Pod undocumented
79 3 my ( $self ) = @_;
80 3 delete $self->{ 'envfrom_count' };
81 3 delete $self->{ 'from_header_count' };
82 3 delete $self->{ 'header_address' };
83 3 delete $self->{ 'header_domain' };
84 3 delete $self->{ 'smtp_address' };
85 3 delete $self->{ 'smtp_domain' };
86 }
87  
88 # error = multiple from headers present
89 # null = no addresses present
90 # null_smtp = no smtp address present
91 # null_header = no header address present
92 # pass = addresses match
93 # domain_pass = domains match
94 # orgdomain_pass = domains in same orgdomain
95  
96 45 ▶sub eom_callback {
Subroutine eom_callback: called CC=12 SCAR=24.9
Pod undocumented
97 45 my ( $self ) = @_;
98  
99 45 my $result;
100 my $comment;
101  
102 45 ▶ if ( $self->{ 'from_header_count' } > 1 ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($self->{'from_header_count'} > 1) { } 4 41 45
elsif ($self->{'envfrom_count'} > 1) { } 0 41 41
elsif (not $self->{'smtp_domain'} || $self->{'header_domain'}) { } 2 39 41
elsif (not $self->{'smtp_domain'}) { } 10 29 39
elsif (not $self->{'header_domain'}) { } 10 19 29
elsif ($self->{'smtp_address'} eq $self->{'header_address'}) { } 9 10 19
elsif ($self->{'smtp_domain'} eq $self->{'header_domain'}) { } 2 8 10
103 4 $result = 'permerror';
104 4 $comment = 'No valid header domain';
105 }
106  
107 elsif ( $self->{ 'envfrom_count' } > 1 ) {
108 0 $result = 'permerror';
109 0 $comment = 'No valid envelope domain';
110 }
111  
112 ▶ elsif ( ( ! $self->{ 'smtp_domain' } ) && ( ! $self->{ 'header_domain' } ) ) {
Condition100%3/3
$self->{'smtp_domain'} || $self->{'header_domain'}
l!l&&r!l&&!r
29102
MC/DC100%2/2
$self->{'smtp_domain'} || $self->{'header_domain'}
$self->{'smtp_domain'} $self->{'header_domain'}
Truth table100%3/3
A || B
A= $self->{'smtp_domain'}B= $self->{'header_domain'}
ABresult
1X1
011
000
113 2 $result = 'permerror';
114 2 $comment = 'No valid domains found';
115 }
116  
117 elsif ( ! $self->{ 'smtp_domain' } ) {
118 10 $result = 'permerror';
119 10 $comment = 'No valid envelope domain';
120 }
121  
122 elsif ( ! $self->{ 'header_domain' } ) {
123 10 $result = 'permerror';
124 10 $comment = 'No valid header domain';
125 }
126  
127 elsif ( $self->{ 'smtp_address' } eq $self->{ 'header_address' } ) {
128 9 $result = 'pass';
129 9 $comment = 'Address match';
130 }
131  
132 elsif ( $self->{ 'smtp_domain' } eq $self->{ 'header_domain' } ) {
133 2 $result = 'domain_pass';
134 2 $comment = 'Domain match';
135 }
136  
137 else {
138  
139 # Get Org domain and check that if different.
140 8 ▶ if ( $self->is_handler_loaded( 'DMARC' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_handler_loaded('DMARC')) { } 4 4 8
141 4 my $dmarc_handler = $self->get_handler('DMARC');
142 4 my $dmarc_object = $dmarc_handler->get_dmarc_object();
143 4 my $org_smtp_domain = eval{ $dmarc_object->get_organizational_domain( $self->{ 'smtp_domain' } ); };
144 4 $self->handle_exception( $@ );
145 4 my $org_header_domain = eval{ $dmarc_object->get_organizational_domain( $self->{ 'header_domain' } ); };
146 4 $self->handle_exception( $@ );
147  
148 4 ▶ if ( $org_smtp_domain eq $org_header_domain ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org_smtp_domain eq $org_header_domain) { } 3 1 4
149 3 $result = 'orgdomain_pass';
150 3 $comment = 'Domain org match';
151 }
152  
153 else {
154 1 $result = 'fail';
155 }
156  
157 }
158  
159 else {
160 4 $result = 'fail';
161 }
162  
163 }
164  
165 45 $self->dbgout( 'AlignedFrom', $result, LOG_DEBUG );
166 45 my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-aligned-from' )->safe_set_value( $result );
167 45 ▶ if ( $comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment) 40 5 45
168 40 $header->add_child( Mail::AuthenticationResults::Header::Comment->new()->safe_set_value( $comment ) );
169 }
170 45 $self->add_auth_header( $header );
171  
172 45 $self->metric_count( 'alignedfrom_total', { 'result' => $result } );
173 }
174  
175 4 1;
176  
177 __END__
« Authentication/Handler/AbusixDataFeed.pm ↑ Summary Authentication/Handler/PTR.pm »