| 1 | package CSAF::Validator::InformativeTests; | ||
| 2 | |||
| 3 | use 5.010001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | use version; | ||
| 8 | |||
| 9 | use List::Util qw(first); | ||
| 10 | use CSAF::Util qw(uniq); | ||
| 11 | |||
| 12 | use Moo; | ||
| 13 | extends 'CSAF::Validator::Base'; | ||
| 14 | |||
| 15 | use constant DEBUG => $ENV{CSAF_DEBUG}; | ||
| 16 | |||
| 17 | has tests => ( | ||
| 18 | is => 'ro', | ||
| 19 | default => | ||
| 20 | sub { ['6.3.1', '6.3.2', '6.3.3', '6.3.4', '6.3.5', '6.3.6', '6.3.7', '6.3.8', '6.3.9', '6.3.10', '6.3.11'] } | ||
| 21 | ); | ||
| 22 | |||
| 23 | my $VERS_REGEXP = qr{^vers:[a-z\\.\\-\\+][a-z0-9\\.\\-\\+]*/.+}; | ||
| 24 | |||
| 25 | |||
| 26 | # 6.3.1 Use of CVSS v2 as the only Scoring System | ||
| 27 | |||
| 28 | sub TEST_6_3_1 { | ||
| 29 | |||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | $self->csaf->vulnerabilities->each(sub { | ||
| 33 | |||
| 34 | my ($vulnerability, $vuln_idx) = @_; | ||
| 35 | |||
| 36 | $vulnerability->scores->each(sub { | ||
| 37 | |||
| 38 | my ($score, $score_idx) = @_; | ||
| 39 | |||
| 40 | if ($score->cvss_v2 && !$score->cvss_v3) { | ||
| 41 | $self->add_message( | ||
| 42 | type => 'info', | ||
| 43 | category => 'informative', | ||
| 44 | path => "/vulnerabilities/$vuln_idx/scores/$score_idx", | ||
| 45 | code => '6.3.1', | ||
| 46 | message => 'Use of CVSS v2 as the only Scoring System' | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | }); | ||
| 51 | |||
| 52 | }); | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | # 6.3.2 Use of CVSS v3.0 | ||
| 58 | |||
| 59 | sub TEST_6_3_2 { | ||
| 60 | |||
| 61 | my $self = shift; | ||
| 62 | |||
| 63 | $self->csaf->vulnerabilities->each(sub { | ||
| 64 | |||
| 65 | my ($vulnerability, $vuln_idx) = @_; | ||
| 66 | |||
| 67 | $vulnerability->scores->each(sub { | ||
| 68 | |||
| 69 | my ($score, $score_idx) = @_; | ||
| 70 | |||
| 71 | if ($score->cvss_v3 && $score->cvss_v3->version eq '3.0') { | ||
| 72 | $self->add_message( | ||
| 73 | type => 'info', | ||
| 74 | category => 'informative', | ||
| 75 | path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v3/version", | ||
| 76 | code => '6.3.2', | ||
| 77 | message => 'Use of CVSS v3.0' | ||
| 78 | ); | ||
| 79 | } | ||
| 80 | |||
| 81 | if ($score->cvss_v3 && $score->cvss_v3->vectorString =~ /^CVSS\:3.0/) { | ||
| 82 | $self->add_message( | ||
| 83 | type => 'info', | ||
| 84 | category => 'informative', | ||
| 85 | path => "/vulnerabilities/$vuln_idx/scores/$score_idx/cvss_v3/vectorString", | ||
| 86 | code => '6.3.2', | ||
| 87 | message => 'Use of CVSS v3.0' | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | }); | ||
| 92 | |||
| 93 | }); | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | # 6.3.3 Missing CVE | ||
| 99 | |||
| 100 | sub TEST_6_3_3 { | ||
| 101 | |||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | $self->csaf->vulnerabilities->each(sub { | ||
| 105 | |||
| 106 | my ($vulnerability, $vuln_idx) = @_; | ||
| 107 | |||
| 108 | if (!$vulnerability->cve) { | ||
| 109 | $self->add_message( | ||
| 110 | type => 'info', | ||
| 111 | category => 'informative', | ||
| 112 | path => "/vulnerabilities/$vuln_idx/cve", | ||
| 113 | code => '6.3.3', | ||
| 114 | message => 'Missing CVE' | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | |||
| 118 | }); | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | # 6.3.4 Missing CWE | ||
| 124 | |||
| 125 | sub TEST_6_3_4 { | ||
| 126 | |||
| 127 | my $self = shift; | ||
| 128 | |||
| 129 | $self->csaf->vulnerabilities->each(sub { | ||
| 130 | |||
| 131 | my ($vulnerability, $vuln_idx) = @_; | ||
| 132 | |||
| 133 | if (!$vulnerability->cwe->id) { | ||
| 134 | $self->add_message( | ||
| 135 | type => 'info', | ||
| 136 | category => 'informative', | ||
| 137 | path => "/vulnerabilities/$vuln_idx/cwe", | ||
| 138 | code => '6.3.4', | ||
| 139 | message => 'Missing CWE' | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | }); | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | # 6.3.5 Use of Short Hash | ||
| 149 | |||
| 150 | sub TEST_6_3_5 { | ||
| 151 | |||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | # /product_tree/branches[](/branches[])*/product/product_identification_helper/hashes[]/file_hashes[]/value | ||
| 155 | |||
| 156 | $self->_TEST_6_3_5_branches($self->csaf->product_tree->branches, "/product_tree/branches"); | ||
| 157 | |||
| 158 | # /product_tree/full_product_names[]/product_identification_helper/hashes[]/file_hashes[]/value | ||
| 159 | |||
| 160 | $self->csaf->product_tree->full_product_names->each(sub { | ||
| 161 | |||
| 162 | my ($full_product_name, $full_product_name_idx) = @_; | ||
| 163 | |||
| 164 | return unless $full_product_name->product_identification_helper; | ||
| 165 | |||
| 166 | $self->_TEST_6_3_5_product_identification_helper( | ||
| 167 | $full_product_name->product_identification_helper, | ||
| 168 | "/product_tree/full_product_names/$full_product_name_idx" | ||
| 169 | ); | ||
| 170 | |||
| 171 | }); | ||
| 172 | |||
| 173 | # /product_tree/relationships[]/full_product_name/product_identification_helper/hashes[]/file_hashes[]/value | ||
| 174 | |||
| 175 | $self->csaf->product_tree->relationships->each(sub { | ||
| 176 | |||
| 177 | my ($relationship, $rel_idx) = @_; | ||
| 178 | |||
| 179 | return unless $relationship->full_product_name; | ||
| 180 | return unless $relationship->full_product_name->product_identification_helper; | ||
| 181 | |||
| 182 | $self->_TEST_6_3_5_product_identification_helper( | ||
| 183 | $relationship->full_product_name->product_identification_helper, | ||
| 184 | "/product_tree/relationships/$rel_idx"); | ||
| 185 | |||
| 186 | }); | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | # 6.3.6 Use of non-self referencing URLs Failing to Resolve | ||
| 192 | |||
| 193 | sub TEST_6_3_6 { } | ||
| 194 | |||
| 195 | |||
| 196 | # 6.3.7 Use of self referencing URLs Failing to Resolve | ||
| 197 | sub TEST_6_3_7 { } | ||
| 198 | |||
| 199 | |||
| 200 | # 6.3.8 Spell check | ||
| 201 | |||
| 202 | sub TEST_6_3_8 { } | ||
| 203 | |||
| 204 | |||
| 205 | # 6.3.9 Branch Categories | ||
| 206 | |||
| 207 | sub TEST_6_3_9 { | ||
| 208 | |||
| 209 | my $self = shift; | ||
| 210 | |||
| 211 | return if (not $self->csaf->product_tree); | ||
| 212 | |||
| 213 | $self->_TEST_6_3_9_branches($self->csaf->product_tree->branches, "/product_tree/branches", []); | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # 6.3.10 Usage of Product Version Range | ||
| 219 | |||
| 220 | sub TEST_6_3_10 { | ||
| 221 | |||
| 222 | |||
| 223 | my $self = shift; | ||
| 224 | |||
| 225 | return if (not $self->csaf->product_tree); | ||
| 226 | |||
| 227 | $self->_TEST_6_3_10_branches($self->csaf->product_tree->branches, "/product_tree/branches"); | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | # 6.3.11 Usage of V as Version Indicator | ||
| 233 | |||
| 234 | sub TEST_6_3_11 { | ||
| 235 | |||
| 236 | my $self = shift; | ||
| 237 | |||
| 238 | return if (not $self->csaf->product_tree); | ||
| 239 | |||
| 240 | $self->_TEST_6_3_11_branches($self->csaf->product_tree->branches, "/product_tree/branches"); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub _TEST_6_3_5_branches { | ||
| 246 | |||
| 247 | my ($self, $branches, $path) = @_; | ||
| 248 | |||
| 249 | $branches->each(sub { | ||
| 250 | |||
| 251 | my ($branch, $branch_idx) = @_; | ||
| 252 | |||
| 253 | $self->_TEST_6_3_5_branches($branch->branches, "$path/$branch_idx/branches"); | ||
| 254 | |||
| 255 | return unless $branch->product; | ||
| 256 | return unless $branch->product->product_identification_helper; | ||
| 257 | |||
| 258 | $self->_TEST_6_3_5_product_identification_helper($branch->product->product_identification_helper, | ||
| 259 | "$path/branch/$branch_idx/product"); | ||
| 260 | |||
| 261 | }); | ||
| 262 | |||
| 263 | |||
| 264 | } | ||
| 265 | |||
| 266 | sub _TEST_6_3_5_product_identification_helper { | ||
| 267 | |||
| 268 | my ($self, $product_identification_helper, $path) = @_; | ||
| 269 | |||
| 270 | $product_identification_helper->hashes->each(sub { | ||
| 271 | |||
| 272 | my ($hash, $hash_idx) = @_; | ||
| 273 | |||
| 274 | $hash->file_hashes->each(sub { | ||
| 275 | my ($file_hash, $file_hash_idx) = @_; | ||
| 276 | |||
| 277 | if (length($file_hash) < 64) { | ||
| 278 | $self->add_message( | ||
| 279 | type => 'info', | ||
| 280 | category => 'informative', | ||
| 281 | path => "$path/product_identification_helper/hashes/$hash_idx/file_hashes/$file_hash_idx/value", | ||
| 282 | code => '6.3.5', | ||
| 283 | message => 'Use of Short Hash' | ||
| 284 | ); | ||
| 285 | } | ||
| 286 | |||
| 287 | }); | ||
| 288 | |||
| 289 | }); | ||
| 290 | |||
| 291 | } | ||
| 292 | |||
| 293 | sub _TEST_6_3_9_branches { | ||
| 294 | |||
| 295 | my ($self, $branches, $path, $categories) = @_; | ||
| 296 | |||
| 297 | my @sorted_categories = qw[vendor product_name product_version]; | ||
| 298 | |||
| 299 | $branches->each(sub { | ||
| 300 | |||
| 301 | my ($branch, $idx) = @_; | ||
| 302 | |||
| 303 | push @{$categories}, $branch->category if $branch->category eq 'vendor'; | ||
| 304 | push @{$categories}, $branch->category if $branch->category eq 'product_name'; | ||
| 305 | push @{$categories}, $branch->category if $branch->category eq 'product_version'; | ||
| 306 | |||
| 307 | $self->_TEST_6_3_9_branches($branch->branches, "$path/$idx/branches", $categories); | ||
| 308 | |||
| 309 | my $is_invalid = undef; | ||
| 310 | |||
| 311 | my @uniq_categories = uniq(@{$categories}); | ||
| 312 | |||
| 313 | for my $category (@sorted_categories) { | ||
| 314 | $is_invalid = 1 if (!first { $_ eq $category } @{$categories}); | ||
| 315 | } | ||
| 316 | |||
| 317 | for (my $i = 0; $i < @uniq_categories; $i++) { | ||
| 318 | $is_invalid = 1 if ($uniq_categories[$i] ne $sorted_categories[$i]); | ||
| 319 | } | ||
| 320 | |||
| 321 | if ($is_invalid) { | ||
| 322 | $self->add_message( | ||
| 323 | type => 'info', | ||
| 324 | category => 'informative', | ||
| 325 | path => "$path/category", | ||
| 326 | code => '6.3.9', | ||
| 327 | message => 'Branch Categories' | ||
| 328 | ); | ||
| 329 | } | ||
| 330 | |||
| 331 | }); | ||
| 332 | } | ||
| 333 | |||
| 334 | sub _TEST_6_3_10_branches { | ||
| 335 | |||
| 336 | my ($self, $branches, $path) = @_; | ||
| 337 | |||
| 338 | $branches->each(sub { | ||
| 339 | |||
| 340 | my ($branch, $idx) = @_; | ||
| 341 | |||
| 342 | $self->_TEST_6_3_10_branches($branch->branches, "$path/$idx/branches"); | ||
| 343 | |||
| 344 | if ($branch->category eq 'product_version_range') { | ||
| 345 | |||
| 346 | $self->add_message( | ||
| 347 | type => 'info', | ||
| 348 | category => 'informative', | ||
| 349 | path => "$path/category", | ||
| 350 | code => '6.3.10', | ||
| 351 | message => 'Usage of Product Version Range' | ||
| 352 | ); | ||
| 353 | |||
| 354 | } | ||
| 355 | |||
| 356 | }); | ||
| 357 | } | ||
| 358 | |||
| 359 | sub _TEST_6_3_11_branches { | ||
| 360 | |||
| 361 | my ($self, $branches, $path) = @_; | ||
| 362 | |||
| 363 | $branches->each(sub { | ||
| 364 | |||
| 365 | my ($branch, $idx) = @_; | ||
| 366 | |||
| 367 | $self->_TEST_6_3_11_branches($branch->branches, "$path/$idx/branches"); | ||
| 368 | |||
| 369 | if ($branch->category eq 'product_version') { | ||
| 370 | |||
| 371 | if ($branch->name =~ /^[vV][0-9].*$/) { | ||
| 372 | $self->add_message( | ||
| 373 | type => 'info', | ||
| 374 | category => 'informative', | ||
| 375 | path => "$path/name", | ||
| 376 | code => '6.3.11', | ||
| 377 | message => 'Usage of V as Version Indicator' | ||
| 378 | ); | ||
| 379 | } | ||
| 380 | |||
| 381 | } | ||
| 382 | |||
| 383 | }); | ||
| 384 | } | ||
| 385 | |||
| 386 | 1; | ||
| 387 | |||
| 388 | __END__ |