CSAF/Validator/OptionalTests.pm untested

filter: Statementstmt 0.0% 0 / 275 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 477 CC 52 SCAR 35.5
CC
52
Cov
0%
TEST_6_2_19
156.0
TEST_6_2_16
42.0
_TEST_weak_algo
42.0
CRAP 34.8
1 package CSAF::Validator::OptionalTests;
2  
3 use 5.010001;
4 use strict;
5 use warnings;
6 use utf8;
7 use version;
8  
9 use CSAF::Util::CVSS qw(decode_cvss_vector_string);
10 use CSAF::Util qw(tracking_id_to_well_filename);
11  
12 use File::Basename;
13 use List::Util qw(first);
14  
15 use I18N::LangTags::List;
16  
17 use Moo;
18 extends 'CSAF::Validator::Base';
19 with 'CSAF::Util::Log';
20  
21 use constant DEBUG => $ENV{CSAF_DEBUG};
22  
23 has tests => (
24 is => 'ro',
25 default => sub { [
26 '6.2.1', '6.2.2', '6.2.3', '6.2.4', '6.2.5', '6.2.6', '6.2.7', '6.2.8', '6.2.9', '6.2.10',
27 '6.2.11', '6.2.12', '6.2.13', '6.2.14', '6.2.15', '6.2.16', '6.2.17', '6.2.18', '6.2.19', '6.2.20'
28 ] }
29 );
30  
31 my $VERS_REGEXP = qr{^vers:[a-z\\.\\-\\+][a-z0-9\\.\\-\\+]*/.+};
32  
33  
34 # 6.2.1 Unused Definition of Product ID (*)
35  
36 sub TEST_6_2_1 { }
37  
38  
39 # 6.2.2 Missing Remediation
40  
41 sub TEST_6_2_2 {
42  
43 my $self = shift;
44  
45 return unless $self->csaf->vulnerabilities->size;
46  
47 my @statuses = qw(first_affected known_affected last_affected under_investigation);
48  
49 $self->csaf->vulnerabilities->each(sub {
50  
51 my ($vulnerability, $vuln_idx) = @_;
52  
53 my $product_status = $vulnerability->product_status;
54  
55 for my $status (@statuses) {
56  
57 my @product_ids = @{$product_status->$status};
58 my $product_idx = 0;
59  
60 foreach my $product_id (@product_ids) {
61 if (!$vulnerability->remediations->size) {
62 $self->add_message(
63 type => 'warning',
64 category => 'optional',
65 path => "/vulnerabilities/$vuln_idx/product_status/$status/$product_idx",
66 code => '6.2.2',
67 message => 'Missing Remediation'
68 );
69 }
70 $product_idx++;
71 }
72  
73 }
74  
75 });
76  
77 }
78  
79  
80 # 6.2.3 Missing Score
81  
82 sub TEST_6_2_3 {
83  
84 my $self = shift;
85  
86 return unless $self->csaf->vulnerabilities->size;
87  
88 my @statuses = qw(first_affected known_affected last_affected);
89  
90 $self->csaf->vulnerabilities->each(sub {
91  
92 my ($vulnerability, $vuln_idx) = @_;
93  
94 my $product_status = $vulnerability->product_status;
95  
96 for my $status (@statuses) {
97  
98 my @product_ids = @{$product_status->$status};
99 my $product_idx = 0;
100  
101 foreach my $product_id (@product_ids) {
102 if (!$vulnerability->scores->size) {
103 $self->add_message(
104 type => 'warning',
105 category => 'optional',
106 path => "/vulnerabilities/$vuln_idx/product_status/$status/$product_idx",
107 code => '6.2.3',
108 message => 'Missing Score'
109 );
110 }
111 $product_idx++;
112 }
113  
114 }
115  
116 });
117  
118 }
119  
120  
121 # 6.2.4 Build Metadata in Revision History
122  
123 sub TEST_6_2_4 {
124  
125 my $self = shift;
126  
127 my $document_revisions = $self->csaf->document->tracking->revision_history;
128  
129 $document_revisions->each(sub {
130  
131 my ($revision, $idx) = @_;
132  
133 if ($revision->number =~ /\+/) {
134 $self->add_message(
135 type => 'warning',
136 category => 'optional',
137 path => "/document/tracking/revision_history/$idx/number",
138 code => '6.2.4',
139 message => 'Build Metadata in Revision History'
140 );
141 }
142  
143 });
144  
145 }
146  
147  
148 # 6.2.5 Older Initial Release Date than Revision History
149  
150 sub TEST_6_2_5 {
151  
152 my $self = shift;
153  
154 my $document_revisions = $self->csaf->document->tracking->revision_history;
155 return unless $document_revisions->size;
156  
157 my $first_document_revision = $document_revisions->first;
158 my $initial_release_date = $self->csaf->document->tracking->initial_release_date;
159  
160 if ($initial_release_date < $first_document_revision->date) {
161 $self->add_message(
162 type => 'warning',
163 category => 'optional',
164 path => '/document/tracking/initial_release_date',
165 code => '6.2.5',
166 message => 'Older Initial Release Date than Revision History'
167 );
168 }
169  
170 }
171  
172  
173 # 6.2.6 Older Current Release Date than Revision History
174  
175 sub TEST_6_2_6 {
176  
177 my $self = shift;
178  
179 my $document_revisions = $self->csaf->document->tracking->revision_history;
180 return unless $document_revisions->size;
181  
182 my $last_document_revision = $document_revisions->last;
183 my $current_release_date = $self->csaf->document->tracking->current_release_date;
184  
185 if ($current_release_date < $last_document_revision->date) {
186 $self->add_message(
187 type => 'warning',
188 category => 'optional',
189 path => '/document/tracking/initial_release_date',
190 code => '6.2.6',
191 message => 'Older Current Release Date than Revision History'
192 );
193 }
194  
195 }
196  
197  
198 # 6.2.7 Missing Date in Involvements
199  
200 sub TEST_6_2_7 {
201  
202 my $self = shift;
203  
204 $self->csaf->vulnerabilities->each(sub {
205  
206 my ($vulnerability, $vuln_idx) = @_;
207  
208 $vulnerability->involvements->each(sub {
209  
210 my ($involvement, $involvement_idx) = @_;
211  
212 if (!$involvement->date) {
213 $self->add_message(
214 type => 'warning',
215 category => 'optional',
216 path => "/vulnerabilities/$vuln_idx/involvements/$involvement_idx",
217 code => '6.2.7',
218 message => 'Missing Date in Involvements'
219 );
220 }
221  
222 });
223  
224 });
225  
226 }
227  
228  
229 # 6.2.8 Use of MD5 as the only Hash Algorithm
230  
231 sub TEST_6_2_8 { shift->_TEST_weak_algo('md5') }
232  
233  
234 # 6.2.9 Use of SHA-1 as the only Hash Algorithm
235  
236 sub TEST_6_2_9 { shift->_TEST_weak_algo('sha1') }
237  
238  
239 # 6.2.10 Missing TLP label
240  
241 sub TEST_6_2_10 {
242  
243 my $self = shift;
244  
245 if (!$self->csaf->document->distribution->tlp->label) {
246 $self->add_message(
247 type => 'warning',
248 category => 'optional',
249 path => '/document/distribution/tlp/label',
250 code => '6.2.10',
251 message => 'Missing TLP label'
252 );
253 }
254  
255 }
256  
257  
258 # 6.2.11 Missing Canonical URL
259  
260 sub TEST_6_2_11 {
261  
262 my $self = shift;
263  
264 my $have_self = 0;
265  
266 my $tracking_id = $self->csaf->document->tracking->id;
267 my $doc_filename = tracking_id_to_well_filename($tracking_id);
268  
269 $self->csaf->document->references->each(sub {
270  
271 my ($reference, $ref_idx) = @_;
272  
273 return if ($reference->category ne 'self');
274  
275 $have_self = 1;
276  
277 my $url = $reference->url;
278  
279 if ($url !~ /^https\:/) {
280 return $self->add_message(
281 type => 'warning',
282 category => 'optional',
283 path => "/document/references/$ref_idx",
284 code => '6.2.11',
285 message => 'Missing Canonical URL'
286 );
287 }
288  
289 if (basename($url) ne $doc_filename) {
290 return $self->add_message(
291 type => 'warning',
292 category => 'optional',
293 path => "document/references/$ref_idx",
294 code => '6.2.11',
295 message => 'Missing Canonical URL'
296 );
297 }
298  
299 });
300  
301 if (!$have_self) {
302 $self->add_message(
303 type => 'warning',
304 category => 'optional',
305 path => '/document/references',
306 code => '6.2.11',
307 message => 'Missing Canonical URL'
308 );
309 }
310  
311 }
312  
313  
314 # 6.2.12 Missing Document Language
315  
316 sub TEST_6_2_12 {
317  
318 my $self = shift;
319  
320 if (!$self->csaf->document->lang) {
321 $self->add_message(
322 type => 'warning',
323 category => 'optional',
324 path => '/document/lang',
325 code => '6.2.12',
326 message => 'Missing Document Language'
327 );
328 }
329  
330 }
331  
332  
333 # 6.2.13 Sorting
334  
335 sub TEST_6_2_13 { DEBUG and shift->log->info('6.2.13 Sorting => TODO Unimplementable test') }
336  
337  
338 # 6.2.14 Use of Private Language
339  
340 sub TEST_6_2_14 {
341  
342 my $self = shift;
343  
344 my %check = (
345 '/document/lang' => $self->csaf->document->lang,
346 '/document/source_lang' => $self->csaf->document->source_lang
347 );
348  
349 foreach (keys %check) {
350  
351 my $path = $_;
352 my $lang = $check{$path};
353  
354 next unless $lang;
355  
356 # Subtags in official testsuite (optional/oasis_csaf_tc-csaf_2_0-2021-6-2-14-*.json)
357 if ($lang =~ /\-(AA|XP|ZZ|QM|QABC)$/i) {
358 return $self->add_message(
359 type => 'warning',
360 category => 'optional',
361 path => $path,
362 code => '6.2.14',
363 message => 'Use of Private Language'
364 );
365 }
366  
367 if ($lang =~ /(q([a-t])([a-z]))/gi) {
368  
369 return $self->add_message(
370 type => 'warning',
371 category => 'optional',
372 path => $path,
373 code => '6.2.14',
374 message => 'Use of Private Language'
375 );
376  
377 }
378  
379 if (!I18N::LangTags::List::is_decent($lang)) {
380 return $self->add_message(
381 type => 'warning',
382 category => 'optional',
383 path => $path,
384 code => '6.2.14',
385 message => 'Use of Private Language'
386 );
387 }
388  
389 }
390  
391 }
392  
393  
394 # 6.2.15 Use of Default Language
395  
396 sub TEST_6_2_15 {
397  
398 my $self = shift;
399  
400 my %check = (
401 '/document/lang' => $self->csaf->document->lang,
402 '/document/source_lang' => $self->csaf->document->source_lang
403 );
404  
405 foreach (keys %check) {
406  
407 my $path = $_;
408 my $lang = $check{$path};
409  
410 next unless $lang;
411  
412 if ($lang eq 'i-default') {
413 return $self->add_message(
414 type => 'warning',
415 category => 'optional',
416 path => $path,
417 code => '6.2.15',
418 message => 'Use of Default Language'
419 );
420 }
421  
422 }
423  
424 }
425  
426  
427 # 6.2.16 Missing Product Identification Helper
428  
429 sub TEST_6_2_16 {
430  
431 my $self = shift;
432  
433 return if (!$self->csaf->product_tree);
434  
435 $self->csaf->product_tree->full_product_names->each(sub {
436  
437 my ($full_product_name, $full_product_name_idx) = @_;
438  
439 if (!$full_product_name->product_identification_helper) {
440 $self->add_message(
441 type => 'warning',
442 category => 'optional',
443 path => "/product_tree/full_product_names/$full_product_name_idx",
444 code => '6.2.16',
445 message => 'Missing Product Identification Helper'
446 );
447 }
448  
449 });
450  
451 $self->csaf->product_tree->relationships->each(sub {
452  
453 my ($relationship, $relationship_idx) = @_;
454  
455 return if (!$relationship->full_product_name);
456  
457 my $path = "/product_tree/relationships/$relationship_idx/full_product_name";
458 my $full_product_name = $relationship->full_product_name;
459  
460 if (!$full_product_name->product_identification_helper) {
461 $self->add_message(
462 type => 'warning',
463 category => 'optional',
464 path => $path,
465 code => '6.2.16',
466 message => 'Missing Product Identification Helper'
467 );
468 }
469  
470 });
471  
472 if ($self->csaf->product_tree->branches->size) {
473 $self->_TEST_6_2_16_walk_branches($self->csaf->product_tree->branches, '/product_tree/branches');
474 }
475  
476 }
477  
478  
479 # 6.2.17 CVE in field IDs
480  
481 sub TEST_6_2_17 {
482  
483 my $self = shift;
484  
485 $self->csaf->vulnerabilities->each(sub {
486  
487 my ($vulnerability, $vuln_idx) = @_;
488  
489 $vulnerability->ids->each(sub {
490  
491 my ($id, $id_idx) = @_;
492  
493 if ($id->text =~ /^CVE-[0-9]{4}-[0-9]{4,}$/) {
494 $self->add_message(
495 type => 'warning',
496 category => 'optional',
497 path => "/vulnerabilities/$vuln_idx/ids/$id_idx",
498 code => '6.2.17',
499 message => 'CVE in field IDs'
500 );
501 }
502  
503 });
504  
505 });
506  
507 }
508  
509  
510 # 6.2.18 Product Version Range without vers
511  
512 sub TEST_6_2_18 {
513  
514 my $self = shift;
515  
516 return if (not $self->csaf->product_tree);
517  
518 $self->_TEST_6_2_18_branches($self->csaf->product_tree->branches, "/product_tree/branches");
519  
520 }
521  
522  
523 # 6.2.19 CVSS for Fixed Products
524  
525 sub TEST_6_2_19 {
526  
527 my $self = shift;
528  
529 $self->csaf->vulnerabilities->each(sub {
530  
531 my ($vulnerability, $vuln_idx) = @_;
532  
533 $vulnerability->scores->each(sub {
534  
535 my ($score, $score_idx) = @_;
536  
537 my $fixed_products = 0;
538  
539 foreach my $product_id (@{$score->products}) {
540 $fixed_products = 1 if (first { $product_id eq $_ } @{$vulnerability->product_status->fixed});
541 $fixed_products = 1 if (first { $product_id eq $_ } @{$vulnerability->product_status->first_fixed});
542 }
543  
544 return if (!$fixed_products);
545  
546 if (my $cvss = $score->cvss_v2) {
547  
548 my $is_invalid = 0;
549  
550 if (!$cvss->targetDistribution) {
551  
552 $is_invalid = 1;
553  
554 my $decoded = decode_cvss_vector_string($score->cvss_v2->vectorString);
555  
556 if (!defined($decoded->{targetDistribution})) {
557 $is_invalid = 1;
558 }
559 else {
560 $is_invalid = 0;
561 }
562  
563 }
564  
565 if ($is_invalid) {
566 $self->add_message(
567 type => 'warning',
568 category => 'optional',
569 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v2",
570 code => '6.2.19',
571 message => 'CVSS for Fixed Products'
572 );
573 }
574 }
575  
576 if (my $cvss = $score->cvss_v3) {
577  
578 my $is_invalid = 0;
579  
580 if ( !$cvss->modifiedIntegrityImpact
581 || !$cvss->modifiedAvailabilityImpact
582 || !$cvss->modifiedConfidentialityImpact)
583 {
584  
585 $is_invalid = 1;
586  
587 my $decoded = decode_cvss_vector_string($score->cvss_v3->vectorString);
588  
589 if ( !defined($decoded->{modifiedIntegrityImpact})
590 || !defined($decoded->{modifiedAvailabilityImpact})
591 || !defined($decoded->{modifiedConfidentialityImpact}))
592 {
593 $is_invalid = 1;
594 }
595 else {
596 $is_invalid = 0;
597 }
598  
599 }
600  
601 if ($is_invalid) {
602 $self->add_message(
603 type => 'warning',
604 category => 'optional',
605 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v3",
606 code => '6.2.19',
607 message => 'CVSS for Fixed Products'
608 );
609 }
610  
611 }
612  
613 });
614  
615 });
616  
617 }
618  
619  
620 # 6.2.20 Additional Properties
621 sub TEST_6_2_20 { DEBUG and shift->log->info('6.2.20 Additional Properties => Test implemented in "CSAF::Parser"') }
622  
623  
624 sub _TEST_6_2_18_branches {
625  
626 my ($self, $branches, $path) = @_;
627  
628 $branches->each(sub {
629  
630 my ($branch, $branch_idx) = @_;
631  
632 $self->_TEST_6_2_18_branches($branch->branches, "$path/$branch_idx/branches");
633  
634 if ($branch->category eq 'product_version_range') {
635  
636 if ($branch->name !~ /$VERS_REGEXP/) {
637 $self->add_message(
638 type => 'warning',
639 category => 'optional',
640 path => "$path/name",
641 code => '6.2.18',
642 message => 'Product Version Range without vers'
643 );
644 }
645  
646 }
647  
648 });
649 }
650  
651 sub _TEST_weak_algo_product_identification_helper {
652  
653 my ($self, $product_identification_helper, $algo, $path) = @_;
654  
655 my $MESSAGES = {
656 md5 => ['6.2.8', 'Use of MD5 as the only Hash Algorithm'],
657 sha1 => ['6.2.9', 'Use of SHA-1 as the only Hash Algorithm'],
658 };
659  
660 my ($code, $message) = @{$MESSAGES->{$algo}};
661  
662 $product_identification_helper->hashes->each(sub {
663  
664 my ($hash, $hash_idx) = @_;
665  
666 my $check = 0;
667  
668 $hash->file_hashes->each(sub {
669 my ($file_hash, $file_hash_idx) = @_;
670 $check++ if ($file_hash->algorithm eq $algo);
671 });
672  
673 if ($check == $hash->file_hashes->size) {
674  
675 $path .= "/product_identification_helper/hashes/$hash_idx/file_hashes";
676  
677 $self->add_message(
678 type => 'warning',
679 category => 'optional',
680 path => $path,
681 code => $code,
682 message => $message
683 );
684  
685 }
686  
687 });
688  
689 }
690  
691 sub _TEST_weak_algo {
692  
693 my ($self, $algo) = @_;
694  
695 return if (!$self->csaf->product_tree);
696  
697 $self->csaf->product_tree->full_product_names->each(sub {
698  
699 my ($full_product_name, $full_product_name_idx) = @_;
700  
701 return if (!$full_product_name->product_identification_helper);
702  
703 my $product_identification_helper = $full_product_name->product_identification_helper;
704  
705 my $path = "/product_tree/full_product_names/$full_product_name_idx";
706  
707 $self->_TEST_weak_algo_product_identification_helper($product_identification_helper, $algo, $path);
708  
709 });
710  
711 $self->csaf->product_tree->relationships->each(sub {
712  
713 my ($relationship, $relationship_idx) = @_;
714  
715 return if (!$relationship->full_product_name);
716 return if (!$relationship->full_product_name->product_identification_helper);
717  
718 my $product_identification_helper = $relationship->full_product_name->product_identification_helper;
719  
720 my $path = "/product_tree/relationships/$relationship_idx/full_product_name";
721  
722 $self->_TEST_weak_algo_product_identification_helper($product_identification_helper, $algo, $path);
723  
724 });
725  
726 if ($self->csaf->product_tree->branches->size) {
727 $self->_TEST_weak_algo_walk_branches($self->csaf->product_tree->branches, $algo, "/product_tree/branches");
728 }
729  
730 }
731  
732 sub _TEST_weak_algo_walk_branches {
733  
734 my ($self, $branches, $algo, $path) = @_;
735  
736 $branches->each(sub {
737  
738 my ($branch, $branch_idx) = @_;
739  
740 $self->_TEST_weak_algo_walk_branches($branch->branches, $algo, "$path/$branch_idx/branches");
741  
742 if ($branch->product && $branch->product->product_identification_helper) {
743 $self->_TEST_weak_algo_product_identification_helper($branch->product->product_identification_helper,
744 $algo, "$path/product/product_identification_helper");
745 }
746  
747 });
748 }
749  
750 sub _TEST_6_2_16_walk_branches {
751  
752 my ($self, $branches, $path) = @_;
753  
754 $branches->each(sub {
755  
756 my ($branch, $branch_idx) = @_;
757  
758 $self->_TEST_6_2_16_walk_branches($branch->branches, "$path/$branch_idx/branches");
759  
760 if ($branch->product && !$branch->product->product_identification_helper) {
761 $self->add_message(
762 type => 'warning',
763 category => 'optional',
764 path => "$path/$branch_idx",
765 code => '6.2.16',
766 message => 'Missing Product Identification Helper'
767 );
768 }
769  
770 });
771 }
772  
773 1;
774  
775 __END__