Admin/Command/Triage.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 199 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.38;
2 use builtin qw( trim true false );
3 use feature qw( class try );
4 no warnings qw(
5 experimental::class
6 experimental::builtin
7 experimental::try
8 experimental::const_attr
9 );
10  
11 use Getopt::Long;
12 use Path::Tiny;
13 use List::Util;
14 use MetaCPAN::Client;
15 use CPAN::Meta::Requirements;
16 use URI;
17  
18 use CPANSEC::Admin::Util;
19 local $| = 1;
20  
21 class CPANSEC::Admin::Command::Triage {
22 field %options;
23  
24 *NOTADIST = sub : const { 1 };
25 *PERLISSUE = sub : const { 2 };
26 *UNRELATED = sub : const { 3 };
27 *THIRDPARTY = sub : const { 4 };
28 *VALIDISSUE = sub : const { 5 };
29 *NOTSURE = sub : const { 6 };
30  
31 my %rejection_criteria = (
32 NOTADIST() => 'Not a CPAN distribution',
33 PERLISSUE() => 'Issue refers to Perl itself',
34 UNRELATED() => 'Unrelated to Perl or Perl modules',
35 THIRDPARTY() => 'Third-party (DarkPAN, not CPAN)',
36 VALIDISSUE() => '** VALID! **',
37 NOTSURE() => '(not sure... better skip this one for now)',
38 );
39  
40 method name { 'triage' }
41  
42 method command ($manager, @args) {
43 %options = $manager->get_options(\@args, {
44 'triage-dir=s' => './triage',
45 'all' => undef,
46 });
47 die "cannot use --all with a filename (@args)" if $options{all} && @args;
48 $options{files} = @args ? [map Path::Tiny::path($_), @args]
49 : [Path::Tiny::path($options{triage_dir})->children( qr/\.yml\z/ )];
50 die 'no files found!' unless $options{files};
51  
52 my $total = scalar $options{files}->@*;
53 my $current = 0;
54 foreach my $file (sort $options{files}->@*) {
55 $manager->info('(' . ++$current . "/$total) processing '$file'");
56 $self->_process_file($manager, $file);
57 }
58 }
59  
60 method _process_file ($manager, $file) {
61 my %data = CPANSEC::Admin::Util::triage_read($file)->@*;
62 return if $options{all} && $data{approved} && $data{approved} eq 'true';
63 say join("\n", 'References:', $data{references}->@*) . "\n\n$data{description}\n";
64 my $msg = CPANSEC::Admin::Util::prompt(
65 "Please triage the information above as:\n"
66 . join("\n", map " $_. $rejection_criteria{$_}" => sort keys %rejection_criteria),
67 1 .. scalar keys %rejection_criteria
68 );
69 if ($msg == NOTSURE()) {
70 $manager->info('Skipped!');
71 }
72 elsif ($msg == VALIDISSUE()) {
73 if ($self->review(\%data)) {
74 CPANSEC::Admin::Util::triage_write($file, \%data);
75 }
76 }
77 else {
78 $manager->info('adding entry to ignored list');
79 $self->add_to_ignored($file, $msg);
80 }
81 return;
82 }
83  
84 method review ($data) {
85 while (!$data->{cve} || $data->{cve} !~ /\ACVE-\d{4}\-\d{4,}\z/) {
86 $data->{cve} = CPANSEC::Admin::Util::prompt('Please type a proper CVE or "~" if your are *SURE* there is none.');
87 last if $data->{cve} && $data->{cve} eq '~';
88 }
89  
90 my $valid_dist;
91 while (!$valid_dist) {
92 my $dist = $data->{cpan_distribution}
93 // CPANSEC::Admin::Util::prompt('Please type a valid distribution name for this issue (with "-", not "::", and no version)');
94 if ($dist !~ /\A(\s|~|:)*\z/n) {
95 try { MetaCPAN::Client->new->distribution($dist); $valid_dist = $dist }
96 catch ($e) { warn $e unless $e =~ /Not Found/ }
97 }
98 }
99 $data->{cpan_distribution} = $valid_dist;
100  
101 my $valid_description;
102 while (!$valid_description) {
103 my $desc = CPANSEC::Admin::Util::prompt(
104 'Please describe the issue in detail. Use literal "\n" for line breaks '
105 . '`backticks` for code and [name](https://...) for links where necessary. '
106 . '50 characters minimum. Please don\'t just copy-paste a CVE description, '
107 . 'follow references so people reading this advisory are able to understand '
108 . 'any possible risks and make a decision.'
109 );
110 $valid_description = $desc if length($desc) > 50;
111 }
112 $data->{description} = $valid_description;
113  
114 while (!$data->{summary} || 20 < length($data->{summary}) < 120) {
115 $data->{summary} = CPANSEC::Admin::Util::prompt('Please provide a summary for the issue (20-120 chars)');
116 }
117  
118 CATEGORY:
119 while (!$data->{categories} || $data->{categories} ne '~') {
120 my %valid_categories = map { $_ => 1 } qw( code-execution denial-of-service crypto-failure);
121 my $cat_input = CPANSEC::Admin::Util::prompt(
122 'Please provide one or more categories for this issue (separated by ";"). '
123 . "Categories can be any of:\n - " . join("\n - " => sort keys %valid_categories)
124 );
125 foreach my $cat (split /\s*;\s*/, $cat_input) {
126 redo CATEGORY unless exists $valid_categories{$cat};
127 }
128 $data->{categories} = $cat_input;
129 }
130  
131 while (
132 ref $data->{references} ne 'ARRAY'
133 || List::Util::notall { URI->new($_)->has_recognized_scheme } $data->{references}->@*
134 ) {
135 my @input = split /;/, CPANSEC::Admin::Util::prompt('Please provide at least one reference URL (separate URLs with ";")');
136 $data->{references} = \@input;
137 }
138 # TODO: check CVSS_2 and CVSS_3
139 my $version_range;
140 while (!defined $version_range) {
141 my $v_range = $data->{version_range}
142 // CPANSEC::Admin::Util::prompt("Please provide the version range for this issue in cpanfile-style, e.g.:\n"
143 . "> 1.0.2, <= 2.3.1, != 2.1.7\n"
144 . "Do NOT provide an upper bound for unfixed issues."
145 );
146 try { CPAN::Meta::Requirements->from_string_hash({ $valid_dist => $v_range }); $version_range = $v_range }
147 catch ($e) { warn $e };
148 }
149 # TODO: check MetaCPAN/BackPAN if given versions are valid
150 if (CPANSEC::Admin::Util::prompt('Ready to commit your changes? (y/n)', 'y', 'n') eq 'y') {
151 $data->{approved} = 'true';
152 return true;
153 }
154 return false;
155 }
156  
157 method add_to_ignored ($file, $msg_index) {
158 my $ignored = Path::Tiny::path($options{triage_dir}, 'false_positives');
159 my $fh = $ignored->opena_raw;
160 say $fh $file->basename('.yml') . ' # ' . $rejection_criteria{$msg_index};
161 $file->remove;
162 }
163 }
164  
165 __END__