lib/Mail/DMARC/Policy.pm

filter: Statementstmt 100% 159 / 159 Branchbran 89.2% 100 / 112 Conditioncond 87.1% 34 / 39 MC/DCmcdc 70.8% 17 / 24 Subroutinesub 100% 31 / 31 Podpod 62.5% 15 / 24 totaltotal 91.5% 356 / 389 CC 92 SCAR 17.3
CC
92
Cov
95%
is_valid
12.0
parse
11.0
ri
7.3
CRAP 5.6
« lib/Mail/DMARC/Report/Store/SQL/Grammars/PostgreSQL.pm ↑ Summary lib/Mail/DMARC.pm »
1 package Mail::DMARC::Policy;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶no warnings 'experimental::args_array_with_signatures'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION = '2.20261110';
8  
9 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use Mail::DMARC::Report::URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 241 ▶sub new( $class, @args ) {
Subroutine new: called CC=6 SCAR=18.1
Pod documented
14 241 ▶ my $package = ref $class ? ref $class : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 241 241
15 241 my $self = bless {}, $package;
16  
17 241 ▶ return $self if !@args; # no args, empty pol
Branch: 1 branch
BranchTrueFalseTotal
unless @args 80 161 241
18 161 ▶ if ( 1 == @args ) { # a string
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @args) 123 38 161
19 123 my $policy = $self->parse( $args[0] );
20 123 $self->is_valid($policy);
21 120 return $policy;
22 }
23  
24 38 ▶ croak "invalid arguments" if @args % 2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 != 0 0 38 38
25 38 my $policy = {@args};
26 38 bless $policy, $package;
27 38 ▶ croak "invalid policy" if !$self->is_valid($policy);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid($policy) 0 35 35
28 35 return bless $policy, $package;
29 }
30  
31 258 ▶sub parse( $self, $str, @junk ) {
Subroutine parse: called CC=11 SCAR=24.0
Pod documented
32 258 ▶ croak "invalid parse request" if @junk;
Branch: 1 branch
BranchTrueFalseTotal
if @junk 0 258 258
33 258 my $cleaned = $str;
34 258 $cleaned =~ s/\s//g; # remove whitespace
35 258 $cleaned =~ s/\\;/;/g; # replace \; with ;
36  
37 # tolerate records seen in the wild (#39)
38 258 $cleaned =~ s/(?:^|;)\K"+|"+(?=;|$)//g;
39 258 $cleaned =~ s/(?:^|;)\Kv=DMARC1,/v=DMARC1;/i;
40  
41 258 my %policy;
42 258 foreach my $tv ( split /;/, $cleaned ) {
43  
44 # fo:0 and sp-none are common typos (#39); RFC 9989 4.8 discards the rest
45 1329 my ( $tag, $value ) = split /=|:|-/, $tv, 2;
46 1329 ▶ next if !defined $value || $value eq '' || $tag !~ /^[a-z]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" or not $tag =~ /^[a-z]+$/i 25 1304 1329
Condition100%6/6
not defined $value or $value eq "" or not $tag =~ /^[a-z]+$/i
l!l&&r!l&&!r
2321304
not defined $value or $value eq ""
l!l&&r!l&&!r
1581306
MC/DC100%3/3
not defined $value or $value eq "" or not $tag =~ /^[a-z]+$/i
not defined $value $value eq "" not $tag =~ /^[a-z]+$/i
Truth table100%4/4
not(A) or B or not(C)
A= not defined $valueB= $value eq ""C= not $tag =~ /^[a-z]+$/i
ABCresult
1XX1
01X1
0011
0000
47 1304 $policy{ lc $tag } = $value;
48 }
49  
50 # RFC 9989 4.8: these values are case-insensitive, and an invalid one
51 # reverts to the default rather than invalidating the record (p, sp and
52 # np are left to is_valid, which applies the rua rule)
53 258 foreach my $tag ( grep { defined $policy{$_} } qw/ p sp np adkim aspf t psd fo / ) {
54 439 $policy{$tag} = lc $policy{$tag};
55 }
56 258 my %valid = (
57 adkim => qr/^[rs]$/,
58 aspf => qr/^[rs]$/,
59 t => qr/^[yn]$/,
60 psd => qr/^[ynu]$/,
61 );
62 258 foreach my $tag ( grep { defined $policy{$_} } keys %valid ) {
63 133 ▶ delete $policy{$tag} if $policy{$tag} !~ $valid{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless $policy{$tag} =~ /$valid{$tag}/ 1 132 133
64 }
65 258 ▶ delete $policy{fo} if defined $policy{fo} && !_is_valid_fo( $policy{fo} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $policy{"fo"} and not _is_valid_fo($policy{"fo"}) 1 257 258
Condition100%3/3
defined $policy{"fo"} and not _is_valid_fo($policy{"fo"})
!ll&&!rl&&r
245121
MC/DC100%2/2
defined $policy{"fo"} and not _is_valid_fo($policy{"fo"})
defined $policy{"fo"} not _is_valid_fo($policy{"fo"})
Truth table100%3/3
A and not(B)
A= defined $policy{"fo"}B= not _is_valid_fo($policy{"fo"})
ABresult
0X0
100
111
66  
67 258 return bless \%policy, ref $self; # inherited defaults + overrides
68 }
69  
70 # RFC 9989 4.8 dmarc-fo: each of 0, 1, d, s at most once, 0 and 1 exclusive
71 13 ▶sub _is_valid_fo($fo) {
Subroutine _is_valid_fo: called CC=4 SCAR=14.0
72 13 my %seen;
73 13 ▶ return 0 if grep { !/^[01ds]$/ || $seen{$_}++ } split /:/, $fo, -1;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /^[01ds]$/ or $seen{$_}++;} split(/:/, $fo, -1) 1 12 13
Condition66%2/3
!/^[01ds]$/ || $seen{$_}++
l!l&&r!l&&!r
01318
MC/DC50%1/2
!/^[01ds]$/ || $seen{$_}++
!/^[01ds]$/ $seen{$_}++
Truth table66%2/3
not(A) || B
A= !/^[01ds]$/B= $seen{$_}++
ABresult
1X1
011
000
74 12 ▶ return !( $seen{0} && $seen{1} );
Condition66%2/3
$seen{'0'} && $seen{'1'}
!ll&&!rl&&r
660
MC/DC0%0/2
$seen{'0'} && $seen{'1'}
$seen{'0'} $seen{'1'}
Truth table66%2/3
A && B
A= $seen{'0'}B= $seen{'1'}
ABresult
0X0
100
111
75 }
76  
77 1 ▶sub stringify($self) {
Subroutine stringify: called CC=2 SCAR=6.9
Pod documented
78 1 my %dmarc_record = %{$self};
79 1 delete @dmarc_record{qw/ domain discovery_method /};
80  
81 1 my $dmarc_txt = 'v=' . ( delete $dmarc_record{v} ); # "v" tag must be first
82 1 foreach my $key ( keys %dmarc_record ) {
83 1 $dmarc_txt .= "; $key=$dmarc_record{$key}";
84 }
85 1 return $dmarc_txt;
86 }
87  
88 11 ▶sub apply_defaults($self) {
Subroutine apply_defaults: called CC=4 SCAR=13.9
Pod documented
89 11 ▶ $self->adkim('r') if !defined $self->adkim;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->adkim 10 1 11
90 11 ▶ $self->aspf('r') if !defined $self->aspf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->aspf 10 1 11
91 11 ▶ $self->fo(0) if !defined $self->fo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->fo 10 1 11
92  
93 # rf, ri, pct are deprecated in DMARCbis (RFC 9989) and MUST be ignored
94 11 return 1;
95 }
96  
97 6 ▶sub v( $self, $val = undef ) {
Subroutine v: called CC=4 SCAR=13.9
Pod documented
98 6 ▶ return $self->{v} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 2 4 6
99 4 ▶ croak "unsupported DMARC version" if 'DMARC1' ne uc $val;
Branch: 1 branch
BranchTrueFalseTotal
if "DMARC1" ne uc $val 1 3 4
100 3 return $self->{v} = $val;
101 }
102  
103 156 ▶sub p( $self, $val = undef ) {
Subroutine p: called CC=4 SCAR=13.9
Pod documented
104 156 ▶ return $self->{p} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 143 13 156
105 13 ▶ croak "invalid p" if !$self->is_valid_p($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_p($val) 3 9 12
106 9 return $self->{p} = $val;
107 }
108  
109 22 ▶sub sp( $self, $val = undef ) {
Subroutine sp: called CC=4 SCAR=13.9
Pod documented
110 22 ▶ return $self->{sp} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 13 9 22
111 9 ▶ croak "invalid sp ($val)" if !$self->is_valid_p($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_p($val) 3 6 9
112 6 return $self->{sp} = $val;
113 }
114  
115 30 ▶sub np( $self, $val = undef ) {
Subroutine np: called CC=4 SCAR=13.9
Pod undocumented
116 30 ▶ return $self->{np} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 20 10 30
117 10 ▶ croak "invalid np ($val)" if !$self->is_valid_p($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_p($val) 3 7 10
118 7 return $self->{np} = $val;
119 }
120  
121 153 ▶sub psd( $self, $val = undef ) {
Subroutine psd: called CC=4 SCAR=13.9
Pod undocumented
122 153 ▶ return $self->{psd} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 143 10 153
123 10 ▶ croak "invalid psd ($val)" if 0 == grep {/^\Q$val\E$/i} qw/ y n u /;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({/^\Q$val\E$/i;} "y", "n", "u") 3 7 10
124 7 return $self->{psd} = lc $val;
125 }
126  
127 53 ▶sub t( $self, $val = undef ) {
Subroutine t: called CC=4 SCAR=13.9
Pod undocumented
128 53 ▶ return $self->{t} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 45 8 53
129 8 ▶ croak "invalid t ($val)" if 0 == grep {/^\Q$val\E$/i} qw/ y n /;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({/^\Q$val\E$/i;} "y", "n") 3 5 8
130 5 return $self->{t} = lc $val;
131 }
132  
133 62 ▶sub adkim( $self, $val = undef ) {
Subroutine adkim: called CC=4 SCAR=13.9
Pod documented
134 62 ▶ return $self->{adkim} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 46 16 62
135 16 ▶ croak "invalid adkim" if 0 == grep {/^\Q$val\E$/ix} qw/ r s /;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({/^\Q$val\E$/xi;} "r", "s") 2 14 16
136 14 return $self->{adkim} = $val;
137 }
138  
139 97 ▶sub aspf( $self, $val = undef ) {
Subroutine aspf: called CC=4 SCAR=13.9
Pod documented
140 97 ▶ return $self->{aspf} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 80 17 97
141 17 ▶ croak "invalid aspf" if 0 == grep {/^\Q$val\E$/ix} qw/ r s /;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == grep({/^\Q$val\E$/xi;} "r", "s") 2 15 17
142 15 return $self->{aspf} = $val;
143 }
144  
145 33 ▶sub fo( $self, $val = undef ) {
Subroutine fo: called CC=4 SCAR=13.9
Pod documented
146 33 ▶ return $self->{fo} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 11 22 33
147 22 ▶ croak "invalid fo: $val" if $val !~ /^[01ds](:[01ds])*$/ix;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^[01ds](:[01ds])*$/xi 4 18 22
148 18 return $self->{fo} = $val;
149 }
150  
151 80 ▶sub rua( $self, $val = undef ) {
Subroutine rua: called CC=4 SCAR=13.9
Pod documented
152 80 ▶ return $self->{rua} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 49 31 80
153 31 ▶ croak "invalid rua" if !$self->is_valid_uri_list($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_uri_list($val) 2 29 31
154 29 return $self->{rua} = $val;
155 }
156  
157 2 ▶sub ruf( $self, $val = undef ) {
Subroutine ruf: called CC=4 SCAR=13.9
Pod documented
158 2 ▶ return $self->{ruf} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 2 2
159 2 ▶ croak "invalid rua" if !$self->is_valid_uri_list($val);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_uri_list($val) 1 1 2
160 1 return $self->{ruf} = $val;
161 }
162  
163 7 ▶sub rf( $self, $val = undef ) {
Subroutine rf: called CC=5 SCAR=16.2
Pod documented
164 7 ▶ return $self->{rf} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 7 7
165 7 foreach my $f ( split /,/, $val ) {
166 7 ▶ croak "invalid format: $f" if !$self->is_valid_rf($f);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_rf($f) 3 4 7
167 }
168 4 return $self->{rf} = $val;
169 }
170  
171 7 ▶sub ri( $self, $val = undef ) {
Subroutine ri: called CC=7 SCAR=19.8
Pod documented
172 7 ▶ return $self->{ri} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 7 7
173 7 ▶ croak "not numeric ($val)!" if $val =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\D/ 2 5 7
174 5 ▶ croak "not an integer!" if $val != int $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val != int $val 0 5 5
175 5 ▶ croak "out of range" if ( $val < 0 || $val > 4294967295 );
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 or $val > 4294967295 1 4 5
Condition66%2/3
$val < 0 or $val > 4294967295
l!l&&r!l&&!r
014
MC/DC50%1/2
$val < 0 or $val > 4294967295
$val < 0 $val > 4294967295
Truth table66%2/3
A or B
A= $val < 0B= $val > 4294967295
ABresult
1X1
011
000
176 4 return $self->{ri} = $val;
177 }
178  
179 12 ▶sub pct( $self, $val = undef ) {
Subroutine pct: called CC=7 SCAR=19.6
Pod documented
180 12 ▶ return $self->{pct} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 1 11 12
181 11 ▶ croak "not numeric ($val)!" if $val =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /\D/ 5 6 11
182 6 ▶ croak "not an integer!" if $val != int $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val != int $val 0 6 6
183 6 ▶ croak "out of range" if $val < 0 || $val > 100;
Branch: 1 branch
BranchTrueFalseTotal
if $val < 0 or $val > 100 1 5 6
Condition66%2/3
$val < 0 or $val > 100
l!l&&r!l&&!r
015
MC/DC50%1/2
$val < 0 or $val > 100
$val < 0 $val > 100
Truth table66%2/3
A or B
A= $val < 0B= $val > 100
ABresult
1X1
011
000
184 5 return $self->{pct} = $val;
185 }
186  
187 52 ▶sub discovery_method( $self, $val = undef ) {
Subroutine discovery_method: called CC=4 SCAR=14.5
Pod undocumented
188 52 ▶ return $self->{discovery_method} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 52 52
189 52 ▶ croak "invalid discovery_method ($val)" if $val !~ /^(?:psl|treewalk)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^(?:psl|treewalk)$/i 0 52 52
190 52 return $self->{discovery_method} = lc $val;
191 }
192  
193 192 ▶sub domain( $self, $val = undef ) {
Subroutine domain: called CC=3 SCAR=11.0
Pod undocumented
194 192 ▶ return $self->{domain} if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 177 15 192
195 15 return $self->{domain} = $val;
196 }
197  
198 12 ▶sub is_valid_rf( $self, $f ) {
Subroutine is_valid_rf: called CC=2 SCAR=6.9
Pod undocumented
199 12 ▶ return ( grep {/^\Q$f\E$/i} qw/ iodef afrf / ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({/^\Q$f\E$/i;} 'iodef', 'afrf') ? : 6 6 12
200 }
201  
202 251 ▶sub is_valid_p( $self, $p ) {
Subroutine is_valid_p: called CC=3 SCAR=11.0
Pod undocumented
203 251 ▶ croak "unspecified p" if !defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 1 250 251
204 250 ▶ return ( grep {/^\Q$p\E$/i} qw/ none reject quarantine / ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({/^\Q$p\E$/i;} 'none', 'reject', 'quarantine') ? : 231 19 250
205 }
206  
207 39 ▶sub is_valid_uri_list( $self, $str ) {
Subroutine is_valid_uri_list: called CC=2 SCAR=7.0
Pod undocumented
208 39 ▶ $self->{uri} ||= Mail::DMARC::Report::URI->new;
Condition66%2/3
$self->{"uri"} ||= "Mail::DMARC::Report::URI"->new
l!l&&r!l&&!r
9300
MC/DC0%0/2
$self->{"uri"} ||= "Mail::DMARC::Report::URI"->new
$self->{"uri"} "Mail::DMARC::Report::URI"->new
Truth table66%2/3
A ||= B
A= $self->{"uri"}B= "Mail::DMARC::Report::URI"->new
ABresult
1X1
011
000
209 39 my $uris = $self->{uri}->parse($str);
210 39 return scalar @$uris;
211 }
212  
213 167 ▶sub is_valid( $self, $obj = undef ) {
Subroutine is_valid: called CC=12 SCAR=24.9
Pod undocumented
214 167 ▶ $obj = $self if !$obj;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 6 161 167
215 167 ▶ croak "missing version specifier" if !$obj->{v};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"v"} 1 166 167
216 166 ▶ croak "invalid version" if 'DMARC1' ne uc $obj->{v};
Branch: 1 branch
BranchTrueFalseTotal
if "DMARC1" ne uc $obj->{"v"} 0 166 166
217  
218 # psd=y domains (PSDs) are not required to have a p= tag
219 166 ▶ my $is_psd = defined $obj->{psd} && lc $obj->{psd} eq 'y';
Condition100%3/3
defined $obj->{'psd'} && lc $obj->{'psd'} eq 'y'
!ll&&!rl&&r
15934
MC/DC100%2/2
defined $obj->{'psd'} && lc $obj->{'psd'} eq 'y'
defined $obj->{'psd'} lc $obj->{'psd'} eq 'y'
Truth table100%3/3
A && B
A= defined $obj->{'psd'}B= lc $obj->{'psd'} eq 'y'
ABresult
0X0
100
111
220 166 ▶ my @invalid = grep { defined $obj->{$_} && !$self->is_valid_p( $obj->{$_} ) }
Condition100%3/3
defined $obj->{$_} && !$self->is_valid_p($obj->{$_})
!ll&&!rl&&r
2862066
MC/DC100%2/2
defined $obj->{$_} && !$self->is_valid_p($obj->{$_})
defined $obj->{$_} !$self->is_valid_p($obj->{$_})
Truth table100%3/3
A && not(B)
A= defined $obj->{$_}B= !$self->is_valid_p($obj->{$_})
ABresult
0X0
100
111
221 qw/ p sp np /;
222  
223 # RFC 9989 4.10.1: act as if the record were just p=none, so sp and np go
224 166 ▶ if ( @invalid || ( !$obj->{p} && !$is_psd ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invalid or not $obj->{"p"} || $is_psd) 11 155 166
Condition100%6/6
@invalid or not $obj->{"p"} || $is_psd
l!l&&r!l&&!r
65155
$obj->{'p'} || $is_psd
l!l&&r!l&&!r
15235
MC/DC100%3/3
@invalid or not $obj->{"p"} || $is_psd
@invalid $obj->{'p'} $is_psd
Truth table100%4/4
A or not(B || C)
A= @invalidB= $obj->{'p'}C= $is_psd
ABCresult
1XX1
0001
01X0
0010
225 croak "missing or invalid policy action"
226 11 ▶ if !( $obj->{rua} && $self->is_valid_uri_list( $obj->{rua} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"rua"} and $self->is_valid_uri_list($obj->{"rua"}) 6 5 11
Condition100%3/3
$obj->{"rua"} and $self->is_valid_uri_list($obj->{"rua"})
!ll&&!rl&&r
515
MC/DC100%2/2
$obj->{"rua"} and $self->is_valid_uri_list($obj->{"rua"})
$obj->{"rua"} $self->is_valid_uri_list($obj->{"rua"})
Truth table100%3/3
A and B
A= $obj->{"rua"}B= $self->is_valid_uri_list($obj->{"rua"})
ABresult
0X0
100
111
227 5 delete @$obj{qw/ sp np /};
228 5 $obj->{p} = 'none';
229 }
230  
231 # everything else is optional
232 160 return 1;
233 }
234  
235 16 1;
236  
237 __END__
« lib/Mail/DMARC/Report/Store/SQL/Grammars/PostgreSQL.pm ↑ Summary lib/Mail/DMARC.pm »