blib/lib/Mail/DMARC/HTTP.pm untested

filter: Statementstmt 0.0% 0 / 202 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 412 CC 41 SCAR 34.7
CC
41
Cov
0%
serve_file
90.0
dmarc_dispatch
72.0
serve_validator
42.0
CRAP 32.1
1 package Mail::DMARC::HTTP;
2 our $VERSION = '2.20260827';
3 use strict;
4 use warnings;
5 use feature 'signatures';
6 use feature 'try';
7 no warnings 'experimental::try'; ## no critic (ProhibitNoWarnings)
8  
9 use parent 'Net::Server::HTTP';
10  
11 use File::ShareDir;
12 use IO::Uncompress::Gunzip;
13 use JSON -convert_blessed_universally;
14 use URI;
15  
16 our $report;
17 use Mail::DMARC::PurePerl;
18  
19 my %mimes = (
20 css => 'text/css',
21 html => 'text/html',
22 js => 'application/javascript',
23 json => 'application/json',
24 );
25  
26 sub new($class) {
27 return bless {}, $class;
28 }
29  
30 sub dmarc_httpd( $self, $http_report ) {
31 $report = $http_report;
32  
33 my $port = $report->config->{http}{port} || 8080;
34 my $ports = $report->config->{https}{port};
35 my $sslkey = $report->config->{https}{ssl_key};
36 my $sslcrt = $report->config->{https}{ssl_crt};
37  
38 warn_unusable_post_max();
39  
40 Net::Server::HTTP->run(
41 app => sub {&dmarc_dispatch},
42 port => [ $port, ( ( $ports && $sslkey && $sslcrt ) ? "$ports/ssl" : () ) ],
43 ipv => '*', # IPv6 if available
44 ( $sslkey ? ( SSL_key_file => $sslkey ) : () ),
45 ( $sslcrt ? ( SSL_cert_file => $sslcrt ) : () ),
46 log_file => 'Sys::Syslog',
47 syslog_ident => 'mail_dmarc',
48 syslog_facility => 'MAIL',
49 );
50 return;
51 }
52  
53 sub dmarc_dispatch($self) {
54 my $path = $self->{request_info}{request_path};
55 if ($path) {
56 warn "path: $path\n";
57  
58 # Parse QUERY_STRING once here via URI (not CGI->new->Vars) to avoid
59 # CGI.pm state issues in persistent Net::Server::HTTP processes.
60 my %vars
61 = URI->new( 'http://x/?' . ( $ENV{QUERY_STRING} // '' ) )->query_form;
62 return report_json_report( \%vars ) if $path eq '/dmarc/json/report';
63 return report_json_rr( \%vars ) if $path eq '/dmarc/json/row';
64 return report_json_domains( \%vars ) if $path eq '/dmarc/json/domains';
65 return serve_validator() if $path eq '/dmarc/json/validate';
66  
67 my %agg = (
68 '/dmarc/json/summary' => 'get_summary',
69 '/dmarc/json/timeseries' => 'get_timeseries',
70 '/dmarc/json/sources' => 'get_sources',
71 '/dmarc/json/source' => 'get_source_detail',
72 );
73 return report_json_agg( $agg{$path}, \%vars ) if $agg{$path};
74 return serve_file($path) if $path =~ /\.(?:js|css|html|gz)$/x;
75 }
76  
77 return serve_file('/dmarc/index.html');
78 }
79  
80 sub serve_pretty_error( $error = undef ) {
81 $error ||= 'Sorry, that operation is not supported.';
82 return print <<"EO_ERROR"
83 Content-Type: text/html
84  
85 <p>$error</p>
86  
87 EO_ERROR
88 ;
89 }
90  
91 sub return_json_error($err) {
92  
93 #warn $err;
94 print JSON->new->utf8->encode( { err => $err } ); # to HTTP client
95 print "\n";
96 return $err; # to caller
97 }
98  
99 my $POST_MAX_DEFAULT = 10 * 1024 * 1024;
100 my $BYTE_COUNT = qr/^[1-9][0-9]*$/;
101  
102 # 0 is not a way to lift the cap; say so rather than quietly using the default
103 sub warn_unusable_post_max() {
104 my $max = $report->config->{http}{post_max};
105 return if !defined $max || $max eq '' || $max =~ $BYTE_COUNT;
106 warn "post_max '$max' is not a byte count; using " . post_max() . "\n";
107 return;
108 }
109  
110 sub post_max() {
111 my $max = $report ? $report->config->{http}{post_max} : undef;
112 return defined $max && $max =~ $BYTE_COUNT ? $max : $POST_MAX_DEFAULT;
113 }
114  
115 # The client is still sending when we refuse an oversized body. Closing with
116 # its bytes unread makes the kernel answer RST, which discards the error we
117 # just queued, so read them off the wire and drop them. Only up to post_max
118 # again: past that the caller is not worth the bandwidth and gets the reset.
119 sub discard_post_body($len) {
120 my $max = post_max();
121 return if $len > 2 * $max;
122  
123 my $seen = 0;
124 while ( $seen < $len ) {
125 my $want = $len - $seen;
126 my $got = read STDIN, my $chunk, $want < 65536 ? $want : 65536;
127 last if !$got;
128 $seen += $got;
129 }
130 return;
131 }
132  
133 # '' when there is no body, undef when it is larger than post_max
134 sub read_post_body() {
135 my ($len) = ( $ENV{CONTENT_LENGTH} // '' ) =~ /^([0-9]+)$/ or return '';
136 if ( $len > post_max() ) {
137 discard_post_body($len);
138 return;
139 }
140  
141 # a short read would truncate the JSON and leave the rest of the body to be
142 # parsed as the next request on a keep-alive connection
143 my $body = '';
144 while ( length $body < $len ) {
145 my $got = read STDIN, my $chunk, $len - length $body;
146 last if !$got;
147 $body .= $chunk;
148 }
149 return $body;
150 }
151  
152 sub serve_validator( $post = undef, $resolver = undef ) {
153 my $too_large;
154 if ( !defined $post ) { # passed in for testing
155 $post = read_post_body();
156 $too_large = !defined $post;
157 }
158 my $json = JSON->new->utf8;
159  
160 print "Content-Type: application/json\n\n";
161  
162 if ($too_large) {
163 return return_json_error(
164 'POST data larger than post_max of ' . post_max() . ' bytes' );
165 }
166 if ( !$post ) { return return_json_error("missing POST data"); }
167  
168 my ( $input, $dmpp, $res );
169 try {
170 $input = $json->decode($post);
171 }
172 catch ($error) {
173 return return_json_error($error);
174 }
175  
176 if ( !$input || !ref $input ) {
177 return return_json_error("invalid request $post");
178 }
179  
180 try {
181 $dmpp = Mail::DMARC::PurePerl->new(%$input);
182 }
183 catch ($error) {
184 return return_json_error($error);
185 }
186  
187 $dmpp->set_resolver($resolver) if $resolver;
188  
189 try {
190 $res = $dmpp->validate();
191 }
192 catch ($error) {
193 return return_json_error($error);
194 }
195  
196 my $return = $json->allow_blessed->convert_blessed->encode($res);
197 print "$return\n";
198 return $return;
199 }
200  
201 # The viewer's assets are a known set of plain names, so each path segment is
202 # matched against what is allowed rather than checked for what is not. A
203 # blacklist of traversal sequences has to anticipate every encoding that
204 # survives the request parser; requiring a leading alphanumeric rejects '.',
205 # '..' and dotfiles outright, and leaves no way to name a directory above the
206 # share directory.
207 my $SAFE_SEGMENT = qr/\A[A-Za-z0-9][A-Za-z0-9._-]*\z/x;
208  
209 sub serve_file($path) {
210 my @bits = split /\//, $path;
211 shift @bits;
212 return serve_pretty_error("file not found")
213 if ( !$bits[0] || 'dmarc' ne $bits[0] );
214 shift @bits;
215  
216 return serve_pretty_error("no such file") if !@bits;
217 foreach my $bit (@bits) {
218 return serve_pretty_error("no such file") if $bit !~ $SAFE_SEGMENT;
219 }
220  
221 $path = join '/', @bits;
222 my $file = $bits[-1];
223  
224 my ($extension) = ( split /\./, $file )[-1];
225 return serve_pretty_error("$extension not recognized") if !$mimes{$extension};
226  
227 my $dir = "share/html"; # distribution dir
228 if ( !-d $dir ) {
229 $dir = File::ShareDir::dist_dir('Mail-DMARC'); # installed loc.
230 $dir .= "/html";
231 }
232 return serve_pretty_error("no such path") if !$dir;
233 return serve_gzip("$dir/$path.gz") if -f "$dir/$path.gz";
234 return serve_pretty_error("no such file") if !-f "$dir/$path";
235  
236 open my $FH, '<', "$dir/$path"
237 or return serve_pretty_error("unable to read $dir/$path: $!");
238 print "Content-Type: $mimes{$extension}\n\n";
239 print <$FH>;
240 close $FH;
241 return 1;
242 }
243  
244 sub serve_gzip($file) {
245 open my $FH, '<', "$file"
246 or return serve_pretty_error("unable to read $file: $!");
247 my $contents = do { local $/; <$FH> }; ## no critic (Local)
248 close $FH;
249  
250 my $decomp = substr( $file, 0, -3 ); # remove .gz suffix
251 my ($extension) = ( split /\./, $decomp )[-1];
252  
253 # browser accepts gz encoding, serve compressed
254 if ( grep {/gzip/} $ENV{HTTP_ACCEPT_ENCODING} ) {
255 my $length = length $contents;
256 return print <<"EO_GZ"
257 Content-Length: $length
258 Content-Type: $mimes{$extension}
259 Content-Encoding: gzip
260  
261 $contents
262 EO_GZ
263 ;
264 }
265  
266 # browser doesn't support gzip, decompress and serve
267 my $out;
268 IO::Uncompress::Gunzip::gunzip( \$contents => \$out )
269 or return serve_pretty_error("unable to decompress");
270 my $length = length $out;
271  
272 return print <<"EO_UNGZ"
273 Content-Length: $length
274 Content-Type: $mimes{$extension}
275  
276 $out
277 EO_UNGZ
278 ;
279 }
280  
281 my @AGG_INT_PARAMS = qw/ since until start length /;
282 my @AGG_STR_PARAMS = qw/ from_domain author source_ip sort_col /;
283  
284 # Which reports to count: those others sent us about our domains, those this
285 # host is preparing to send about other people's, or both.
286 my %AGG_REPORT_KINDS = map { $_ => 1 } qw/ received outgoing all /;
287  
288 my $AGG_DEFAULT_DAYS = 30;
289  
290 sub agg_params($vars) {
291 my %args;
292  
293 foreach my $param (@AGG_INT_PARAMS) {
294 next if !defined $vars->{$param};
295 next if $vars->{$param} !~ /\A-?[0-9]{1,12}\z/x;
296 $args{$param} = 0 + $vars->{$param};
297 }
298  
299 foreach my $param (@AGG_STR_PARAMS) {
300 next if !defined $vars->{$param} || '' eq $vars->{$param};
301 next if length $vars->{$param} > 253;
302 $args{$param} = $vars->{$param};
303 }
304  
305 $args{reports} = $vars->{reports}
306 if defined $vars->{reports} && $AGG_REPORT_KINDS{ $vars->{reports} };
307  
308 # Without a default window an unparameterized request would aggregate the
309 # entire report history.
310 $args{until} //= time;
311 $args{since} //= $args{until} - ( $AGG_DEFAULT_DAYS * 86400 );
312  
313 return %args;
314 }
315  
316 sub serve_json($data) {
317 print "Content-Type: application/json\n\n";
318 print encode_json $data;
319 return;
320 }
321  
322 sub report_json_agg( $method, $vars ) {
323 my $backend = $report->store->backend;
324 if ( !$backend->can($method) ) {
325 print "Content-Type: application/json\n\n";
326 return return_json_error("report store does not support $method");
327 }
328  
329 my $data;
330 try {
331 $data = $backend->$method( agg_params($vars) );
332 }
333 catch ($error) {
334 print "Content-Type: application/json\n\n";
335 return return_json_error("$error");
336 }
337  
338 return serve_json($data);
339 }
340  
341 sub report_json_domains($vars) {
342 my $data;
343 my @args;
344 push @args, reports => $vars->{reports}
345 if defined $vars->{reports} && $AGG_REPORT_KINDS{ $vars->{reports} };
346 try {
347 $data = $report->store->backend->get_report_domains(@args);
348 }
349 catch ($error) {
350 print "Content-Type: application/json\n\n";
351 return return_json_error("$error");
352 }
353  
354 return serve_json($data);
355 }
356  
357 sub report_json_report($vars) {
358 my %args = %$vars;
359 delete $args{reports}
360 if defined $args{reports} && !$AGG_REPORT_KINDS{ $args{reports} };
361 return serve_json( $report->store->backend->get_report(%args) );
362 }
363  
364 sub report_json_rr($vars) {
365 return serve_json(
366 $report->store->backend->get_rr( rid => $vars->{rid} ) );
367 }
368  
369 1;
370  
371 __END__
372  
373 =pod
374  
375 =head1 NAME
376  
377 Mail::DMARC::HTTP - view stored reports via HTTP
378  
379 =head1 VERSION
380  
381 version 2.20260827
382  
383 =head1 SYNOPSIS
384  
385 See the POD docs / man page for L<dmarc_httpd>.
386  
387 =head1 AUTHORS
388  
389 =over 4
390  
391 =item *
392  
393 Matt Simerson <msimerson@cpan.org>
394  
395 =item *
396  
397 Davide Migliavacca <shari@cpan.org>
398  
399 =item *
400  
401 Marc Bradshaw <marc@marcbradshaw.net>
402  
403 =back
404  
405 =head1 COPYRIGHT AND LICENSE
406  
407 This software is copyright (c) 2026 by Matt Simerson.
408  
409 This is free software; you can redistribute it and/or modify it under
410 the same terms as the Perl 5 programming language system itself.
411  
412 =cut