CSAF/Validator/MandatoryTests.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« CSAF/Type/Tracking.pm ↑ Summary CSAF/Util.pm »
1 package CSAF::Validator::MandatoryTests;
2  
3 33 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 ▶use CSAF::Util::CWE qw(get_weakness_name weakness_exists);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use CSAF::Util qw(collect_product_ids product_in_group_exists);
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use CSAF::Schema;
12  
13 use CVSS;
14 use CVSS::v2;
15 use CVSS::v3;
16  
17 use List::MoreUtils qw(uniq duplicates);
18 use List::Util qw(first);
19 use URI::PackageURL;
20  
21 use Moo;
22 extends 'CSAF::Validator::Base';
23 with 'CSAF::Util::Log';
24  
25 use constant DEBUG => $ENV{CSAF_DEBUG};
26  
27 has tests => (
28 is => 'ro',
29 default => sub { [
30 '6.1.1', '6.1.2', '6.1.3', '6.1.4', '6.1.5', '6.1.6', '6.1.7', '6.1.8',
31 '6.1.9', '6.1.10', '6.1.11', '6.1.12', '6.1.13', '6.1.14', '6.1.15', '6.1.16',
32 '6.1.17', '6.1.18', '6.1.19', '6.1.20', '6.1.21', '6.1.22', '6.1.23', '6.1.24',
33 '6.1.25', '6.1.26', '6.1.27.1', '6.1.27.2', '6.1.27.3', '6.1.27.4', '6.1.27.5', '6.1.27.6',
34 '6.1.27.7', '6.1.27.8', '6.1.27.9', '6.1.27.10', '6.1.27.11', '6.1.28', '6.1.29', '6.1.30',
35 '6.1.31', '6.1.32', '6.1.33',
36 ] }
37 );
38  
39 my $PURL_REGEX = qr{^pkg:[A-Za-z\\.\\-\\+][A-Za-z0-9\\.\\-\\+]*/.+};
40  
41 my $SEMVER_REGEXP
42 = qr{^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$};
43  
44  
45 # 6.1.1 Missing Definition of Product ID
46  
47 sub TEST_6_1_1 {
48  
49 my $self = shift;
50  
51 my $product_ids = $CSAF::CACHE->{products} || {};
52  
53 my @product_statuses = (
54 'first_affected', 'first_fixed', 'fixed', 'known_affected',
55 'known_not_affected', 'last_affected', 'recommended', 'under_investigation',
56 );
57  
58 $self->csaf->vulnerabilities->each(sub {
59  
60 my ($vulnerability, $vuln_idx) = @_;
61  
62 # /vulnerabilities[]/product_status/first_affected[]
63 # /vulnerabilities[]/product_status/first_fixed[]
64 # /vulnerabilities[]/product_status/fixed[]
65 # /vulnerabilities[]/product_status/known_affected[]
66 # /vulnerabilities[]/product_status/known_not_affected[]
67 # /vulnerabilities[]/product_status/last_affected[]
68 # /vulnerabilities[]/product_status/recommended[]
69 # /vulnerabilities[]/product_status/under_investigation[]
70  
71 foreach my $product_status (@product_statuses) {
72  
73 my $method = $vulnerability->product_status->can($product_status);
74 my @products = @{$method->($vulnerability->product_status)};
75  
76 foreach my $product (@products) {
77 if (!first { $product eq $_ } keys %{$product_ids}) {
78 $self->add_message(
79 category => 'mandatory',
80 path => "/vulnerabilities/$vuln_idx/product_status/$product_status",
81 code => '6.1.1',
82 message => "Missing Definition of Product ID ($product)"
83 );
84 }
85 }
86  
87 }
88  
89  
90 # /vulnerabilities[]/scores[]/products[]
91  
92 $vulnerability->scores->each(sub {
93  
94 my ($score, $score_idx) = @_;
95  
96 foreach my $product (@{$score->products}) {
97 if (!first { $product eq $_ } keys %{$product_ids}) {
98 $self->add_message(
99 category => 'mandatory',
100 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/products",
101 code => '6.1.1',
102 message => "Missing Definition of Product ID ($product)"
103 );
104 }
105 }
106  
107 });
108  
109  
110 # /vulnerabilities[]/remediations[]/product_ids[]
111  
112 $vulnerability->remediations->each(sub {
113  
114 my ($remediation, $remediation_idx) = @_;
115  
116 foreach my $product (@{$remediation->product_ids}) {
117 if (!first { $product eq $_ } keys %{$product_ids}) {
118 $self->add_message(
119 category => 'mandatory',
120 path => "/vulnerabilities/$vuln_idx/remediations/$remediation_idx/product_ids",
121 code => '6.1.1',
122 message => "Missing Definition of Product ID ($product)"
123 );
124 }
125 }
126  
127 });
128  
129  
130 # /vulnerabilities[]/threats[]/product_ids[]
131  
132 $vulnerability->threats->each(sub {
133  
134 my ($threat, $threat_idx) = @_;
135  
136 foreach my $product (@{$threat->product_ids}) {
137 if (!first { $product eq $_ } keys %{$product_ids}) {
138 $self->add_message(
139 category => 'mandatory',
140 path => "/vulnerabilities/$vuln_idx/threats/$threat_idx/product_ids",
141 code => '6.1.1',
142 message => "Missing Definition of Product ID ($product)"
143 );
144 }
145 }
146  
147 });
148  
149 });
150  
151  
152 # /product_tree/product_groups[]/product_ids[]
153  
154 $self->csaf->product_tree->product_groups->each(sub {
155  
156 my ($product_group, $product_group_idx) = @_;
157  
158 foreach my $product (@{$product_group->product_ids}) {
159 if (!first { $product eq $_ } keys %{$product_ids}) {
160 $self->add_message(
161 category => 'mandatory',
162 path => "/product_tree/product_groups/$product_group_idx/product_ids",
163 code => '6.1.1',
164 message => "Missing Definition of Product ID ($product)"
165 );
166 }
167 }
168  
169 });
170  
171  
172 # /product_tree/relationships[]/product_reference
173 # /product_tree/relationships[]/relates_to_product_reference
174  
175 $self->csaf->product_tree->relationships->each(sub {
176  
177 my ($relationship, $rel_idx) = @_;
178  
179 if (my $product = $relationship->product_reference) {
180  
181 if (!first { $product eq $_ } keys %{$product_ids}) {
182 $self->add_message(
183 category => 'mandatory',
184 path => "/product_tree/relationships/$rel_idx/product_reference",
185 code => '6.1.1',
186 message => "Missing Definition of Product ID ($product)"
187 );
188 }
189  
190 }
191  
192 if (my $product = $relationship->relates_to_product_reference) {
193  
194 if (!first { $product eq $_ } keys %{$product_ids}) {
195 $self->add_message(
196 category => 'mandatory',
197 path => "/product_tree/relationships/$rel_idx/relates_to_product_reference",
198 code => '6.1.1',
199 message => "Missing Definition of Product ID ($product)"
200 );
201 }
202  
203 }
204  
205 });
206  
207  
208 }
209  
210  
211 # 6.1.2 Multiple Definition of Product ID
212  
213 sub TEST_6_1_2 {
214  
215 my $self = shift;
216  
217 if ($self->csaf->product_tree->branches->size) {
218  
219 my @product_ids = ();
220  
221 $self->csaf->product_tree->branches->each(sub {
222 my ($branch) = @_;
223 push @product_ids, collect_product_ids($branch);
224 });
225  
226 if (duplicates @product_ids) {
227  
228 $self->add_message(
229 category => 'mandatory',
230 path => '/product_tree/branches[](/branches[])*/product/product_id',
231 code => '6.1.2',
232 message => 'Multiple Definition of Product ID'
233 );
234  
235 }
236  
237 }
238  
239 if ($self->csaf->product_tree->full_product_names->size) {
240  
241 my @product_ids = ();
242  
243 $self->csaf->product_tree->full_product_names->each(sub {
244 my ($product, $idx) = @_;
245 push @product_ids, collect_product_ids($product);
246 });
247  
248 if (duplicates @product_ids) {
249  
250 $self->add_message(
251 category => 'mandatory',
252 path => '/product_tree/full_product_names[]/product_id',
253 code => '6.1.2',
254 message => 'Multiple Definition of Product ID'
255 );
256  
257 }
258  
259 }
260  
261 if ($self->csaf->product_tree->relationships->size) {
262  
263 my @product_ids = ();
264  
265 $self->csaf->product_tree->relationships->each(sub {
266  
267 my ($relationship, $rel_idx) = @_;
268  
269 if (my $product_id = $relationship->full_product_name->product_id) {
270 push @product_ids, $product_id;
271 }
272  
273 if (duplicates @product_ids) {
274  
275 $self->add_message(
276 category => 'mandatory',
277 path => "/product_tree/relationships/$rel_idx/full_product_name/product_id",
278 code => '6.1.2',
279 message => 'Multiple Definition of Product ID'
280 );
281  
282 }
283  
284 });
285  
286 }
287  
288 }
289  
290  
291 # 6.1.3 Circular Definition of Product ID
292  
293 sub TEST_6_1_3 {
294  
295 my $self = shift;
296  
297 $self->csaf->product_tree->relationships->each(sub {
298  
299 my ($relationship, $rel_idx) = @_;
300  
301 if ($relationship->product_reference eq $relationship->full_product_name->product_id) {
302 $self->add_message(
303 category => 'mandatory',
304 path => "/product_tree/relationships/$rel_idx/full_product_name/product_id",
305 code => '6.1.3',
306 message => 'Circular Definition of Product ID'
307 );
308 }
309  
310 if ($relationship->relates_to_product_reference eq $relationship->full_product_name->product_id) {
311 $self->add_message(
312 category => 'mandatory',
313 path => "/product_tree/relationships/$rel_idx/full_product_name/product_id",
314 code => '6.1.3',
315 message => 'Circular Definition of Product ID'
316 );
317 }
318  
319 });
320  
321 }
322  
323  
324 # 6.1.4 Missing Definition of Product Group ID
325  
326 sub TEST_6_1_4 {
327  
328 my $self = shift;
329  
330 return unless $self->csaf->vulnerabilities->size;
331  
332 my $group_ids = $CSAF::CACHE->{groups} || {};
333  
334 $self->csaf->vulnerabilities->each(sub {
335  
336 my ($vulnerability, $vuln_idx) = @_;
337  
338 $vulnerability->threats->each(sub {
339  
340 my ($threat, $threat_idx) = @_;
341  
342 foreach my $group_id (@{$threat->group_ids}) {
343  
344 if (!first { $group_id eq $_ } keys %{$group_ids}) {
345  
346 $self->add_message(
347 category => 'mandatory',
348 path => "/vulnerabilities/$vuln_idx/threats/$threat_idx/group_ids",
349 code => '6.1.4',
350 message => 'Missing Definition of Product Group ID'
351 );
352  
353 }
354 }
355  
356 });
357  
358 });
359  
360 }
361  
362  
363 # 6.1.5 Multiple Definition of Product Group ID
364  
365 sub TEST_6_1_5 {
366  
367 my $self = shift;
368  
369 my $check = {};
370  
371 $self->csaf->product_tree->product_groups->each(sub {
372  
373 my ($group, $group_idx) = @_;
374  
375 foreach my $product_id (@{$group->product_ids}) {
376  
377 $check->{$product_id}++;
378  
379 if ($check->{$product_id} > 1) {
380 $self->add_message(
381 category => 'mandatory',
382 path => "/product_tree/product_groups/$group_idx/group_id",
383 code => '6.1.5',
384 message => 'Multiple Definition of Product Group ID'
385 );
386 }
387  
388 }
389  
390 });
391  
392 }
393  
394  
395 # 6.1.6 Contradicting Product Status
396  
397 sub TEST_6_1_6 {
398  
399 my $self = shift;
400  
401 return unless $self->csaf->vulnerabilities->size;
402  
403 $self->csaf->vulnerabilities->each(sub {
404  
405 my ($vulnerability, $vuln_idx) = @_;
406  
407 my $product_status = $vulnerability->product_status;
408  
409 my @affected_group = uniq(
410 @{$product_status->first_affected},
411 @{$product_status->known_affected},
412 @{$product_status->last_affected}
413 );
414  
415 my @not_affected_group = uniq(@{$product_status->known_not_affected});
416 my @fixed_group = uniq(@{$product_status->first_fixed}, @{$product_status->fixed});
417 my @under_investigation_group = uniq(@{$product_status->under_investigation});
418  
419 my @check = (@affected_group, @not_affected_group, @fixed_group, @under_investigation_group);
420  
421 if (duplicates @check) {
422  
423 $self->add_message(
424 category => 'mandatory',
425 path => "/vulnerabilities/$vuln_idx/product_status",
426 code => '6.1.6',
427 message => 'Contradicting Product Status'
428 );
429  
430 }
431  
432 });
433  
434 }
435  
436  
437 # 6.1.7 Multiple Scores with same Version per Product
438  
439 sub TEST_6_1_7 {
440  
441 my $self = shift;
442  
443 $self->csaf->vulnerabilities->each(sub {
444  
445 my ($vulnerability, $vuln_idx) = @_;
446  
447 my $check = {};
448  
449 $vulnerability->scores->each(sub {
450  
451 my ($score, $score_idx) = @_;
452  
453 foreach my $product (@{$score->products}) {
454  
455 $check->{$product}++;
456  
457 if ($check->{$product} > 1) {
458 $self->add_message(
459 category => 'mandatory',
460 path => "/vulnerabilities/$vuln_idx/score/$score_idx/products",
461 code => '6.1.7',
462 message => 'Multiple Scores with same Version per Product'
463 );
464 }
465  
466 }
467 });
468  
469 });
470  
471 }
472  
473  
474 # 6.1.8 Invalid CVSS
475  
476 sub TEST_6_1_8 {
477  
478 # /vulnerabilities[]/scores[]/cvss_v2
479 # /vulnerabilities[]/scores[]/cvss_v3
480  
481 my $self = shift;
482  
483 my $SCHEMAS = {
484 cvss2 => {'$ref' => 'https://www.first.org/cvss/cvss-v2.0.json'},
485 cvss3 => {
486 oneOf => [
487 {'$ref' => 'https://www.first.org/cvss/cvss-v3.0.json'},
488 {'$ref' => 'https://www.first.org/cvss/cvss-v3.1.json'}
489 ]
490 }
491 };
492  
493 $self->csaf->vulnerabilities->each(sub {
494  
495 my ($vulnerability, $vuln_idx) = @_;
496  
497 $vulnerability->scores->each(sub {
498  
499 my ($score, $score_idx) = @_;
500  
501 if (my $cvss_v3 = $score->cvss_v3) {
502  
503 my $v = CSAF::Schema->validator(($cvss_v3->version eq '3.1' ? 'cvss-v3.1' : 'cvss-v3.0'));
504 my @schema_errors = $v->validate($cvss_v3->TO_JSON);
505  
506 foreach my $schema_error (@schema_errors) {
507 $self->add_message(
508 category => 'mandatory',
509 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v3" . $schema_error->path,
510 code => '6.1.8',
511 message => sprintf('Invalid CVSS: %s', $schema_error->message)
512 );
513 }
514  
515 }
516  
517 if (my $cvss_v2 = $score->cvss_v2) {
518  
519 my $v = CSAF::Schema->validator('cvss-v2.0');
520 my @schema_errors = $v->validate($cvss_v2->TO_JSON);
521  
522 foreach my $schema_error (@schema_errors) {
523 $self->add_message(
524 category => 'mandatory',
525 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v2" . $schema_error->path,
526 code => '6.1.8',
527 message => sprintf('Invalid CVSS: %s', $schema_error->message)
528 );
529 }
530  
531 }
532  
533 });
534 });
535  
536 }
537  
538  
539 # 6.1.9 Invalid CVSS computation
540  
541 sub TEST_6_1_9 {
542  
543 my $self = shift;
544  
545 $self->csaf->vulnerabilities->each(sub {
546  
547 my ($vulnerability, $vuln_idx) = @_;
548  
549 $vulnerability->scores->each(sub {
550  
551 my ($score, $score_idx) = @_;
552  
553 if (my $cvss_v2 = $score->cvss_v2) {
554  
555 # /vulnerabilities[]/scores[]/cvss_v2/baseScore
556 # /vulnerabilities[]/scores[]/cvss_v2/temporalScore
557 # /vulnerabilities[]/scores[]/cvss_v2/environmentalScore
558  
559 my $cvss = CVSS->from_vector_string($cvss_v2->vectorString);
560  
561 my @scores = qw(
562 baseScore
563 temporalScore
564 environmentalScore
565 );
566  
567 foreach my $score (@scores) {
568  
569 if (($cvss_v2->$score && $cvss->$score) && ($cvss->$score != $cvss_v2->$score)) {
570 $self->add_message(
571 category => 'mandatory',
572 path => "/vulnerabilities/$vuln_idx/score/$score_idx/cvss_v2",
573 code => '6.1.9',
574 message => 'Invalid CVSS computation'
575 );
576 }
577  
578 }
579  
580 }
581  
582 if (my $cvss_v3 = $score->cvss_v3) {
583  
584 # /vulnerabilities[]/scores[]/cvss_v3/baseScore
585 # /vulnerabilities[]/scores[]/cvss_v3/baseSeverity
586 # /vulnerabilities[]/scores[]/cvss_v3/temporalScore
587 # /vulnerabilities[]/scores[]/cvss_v3/temporalSeverity
588 # /vulnerabilities[]/scores[]/cvss_v3/environmentalScore
589 # /vulnerabilities[]/scores[]/cvss_v3/environmentalSeverity
590  
591 my $cvss = CVSS->from_vector_string($cvss_v3->vectorString);
592  
593 my @scores = (qw[
594 baseScore
595 temporalScore
596 environmentalScore
597 ]);
598  
599 my @severities = qw(
600 baseSeverity
601 temporalSeverity
602 environmentalSeverity
603 );
604  
605 foreach my $score (@scores) {
606  
607 if (($cvss_v3->$score && $cvss->$score) && ($cvss->$score != $cvss_v3->$score)) {
608 $self->add_message(
609 category => 'mandatory',
610 path => "/vulnerabilities/$vuln_idx/score/$score_idx/cvss_v3",
611 code => '6.1.9',
612 message => 'Invalid CVSS computation'
613 );
614 }
615  
616 }
617  
618 foreach my $severity (@severities) {
619  
620 if (($cvss_v3->$severity && $cvss->$severity) && ($cvss->$severity ne $cvss_v3->$severity)) {
621 $self->add_message(
622 category => 'mandatory',
623 path => "/vulnerabilities/$vuln_idx/score/$score_idx/cvss_v3",
624 code => '6.1.9',
625 message => 'Invalid CVSS computation'
626 );
627 }
628  
629 }
630  
631 }
632  
633 });
634 });
635 }
636  
637  
638 # 6.1.10 Inconsistent CVSS
639  
640 sub TEST_6_1_10 {
641  
642 my $self = shift;
643  
644 $self->csaf->vulnerabilities->each(sub {
645  
646 my ($vulnerability, $vuln_idx) = @_;
647  
648 $vulnerability->scores->each(sub {
649  
650 my ($score, $score_idx) = @_;
651  
652 # CVSS 2.0
653  
654 if (my $cvss_v2 = $score->cvss_v2) {
655  
656 my $vector_string = $cvss_v2->vectorString;
657 my $cvss = CVSS->from_vector_string($vector_string);
658  
659 return unless $vector_string;
660  
661 my @metrics = (qw[
662 accessVector
663 accessComplexity
664 authentication
665 confidentialityImpact
666 integrityImpact
667 availabilityImpact
668 exploitability
669 remediationLevel
670 reportConfidence
671 collateralDamagePotential
672 targetDistribution
673 confidentialityRequirement
674 integrityRequirement
675 availabilityRequirement
676 ]);
677  
678 foreach my $metric (@metrics) {
679  
680 my $doc_metric_value = $cvss_v2->$metric;
681  
682 if ($doc_metric_value && $doc_metric_value ne $cvss->$metric) {
683 $self->add_message(
684 category => 'mandatory',
685 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v2/$metric",
686 code => '6.1.10',
687 message => 'Inconsistent CVSS'
688 );
689 }
690 }
691  
692 }
693  
694 # CVSS 3.x
695  
696 if (my $cvss_v3 = $score->cvss_v3) {
697  
698 my $vector_string = $cvss_v3->vectorString;
699 my $cvss = CVSS->from_vector_string($vector_string);
700  
701 return unless $vector_string;
702  
703 my @metrics = (qw[
704 availabilityImpact
705 attackComplexity
706 attackVector
707 confidentialityImpact
708 exploitCodeMaturity
709 integrityImpact
710 privilegesRequired
711 reportConfidence
712 remediationLevel
713 scope
714 userInteraction
715 modifiedAvailabilityImpact
716 modifiedAttackComplexity
717 modifiedAttackVector
718 modifiedConfidentialityImpact
719 modifiedIntegrityImpact
720 modifiedPrivilegesRequired
721 modifiedScope
722 modifiedUserInteraction
723 ]);
724  
725 foreach my $metric (@metrics) {
726  
727 my $doc_metric_value = $cvss_v3->$metric;
728  
729 if ($doc_metric_value && $doc_metric_value ne $cvss->$metric) {
730 $self->add_message(
731 category => 'mandatory',
732 path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v3/$metric",
733 code => '6.1.10',
734 message => 'Inconsistent CVSS'
735 );
736 }
737 }
738  
739 }
740  
741 });
742  
743 });
744  
745 }
746  
747  
748 # 6.1.11 CWE
749  
750 sub TEST_6_1_11 {
751  
752 my $self = shift;
753  
754 $self->csaf->vulnerabilities->each(sub {
755  
756 my ($vulnerability, $vuln_idx) = @_;
757  
758 if (my $cwe_id = $vulnerability->cwe->id) {
759  
760 if (!weakness_exists($cwe_id)) {
761  
762 $self->add_message(
763 category => 'mandatory',
764 path => "/vulnerabilities/$vuln_idx/cwe/id",
765 code => '6.1.11',
766 message => 'Unknown CWE'
767 );
768  
769 }
770  
771 }
772  
773 if (my $cwe_name = $vulnerability->cwe->name) {
774  
775 if (get_weakness_name($vulnerability->cwe->id) ne $cwe_name) {
776  
777 $self->add_message(
778 category => 'mandatory',
779 path => "/vulnerabilities/$vuln_idx/cwe/name",
780 code => '6.1.11',
781 message => 'CWE name differs from the official CWE catalog'
782 );
783  
784 }
785 }
786  
787 });
788  
789 }
790  
791  
792 # 6.1.12 Language
793  
794 sub TEST_6_1_12 { # TODO INCOMPLETE
795  
796 my $self = shift;
797  
798 DEBUG and $self->log->warn('Incomplete Mandatory Test 6.1.12');
799  
800 my $document_lang = $self->csaf->document->lang;
801 my $document_source_lang = $self->csaf->document->source_lang;
802  
803 if ($document_lang && $document_lang !~ /[a-z]{2,3}/) {
804 $self->add_message(
805 category => 'mandatory',
806 path => '/document/lang',
807 code => '6.1.12',
808 message => 'Language code is invalid'
809 );
810 }
811  
812 if ($document_source_lang && $document_source_lang !~ /[a-z]{2,3}/) {
813 $self->add_message(
814 category => 'mandatory',
815 path => '/document/source_lang',
816 code => '6.1.12',
817 message => 'Language code is invalid'
818 );
819 }
820  
821 }
822  
823  
824 # 6.1.13 PURL
825  
826 sub TEST_6_1_13 {
827  
828 my $self = shift;
829  
830 # /product_tree/branches[](/branches[])*/product/product_identification_helper/purl
831  
832 $self->_TEST_6_1_13_branches($self->csaf->product_tree->branches, "/product_tree/branches");
833  
834 # /product_tree/full_product_names[]/product_identification_helper/purl
835  
836 $self->csaf->product_tree->full_product_names->each(sub {
837  
838 my ($full_product_name, $idx) = @_;
839  
840 return unless $full_product_name->product_identification_helper;
841  
842 if (my $purl = $full_product_name->product_identification_helper->purl) {
843 $self->_TEST_6_1_13_check_purl($purl,
844 "/product_tree/full_product_names/$idx/product_identification_helper/purl");
845 }
846  
847 });
848  
849 # /product_tree/relationships[]/full_product_name/product_identification_helper/purl
850  
851 $self->csaf->product_tree->relationships->each(sub {
852  
853 my ($relationship, $rel_idx) = @_;
854  
855 return unless $relationship->full_product_name;
856 return unless $relationship->full_product_name->product_identification_helper;
857  
858 if (my $purl = $relationship->full_product_name->product_identification_helper->purl) {
859 $self->_TEST_6_1_13_check_purl($purl,
860 "/product_tree/relationships/$rel_idx/full_product_name/product_identification_helper/purl");
861 }
862  
863 });
864  
865 }
866  
867  
868 # 6.1.14 Sorted Revision History
869  
870 sub TEST_6_1_14 {
871  
872 my $self = shift;
873  
874 return unless $self->csaf->document->tracking->revision_history->size;
875  
876 # TODO use semver for non-decimal version
877  
878 my $last_rev_version = $self->csaf->document->tracking->revision_history->last->number;
879 my $doc_version = $self->csaf->document->tracking->version;
880  
881 my $revision_dates = {};
882  
883 foreach my $revision (@{$self->csaf->document->tracking->revision_history->items}) {
884 $revision_dates->{$revision->date->epoch} = $revision->number;
885 }
886  
887 my $prev_revision_number = 0;
888  
889 foreach (sort { $a <=> $b } keys %{$revision_dates}) {
890  
891 my $revision_number = $revision_dates->{$_};
892  
893 eval {
894 if ($prev_revision_number && version->parse($prev_revision_number) > version->parse($revision_number)) {
895  
896 return $self->add_message(
897 category => 'mandatory',
898 path => '/document/tracking/revision_history',
899 code => '6.1.14',
900 message => 'Sorted Revision History'
901 );
902  
903 }
904 };
905 $prev_revision_number = $revision_number;
906 }
907  
908 eval {
909  
910 if (version->parse($last_rev_version) > version->parse($doc_version)) {
911  
912 return $self->add_message(
913 category => 'mandatory',
914 path => '/document/tracking/revision_history',
915 code => '6.1.14',
916 message => 'Sorted Revision History'
917 );
918  
919 }
920  
921 };
922  
923 }
924  
925  
926 # 6.1.15 Translator
927  
928 sub TEST_6_1_15 {
929  
930 my $self = shift;
931  
932 if ($self->csaf->document->publisher->category eq 'translator' && !$self->csaf->document->source_lang) {
933  
934 $self->add_message(
935 category => 'mandatory',
936 path => '/document/publisher/category',
937 code => '6.1.15',
938 message => 'Missing "source_lang" for "translator" publisher category'
939 );
940  
941 }
942  
943 }
944  
945  
946 # 6.1.16 Latest Document Version
947  
948 sub TEST_6_1_16 {
949  
950 my $self = shift;
951  
952 my $current_version = 0;
953 my $last_version = undef;
954  
955 # TODO Use semver instead of version module
956 eval {
957  
958 foreach my $revision (@{$self->csaf->document->tracking->revision_history->items}) {
959 $last_version = $revision->number if (version->parse($current_version) < version->parse($revision->number));
960 $current_version = $revision->number;
961 }
962  
963 if (version->parse($last_version) > version->parse($self->csaf->document->tracking->version)) {
964  
965 $self->add_message(
966 category => 'mandatory',
967 path => '/document/tracking/version',
968 code => '6.1.16',
969 message => 'Detected newer revision of document'
970 );
971  
972 }
973  
974 }
975  
976 }
977  
978  
979 # 6.1.17 Document Status Draft
980  
981 sub TEST_6_1_17 {
982  
983 my $self = shift;
984  
985 my $document_version = $self->csaf->document->tracking->version;
986 my $document_status = $self->csaf->document->tracking->status;
987  
988 $document_version =~ /$SEMVER_REGEXP/;
989  
990 if ($document_status ne 'draft' && ($document_version eq '0' || (%+ && ($+{major} == 0 || $+{prerelease})))) {
991 $self->add_message(
992 category => 'mandatory',
993 path => '/document/tracking/version',
994 code => '6.1.17',
995 message => 'Incompatible document status & version'
996 );
997 }
998  
999 }
1000  
1001  
1002 # 6.1.18 Released Revision History
1003  
1004 sub TEST_6_1_18 {
1005  
1006 my $self = shift;
1007  
1008 my $document_status = $self->csaf->document->tracking->status;
1009 my $document_revisions = $self->csaf->document->tracking->revision_history;
1010  
1011 if ($document_status =~ /(final|interim)/) {
1012  
1013 $document_revisions->each(sub {
1014  
1015 my ($revision, $rev_idx) = @_;
1016  
1017 $revision->number =~ /$SEMVER_REGEXP/;
1018  
1019 if ($revision->number eq '0' || (%+ && ($+{major} == 0))) {
1020 $self->add_message(
1021 category => 'mandatory',
1022 path => "/document/tracking/revision_history/$rev_idx/number",
1023 code => '6.1.18',
1024 message => 'Incompatible revision number with document status'
1025 );
1026 }
1027  
1028 });
1029  
1030 }
1031  
1032 }
1033  
1034  
1035 # 6.1.19 Revision History Entries for Pre-release Versions
1036  
1037 sub TEST_6_1_19 {
1038  
1039 my $self = shift;
1040  
1041 my $document_revisions = $self->csaf->document->tracking->revision_history;
1042  
1043 $document_revisions->each(sub {
1044  
1045 my ($revision, $rev_idx) = @_;
1046  
1047 $revision->number =~ /$SEMVER_REGEXP/;
1048  
1049 if (%+ && $+{prerelease}) {
1050 $self->add_message(
1051 category => 'mandatory',
1052 path => "/document/tracking/revision_history/$rev_idx/number",
1053 code => '6.1.19',
1054 message => 'Revision History contains a pre-release'
1055 );
1056 }
1057  
1058 });
1059  
1060 }
1061  
1062  
1063 # 6.1.20 Non-draft Document Version
1064  
1065 sub TEST_6_1_20 {
1066  
1067 my $self = shift;
1068  
1069 my $document_version = $self->csaf->document->tracking->version;
1070 my $document_status = $self->csaf->document->tracking->status;
1071  
1072 if ($document_status =~ /(final|interim)/) {
1073  
1074 $document_version =~ /$SEMVER_REGEXP/;
1075  
1076 if (%+ && $+{prerelease}) {
1077 $self->add_message(
1078 category => 'mandatory',
1079 path => '/document/tracking/version',
1080 code => '6.1.20',
1081 message => qq{Detected a pre-release version with "$document_status" document}
1082 );
1083 }
1084 }
1085  
1086  
1087 }
1088  
1089  
1090 # 6.1.21 Missing Item in Revision History
1091  
1092 sub TEST_6_1_21 {
1093  
1094 my $self = shift;
1095  
1096 my @revision_numbers = ();
1097  
1098 foreach my $revision (@{$self->csaf->document->tracking->revision_history->items}) {
1099 push @revision_numbers, $revision->number if ($revision->number =~ /^(\d+)$/);
1100 }
1101  
1102 my $prev_revision_number = 0;
1103  
1104 foreach my $revision_number (sort @revision_numbers) {
1105 if (($revision_number - $prev_revision_number) > 1) {
1106 return $self->add_message(
1107 category => 'mandatory',
1108 path => '/document/tracking/revision_history',
1109 code => '6.1.21',
1110 message => 'Missing Item in Revision History'
1111 );
1112 }
1113 $prev_revision_number = $revision_number;
1114 }
1115  
1116 }
1117  
1118  
1119 # 6.1.22 Multiple Definition in Revision History
1120  
1121 sub TEST_6_1_22 {
1122  
1123 my $self = shift;
1124  
1125 my $check = {};
1126  
1127 $self->csaf->document->tracking->revision_history->each(sub {
1128  
1129 my ($revision, $rev_idx) = @_;
1130  
1131 $check->{$revision->number}++;
1132  
1133 if ($check->{$revision->number} > 1) {
1134  
1135 $self->add_message(
1136 category => 'mandatory',
1137 path => "/document/tracking/revision_history/$rev_idx/number",
1138 code => '6.1.22',
1139 message => 'Multiple Definition in Revision History'
1140 );
1141  
1142 }
1143  
1144 });
1145  
1146 }
1147  
1148  
1149 # 6.1.23 Multiple Use of Same CVE
1150  
1151 sub TEST_6_1_23 {
1152  
1153 my $self = shift;
1154  
1155 my $check = {};
1156  
1157 $self->csaf->vulnerabilities->each(sub {
1158  
1159 my ($vulnerability, $vuln_idx) = @_;
1160  
1161 return unless $vulnerability->cve;
1162  
1163 $check->{$vulnerability->cve}++;
1164  
1165 if ($check->{$vulnerability->cve} > 1) {
1166 $self->add_message(
1167 category => 'mandatory',
1168 path => "/vulnerabilities/$vuln_idx/cve",
1169 code => '6.1.23',
1170 message => sprintf('Multiple Use of Same CVE (%s)', $vulnerability->cve)
1171 );
1172 }
1173  
1174 });
1175  
1176 }
1177  
1178  
1179 # 6.1.24 Multiple Definition in Involvements
1180  
1181 sub TEST_6_1_24 {
1182  
1183 my $self = shift;
1184  
1185 $self->csaf->vulnerabilities->each(sub {
1186  
1187 my ($vulnerability, $vuln_idx) = @_;
1188  
1189 return if (!$vulnerability->involvements->size);
1190  
1191 my $check = {};
1192  
1193 foreach my $involvement ($vulnerability->involvements->each) {
1194  
1195 next unless ($involvement->date);
1196  
1197 $check->{$involvement->date->epoch} //= {};
1198 $check->{$involvement->date->epoch}->{$involvement->party} //= 0;
1199 $check->{$involvement->date->epoch}->{$involvement->party}++;
1200  
1201 if ($check->{$involvement->date->epoch}->{$involvement->party} > 1) {
1202 return $self->add_message(
1203 category => 'mandatory',
1204 path => "/vulnerabilities/$vuln_idx/involvements",
1205 code => '6.1.24',
1206 message => 'Multiple Definition in Involvements'
1207 );
1208 }
1209 }
1210  
1211 });
1212  
1213 }
1214  
1215  
1216 # 6.1.25 Multiple Use of Same Hash Algorithm
1217  
1218 sub TEST_6_1_25 { # TODO INCOMPLETE
1219  
1220 my $self = shift;
1221  
1222 DEBUG and $self->log->warn('Incomplete Mandatory Test 6.1.25');
1223  
1224 # /product_tree/branches[](/branches[])*/product/product_identification_helper/hashes[]/file_hashes
1225  
1226 $self->_TEST_6_1_25_branches($self->csaf->product_tree->branches, '/product_tree/branches');
1227  
1228 # /product_tree/relationships[]/full_product_name/product_identification_helper/hashes[]/file_hashes
1229  
1230 # TODO INCOMPLETE TEST
1231  
1232 # /product_tree/full_product_names[]/product_identification_helper/hashes[]/file_hashes
1233  
1234 my $full_product_names = $self->csaf->product_tree->full_product_names;
1235  
1236 $full_product_names->each(sub {
1237  
1238 my ($full_product_name, $idx) = @_;
1239  
1240 return unless $full_product_name->product_identification_helper;
1241  
1242 $full_product_name->product_identification_helper->hashes->each(sub {
1243  
1244 my ($hash, $hash_idx) = @_;
1245  
1246 my $check = {};
1247  
1248 $hash->file_hashes->each(sub {
1249  
1250 my ($file_hash, $file_hash_idx) = @_;
1251  
1252 $check->{$file_hash->algorithm}++;
1253  
1254 if ($check->{$file_hash->algorithm} > 1) {
1255  
1256 my $path = "/product_tree/full_product_names/$idx/product_identification_helper"
1257 . "/hashes/$hash_idx/file_hashes/$file_hash_idx/";
1258  
1259 $self->add_message(
1260 category => 'mandatory',
1261 path => $path,
1262 code => '6.1.25',
1263 message => sprintf('Multiple Use of Same Hash Algorithm (%s)', $file_hash->algorithm)
1264 );
1265  
1266 }
1267  
1268 });
1269  
1270 });
1271  
1272 });
1273  
1274 }
1275  
1276  
1277 # 6.1.26 Prohibited Document Category Name
1278  
1279 sub TEST_6_1_26 {
1280  
1281 my $self = shift;
1282  
1283 my $document_category = $self->csaf->document->category;
1284  
1285 if ($document_category
1286 !~ /(csaf_base|csaf_security_incident_response|csaf_informational_advisory|csaf_security_advisory|csaf_vex)/)
1287 {
1288  
1289 if ($document_category =~ /^csaf_/i) {
1290 $self->add_message(
1291 category => 'mandatory',
1292 path => '/document/category',
1293 code => '6.1.26',
1294 message => 'Reserved CSAF document category prefix'
1295 );
1296 }
1297  
1298 my $check_similar_category = 0;
1299  
1300 my @similar_categories = qw(
1301 informationaladvisory
1302 securityincidentresponse
1303 securityadvisory
1304 vex
1305 );
1306  
1307 (my $normalized_category = lc $document_category) =~ s/[-_\s]//g;
1308  
1309 if (first { $normalized_category =~ /^$_/ } @similar_categories) {
1310 $self->add_message(
1311 category => 'mandatory',
1312 path => '/document/category',
1313 code => '6.1.26',
1314 message => 'Prohibited document category'
1315 );
1316 }
1317  
1318 }
1319 }
1320  
1321  
1322 # 6.1.27.1 Document Notes
1323  
1324 sub TEST_6_1_27_1 {
1325  
1326 my $self = shift;
1327  
1328 my $document_category = $self->csaf->document->category;
1329 my $document_notes = $self->csaf->document->notes->items;
1330  
1331 return if (not $document_category =~ /(csaf_informational_advisory|csaf_security_incident_response)/);
1332  
1333 my $have_valid_category = undef;
1334  
1335 foreach my $note (@{$document_notes}) {
1336 foreach my $category (qw(description details general summary)) {
1337 $have_valid_category = 1 if ($note->category eq $category);
1338 }
1339 }
1340  
1341 if (not $have_valid_category) {
1342 $self->add_message(
1343 category => 'mandatory',
1344 path => '/document/notes',
1345 code => '6.1.27.1',
1346 message =>
1347 'The document notes do not contain an item which has a category of "description", "details", "general" or "summary"'
1348 );
1349 }
1350  
1351 }
1352  
1353  
1354 # 6.1.27.2 Document References
1355  
1356 sub TEST_6_1_27_2 {
1357  
1358 my $self = shift;
1359  
1360 my $document_category = $self->csaf->document->category;
1361 my $document_references = $self->csaf->document->references->items;
1362  
1363 return if (not $document_category =~ /(csaf_informational_advisory|csaf_security_incident_response)/);
1364  
1365 my $have_external_references = undef;
1366  
1367 foreach my $reference (@{$document_references}) {
1368 $have_external_references = 1 if ($reference->category eq 'external');
1369 }
1370  
1371 if (not $have_external_references) {
1372 $self->add_message(
1373 category => 'mandatory',
1374 path => '/document/references',
1375 code => '6.1.27.2',
1376 message => 'The document references do not contain any item which has the category "external"'
1377 );
1378 }
1379  
1380 }
1381  
1382  
1383 # 6.1.27.3 Vulnerabilities
1384  
1385 sub TEST_6_1_27_3 {
1386  
1387 my $self = shift;
1388  
1389 if ($self->csaf->document->category eq 'csaf_informational_advisory' && @{$self->csaf->vulnerabilities->items}) {
1390  
1391 $self->add_message(
1392 category => 'mandatory',
1393 path => '/vulnerabilities',
1394 code => '6.1.27.3',
1395 message =>
1396 'The "csaf_informational_advisory" profile deals with information that are not classified as vulnerabilities. Therefore, it must not have the "/vulnerabilities" element'
1397 );
1398  
1399 }
1400  
1401 }
1402  
1403  
1404 # 6.1.27.4 Product Tree
1405  
1406 sub TEST_6_1_27_4 {
1407  
1408 my $self = shift;
1409  
1410 my $document_category = $self->csaf->document->category;
1411 my $product_tree = $self->csaf->product_tree->TO_CSAF; # TODO !?
1412  
1413 if ($document_category =~ /(csaf_security_advisory|csaf_vex)/ && !$product_tree) {
1414  
1415 $self->add_message(
1416 category => 'mandatory',
1417 path => '/product_tree',
1418 code => '6.1.27.4',
1419 message => 'The element "/product_tree" does not exist'
1420 );
1421  
1422 }
1423  
1424 }
1425  
1426  
1427 # 6.1.27.5 Vulnerability Notes
1428  
1429 sub TEST_6_1_27_5 {
1430  
1431 my $self = shift;
1432  
1433 return if (not $self->csaf->document->category =~ /(csaf_security_advisory|csaf_vex)/);
1434  
1435 $self->csaf->vulnerabilities->each(sub {
1436  
1437 my ($vulnerability, $vuln_idx) = @_;
1438  
1439 if (!$vulnerability->notes->size) {
1440 $self->add_message(
1441 category => 'mandatory',
1442 path => "/vulnerabilities/$vuln_idx",
1443 code => '6.1.27.5',
1444 message => 'The vulnerability item has no "notes" element'
1445 );
1446 }
1447  
1448 });
1449  
1450 }
1451  
1452  
1453 # 6.1.27.6 Product Status
1454  
1455 sub TEST_6_1_27_6 {
1456  
1457 my $self = shift;
1458  
1459 return if (not $self->csaf->document->category eq 'csaf_security_advisory');
1460  
1461 $self->csaf->vulnerabilities->each(sub {
1462  
1463 my ($vulnerability, $vuln_idx) = @_;
1464  
1465 if (!$vulnerability->product_status->TO_CSAF) {
1466 $self->add_message(
1467 category => 'mandatory',
1468 path => "/vulnerabilities/$vuln_idx",
1469 code => '6.1.27.6',
1470 message => 'The vulnerability item has no "product_status" element'
1471 );
1472 }
1473  
1474 });
1475  
1476 }
1477  
1478  
1479 # 6.1.27.7 VEX Product Status
1480  
1481 sub TEST_6_1_27_7 {
1482  
1483 my $self = shift;
1484  
1485 return if (not $self->csaf->document->category eq 'csaf_vex');
1486  
1487 $self->csaf->vulnerabilities->each(sub {
1488  
1489 my ($vulnerability, $vuln_idx) = @_;
1490  
1491 my @check = (
1492 @{$vulnerability->product_status->fixed},
1493 @{$vulnerability->product_status->known_affected},
1494 @{$vulnerability->product_status->known_not_affected},
1495 @{$vulnerability->product_status->under_investigation}
1496 );
1497  
1498 unless (@check) {
1499 $self->add_message(
1500 category => 'mandatory',
1501 path => "/vulnerabilities/$vuln_idx/product_status",
1502 code => '6.1.27.7',
1503 message =>
1504 'None of the elements "fixed", "known_affected", "known_not_affected", or "under_investigation" is present in "product_status"'
1505 );
1506 }
1507  
1508 });
1509  
1510  
1511 }
1512  
1513  
1514 # 6.1.27.8 Vulnerability ID
1515  
1516 sub TEST_6_1_27_8 {
1517  
1518 my $self = shift;
1519  
1520 return if (not $self->csaf->document->category eq 'csaf_vex');
1521  
1522 $self->csaf->vulnerabilities->each(sub {
1523  
1524 my ($vulnerability, $vuln_idx) = @_;
1525  
1526 if (!$vulnerability->cve && $vulnerability->ids->size == 0) {
1527 $self->add_message(
1528 category => 'mandatory',
1529 path => "/vulnerabilities/$vuln_idx",
1530 code => '6.1.27.8',
1531 message => 'None of the elements "cve" or "ids" is present'
1532 );
1533 }
1534  
1535 });
1536  
1537 }
1538  
1539  
1540 # 6.1.27.9 Impact Statement
1541  
1542 sub TEST_6_1_27_9 {
1543  
1544 my $self = shift;
1545  
1546 return if (not $self->csaf->document->category eq 'csaf_vex');
1547 return unless $self->csaf->vulnerabilities->size;
1548  
1549 my $product_ids = $CSAF::CACHE->{products} || {};
1550  
1551 $self->csaf->vulnerabilities->each(sub {
1552  
1553 my ($vulnerability, $vuln_idx) = @_;
1554  
1555 return unless @{$vulnerability->product_status->known_not_affected};
1556  
1557 my @status_product_ids = @{$vulnerability->product_status->known_not_affected};
1558  
1559 CSAF::Util::List->new(@status_product_ids)->each(sub {
1560  
1561 my ($product_id, $product_id_idx) = @_;
1562  
1563 my $threat_test = 0;
1564 my $flag_test = 0;
1565  
1566 $vulnerability->threats->grep(sub { $_->category eq 'impact' })->each(sub {
1567  
1568 my ($threat, $threat_idx) = @_;
1569  
1570 if (first { $product_id eq $_ } @{$threat->product_ids}) {
1571 $threat_test = 1;
1572 return;
1573 }
1574  
1575 foreach my $group_id (@{$threat->group_ids}) {
1576  
1577 if (product_in_group_exists($self->csaf, $product_id, $group_id)) {
1578 $threat_test = 1;
1579 return;
1580 }
1581  
1582 }
1583  
1584 $threat_test = 0;
1585  
1586 });
1587  
1588 $vulnerability->flags->each(sub {
1589  
1590 my ($flag, $flag_idx) = @_;
1591  
1592 if (first { $product_id eq $_ } @{$flag->product_ids}) {
1593 $flag_test = 1;
1594 return;
1595 }
1596  
1597 foreach my $group_id (@{$flag->group_ids}) {
1598  
1599 if (product_in_group_exists($self->csaf, $product_id, $group_id)) {
1600 $flag_test = 1;
1601 return;
1602 }
1603  
1604 }
1605  
1606 $flag_test = 0;
1607  
1608 });
1609  
1610 if (!$flag_test && !$threat_test) {
1611 $self->add_message(
1612 category => 'mandatory',
1613 path =>
1614 sprintf('/vulnerabilities/%s/product_status/known_not_affected/%s', $vuln_idx, $product_id_idx),
1615 code => '6.1.27.9',
1616 message => 'Impact Statement'
1617 );
1618 }
1619  
1620 });
1621  
1622 });
1623  
1624 }
1625  
1626  
1627 # 6.1.27.10 Action Statement
1628  
1629 sub TEST_6_1_27_10 {
1630  
1631 my $self = shift;
1632  
1633 return if (not $self->csaf->document->category eq 'csaf_vex');
1634 return unless $self->csaf->vulnerabilities->size;
1635  
1636 my $product_ids = $CSAF::CACHE->{products} || {};
1637  
1638 $self->csaf->vulnerabilities->each(sub {
1639  
1640 my ($vulnerability, $vuln_idx) = @_;
1641  
1642 return unless @{$vulnerability->product_status->known_affected};
1643  
1644 my @status_product_ids = @{$vulnerability->product_status->known_affected};
1645  
1646 CSAF::Util::List->new(@status_product_ids)->each(sub {
1647  
1648 my ($product_id, $product_id_idx) = @_;
1649  
1650 my $threat_test = 0;
1651  
1652 $vulnerability->remediations->each(sub {
1653  
1654 my ($remediation, $remediation_idx) = @_;
1655  
1656 if (first { $product_id eq $_ } @{$remediation->product_ids}) {
1657 $threat_test = 1;
1658 return;
1659 }
1660  
1661 foreach my $group_id (@{$remediation->group_ids}) {
1662  
1663 if (product_in_group_exists($self->csaf, $product_id, $group_id)) {
1664 $threat_test = 1;
1665 return;
1666 }
1667  
1668 }
1669  
1670 $threat_test = 0;
1671  
1672 });
1673  
1674 if (!$threat_test) {
1675 $self->add_message(
1676 category => 'mandatory',
1677 path => "/vulnerabilities/$vuln_idx/product_status/known_not_affected/$product_id_idx",
1678 code => '6.1.27.10',
1679 message => 'Action Statement'
1680 );
1681 }
1682  
1683 });
1684  
1685 });
1686  
1687 }
1688  
1689  
1690 # 6.1.27.11 Vulnerabilities
1691  
1692 sub TEST_6_1_27_11 {
1693  
1694 my $self = shift;
1695  
1696 if ( $self->csaf->document->category =~ /(csaf_security_advisory|csaf_vex)/
1697 && $self->csaf->vulnerabilities->size == 0)
1698 {
1699  
1700 $self->add_message(
1701 category => 'mandatory',
1702 path => '/vulnerabilities',
1703 code => '6.1.27.11',
1704 message => 'The element "/vulnerabilities" does not exist'
1705 );
1706  
1707 }
1708  
1709 }
1710  
1711  
1712 # 6.1.28 Translation
1713  
1714 sub TEST_6_1_28 {
1715  
1716 my $self = shift;
1717  
1718 my $document_lang = $self->csaf->document->lang;
1719 my $document_source_lang = $self->csaf->document->source_lang;
1720  
1721 if ($document_lang && $document_source_lang && ($document_lang eq $document_source_lang)) {
1722 $self->add_message(
1723 category => 'mandatory',
1724 path => '/document/lang',
1725 code => '6.1.28',
1726 message => qq{The document language and the source language have the same value "$document_lang"}
1727 );
1728 }
1729  
1730 }
1731  
1732  
1733 # 6.1.29 Remediation without Product Reference
1734  
1735 sub TEST_6_1_29 {
1736  
1737 my $self = shift;
1738  
1739 return unless $self->csaf->vulnerabilities->size;
1740  
1741 my $product_ids = $CSAF::CACHE->{products} || {};
1742  
1743 $self->csaf->vulnerabilities->each(sub {
1744  
1745 my ($vulnerability, $vuln_idx) = @_;
1746  
1747 return unless $vulnerability->remediations->size;
1748  
1749 $vulnerability->remediations->each(sub {
1750  
1751 my ($remediation, $remediation_idx) = @_;
1752  
1753 if (!@{$remediation->product_ids}) {
1754 return $self->add_message(
1755 category => 'mandatory',
1756 path => "/vulnerabilities/$vuln_idx/remediations/$remediation_idx",
1757 code => '6.1.29',
1758 message => 'Remediation without Product Reference'
1759 );
1760 }
1761  
1762 foreach my $product_id (@{$remediation->product_ids}) {
1763 if (!first { $product_id eq $_ } keys %{$product_ids}) {
1764 return $self->add_message(
1765 category => 'mandatory',
1766 path => "/vulnerabilities/$vuln_idx/remediations/$remediation_idx",
1767 code => '6.1.29',
1768 message => 'Remediation without Product Reference'
1769 );
1770 }
1771 }
1772  
1773 });
1774  
1775 });
1776  
1777 }
1778  
1779  
1780 # 6.1.30 Mixed Integer and Semantic Versioning
1781  
1782 sub TEST_6_1_30 {
1783  
1784 my $self = shift;
1785  
1786 my $document_version = $self->csaf->document->tracking->version;
1787 my $document_revisions = $self->csaf->document->tracking->revision_history;
1788  
1789 my $revision_ver_in_semver = 0;
1790 my $revision_ver_in_int = 0;
1791  
1792 my $document_ver_in_semver = 0;
1793 my $document_ver_in_int = 0;
1794  
1795 $document_revisions->each(sub {
1796  
1797 my ($revision, $rev_idx) = @_;
1798  
1799 if ($revision->number =~ /$SEMVER_REGEXP/) {
1800 $revision_ver_in_semver++;
1801 }
1802 else {
1803 $revision_ver_in_int++;
1804 }
1805  
1806 });
1807  
1808 if ($document_version =~ /$SEMVER_REGEXP/) {
1809 $document_ver_in_semver++;
1810 }
1811 else {
1812 $document_ver_in_int++;
1813 }
1814  
1815 if ($document_ver_in_int && $revision_ver_in_semver || $document_ver_in_semver && $revision_ver_in_int) {
1816 $self->add_message(
1817 category => 'mandatory',
1818 path => "/document/tracking/version",
1819 code => '6.1.30',
1820 message => 'Mixed Integer and Semantic Versioning'
1821 );
1822 }
1823  
1824 }
1825  
1826  
1827 # 6.1.31 Version Range in Product Version
1828  
1829 sub TEST_6_1_31 {
1830  
1831 my $self = shift;
1832  
1833 return if (not $self->csaf->product_tree);
1834  
1835 $self->_TEST_6_1_31_branches($self->csaf->product_tree->branches, "/product_tree/branches");
1836  
1837 }
1838  
1839  
1840 # 6.1.32 Flag without Product Reference
1841  
1842 sub TEST_6_1_32 {
1843  
1844 my $self = shift;
1845  
1846 return unless $self->csaf->vulnerabilities->size;
1847  
1848 my $product_ids = $CSAF::CACHE->{products} || {};
1849  
1850 $self->csaf->vulnerabilities->each(sub {
1851  
1852 my ($vulnerability, $vuln_idx) = @_;
1853  
1854 return unless $vulnerability->flags->size;
1855  
1856 $vulnerability->flags->each(sub {
1857  
1858 my ($flag, $flag_idx) = @_;
1859  
1860 if (!@{$flag->group_ids} && !@{$flag->product_ids}) {
1861 return $self->add_message(
1862 category => 'mandatory',
1863 path => "/vulnerabilities/$vuln_idx/flags/$flag_idx",
1864 code => '6.1.32',
1865 message => 'Flag without Product Reference'
1866 );
1867 }
1868  
1869 foreach my $product (keys %{$product_ids}) {
1870  
1871 if (@{$flag->product_ids} && !first { $product eq $_ } @{$flag->product_ids}) {
1872 return $self->add_message(
1873 category => 'mandatory',
1874 path => "/vulnerabilities/$vuln_idx/flags/$flag_idx",
1875 code => '6.1.32',
1876 message => 'Flag without Product Reference'
1877 );
1878 }
1879  
1880 if (@{$flag->group_ids} && !first { $product eq $_ } @{$flag->group_ids}) {
1881 return $self->add_message(
1882 category => 'mandatory',
1883 path => "/vulnerabilities/$vuln_idx/flags/$flag_idx",
1884 code => '6.1.32',
1885 message => 'Flag without Product Reference'
1886 );
1887 }
1888 }
1889  
1890 });
1891  
1892 });
1893  
1894 }
1895  
1896  
1897 # 6.1.33 Multiple Flags with VEX Justification Codes per Product
1898  
1899 sub TEST_6_1_33 {
1900  
1901 my $self = shift;
1902  
1903 return unless $self->csaf->vulnerabilities->size;
1904  
1905 $self->csaf->vulnerabilities->each(sub {
1906  
1907 my ($vulnerability, $vuln_idx) = @_;
1908  
1909 my @product_ids = ();
1910  
1911 $vulnerability->flags->each(sub {
1912  
1913 my ($flag, $flag_idx) = @_;
1914  
1915 foreach my $product_id (@{$flag->product_ids}) {
1916  
1917 if (first { $product_id eq $_ } @product_ids) {
1918 return $self->add_message(
1919 category => 'mandatory',
1920 path => "/vulnerabilities/$vuln_idx/flags/$flag_idx",
1921 code => '6.1.33',
1922 message => 'Multiple Flags with VEX Justification Codes per Product'
1923 );
1924 }
1925  
1926 push @product_ids, $product_id;
1927  
1928 }
1929  
1930 foreach my $group_id (@{$flag->group_ids}) {
1931  
1932 $self->csaf->product_tree->product_groups->each(sub {
1933  
1934 my ($group) = @_;
1935  
1936 if ($group->group_id eq $group_id) {
1937  
1938 foreach my $product_id (@{$group->product_ids}) {
1939  
1940 if (first { $product_id eq $_ } @product_ids) {
1941 return $self->add_message(
1942 category => 'mandatory',
1943 path => "/vulnerabilities/$vuln_idx/flags/$flag_idx",
1944 code => '6.1.33',
1945 message => 'Multiple Flags with VEX Justification Codes per Product'
1946 );
1947 }
1948  
1949 push @product_ids, $product_id;
1950  
1951 }
1952  
1953 }
1954  
1955 });
1956  
1957 }
1958  
1959 });
1960  
1961 });
1962  
1963 }
1964  
1965  
1966 sub _TEST_6_1_13_branches {
1967  
1968 my ($self, $branches, $path) = @_;
1969  
1970 $branches->each(sub {
1971  
1972 my ($branch, $branch_idx) = @_;
1973  
1974 $self->_TEST_6_1_13_branches($branch->branches, "$path/$branch_idx/branches");
1975  
1976 return unless $branch->product;
1977 return unless $branch->product->product_identification_helper;
1978 return unless $branch->product->product_identification_helper->purl;
1979  
1980 $self->_TEST_6_1_13_check_purl($branch->product->product_identification_helper->purl,
1981 "$path/$branch_idx/product");
1982  
1983 });
1984  
1985 }
1986  
1987 sub _TEST_6_1_13_check_purl {
1988  
1989 my ($self, $purl, $path) = @_;
1990  
1991 my $is_invalid = 0;
1992  
1993 $is_invalid = 1 if $purl !~ /$PURL_REGEX/;
1994  
1995 eval { URI::PackageURL->from_string($purl) };
1996  
1997 if ($@) {
1998 $is_invalid = 1 if $@;
1999 DEBUG and $self->log->error($@);
2000 }
2001  
2002 if ($is_invalid) {
2003 $self->add_message(category => 'mandatory', path => $path, code => '6.1.13', message => 'Invalid purl');
2004 }
2005  
2006 }
2007  
2008 sub _TEST_6_1_25_branches {
2009  
2010 my ($self, $branches, $path) = @_;
2011  
2012 $branches->each(sub {
2013  
2014 my ($branch, $branch_idx) = @_;
2015  
2016 $self->_TEST_6_1_25_branches($branch->branches, "$path/$branch_idx/branches");
2017  
2018 if ( $branch->product
2019 && $branch->product->product_identification_helper
2020 && $branch->product->product_identification_helper->hashes->size)
2021 {
2022  
2023 $branch->product->product_identification_helper->hashes->each(sub {
2024  
2025 my ($hash, $hash_idx) = @_;
2026  
2027 my $check = {};
2028  
2029 $hash->file_hashes->each(sub {
2030  
2031 my ($file_hash, $file_hash_idx) = @_;
2032  
2033 $check->{$file_hash->algorithm}++;
2034  
2035 if ($check->{$file_hash->algorithm} > 1) {
2036  
2037 $self->add_message(
2038 type => 'Mandatory Test',
2039 path =>
2040 "/$path/$branch_idx/product_identification_helper/hashes/$hash_idx/file_hashes/$file_hash_idx/",
2041 code => '6.1.25',
2042 message => sprintf('Multiple Use of Same Hash Algorithm (%s)', $file_hash->algorithm)
2043 );
2044  
2045 }
2046  
2047 });
2048  
2049 });
2050  
2051 }
2052  
2053 });
2054 }
2055  
2056 sub _TEST_6_1_31_branches {
2057  
2058 my ($self, $branches, $path) = @_;
2059  
2060 my @bad_ranges = qw( < <= > >= );
2061 my @bad_words = qw( after all before earlier later prior versions );
2062  
2063 $branches->each(sub {
2064  
2065 my ($branch, $idx) = @_;
2066  
2067 $self->_TEST_6_1_31_branches($branch->branches, "$path/$idx/branches");
2068  
2069 if ($branch->category eq 'product_version') {
2070  
2071 my @branch_name_parts = split /\s/, lc $branch->name;
2072  
2073 foreach my $word (@bad_words) {
2074  
2075 if (first { $word eq $_ } @branch_name_parts) {
2076 $self->add_message(
2077 category => 'mandatory',
2078 path => "$path/name",
2079 code => '6.1.31',
2080 message => 'Version Range in Product Version'
2081 );
2082 }
2083 }
2084  
2085 foreach my $range (@bad_ranges) {
2086 if (lc $branch->name =~ /$range/) {
2087 $self->add_message(
2088 category => 'mandatory',
2089 path => "$path/name",
2090 code => '6.1.31',
2091 message => 'Version Range in Product Version'
2092 );
2093 }
2094 }
2095 }
2096  
2097 });
2098 }
2099  
2100 1;
2101  
2102 __END__
« CSAF/Type/Tracking.pm ↑ Summary CSAF/Util.pm »