lib/Mail/DMARC/Report/Receive.pm

filter: Statementstmt 28.5% 78 / 273 Branchbran 11.1% 14 / 126 Conditioncond 14.8% 4 / 27 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 67.7% 21 / 31 Podpod 38.4% 5 / 13 totaltotal 25.2% 123 / 487 CC 88 SCAR 49.2
CC
88
Cov
23%
from_imap
416.4
from_email_simple
240.0
do_node_record
132.0
CRAP 136.8
↑ Summary blib/lib/Mail/DMARC/PurePerl.pm »
1 package Mail::DMARC::Report::Receive;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '2.20260827';
9  
10 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Email::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::Uncompress::Unzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use IO::Uncompress::Gunzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use parent 'Mail::DMARC::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 require Mail::DMARC::Policy;
23 2 require Mail::DMARC::Report;
24 2 require Mail::DMARC::Report::Aggregate::Record;
25  
26 0 ▶sub from_imap($self) {
Subroutine from_imap: not called CC=21 SCAR=60.3
Pod documented
27 0 try {
28 0 load "Net::IMAP::Simple";
29 }
30 catch ($error) {
31 0 croak "Net::IMAP::Simple seems to not work, is it installed? $error";
32 }
33  
34 0 ▶ my $server = $self->config->{imap}{server} or croak "no imap server conf";
Branch: 1 branch
BranchTrueFalseTotal
my $server = $self->config->{"imap"}{"server"} or croak("no imap server conf") 0 0 0
35 0 ▶ my $folder = $self->config->{imap}{folder} or croak "no imap folder conf";
Branch: 1 branch
BranchTrueFalseTotal
my $folder = $self->config->{"imap"}{"folder"} or croak("no imap folder conf") 0 0 0
36 0 my $a_done = $self->config->{imap}{a_done};
37 0 my $f_done = $self->config->{imap}{f_done};
38 0 ▶ my $port = $self->config->{imap}{port} // 993;
Condition0%0/2
$self->config->{"imap"}{"port"} // 993
l!l
00
MC/DC0%0/1
$self->config->{"imap"}{"port"} // 993
$self->config->{"imap"}{"port"}
Truth table0%0/2
A
A= $self->config->{"imap"}{"port"}
Aresult
11
00
39  
40 0 ▶ if ( $port != 143 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($port != 143) 0 0 0
41 0 try {
42 0 require IO::Socket::SSL;
43 }
44 catch ($error) {
45 0 croak "Can't load IO::Socket::SSL: $error\n";
46 }
47  
48 0 ▶ if ( defined $self->config->{imap}{SSL_verify_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config->{"imap"}{"SSL_verify_mode"}) 0 0 0
49 IO::Socket::SSL::set_ctx_defaults(
50 SSL_verifycn_scheme => 'imap',
51 SSL_verify_mode => $self->config->{imap}{SSL_verify_mode},
52 0 );
53 }
54 }
55  
56 2 ▶ no warnings qw(once); ## no critic (Warn)
Subroutine BEGIN: called CC=1 SCAR=0.0
57 my $imap = Net::IMAP::Simple->new(
58 $server,
59 port => $port,
60 use_ssl => $port != 143,
61 )
62 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
my $imap = "Net::IMAP::Simple"->new($server, "port", $port, "use_ssl", $port != 143) or do { my $err = $port == 143 ? $Net::IMAP::Simple::errstr : $Net::IMAP::Simple::SSL::errstr; croak("Unable to connect to IMAP: $err\n") } 0 0 0
63 ## no critic (PackageVar)
64 0 ▶ my $err
Branch: 1 branch
BranchTrueFalseTotal
$port == 143 ? : 0 0 0
65 = $port == 143
66 ? $Net::IMAP::Simple::errstr
67 : $Net::IMAP::Simple::SSL::errstr;
68 0 croak "Unable to connect to IMAP: $err\n";
69 };
70  
71 0 ▶ print "connected to IMAP server $server:$port\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
72  
73 $imap->login( $self->config->{imap}{user}, $self->config->{imap}{pass} )
74 0 ▶ or croak "Login failed: " . $imap->errstr . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap->login($self->config->{"imap"}{"user"}, $self->config->{"imap"}{"pass"}) or croak('Login failed: ' . $imap->errstr . "\n") 0 0 0
75  
76 0 ▶ print "\tlogged in\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
77  
78 0 my $nm = $imap->select( $self->config->{imap}{folder} );
79 0 $imap->expunge_mailbox( $self->config->{imap}{folder} );
80 0 my @mess = $imap->search( 'UNSEEN', 'DATE' );
81 0 ▶ if ( !@mess ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@mess) 0 0 0
82  
83 # imap server might not support SORT extension *Gmail*
84 0 @mess = $imap->search('UNSEEN');
85 }
86  
87 0 ▶ print "\tfound " . scalar @mess . " messages\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
88  
89 0 foreach my $i (@mess) {
90 0 ▶ print $imap->seen($i) ? '*' : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$imap->seen($i) ? : 0 0 0
91 0 printf "[%03d] ", $i;
92 0 ▶ my $message = $imap->get($i) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $message = $imap->get($i) or do { carp("unable to get message $i\n"); next } 0 0 0
93 0 carp "unable to get message $i\n";
94 0 next;
95 };
96 0 my $type = $self->from_email_simple( Email::Simple->new("$message") );
97 0 ▶ next if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
98 0 ▶ my $done_box
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'aggregate' ? : 0 0 0
$type eq 'forensic' ? : 0 0 0
99 = $type eq 'aggregate' ? $a_done
100 : $type eq 'forensic' ? $f_done
101 : croak "unknown type!";
102  
103 0 $imap->add_flags( $i, '\Seen' );
104 0 ▶ if ($done_box) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_box) 0 0 0
105 0 ▶ $imap->copy( $i, $done_box ) or do {
Branch: 1 branch
BranchTrueFalseTotal
$imap->copy($i, $done_box) or do { carp($imap->errstr); next } 0 0 0
106 0 carp $imap->errstr;
107 0 next;
108 };
109 0 $imap->add_flags( $i, '\Deleted' );
110 }
111 }
112  
113 0 $imap->quit;
114 0 return 1;
115 }
116  
117 2 ▶sub from_file( $self, $file = undef ) {
Subroutine from_file: called CC=12 SCAR=45.8
Pod documented
118 2 ▶ croak "missing message" if !$file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 1 2
119 1 ▶ croak "No such file $file: $!" if !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 1 0 1
120  
121 0 my $contents = $self->slurp($file);
122  
123 # Detect gzip by magic bytes \x1f\x8b
124 0 ▶ if ( substr( $contents, 0, 2 ) eq "\x1f\x8b" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($contents, 0, 2) eq "\37\213") 0 0 0
125 0 my $xml;
126 0 ▶ IO::Uncompress::Gunzip::gunzip( \$contents, \$xml )
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip(\$contents, \$xml) or croak("gunzip failed: $IO::Uncompress::Gunzip::GunzipError") 0 0 0
127 or croak "gunzip failed: $IO::Uncompress::Gunzip::GunzipError";
128 0 $self->_init_for_file($file);
129 0 ▶ return $self->handle_body($xml) ? 'aggregate' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->handle_body($xml) ? : 0 0 0
130 }
131  
132 # Detect zip by magic bytes PK\x03\x04
133 0 ▶ if ( substr( $contents, 0, 4 ) eq "PK\x03\x04" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($contents, 0, 4) eq "PK\3\4") 0 0 0
134 0 my $xml;
135 0 ▶ IO::Uncompress::Unzip::unzip( \$contents, \$xml )
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Unzip::unzip(\$contents, \$xml) or croak("unzip failed: $IO::Uncompress::Unzip::UnzipError") 0 0 0
136 or croak "unzip failed: $IO::Uncompress::Unzip::UnzipError";
137 0 $self->_init_for_file($file);
138 0 ▶ return $self->handle_body($xml) ? 'aggregate' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->handle_body($xml) ? : 0 0 0
139 }
140  
141 # Detect XML by content (starts with optional BOM/whitespace then '<')
142 0 ▶ if ( $contents =~ /\A(?:\xef\xbb\xbf)?\s*</ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /\A(?:\xef\xbb\xbf)?\s*</) 0 0 0
143 0 $self->_init_for_file($file);
144 0 ▶ return $self->handle_body($contents) ? 'aggregate' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->handle_body($contents) ? : 0 0 0
145 }
146  
147 0 return $self->from_email_simple( Email::Simple->new($contents) );
148 }
149  
150 0 ▶sub _init_for_file( $self, $file ) {
Subroutine _init_for_file: not called CC=1 SCAR=6.9
151 0 $self->report->init();
152 0 $self->{_envelope_to} = undef;
153 0 $self->{_header_from} = undef;
154 0 $self->get_submitter_from_filename( File::Basename::basename($file) );
155 0 return;
156 }
157  
158 0 ▶sub from_mbox( $self, $file_name ) {
Subroutine from_mbox: not called CC=4 SCAR=30.0
Pod documented
159 0 ▶ croak "missing mbox file" if !$file_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $file_name 0 0 0
160  
161 # TODO: replace this module
162 # commented out due to build test failures
163 # load "Mail::Mbox::MessageParser";
164 # croak "is Mail::Mbox::MessageParser installed?" if $@;
165  
166 # my $file_handle = FileHandle->new($file_name);
167  
168 0 my $folder_reader; # = Mail::Mbox::MessageParser->new(
169 # { 'file_name' => $file_name,
170 # 'file_handle' => $file_handle,
171 # 'enable_cache' => 1,
172 # 'enable_grep' => 1,
173 # }
174 # );
175  
176 0 ▶ croak $folder_reader unless ref $folder_reader;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $folder_reader 0 0 0
177  
178 0 my $prologue = $folder_reader->prologue;
179 0 print $prologue;
180  
181 0 ▶ while ( !$folder_reader->end_of_file() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($folder_reader->end_of_file) 0 0 0
182 0 $self->from_email_simple(
183 Email::Simple->new( $folder_reader->read_next_email() ) );
184 }
185 0 return 1;
186 }
187  
188 0 ▶sub from_email_simple( $self, $email ) {
Subroutine from_email_simple: not called CC=15 SCAR=54.8
Pod documented
189  
190 0 $self->report->init();
191 0 $self->{_envelope_to} = undef;
192 0 $self->{_header_from} = undef;
193 0 $self->get_submitter_from_subject( $email->header('Subject') );
194  
195 0 my $unzipper = {
196 gz => \&IO::Uncompress::Gunzip::gunzip, # 2013 draft
197 zip => \&IO::Uncompress::Unzip::unzip, # legacy format
198 };
199  
200 0 my $rep_type;
201 0 foreach my $part ( Email::MIME->new( $email->as_string )->parts ) {
202 0 ▶ my ( $c_type, @ignore ) = split ';', ( $part->content_type || '' );
Condition0%0/2
$part->content_type || ''
l!l
00
MC/DC0%0/1
$part->content_type || ''
$part->content_type
Truth table0%0/2
A
A= $part->content_type
Aresult
11
00
203 0 ▶ next if $c_type =~ m{^text/(plain|html)$};
Branch: 1 branch
BranchTrueFalseTotal
if $c_type =~ m[^text/(plain|html)$] 0 0 0
204 0 ▶ if ( $c_type eq 'text/rfc822-headers' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_type eq "text/rfc822-headers") 0 0 0
205 0 warn "TODO: handle forensic reports\n"; ## no critic (Carp)
206 0 $rep_type = 'forensic';
207 0 next;
208 }
209 0 ▶ if ( $c_type eq 'message/feedback-report' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_type eq "message/feedback-report") 0 0 0
210 0 warn "TODO: handle forensic reports\n"; ## no critic (Carp)
211 0 $rep_type = 'forensic';
212 0 next;
213 }
214  
215 0 my $bigger;
216 0 ▶ my $filename = $part->{ct}{attributes}{name} || '';
Condition0%0/2
$part->{'ct'}{'attributes'}{'name'} || ''
l!l
00
MC/DC0%0/1
$part->{'ct'}{'attributes'}{'name'} || ''
$part->{'ct'}{'attributes'}{'name'}
Truth table0%0/2
A
A= $part->{'ct'}{'attributes'}{'name'}
Aresult
11
00
217  
218 0 ▶ if ( $c_type eq 'application/zip'
Branch: 1 branch
BranchTrueFalseTotal
if ($c_type eq "application/zip" or $c_type eq "application/x-zip-compressed") 0 0 0
Condition0%0/3
$c_type eq "application/zip" or $c_type eq "application/x-zip-compressed"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c_type eq "application/zip" or $c_type eq "application/x-zip-compressed"
$c_type eq "application/zip" $c_type eq "application/x-zip-compressed"
Truth table0%0/3
A or B
A= $c_type eq "application/zip"B= $c_type eq "application/x-zip-compressed"
ABresult
1X1
011
000
219 || $c_type eq 'application/x-zip-compressed' )
220 {
221 0 try {
222 0 $self->get_submitter_from_filename($filename);
223 0 $unzipper->{zip}->( \$part->body, \$bigger );
224 0 $self->handle_body($bigger);
225 0 $rep_type = 'aggregate';
226 }
227 catch ($error) {
228 0 warn "failure processing $filename: $error";
229 }
230 0 next;
231 }
232 0 ▶ if ( $c_type eq 'application/gzip' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_type eq "application/gzip") 0 0 0
233 0 try {
234 0 $self->get_submitter_from_filename($filename);
235 0 $unzipper->{gz}->( \$part->body, \$bigger );
236 0 $self->handle_body($bigger);
237 0 $rep_type = 'aggregate';
238 }
239 catch ($error) {
240 0 warn "failure processing $filename: $error";
241 }
242 0 next;
243 }
244 0 ▶ if ( $filename =~ /xml\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /xml\.gz$/) 0 0 0
245 0 ▶ if ( $c_type eq 'application/octet-stream'
Branch: 1 branch
BranchTrueFalseTotal
if ($c_type eq "application/octet-stream" or $c_type eq "multipart/alternative") 0 0 0
Condition0%0/3
$c_type eq "application/octet-stream" or $c_type eq "multipart/alternative"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c_type eq "application/octet-stream" or $c_type eq "multipart/alternative"
$c_type eq "application/octet-stream" $c_type eq "multipart/alternative"
Truth table0%0/3
A or B
A= $c_type eq "application/octet-stream"B= $c_type eq "multipart/alternative"
ABresult
1X1
011
000
246 || $c_type eq 'multipart/alternative' )
247 {
248 0 try {
249 0 $self->get_submitter_from_filename($filename);
250 0 $unzipper->{gz}->( \$part->body, \$bigger );
251 0 $self->handle_body($bigger);
252 0 $rep_type = 'aggregate';
253 }
254 catch ($error) {
255 0 warn "failure processing $filename: $error";
256 }
257 0 next;
258 }
259 }
260  
261 0 ▶ if ( $c_type eq 'multipart/alternative' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c_type eq 'multipart/alternative') { } 0 0 0
elsif ($c_type eq 'multipart/related') { } 0 0 0
262 }
263 elsif ( $c_type eq 'multipart/related' ) {
264 }
265 else {
266 0 warn "Unknown message part $c_type\n"; ## no critic (Carp)
267 }
268 }
269 0 return $rep_type;
270 }
271  
272 2 ▶sub get_submitter_from_filename( $self, $filename ) {
Subroutine get_submitter_from_filename: called CC=3 SCAR=11.3
Pod undocumented
273 2 ▶ return if $self->{_envelope_to}; # already parsed from Subject:
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_envelope_to"} 1 1 2
274 1 my ( $submitter_dom, $report_dom, $begin, $end ) = split /!/, $filename;
275 1 ▶ $self->{_header_from} ||= $report_dom;
Condition33%1/3
$self->{"_header_from"} ||= $report_dom
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_header_from"} ||= $report_dom
$self->{"_header_from"} $report_dom
Truth table33%1/3
A ||= B
A= $self->{"_header_from"}B= $report_dom
ABresult
1X1
011
000
276 1 return $self->{_envelope_to} = $submitter_dom;
277 }
278  
279 6 ▶sub get_submitter_from_subject( $self, $subject ) {
Subroutine get_submitter_from_subject: called CC=7 SCAR=19.7
Pod undocumented
280  
281 # The 2013 DMARC spec section 12.2.1 suggests that the header SHOULD conform
282 # to a supplied ABNF. Rather than "require" such conformance, this method is
283 # more concerned with reliably extracting the submitter domain. Quickly.
284 6 $subject = lc Encode::decode( 'MIME-Header', $subject );
285 6 print $subject . "\n";
286 6 ▶ $subject = substr( $subject, 8 ) if 'subject:' eq substr( $subject, 0, 8 );
Branch: 1 branch
BranchTrueFalseTotal
if "subject:" eq substr($subject, 0, 8) 6 0 6
287 6 $subject =~ s/(?:report\sdomain|submitter|report-id)//gx; # strip keywords
288 6 $subject =~ s/\s+//g; # remove white space
289 6 my ( undef, $report_dom, $sub_dom, $report_id ) = split /:/, $subject;
290 6 my $meta = $self->report->aggregate->metadata;
291  
292 6 ▶ if ( $report_id && !$meta->uuid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($report_id and not $meta->uuid) 2 4 6
Condition66%2/3
$report_id and not $meta->uuid
!ll&&!rl&&r
042
MC/DC50%1/2
$report_id and not $meta->uuid
$report_id not $meta->uuid
Truth table66%2/3
A and not(B)
A= $report_idB= not $meta->uuid
ABresult
0X0
100
111
293  
294 # remove <containment brackets> if present
295 2 ▶ $report_id = substr( $report_id, 1 ) if '<' eq substr( $report_id, 0, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if "<" eq substr($report_id, 0, 1) 1 1 2
296 2 ▶ chop $report_id if '>' eq substr( $report_id, -1, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ">" eq substr($report_id, -1, 1) 1 1 2
297 2 $meta->uuid($report_id);
298 }
299 6 ▶ $self->{_header_from} ||= $report_dom;
Condition33%1/3
$self->{"_header_from"} ||= $report_dom
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"_header_from"} ||= $report_dom
$self->{"_header_from"} $report_dom
Truth table33%1/3
A ||= B
A= $self->{"_header_from"}B= $report_dom
ABresult
1X1
011
000
300 6 return $self->{_envelope_to} = $sub_dom;
301 }
302  
303 0 ▶sub handle_body( $self, $body ) {
Subroutine handle_body: not called CC=5 SCAR=34.0
Pod documented
304  
305 0 ▶ print "handling decompressed body\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
306 0 ▶ if ( $body =~ /xmlns=/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /xmlns=/) 0 0 0
307  
308 # RFC 9990 defines xmlns="urn:ietf:params:xml:ns:dmarc-2.0" as the
309 # official namespace. Strip it so un-namespaced XPath queries
310 # (/feedback/...) continue to work with XML::LibXML.
311 0 ▶ print "NOTICE: removing xmlns from XML document\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
312 0 $body =~ s/\s+xmlns="[^"]*"//g;
313 }
314  
315 0 my $dom = XML::LibXML->load_xml( string => $body );
316 0 $self->do_node_report_metadata( $dom->findnodes("/feedback/report_metadata") );
317 0 $self->do_node_policy_published(
318 $dom->findnodes("/feedback/policy_published") );
319  
320 0 foreach my $record ( $dom->findnodes("/feedback/record") ) {
321 0 $self->do_node_record($record);
322 }
323  
324 0 return $self->report->save_aggregate();
325 }
326  
327 8 ▶sub report($self) {
Subroutine report: called CC=2 SCAR=6.9
Pod undocumented
328 8 ▶ return $self->{report} if ref $self->{report};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"report"} 7 1 8
329 1 return $self->{report} = Mail::DMARC::Report->new();
330 }
331  
332 0 ▶sub do_node_report_metadata( $self, $node ) {
Subroutine do_node_report_metadata: not called CC=7 SCAR=40.3
Pod undocumented
333  
334 0 foreach my $n (qw/ org_name email extra_contact_info /) {
335 0 $self->report->aggregate->metadata->$n(
336 $node->findnodes("./$n")->string_value );
337 }
338  
339 0 my $rid = $node->findnodes("./report_id")->string_value;
340 0 ▶ $rid = substr( $rid, 1 ) if '<' eq substr( $rid, 0, 1 ); # remove <
Branch: 1 branch
BranchTrueFalseTotal
if "<" eq substr($rid, 0, 1) 0 0 0
341 0 ▶ chop $rid if '>' eq substr( $rid, -1, 1 ); # remove >
Branch: 1 branch
BranchTrueFalseTotal
if ">" eq substr($rid, -1, 1) 0 0 0
342 0 $self->report->aggregate->metadata->report_id($rid);
343  
344 0 ▶ if ( !$self->report->aggregate->metadata->uuid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->report->aggregate->metadata->uuid) 0 0 0
345 0 $self->report->aggregate->metadata->uuid($rid);
346 }
347  
348 0 foreach my $n (qw/ begin end /) {
349 0 $self->report->aggregate->metadata->$n(
350 $node->findnodes("./date_range/$n")->string_value );
351 }
352  
353 0 foreach my $n ( $node->findnodes("./error") ) {
354 0 $self->report->aggregate->metadata->error( $n->string_value );
355 }
356 0 return $self->report->aggregate->metadata;
357 }
358  
359 0 ▶sub do_node_policy_published( $self, $node ) {
Subroutine do_node_policy_published: not called CC=3 SCAR=24.8
Pod undocumented
360  
361 0 my $pol = Mail::DMARC::Policy->new();
362  
363 0 foreach my $n (qw/ domain adkim aspf p sp np t psd fo discovery_method /) {
364 0 ▶ my $val = $node->findnodes("./$n")->string_value or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $node->findnodes("./$n")->string_value or next 0 0 0
365 0 $val =~ s/\s*//g; # remove whitespace
366 0 try { $pol->$n($val) } catch ($e) { }; # ignore unknown/invalid tag values
367 }
368  
369 0 $self->report->aggregate->policy_published($pol);
370 0 return $pol;
371 }
372  
373 0 ▶sub do_node_record( $self, $node ) {
Subroutine do_node_record: not called CC=11 SCAR=48.8
Pod undocumented
374  
375 0 my $rec = Mail::DMARC::Report::Aggregate::Record->new;
376 0 $self->do_node_record_auth( \$rec, $node );
377  
378 0 foreach my $r (qw/ source_ip count /) {
379 0 $rec->row->$r( $node->findnodes("./row/$r")->string_value );
380 }
381  
382 # policy_evaluated
383 0 foreach my $pe (qw/ disposition dkim spf /) {
384 0 my $ResultType
385 = $node->findnodes("./row/policy_evaluated/$pe")->string_value;
386 0 ▶ if ( $pe eq 'disposition' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pe eq 'disposition') { } 0 0 0
387 0 ▶ if ( $ResultType !~ /^(none|quarantine|reject)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ResultType =~ /^(none|quarantine|reject)$/) 0 0 0
388 0 $ResultType = 'none'; # invalid DispositionType (Facebook)
389 }
390 }
391 else {
392 0 ▶ if ( $ResultType !~ /^(pass|fail)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ResultType =~ /^(pass|fail)$/) 0 0 0
393 0 $ResultType = 'pass'; # invalid ResultType (also FaceBook)
394 }
395 }
396 0 $rec->row->policy_evaluated->$pe($ResultType);
397 }
398  
399 # reason
400 0 $self->do_node_record_reason( \$rec, $node );
401  
402 # identifiers
403 0 foreach my $i (qw/ envelope_to envelope_from header_from /) {
404 0 $rec->identifiers->$i( $node->findnodes("./identifiers/$i")->string_value );
405 }
406  
407 # for reports from junc.org with mis-labeled identifiers
408 0 ▶ if ( !$rec->identifiers->header_from ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec->identifiers->header_from) 0 0 0
409 0 $rec->identifiers->header_from(
410 $node->findnodes("./identities/header_from")->string_value );
411 }
412  
413 # last resort...
414 0 ▶ if ( !$rec->identifiers->envelope_to ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec->identifiers->envelope_to) 0 0 0
415 0 $rec->identifiers->envelope_to( $self->{_envelope_to} );
416 }
417 0 ▶ if ( !$rec->identifiers->header_from ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rec->identifiers->header_from) 0 0 0
418 0 $rec->identifiers->header_from( $self->{_header_from} );
419 }
420  
421 0 ▶ print Data::Dumper::Dumper($rec) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
422 0 $self->report->aggregate->record($rec);
423 0 return $rec;
424 }
425  
426 0 ▶sub do_node_record_auth( $self, $row, $node ) {
Subroutine do_node_record_auth: not called CC=7 SCAR=40.3
Pod undocumented
427  
428 0 my @spf = qw/ domain scope result /;
429  
430 0 foreach ( $node->findnodes("./auth_results/spf") ) {
431 my %spf
432 0 = map { $_ => $node->findnodes("./auth_results/spf/$_")->string_value }
433 @spf;
434  
435 0 ▶ if ( $spf{scope} && !$self->is_valid_spf_scope( $spf{scope} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf{"scope"} and not $self->is_valid_spf_scope($spf{"scope"})) 0 0 0
Condition0%0/3
$spf{"scope"} and not $self->is_valid_spf_scope($spf{"scope"})
!ll&&!rl&&r
000
MC/DC0%0/2
$spf{"scope"} and not $self->is_valid_spf_scope($spf{"scope"})
$spf{"scope"} not $self->is_valid_spf_scope($spf{"scope"})
Truth table0%0/3
A and not(B)
A= $spf{"scope"}B= not $self->is_valid_spf_scope($spf{"scope"})
ABresult
0X0
100
111
436 0 carp "invalid scope: $spf{scope}, ignoring";
437 0 delete $spf{scope};
438 }
439  
440 # this is for reports from ivenue.com with result=unknown
441 0 ▶ if ( $spf{result} && !$self->is_valid_spf_result( $spf{result} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spf{"result"} and not $self->is_valid_spf_result($spf{"result"})) 0 0 0
Condition0%0/3
$spf{"result"} and not $self->is_valid_spf_result($spf{"result"})
!ll&&!rl&&r
000
MC/DC0%0/2
$spf{"result"} and not $self->is_valid_spf_result($spf{"result"})
$spf{"result"} not $self->is_valid_spf_result($spf{"result"})
Truth table0%0/3
A and not(B)
A= $spf{"result"}B= not $self->is_valid_spf_result($spf{"result"})
ABresult
0X0
100
111
442 0 carp "invalid SPF result: $spf{result}, setting to temperror";
443 0 $spf{result} = 'temperror';
444 }
445 0 $$row->auth_results->spf( \%spf );
446 }
447  
448 0 my @dkim = qw/ domain selector result human_result /;
449 0 foreach ( $node->findnodes("./auth_results/dkim") ) {
450 my %dkim
451 0 = map { $_ => $node->findnodes("./auth_results/dkim/$_")->string_value }
452 @dkim;
453 0 $$row->auth_results->dkim( \%dkim );
454 }
455  
456 0 return;
457 }
458  
459 0 ▶sub do_node_record_reason( $self, $row, $node ) {
Subroutine do_node_record_reason: not called CC=3 SCAR=24.8
Pod undocumented
460  
461 # my @types = qw/ forwarded sampled_out trusted_forwarder mailing_list
462 # local_policy other /;
463  
464 0 foreach my $r ( $node->findnodes("./row/policy_evaluated/reason") ) {
465 0 ▶ my $type = $r->findnodes('./type')->string_value or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $r->findnodes("./type")->string_value or next 0 0 0
466 0 my $comment = $r->findnodes('./comment')->string_value;
467 0 $$row->row->policy_evaluated->reason(
468 { type => $type, comment => $comment } );
469 }
470 0 return;
471 }
472  
473 2 1;
474  
475 __END__
476  
477 =pod
478  
479 =head1 NAME
480  
481 Mail::DMARC::Report::Receive - process incoming DMARC reports
482  
483 =head1 VERSION
484  
485 version 2.20260827
486  
487 =head1 DESCRIPTION
488  
489 Receive DMARC reports and save them to the report store/database.
490  
491 =head1 METHODS
492  
493 =head2 from_imap, from_file, from_mbox
494  
495 These methods are called by L<dmarc_receive> program, which has its own documentation and usage instructions.
496  
497 C<from_file> accepts a file path and auto-detects the file type:
498  
499 =over 4
500  
501 =item * B<gzip> (magic bytes C<\x1f\x8b>) - decompresses with L<IO::Uncompress::Gunzip> and parses as XML
502  
503 =item * B<zip> (magic bytes C<PK\x03\x04>) - decompresses with L<IO::Uncompress::Unzip> and parses as XML
504  
505 =item * B<XML> (content starting with C<E<lt>>) - parses directly
506  
507 =item * B<email message> - falls back to passing the file to from_email_simple
508  
509 =back
510  
511 C<from_imap> and C<from_mbox> accept a message (or list of messages) and create an Email::Simple object from each, passing that object to from_email_simple.
512  
513 =head2 from_email_simple
514  
515 Accepts an Email::Simple message object. Returns the type of DMARC report detected or undef if no DMARC report was detected.
516  
517 When forensic reports are detected, no further processing is done.
518  
519 When an aggregate report is detected, the report details are extracted from the message body as well as the Subject field/header and attachment metadata.
520  
521 Parsing of the Subject and MIME metadata is necessary because the 2013 draft DMARC specification does not REQUIRE the envelope_to domain name to be included in the XML report. For example, the only way to B<know> that the email which generated this particular report was sent to hotmail.com is to extract the envelope_to domain from the message metadata (Org Name=Microsoft, hotmail.com is not in the XML). So far, every messsage I have seen has had the envelope_to domain in one location or the other.
522  
523 To extract messages from the message body, the MIME attachments are decompressed and passed to L<handle_body>.
524  
525 =head2 handle_body
526  
527 Accepts a XML message, parsing it with XML::LibXML and XPath expressions. The parsed data is stored in a L<Mail::DMARC::Report> object. When the parsing is complete, the report object is saved to the report store.
528  
529 =head1 AUTHORS
530  
531 =over 4
532  
533 =item *
534  
535 Matt Simerson <msimerson@cpan.org>
536  
537 =item *
538  
539 Davide Migliavacca <shari@cpan.org>
540  
541 =item *
542  
543 Marc Bradshaw <marc@marcbradshaw.net>
544  
545 =back
546  
547 =head1 COPYRIGHT AND LICENSE
548  
549 This software is copyright (c) 2026 by Matt Simerson.
550  
551 This is free software; you can redistribute it and/or modify it under
552 the same terms as the Perl 5 programming language system itself.
553  
554 =cut
↑ Summary blib/lib/Mail/DMARC/PurePerl.pm »