lib/Mail/DMARC/Report/Aggregate.pm

filter: Statementstmt 89.9% 107 / 119 Branchbran 70.8% 34 / 48 Conditioncond 72.2% 13 / 18 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 10 totaltotal 79.4% 178 / 224 CC 43 SCAR 20.0
CC
43
Cov
83%
get_policy_evaluated_as_xml
17.6
get_auth_results_as_xml
9.0
get_identifiers_as_xml
8.2
CRAP 7.4
« blib/lib/Mail/DMARC/Report/Aggregate/Record/Auth_Results.pm ↑ Summary lib/Mail/DMARC/Report/URI.pm »
1 package Mail::DMARC::Report::Aggregate;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '2.20260827';
7  
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Mail::DMARC::Report::Aggregate::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 354 ▶sub metadata($self) {
Subroutine metadata: called CC=2 SCAR=6.9
Pod undocumented
16 354 ▶ return $self->{metadata} if ref $self->{metadata};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"metadata"} 317 37 354
17 37 return $self->{metadata} = Mail::DMARC::Report::Aggregate::Metadata->new;
18 }
19  
20 143 ▶sub policy_published( $self, $policy = undef ) {
Subroutine policy_published: called CC=4 SCAR=13.9
Pod undocumented
21 143 ▶ return $self->{policy_published} if !$policy;
Branch: 1 branch
BranchTrueFalseTotal
unless $policy 109 34 143
22 34 ▶ croak "not a policy object!" if 'Mail::DMARC::Policy' ne ref $policy;
Branch: 1 branch
BranchTrueFalseTotal
if "Mail::DMARC::Policy" ne ref $policy 0 34 34
23 34 return $self->{policy_published} = $policy;
24 }
25  
26 68 ▶sub record( $self, $record = undef, @extra ) { ## no critic (Ambiguous)
Subroutine record: called CC=7 SCAR=19.9
Pod undocumented
27 68 ▶ if ( !$record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 38 30 68
28 38 ▶ return $self->{record} || [];
Condition100%2/2
$self->{'record'} || []
l!l
371
MC/DC100%1/1
$self->{'record'} || []
$self->{'record'}
Truth table100%2/2
A
A= $self->{'record'}
Aresult
11
00
29 }
30  
31 30 ▶ if (@extra) { croak "invalid args"; }
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) 0 30 30
32  
33 30 ▶ if ( 'Mail::DMARC::Report::Aggregate::Record' ne ref $record ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Mail::DMARC::Report::Aggregate::Record" ne ref $record) 0 30 30
34 0 croak "not a record object";
35 }
36  
37 30 ▶ $self->{record} ||= [];
Condition100%2/2
$self->{"record"} ||= []
l!l
327
MC/DC100%1/1
$self->{"record"} ||= []
$self->{"record"}
Truth table100%2/2
A
A= $self->{"record"}
Aresult
11
00
38  
39 30 push @{ $self->{record} }, $record;
40  
41 30 return $self->{record};
42 }
43  
44 0 ▶sub dump_report($self) {
Subroutine dump_report: not called CC=1 SCAR=6.9
Pod undocumented
45 0 carp Dumper( $self->{metadata}, $self->{policy_published}, $self->{record} );
46 0 return;
47 }
48  
49 12 ▶sub as_xml($self) {
Subroutine as_xml: called CC=1 SCAR=0.0
Pod undocumented
50 12 my $meta = $self->metadata->as_xml;
51 12 my $pubp = $self->get_policy_published_as_xml;
52 12 my $reco = $self->get_record_as_xml;
53  
54 return <<"EO_XML"
55 <?xml version="1.0"?>
56 <feedback xmlns="urn:ietf:params:xml:ns:dmarc-2.0">
57 $meta
58 $pubp
59 $reco</feedback>
60 EO_XML
61 12 ;
62 }
63  
64 14 ▶sub get_record_as_xml($self) {
Subroutine get_record_as_xml: called CC=5 SCAR=16.3
Pod undocumented
65  
66 14 my $rec_xml = '';
67 14 foreach my $rec ( @{ $self->{record} } ) {
68 17 $rec_xml .= "\t<record>\n";
69 17 ▶ my $ip = $rec->{row}{source_ip} or croak "no source IP!?";
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $rec->{"row"}{"source_ip"} or croak("no source IP!?") 17 0 17
70 17 ▶ my $count = $rec->{row}{count} or croak "no count!?";
Branch: 1 branch
BranchTrueFalseTotal
my $count = $rec->{"row"}{"count"} or croak("no count!?") 17 0 17
71 17 ▶ $rec->{row}{policy_evaluated}{disposition} or croak "no disposition?";
Branch: 1 branch
BranchTrueFalseTotal
$rec->{"row"}{"policy_evaluated"}{"disposition"} or croak("no disposition?") 17 0 17
72 17 $ip = XML::LibXML::Text->new($ip)->toString();
73 17 $count = XML::LibXML::Text->new($count)->toString();
74 17 $rec_xml
75 .= "\t\t<row>\n"
76 . "\t\t\t<source_ip>$ip</source_ip>\n"
77 . "\t\t\t<count>$count</count>\n"
78 . $self->get_policy_evaluated_as_xml($rec)
79 . "\t\t</row>\n"
80 . $self->get_identifiers_as_xml($rec)
81 . $self->get_auth_results_as_xml($rec);
82 17 $rec_xml .= "\t</record>\n";
83 }
84 14 return $rec_xml;
85 }
86  
87 17 ▶sub get_identifiers_as_xml( $self, $rec ) {
Subroutine get_identifiers_as_xml: called CC=8 SCAR=21.0
Pod undocumented
88 17 my $id = "\t\t<identifiers>\n";
89 17 foreach my $f (qw/ envelope_to envelope_from header_from /) {
90 51 ▶ if ( $f eq 'header_from' ) { # min occurs = 1
Branch: 3 branches
BranchTrueFalseTotal
if ($f eq 'header_from') { } 17 34 51
elsif ($f eq 'envelope_from') { } 17 17 34
elsif ($f eq 'envelope_to') { } 17 0 17
91 17 ▶ croak "missing header_from!" if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 0 17 17
92 }
93 elsif ( $f eq 'envelope_from' ) { # min occurs = 1
94 17 ▶ $rec->{identifiers}{$f} = '' if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 0 17 17
95 }
96 elsif ( $f eq 'envelope_to' ) { # min occurs = 0
97 17 ▶ next if !$rec->{identifiers}{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{"identifiers"}{$f} 7 10 17
98 }
99  
100 44 my $val = XML::LibXML::Text->new( $rec->{identifiers}{$f} )->toString();
101 44 $id .= "\t\t\t<$f>$val</$f>\n";
102 }
103 17 $id .= "\t\t</identifiers>\n";
104 17 return $id;
105 }
106  
107 17 ▶sub get_auth_results_as_xml( $self, $rec ) {
Subroutine get_auth_results_as_xml: called CC=9 SCAR=22.0
Pod undocumented
108 17 my $ar = "\t\t<auth_results>\n";
109  
110 17 foreach my $dkim_sig ( @{ $rec->{auth_results}{dkim} } ) {
111 11 $ar .= "\t\t\t<dkim>\n";
112 11 foreach my $g (qw/ domain selector result human_result /) {
113 44 ▶ next if !defined $dkim_sig->{$g} && $g ne 'selector';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $dkim_sig->{$g} and $g ne "selector" 1 43 44
Condition100%3/3
not defined $dkim_sig->{$g} and $g ne "selector"
!ll&&!rl&&r
4211
MC/DC100%2/2
not defined $dkim_sig->{$g} and $g ne "selector"
not defined $dkim_sig->{$g} $g ne "selector"
Truth table100%3/3
not(A) and B
A= not defined $dkim_sig->{$g}B= $g ne "selector"
ABresult
0X0
100
111
114 my $val
115 = defined $dkim_sig->{$g}
116 43 ▶ ? XML::LibXML::Text->new( $dkim_sig->{$g} )->toString()
Branch: 1 branch
BranchTrueFalseTotal
defined $dkim_sig->{$g} ? : 42 1 43
117 : '';
118 43 $ar .= "\t\t\t\t<$g>$val</$g>\n";
119 }
120 11 $ar .= "\t\t\t</dkim>\n";
121 }
122  
123 17 foreach my $spf ( @{ $rec->{auth_results}{spf} } ) {
124 14 $ar .= "\t\t\t<spf>\n";
125 14 foreach my $g (qw/ domain scope result /) {
126 42 ▶ next if !defined $spf->{$g};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spf->{$g} 0 42 42
127 42 my $val = XML::LibXML::Text->new( $spf->{$g} )->toString();
128 42 $ar .= "\t\t\t\t<$g>$val</$g>\n";
129 }
130 14 $ar .= "\t\t\t</spf>\n";
131 }
132  
133 17 $ar .= "\t\t</auth_results>\n";
134 17 return $ar;
135 }
136  
137 14 ▶sub get_policy_published_as_xml($self) {
Subroutine get_policy_published_as_xml: called CC=8 SCAR=20.8
Pod undocumented
138 14 ▶ my $pp = $self->policy_published or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $pp = $self->policy_published or return "" 14 0 14
139 14 my $xml = "\t<policy_published>\n\t\t<domain>$pp->{domain}</domain>\n";
140 14 foreach my $f (qw/ adkim aspf p sp np t psd fo discovery_method /) {
141 126 my $v = $pp->{$f};
142  
143 # Set some default values for missing optional fields if necessary
144 126 ▶ if ( $f eq 'sp' && !defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "sp" and not defined $v) 14 112 126
Condition66%2/3
$f eq "sp" and not defined $v
!ll&&!rl&&r
112014
MC/DC50%1/2
$f eq "sp" and not defined $v
$f eq "sp" not defined $v
Truth table66%2/3
A and not(B)
A= $f eq "sp"B= not defined $v
ABresult
0X0
100
111
145 14 $v = $pp->{'p'};
146 }
147 126 ▶ if ( $f eq 'fo' && !defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "fo" and not defined $v) 11 115 126
Condition100%3/3
$f eq "fo" and not defined $v
!ll&&!rl&&r
112311
MC/DC100%2/2
$f eq "fo" and not defined $v
$f eq "fo" not defined $v
Truth table100%3/3
A and not(B)
A= $f eq "fo"B= not defined $v
ABresult
0X0
100
111
148 11 $v = '0';
149 }
150 126 ▶ next if !defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 81 45 126
151 45 $v = XML::LibXML::Text->new($v)->toString();
152 45 $xml .= "\t\t<$f>$v</$f>\n";
153 }
154 14 $xml .= "\t</policy_published>";
155 14 return $xml;
156 }
157  
158 17 ▶sub get_policy_evaluated_as_xml( $self, $rec ) {
Subroutine get_policy_evaluated_as_xml: called CC=7 SCAR=28.7
Pod undocumented
159 17 my $pe = "\t\t\t<policy_evaluated>\n";
160  
161 17 foreach my $f (qw/ disposition dkim spf /) {
162 51 my $val = XML::LibXML::Text->new( $rec->{row}{policy_evaluated}{$f} )
163 ->toString();
164 51 $pe .= "\t\t\t\t<$f>$val</$f>\n";
165 }
166  
167 17 my $reasons = $rec->{row}{policy_evaluated}{reason};
168 17 ▶ if ( $reasons && scalar @$reasons ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reasons and scalar @$reasons) 0 17 17
Condition50%1/2
$reasons and scalar @$reasons
!ll
017
MC/DC0%0/1
$reasons and scalar @$reasons
$reasons
Truth table50%1/2
A
A= $reasons
Aresult
00
11
169 0 foreach my $reason (@$reasons) {
170 0 my $typeval = XML::LibXML::Text->new( $reason->{type} )->toString();
171 0 $pe .= "\t\t\t\t<reason>\n";
172 0 $pe .= "\t\t\t\t\t<type>$typeval</type>\n";
173  
174 # comment is minOccurs=0 in the RFC 7489 schema; only emit when present
175 0 ▶ if ( defined $reason->{comment} && $reason->{comment} =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason->{"comment"} and $reason->{"comment"} =~ /\S/) 0 0 0
Condition0%0/3
defined $reason->{"comment"} and $reason->{"comment"} =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $reason->{"comment"} and $reason->{"comment"} =~ /\S/
defined $reason->{"comment"} $reason->{"comment"} =~ /\S/
Truth table0%0/3
A and B
A= defined $reason->{"comment"}B= $reason->{"comment"} =~ /\S/
ABresult
0X0
100
111
176 my $commentval
177 0 = XML::LibXML::Text->new( $reason->{comment} )->toString();
178 0 $pe .= "\t\t\t\t\t<comment>$commentval</comment>\n";
179 }
180 0 $pe .= "\t\t\t\t</reason>\n";
181 }
182 }
183 17 $pe .= "\t\t\t</policy_evaluated>\n";
184 17 return $pe;
185 }
186  
187 9 1;
188  
189 __END__
190  
191 =pod
192  
193 =head1 NAME
194  
195 Mail::DMARC::Report::Aggregate - aggregate report object
196  
197 =head1 VERSION
198  
199 version 2.20260827
200  
201 =head1 DESCRIPTION
202  
203 This class is used as the canonization of an aggregate report.
204  
205 When reports are received, the XML is parsed into an L<Aggregate|Mail::DMARC::Report::Aggregate> object, which then gets passed to the Report::Store and saved. When sending DMARC reports, data is extracted from the L<Store|Mail::DMARC::Report::Store> as an Aggregate object, exported as XML, and sent.
206  
207 =head1 2013 Draft Description
208  
209 AGGREGATE REPORTS
210  
211 The report SHOULD include the following data:
212  
213 o Enough information for the report consumer to re-calculate DMARC
214 disposition based on the published policy, message disposition, and
215 SPF, DKIM, and identifier alignment results. {R12}
216  
217 o Data for each sender subdomain separately from mail from the
218 sender's organizational domain, even if no subdomain policy is
219 applied. {R13}
220  
221 o Sending and receiving domains {R17}
222  
223 o The policy requested by the Domain Owner and the policy actually
224 applied (if different) {R18}
225  
226 o The number of successful authentications {R19}
227  
228 o The counts of messages based on all messages received even if
229 their delivery is ultimately blocked by other filtering agents {R20}
230  
231 Aggregate reports are most useful when they all cover a common time
232 period. By contrast, correlation of these reports from multiple
233 generators when they cover incongruous time periods is difficult or
234 impossible. Report generators SHOULD, wherever possible, adhere to
235 hour boundaries for the reporting period they are using. For
236 example, starting a per-day report at 00:00; starting per-hour
237 reports at 00:00, 01:00, 02:00; et cetera. Report Generators using a
238 24-hour report period are strongly encouraged to begin that period at
239 00:00 UTC, regardless of local timezone or time of report production,
240 in order to facilitate correlation.
241  
242 =head1 Report Structure
243  
244 This is a translation of the XML report format in the 2013 Draft, converted to perl data structures.
245  
246 feedback => {
247 version => 1.0, # decimal
248 report_metadata => { # info about DMARC reporter
249 report_id => string
250 org_name => 'Art Farm',
251 email => 'no-reply@theartfarm.com',
252 extra_contact_info => string # min 0
253 date_range => {
254 begin => epoch time,
255 end => epoch time,
256 },
257 error => string, # min 0, max unbounded
258 },
259 policy_published => {
260 domain => string
261 adkim => r, s
262 aspf => r, s
263 p => none, quarantine, reject
264 sp => none, quarantine, reject
265 pct => integer
266 fo => string
267 },
268 record => [
269 { row => {
270 source_ip => # IPAddress
271 count => # integer
272 policy_evaluated => { # min=1
273 disposition => # none, quarantine, reject
274 dkim => # pass, fail
275 spf => # pass, fail
276 reason => [ # min 0, max unbounded
277 { type => # forwarded sampled_out, trusted_forwarder, mailing_list, local_policy, other
278 comment => # string, min 0
279 },
280 ],
281 }
282 },
283 identifiers => {
284 envelope_to min=0
285 envelope_from min=1
286 header_from min=1
287 },
288 auth_results => {
289 spf => [ # min 1, max unbounded
290 { domain => # min 1
291 scope => # min 1, helo, mfrom
292 result => # min 1, none neutral pass fail softfail temperror permerror
293 }
294 ] # ( unknown -> temperror, error -> permerror )
295 dkim => [ # min 0, max unbounded
296 { domain => , # min 1, the d= parameter in the signature
297 selector => , # min 0, string
298 result => , # none pass fail policy neutral temperror permerror
299 human_result => # min 0, string
300 },
301 ],
302 },
303 ]
304 },
305 };
306  
307 =head1 AUTHORS
308  
309 =over 4
310  
311 =item *
312  
313 Matt Simerson <msimerson@cpan.org>
314  
315 =item *
316  
317 Davide Migliavacca <shari@cpan.org>
318  
319 =item *
320  
321 Marc Bradshaw <marc@marcbradshaw.net>
322  
323 =back
324  
325 =head1 COPYRIGHT AND LICENSE
326  
327 This software is copyright (c) 2026 by Matt Simerson.
328  
329 This is free software; you can redistribute it and/or modify it under
330 the same terms as the Perl 5 programming language system itself.
331  
332 =cut
« blib/lib/Mail/DMARC/Report/Aggregate/Record/Auth_Results.pm ↑ Summary lib/Mail/DMARC/Report/URI.pm »