BIMI/Indicator.pm

filter: Statementstmt 73.4% 105 / 143 Branchbran 43.5% 27 / 62 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.9% 20 / 23 Podpod 100% 7 / 7 totaltotal 64.9% 161 / 248 CC 36 SCAR 43.7
CC
36
Cov
63%
app_validate
272.0
_build_is_valid
15.2
_build_data
5.8
CRAP 79.4
« BIMI/VMC.pm ↑ Summary BIMI/App/Command/checkrecord.pm »
1 package Mail::BIMI::Indicator;
2 # ABSTRACT: Class to model a BIMI indicator
3 29 our $VERSION = '3.20260302'; # VERSION
4 30 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Mail::BIMI::Prelude;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use File::Slurp qw{ read_file write_file };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use IO::Uncompress::Gunzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Term::ANSIColor qw{ :constants };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use XML::LibXML 2.0202;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 our @VALIDATOR_PROFILES = qw{ SVG_1.2_BIMI SVG_1.2_PS Tiny-1.2 };
14  
15 29 extends 'Mail::BIMI::Base';
16 29 with(
17 'Mail::BIMI::Role::HasError',
18 'Mail::BIMI::Role::HasHTTPClient',
19 'Mail::BIMI::Role::Data',
20 'Mail::BIMI::Role::Cacheable',
21 );
22 29 has uri => ( is => 'rw', isa => 'Str', traits => ['CacheKey'],
23 documentation => 'inputs: URL to retrieve Indicator from', );
24 29 has source => ( is => 'rw', isa => 'Str', traits => ['Cacheable'],
25 documentation => 'Human readable summary of where this indicator was retrieved from' );
26 29 has data => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_data', traits => ['Cacheable'],
27 documentation => 'inputs: Raw data representing the Indicator; Fetches from uri if not given', );
28 29 has data_uncompressed => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_data_uncompressed', traits => ['Cacheable'],
29 documentation => 'Raw data in uncompressed form' );
30 29 has data_xml => ( is => 'rw', lazy => 1, builder => '_build_data_xml',
31 documentation => 'XML::LibXML object representing the Indicator' );
32 29 has is_valid => ( is => 'rw', lazy => 1, builder => '_build_is_valid', traits => ['Cacheable'],
33 documentation => 'Is this indicator valid' );
34 29 has parser => ( is => 'rw', lazy => 1, builder => '_build_parser',
35 documentation => 'XML::LibXML::RelaxNG parser object used to validate the Indicator XML' );
36 29 has header => ( is => 'rw', lazy => 1, builder => '_build_header', traits => ['Cacheable'],
37 documentation => 'Indicator data encoded as Base64 ready for insertion as BIMI-Indicator header' );
38 29 has validator_profile => ( is => 'rw', isa => enum(\@VALIDATOR_PROFILES), lazy => 1, builder => '_build_validator_profile', traits => ['Cacheable'],
39 documentation => 'inputs: Validator profile used to validate the Indicator', );
40  
41  
42 ▶sub _build_validator_profile($self) {
Subroutine _build_validator_profile: called CC=1 SCAR=0.0
43 9 return $self->bimi_object->options->svg_profile;
44 }
45  
46  
47 4 ▶sub cache_valid_for($self) { return 3600 }
Subroutine cache_valid_for: called CC=1 SCAR=0.0
Pod documented
48  
49  
50 8 ▶sub http_client_max_fetch_size($self) { return $self->bimi_object->options->svg_max_fetch_size };
Subroutine http_client_max_fetch_size: called CC=1 SCAR=0.0
Pod documented
51  
52 ▶sub _build_data_uncompressed($self) {
Subroutine _build_data_uncompressed: called CC=3 SCAR=11.0
53 28 my $data = $self->data;
54 28 ▶ if ( $data =~ /^\037\213/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\037\213/u) { } 2 26 28
55 2 $self->log_verbose('Uncompressing SVG');
56  
57 2 my $unzipped;
58 2 IO::Uncompress::Gunzip::gunzip(\$data,\$unzipped);
59 2 ▶ if ( !$unzipped ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($unzipped) 1 1 2
60 1 $self->add_error('SVG_UNZIP_ERROR');
61 1 return '';
62 }
63 1 return $unzipped;
64 }
65 else {
66 26 return $data;
67 }
68 }
69  
70  
71 ▶sub data_maybe_compressed($self) {
Subroutine data_maybe_compressed: called CC=1 SCAR=0.0
Pod documented
72 # Alias for clarity, the data is as received.
73 1 return $self->data;
74 }
75  
76  
77 ▶sub data_uncompressed_normalized($self) {
Subroutine data_uncompressed_normalized: not called CC=1 SCAR=6.9
Pod documented
78 0 my $data = $self->data_uncompressed;
79 0 $data =~ s/\r\n?/\n/g;
80 0 return $data;
81 }
82  
83 ▶sub _build_data_xml($self) {
Subroutine _build_data_xml: called CC=3 SCAR=11.0
84 26 my $xml;
85 26 my $data = $self->data_uncompressed;
86 26 ▶ if ( !$data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 25 26
87 1 $self->add_error('SVG_GET_ERROR');
88 1 return;
89 }
90 eval {
91 25 $xml = XML::LibXML->new->load_xml(string => $self->data_uncompressed);
92 24 1;
93 25 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $xml = "XML::LibXML"->new->load_xml("string", $self->data_uncompressed); 1 } } or do { $self->add_error("SVG_INVALID_XML"); $self->log_verbose("Invalid XML :\n" . $self->data_uncompressed); return } 1 24 25
94 1 $self->add_error('SVG_INVALID_XML');
95 1 $self->log_verbose("Invalid XML :\n".$self->data_uncompressed);
96 1 return;
97 };
98 24 return $xml;
99 }
100  
101 ▶sub _build_parser($self) {
Subroutine _build_parser: called CC=1 SCAR=0.0
102 23 state $parser = XML::LibXML::RelaxNG->new( string => $self->get_data_from_file($self->validator_profile.'.rng'), no_network => 1 );
103 23 return $parser;
104 }
105  
106 ▶sub _build_data($self) {
Subroutine _build_data: called CC=5 SCAR=17.6
107 23 ▶ if ( ! $self->uri ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->uri) 1 22 23
108 1 $self->add_error('CODE_MISSING_LOCATION');
109 1 return '';
110 }
111 22 ▶ if ($self->bimi_object->options->svg_from_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->svg_from_file) 1 21 22
112 1 $self->log_verbose('Reading SVG from file '.$self->bimi_object->options->svg_from_file);
113 1 return scalar read_file $self->bimi_object->options->svg_from_file;
114 }
115 21 $self->log_verbose('HTTP Fetch: '.$self->uri);
116 21 my $response = $self->http_client_get( $self->uri );
117 21 ▶ if ( !$response->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 21 21
118 0 ▶ if ( $response->{status} == 599 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'status'} == 599) { } 0 0 0
119 0 $self->add_error('SVG_FETCH_ERROR',$response->{content});
120 }
121 else {
122 0 $self->add_error('SVG_FETCH_ERROR',$response->{status});
123 }
124 0 return '';
125 }
126 21 return $response->{content};
127 }
128  
129 ▶sub _build_is_valid($self) {
Subroutine _build_is_valid: called CC=12 SCAR=27.2
130  
131 25 ▶ if (!(defined $self->data||$self->uri)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data or $self->uri) 0 25 25
Condition33%1/3
defined $self->data or $self->uri
l!l&&r!l&&!r
2500
MC/DC0%0/2
defined $self->data or $self->uri
defined $self->data $self->uri
Truth table33%1/3
A or B
A= defined $self->dataB= $self->uri
ABresult
1X1
011
000
132 0 $self->add_error('CODE_NOTHING_TO_VALIDATE');
133 0 return 0;
134 }
135  
136 25 ▶ if (!defined $self->data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data) 0 25 25
137 0 $self->add_error('CODE_NO_DATA');
138 0 return 0;
139 }
140  
141 25 ▶ if (!$self->data && $self->errors->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->data and @{$self->errors}) 0 25 25
Condition33%1/3
not $self->data and @{$self->errors}
!ll&&!rl&&r
2500
MC/DC0%0/2
not $self->data and @{$self->errors}
not $self->data @{$self->errors}
Truth table33%1/3
not(A) and B
A= not $self->dataB= @{$self->errors}
ABresult
0X0
100
111
142 0 return 0;
143 }
144  
145 25 my $is_valid;
146 25 ▶ if ( length $self->data_uncompressed > $self->bimi_object->options->svg_max_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->data_uncompressed > $self->bimi_object->options->svg_max_size) { } 1 24 25
147 1 $self->add_error('SVG_SIZE');
148 }
149 else {
150 24 ▶ if ( $self->bimi_object->options->no_validate_svg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bimi_object->options->no_validate_svg) { } 1 23 24
151 1 $is_valid=1;
152 1 $self->log_verbose('Skipping SVG validation');
153 }
154 else {
155 eval {
156 23 my $data_xml = $self->data_xml;
157 23 ▶ if ($data_xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_xml) 23 0 23
158 23 $self->parser->validate( $data_xml );
159 22 $is_valid=1;
160 22 $self->log_verbose('SVG is valid');
161 }
162 22 1;
163 23 ▶ } || do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $data_xml = $self->data_xml; if ($data_xml) { $self->parser->validate($data_xml); $is_valid = 1; $self->log_verbose("SVG is valid"); } ; 1 } } or do { my $validation_error = $@; my $error_text = ref $validation_error eq "XML::LibXML::Error" ? $validation_error->as_string : $validation_error; $self->add_error("SVG_VALIDATION_ERROR", $error_text) } 1 22 23
164 1 my $validation_error = $@;
165 1 ▶ my $error_text = ref $validation_error eq 'XML::LibXML::Error' ? $validation_error->as_string : $validation_error;
Branch: 1 branch
BranchTrueFalseTotal
ref $validation_error eq 'XML::LibXML::Error' ? : 1 0 1
166 1 $self->add_error('SVG_VALIDATION_ERROR',$error_text);
167 };
168 }
169 }
170  
171 25 ▶ return 0 if $self->errors->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->errors} 2 23 25
172 23 return 1;
173 }
174  
175 ▶sub _build_header($self) {
Subroutine _build_header: called CC=2 SCAR=7.0
176 4 ▶ return if !$self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 4 4
177 4 my $base64 = encode_base64( $self->data_uncompressed );
178 4 $base64 =~ s/\n//g;
179 4 my @parts = unpack("(A70)*", $base64);
180 4 return join("\n ", @parts);
181 }
182  
183  
184 ▶sub header_crlf($self) {
Subroutine header_crlf: not called CC=1 SCAR=6.9
Pod documented
185 0 my $base64 = encode_base64( $self->data_uncompressed );
186 0 $base64 =~ s/\n//g;
187 0 my @parts = unpack("(A70)*", $base64);
188 0 return "BIMI-Indicator: " . join("\r\n ", @parts)
189 }
190  
191  
192 ▶sub finish($self) {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
193 5 $self->_write_cache;
194 }
195  
196  
197 ▶sub app_validate($self) {
Subroutine app_validate: not called CC=16 SCAR=56.1
Pod documented
198 0 my $size_raw = 0;
199 0 ▶ $size_raw = length($self->data) if $self->data;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data 0 0 0
200 0 my $size_uncompressed = 0;
201 0 ▶ $size_uncompressed = length($self->data_uncompressed) if $self->data_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data_uncompressed 0 0 0
202 0 my $size_header = 0;
203 0 ▶ $size_header = length($self->header_crlf) if $self->header_crlf;
Branch: 1 branch
BranchTrueFalseTotal
if $self->header_crlf 0 0 0
204  
205 0 ▶ say 'Indicator'.($self->source ? ' (From '.$self->source.')' : '' ).' Returned: '.($self->is_valid ? GREEN."\x{2713}" : BRIGHT_RED."\x{26A0}").RESET;
Branch: 2 branches
BranchTrueFalseTotal
$self->source ? : 0 0 0
$self->is_valid ? : 0 0 0
206 0 ▶ say YELLOW.' GZipped '.WHITE.': '.CYAN.($self->data_uncompressed eq $self->data?'No':'Yes').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$self->data_uncompressed eq $self->data ? : 0 0 0
207 0 ▶ say YELLOW.' BIMI-Indicator '.WHITE.': '.CYAN.$self->header.RESET if $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_valid 0 0 0
208 0 say YELLOW.' Profile Used '.WHITE.': '.CYAN.$self->validator_profile.RESET;
209 0 ▶ say YELLOW.' Is Valid '.WHITE.': '.($self->is_valid?GREEN.'Yes':BRIGHT_RED.'No').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid ? : 0 0 0
210 0 ▶ say YELLOW.' Size (raw) '.WHITE.': '.$size_raw.' bytes'.RESET if $size_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $size_raw 0 0 0
211 0 ▶ say YELLOW.' Size (unzip) '.WHITE.': '.$size_uncompressed.' bytes'.RESET if $size_uncompressed;
Branch: 1 branch
BranchTrueFalseTotal
if $size_uncompressed 0 0 0
212 0 ▶ say YELLOW.' Size (header) '.WHITE.': '.$size_header.' bytes'.RESET if $size_header;
Branch: 1 branch
BranchTrueFalseTotal
if $size_header 0 0 0
213 0 ▶ if ( ! $self->is_valid ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_valid) 0 0 0
214 0 say "Errors:";
215 0 foreach my $error ( $self->errors->@* ) {
216 0 my $error_code = $error->code;
217 0 my $error_text = $error->description;
218 0 ▶ my $error_detail = $error->detail // '';
Condition0%0/2
$error->detail // ""
l!l
00
MC/DC0%0/1
$error->detail // ""
$error->detail
Truth table0%0/2
A
A= $error->detail
Aresult
11
00
219 0 $error_detail =~ s/\n/\n /g;
220 0 ▶ say BRIGHT_RED." $error_code ".WHITE.': '.CYAN.$error_text.($error_detail?"\n ".$error_detail:'').RESET;
Branch: 1 branch
BranchTrueFalseTotal
$error_detail ? : 0 0 0
221 }
222 }
223 }
224  
225 29 1;
226  
227 __END__
228  
229 =pod
230  
231 =encoding UTF-8
232  
233 =head1 NAME
234  
235 Mail::BIMI::Indicator - Class to model a BIMI indicator
236  
237 =head1 VERSION
238  
239 version 3.20260302
240  
241 =head1 DESCRIPTION
242  
243 Class for representing, retrieving, validating, and processing a BIMI Indicator
244  
245 =head1 INPUTS
246  
247 These values are used as inputs for lookups and verifications, they are typically set by the caller based on values found in the message being processed
248  
249 =head2 data
250  
251 is=rw
252  
253 Raw data representing the Indicator; Fetches from uri if not given
254  
255 =head2 uri
256  
257 is=rw
258  
259 URL to retrieve Indicator from
260  
261 =head2 validator_profile
262  
263 is=rw
264  
265 Validator profile used to validate the Indicator
266  
267 =head1 ATTRIBUTES
268  
269 These values are derived from lookups and verifications made based upon the input values, it is however possible to override these with other values should you wish to, for example, validate a record before it is published in DNS, or validate an Indicator which is only available locally
270  
271 =head2 cache_backend
272  
273 is=ro
274  
275 =head2 data_uncompressed
276  
277 is=rw
278  
279 Raw data in uncompressed form
280  
281 =head2 data_xml
282  
283 is=rw
284  
285 XML::LibXML object representing the Indicator
286  
287 =head2 errors
288  
289 is=rw
290  
291 =head2 header
292  
293 is=rw
294  
295 Indicator data encoded as Base64 ready for insertion as BIMI-Indicator header
296  
297 =head2 http_client
298  
299 is=rw
300  
301 HTTP::Tiny::Paranoid (or similar) object used for HTTP operations
302  
303 =head2 is_valid
304  
305 is=rw
306  
307 Is this indicator valid
308  
309 =head2 parser
310  
311 is=rw
312  
313 XML::LibXML::RelaxNG parser object used to validate the Indicator XML
314  
315 =head2 source
316  
317 is=rw
318  
319 Human readable summary of where this indicator was retrieved from
320  
321 =head2 warnings
322  
323 is=rw
324  
325 =head1 CONSUMES
326  
327 =over 4
328  
329 =item * L<Mail::BIMI::Role::Cacheable>
330  
331 =item * L<Mail::BIMI::Role::Data>
332  
333 =item * L<Mail::BIMI::Role::HasError>
334  
335 =item * L<Mail::BIMI::Role::HasError|Mail::BIMI::Role::HasHTTPClient|Mail::BIMI::Role::Data|Mail::BIMI::Role::Cacheable>
336  
337 =item * L<Mail::BIMI::Role::HasHTTPClient>
338  
339 =back
340  
341 =head1 EXTENDS
342  
343 =over 4
344  
345 =item * L<Mail::BIMI::Base>
346  
347 =back
348  
349 =head1 METHODS
350  
351 =head2 I<cache_valid_for()>
352  
353 How long should the cache for this class be valid
354  
355 =head2 I<http_client_max_fetch_size()>
356  
357 Maximum permitted HTTP fetch
358  
359 =head2 I<data_maybe_compressed()>
360  
361 Synonym for data; returns the data in a maybe compressed format
362  
363 =head2 I<data_uncompressed_normalized()>
364  
365 Returns the uncompressed data with normalized line endings
366  
367 =head2 I<header_crlf()>
368  
369 Returns the proposed BIMI-Indicator in its entirity with crlf line endings
370 even when the Indicator has not validated.
371 This is used to find the size of the header in validator code and MUST NOT
372 be used to add the header to emails.
373  
374 =head2 I<finish()>
375  
376 Finish and clean up, write cache if enabled.
377  
378 =head2 I<app_validate()>
379  
380 Output human readable validation status of this object
381  
382 =head1 REQUIRES
383  
384 =over 4
385  
386 =item * L<File::Slurp|File::Slurp>
387  
388 =item * L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>
389  
390 =item * L<MIME::Base64|MIME::Base64>
391  
392 =item * L<Mail::BIMI::Prelude|Mail::BIMI::Prelude>
393  
394 =item * L<Moose|Moose>
395  
396 =item * L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints>
397  
398 =item * L<Term::ANSIColor|Term::ANSIColor>
399  
400 =item * L<XML::LibXML|XML::LibXML>
401  
402 =back
403  
404 =head1 AUTHOR
405  
406 Marc Bradshaw <marc@marcbradshaw.net>
407  
408 =head1 COPYRIGHT AND LICENSE
409  
410 This software is copyright (c) 2020 by Marc Bradshaw.
411  
412 This is free software; you can redistribute it and/or modify it under
413 the same terms as the Perl 5 programming language system itself.
414  
415 =cut
« BIMI/VMC.pm ↑ Summary BIMI/App/Command/checkrecord.pm »