| 1 | use v5.38; | ||
| 2 | use feature 'class'; | ||
| 3 | no warnings qw( | ||
| 4 | experimental::class | ||
| 5 | experimental::for_list | ||
| 6 | ); | ||
| 7 | |||
| 8 | use Net::NVD; | ||
| 9 | use Path::Tiny; | ||
| 10 | use CPANSEC::Admin::Util; | ||
| 11 | |||
| 12 | class CPANSEC::Admin::Command::CVEScan { | ||
| 13 | field %options; | ||
| 14 | |||
| 15 | method name { 'cvescan' } | ||
| 16 | |||
| 17 | method command ($manager, @args){ | ||
| 18 | %options = $manager->get_options(\@args, { | ||
| 19 | 'triage-dir=s' => './triage', | ||
| 20 | 'index-file=s' => '{triage_dir}/last_visited_index', | ||
| 21 | 'from=i' => undef, | ||
| 22 | 'ignore=s' => undef, | ||
| 23 | 'limit=i' => undef, | ||
| 24 | 'no-index-update' => undef, | ||
| 25 | }); | ||
| 26 | $options{from} = _read_index_file($options{index_file}) unless $options{from}; | ||
| 27 | my %ignored = _fetch_ignore_list($options{ignore}); | ||
| 28 | |||
| 29 | $manager->info('querying NVD. Initial index is ' . $options{from}); | ||
| 30 | my $cves = _get_cves(%options); | ||
| 31 | |||
| 32 | $manager->info(@$cves . ' CVEs set for triage.'); | ||
| 33 | return if !@$cves; | ||
| 34 | my $candidates = _find_eligible_candidates($cves, \%ignored); | ||
| 35 | |||
| 36 | if (@$candidates) { | ||
| 37 | $manager->info('processing ' . @$candidates . ' eligible candidates'); | ||
| 38 | my @processed = map _process_candidate($_), @$candidates; | ||
| 39 | |||
| 40 | $manager->info('storing candidates for triage'); | ||
| 41 | _save_for_triage(\%options, \@processed); | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | $manager->info('no eligible candidates found.'); | ||
| 45 | } | ||
| 46 | |||
| 47 | if (!$options{no_index_update}) { | ||
| 48 | $manager->info('updating index file'); | ||
| 49 | _update_index_file(\%options, scalar(@$cves)); | ||
| 50 | } | ||
| 51 | |||
| 52 | $manager->info('complete'); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _update_index_file ($opts, $entries_read) { | ||
| 56 | my $new_index = $opts->{from} + $entries_read; | ||
| 57 | Path::Tiny::path($opts->{index_file})->spew_raw($new_index); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _fetch_ignore_list ($path) { | ||
| 61 | return () unless defined $path; | ||
| 62 | $path = Path::Tiny::path($path); | ||
| 63 | die "$path not found (for --ignore)." unless $path->is_file; | ||
| 64 | my %ignored = map { /(CVE\-\d+\-\d+)/ ? ($1 => 1) : () } $path->lines; | ||
| 65 | return %ignored; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _save_for_triage ($opts, $candidates) { | ||
| 69 | my $basedir = Path::Tiny::path( $opts->{'triage_dir'} ); | ||
| 70 | foreach my $candidate (@$candidates) { | ||
| 71 | CPANSEC::Admin::Util::triage_write($basedir->child($candidate->[1]) . '.yml', $candidate); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _process_candidate ($cve) { | ||
| 76 | my ($description) = map $_->{value}, grep $_->{lang} eq 'en', $cve->{descriptions}->@*; | ||
| 77 | $description //= 'REPLACE ME'; | ||
| 78 | |||
| 79 | # use the highest CVSS version we can find. | ||
| 80 | my ($cvss_version, $cvss_value); | ||
| 81 | foreach my ($k, $v) ($cve->{metrics}->%*) { | ||
| 82 | my ($version) = $k =~ /cvssMetricV(\d)/; | ||
| 83 | if (!$cvss_version || $cvss_version < $version) { | ||
| 84 | $cvss_version = $version; | ||
| 85 | $cvss_value = $v->[0]{cvssData}{vectorString}; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | return [ | ||
| 90 | cve => $cve->{id}, | ||
| 91 | summary => '~', | ||
| 92 | description => $description, | ||
| 93 | categories => '~', | ||
| 94 | ($cvss_version | ||
| 95 | ? ("CVSS_$cvss_version" => $cvss_value) | ||
| 96 | : () | ||
| 97 | ), | ||
| 98 | references => [ map $_->{url}, $cve->{references}->@* ], | ||
| 99 | cpan_distribution => '~', | ||
| 100 | version_range => '~', | ||
| 101 | ]; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _find_eligible_candidates ($cves, $ignored) { | ||
| 105 | my @candidates; | ||
| 106 | foreach my $cve (@$cves) { | ||
| 107 | push @candidates, $cve | ||
| 108 | if !$ignored->{ $cve->{id } } | ||
| 109 | && $cve->{descriptions}[0]{value} =~ /(cpan|perl)\b/in; | ||
| 110 | } | ||
| 111 | return \@candidates; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _get_cves (%opts) { | ||
| 115 | my $nvd = Net::NVD->new; | ||
| 116 | my @cves = $nvd->search( | ||
| 117 | # keyword_search => 'perl cpan', | ||
| 118 | # ^^ NVD keyword search would be perfect, but for some reason | ||
| 119 | # it misses a LOT of entries. So we filter ourselves. | ||
| 120 | start_index => $opts{from}, | ||
| 121 | no_rejected => 1, | ||
| 122 | ($opts{limit} ? (results_per_page => $opts{limit}) : ()), | ||
| 123 | ); | ||
| 124 | return \@cves; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _read_index_file ($path) { | ||
| 128 | return 0 unless $path; | ||
| 129 | $path = Path::Tiny::path($path); | ||
| 130 | return 0 unless $path->is_file; | ||
| 131 | my $data = $path->slurp; | ||
| 132 | chomp $data if $data =~ /\n/; | ||
| 133 | die "invalid index '$data' in $path." unless $data =~ /\A[0-9]+\z/; | ||
| 134 | return $data; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | __END__ |