Admin/Command/Publish.pm

filter: Statementstmt 54.2% 58 / 107 Branchbran 38.8% 21 / 54 Conditioncond 36.6% 11 / 30 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 69.5% 16 / 23 Podpod 0.0% 0 / 4 totaltotal 47.0% 110 / 234 CC 42 SCAR 29.5
CC
42
Cov
47%
_parse_references
56.0
command
42.0
_process_file
30.0
CRAP 19.1
« Admin/Util.pm ↑ Summary Admin/Command.pm »
1 1 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use builtin qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
6 experimental::class
7 experimental::builtin
8 1 );
9  
10 1 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use CPANSEC::Admin::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 class CPANSEC::Admin::Command::Publish {
17 1 field %options;
18 1 my $current_id;
19 1 my $time = gmtime;
20  
21 0 ▶ method name { 'publish' }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 0 ▶ method command ($manager, @args) {
Subroutine command: not called CC=6 SCAR=37.4
Pod undocumented
24 0 %options = $manager->get_options(\@args, {
25 'triage-dir=s' => './triage',
26 'published-dir' => './advisories',
27 'all' => undef,
28 });
29 0 ▶ die "cannot use --all with a filename (@args)" if $options{all} && @args;
Branch: 1 branch
BranchTrueFalseTotal
if $options{"all"} and @args 0 0 0
Condition0%0/3
$options{"all"} and @args
!ll&&!rl&&r
000
MC/DC0%0/2
$options{"all"} and @args
$options{"all"} @args
Truth table0%0/3
A and B
A= $options{"all"}B= @args
ABresult
0X0
100
111
30 $options{files} = @args ? [map Path::Tiny::path($_), @args]
31 0 ▶ : [Path::Tiny::path($options{triage_dir})->children( qr/\.yml\z/ )];
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
32 0 ▶ die 'no files found!' unless $options{files};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"files"} 0 0 0
33  
34 0 $current_id = _find_last_used_id($options{published_dir});
35 0 foreach my $file (sort $options{files}->@*) {
36 0 $self->_process_file($manager, $file);
37 }
38 }
39  
40 0 ▶ method _process_file ($manager, $file) {
Subroutine _process_file: not called CC=5 SCAR=34.0
41 0 my %data = CPANSEC::Admin::Util::triage_read($file)->@*;
42 0 ▶ if (!$data{approved} || $data{approved} ne 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $data{"approved"} or $data{"approved"} ne "true") 0 0 0
Condition0%0/3
not $data{"approved"} or $data{"approved"} ne "true"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $data{"approved"} or $data{"approved"} ne "true"
not $data{"approved"} $data{"approved"} ne "true"
Truth table0%0/3
not(A) or B
A= not $data{"approved"}B= $data{"approved"} ne "true"
ABresult
1X1
011
000
43 0 ▶ $manager->info("report $file is not approved yet, please use 'triage' first") unless $options{all};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"all"} 0 0 0
44 0 return;
45 }
46 0 my $osv = _triage2osv(%data);
47 0 my $target = Path::Tiny::path($options{published_dir}, $osv->{id} . '.json');
48 0 ▶ die "$target already exists!" if $target->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $target->exists 0 0 0
49 0 $target->spew_raw(JSON::encode_json($osv));
50 0 $file->remove;
51 0 $current_id++;
52 }
53  
54 0 ▶ sub _find_last_used_id($published_dir) {
Subroutine _find_last_used_id: not called CC=2 SCAR=17.9
55 0 my ($file) = sort { $b <=> $a } Path::Tiny::path($published_dir)->children(qr/\ACPANSEC\-\d+\-\d+\.json\z/);
56 0 my ($year, $id) = $file =~ /\ACPANSEC\-(\d+)\-(\d+)\.json\z/;
57 0 ▶ $id = 0 if $year < $time->year;
Branch: 1 branch
BranchTrueFalseTotal
if $year < $time->year 0 0 0
58 0 return $id + 1;
59 }
60  
61 0 ▶ sub _triage2osv (%data) {
Subroutine _triage2osv: not called CC=4 SCAR=30.0
62 0 my $id = sprintf('CPANSEC-%04d-%04d', $time->year, $current_id);
63 return {
64 schema_version => '1.6.0',
65 id => $id,
66 modified => $time->datetime . 'Z',
67 published => $time->datetime . 'Z',
68 aliases => [ $data{cve} ],
69 summary => $data{summary},
70 details => $data{description},
71 ($data{CVSS_2} || $data{CVSS_3} ?
72 (severity => [{
73 ($data{CVSS_2} ? (type => 'CVSS_V2', score => $data{CVSS_2})
74 : (type => 'CVSS_V3', score => $data{CVSS_3})),
75 }])
76 : ()
77 ),
78 affected => [{
79 package => {
80 ecosystem => 'CPAN',
81 name => $data{cpan_distribution},
82 purl => 'pkg:cpan/' . $data{cpan_distribution},
83 },
84 versions => _get_versions_from_range($data{cpan_distribution}, $data{version_range}),
85 ecosystem_specific => {
86 source => 'https://github.com/CPAN-Security/cpan-advisory-database/blob/advisories/' . $id . '.json',
87 categories => [split /\s*;\s*/ => $data{categories}],
88 version_range => $data{version_range},
89 },
90 } ],
91 0 ▶ references => _parse_references($data{references}),
Branch: 2 branches
BranchTrueFalseTotal
$data{'CVSS_2'} || $data{'CVSS_3'} ? : 0 0 0
$data{'CVSS_2'} ? : 0 0 0
Condition0%0/3
$data{"CVSS_2"} or $data{"CVSS_3"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$data{"CVSS_2"} or $data{"CVSS_3"}
$data{"CVSS_2"} $data{"CVSS_3"}
Truth table0%0/3
A or B
A= $data{"CVSS_2"}B= $data{"CVSS_3"}
ABresult
1X1
011
000
92 database_specific => { license => 'CC0-1.0' },
93 };
94 }
95  
96 1 ▶ sub split_version_range ($version_range) {
Subroutine split_version_range: called CC=9 SCAR=23.0
Pod undocumented
97 1 my (@greater, @lower, @equal, @not_equal);
98 1 foreach my $expr (split /\s*,\s*/, $version_range) {
99 6 ▶ if ($expr =~ /\A\s*([>=<!]=?)?\s*([0-9]\S*)\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\A\s*([>=<!]=?)?\s*([0-9]\S*)\s*\z/u) { } 6 0 6
100 6 my ($op, $ver) = ($1, $2);
101 6 $ver = version->parse($ver);
102 6 ▶ if ($op eq '>') {
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '>') { } 1 5 6
elsif ($op eq '>=') { } 1 4 5
elsif ($op eq '<') { } 2 2 4
elsif ($op eq '<=') { } 0 2 2
elsif ($op eq '!=') { } 1 1 2
elsif ($op eq '==') { } 1 0 1
103 1 push @greater, $ver;
104 }
105 elsif ($op eq '>=') {
106 1 push @greater, $ver;
107 1 push @equal, $ver;
108 }
109 elsif ($op eq '<') {
110 2 push @lower, $ver;
111 }
112 elsif ($op eq '<=') {
113 0 push @lower, $ver;
114 0 push @equal, $ver;
115 }
116 elsif ($op eq '!=') {
117 1 push @not_equal, $ver;
118 }
119 elsif ($op eq '==') {
120 1 push @equal, $ver;
121 }
122 else {
123 0 die "unknown operator '$op' in '$expr'";
124 }
125 }
126 else {
127 0 die "unknown version range '$expr'";
128 }
129 }
130 1 return { greater => \@greater, lower => \@lower, equal => \@equal, not_equal => \@not_equal };
131 }
132  
133 0 ▶ sub _get_versions_from_range ($distname, $version_range) {
Subroutine _get_versions_from_range: not called CC=3 SCAR=24.8
134 0 my $ranges = split_version_range ($version_range);
135  
136 my $response = JSON::decode_json(
137 HTTP::Tiny->new->post('https://fastapi.metacpan.org/release?size=500', {
138 content => encode_json({
139 query => { term => { distribution => $distname } },
140 fields => ['version']
141 })
142 })->{content}
143 0 );
144 0 my @all_versions = map version->parse($_->{fields}{version}), $response->{hits}{hits}->@*;
145  
146 0 my @versions_in_range;
147 0 foreach my $version (@all_versions) {
148 0 ▶ push @versions_in_range, $version if version_in_range($version, $ranges);
Branch: 1 branch
BranchTrueFalseTotal
if version_in_range($version, $ranges) 0 0 0
149 }
150 0 return [sort @versions_in_range];
151 }
152  
153 10 ▶ sub version_in_range ($version, $range) {
Subroutine version_in_range: called CC=13 SCAR=27.2
Pod undocumented
154 10 ▶ return true if List::Util::any { $version == $_ } $range->{equal}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $version == $_; } , @{$$range{"equal"}}) 1 9 10
155 9 ▶ return false if List::Util::any { $version == $_ } $range->{not_equal}->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $version == $_; } , @{$$range{"not_equal"}}) 1 8 9
156 8 my @greater = sort $range->{greater}->@*;
157 8 my @lower = sort $range->{lower}->@*;
158 8 ▶ return true if @greater && (!@lower || $greater[-1] > $lower[-1]) && $version > $greater[-1];
Branch: 1 branch
BranchTrueFalseTotal
if @greater and !@lower || $greater[-1] > $lower[-1] and $version > $greater[-1] 1 7 8
Condition44%4/9
@greater and !@lower || $greater[-1] > $lower[-1] and $version > $greater[-1]
!ll&&!rl&&r
071
@greater and !@lower || $greater[-1] > $lower[-1]
!ll&&!rl&&r
008
not @lower or $greater[-1] > $lower[-1]
l!l&&r!l&&!r
080
MC/DC25%1/4
@greater and !@lower || $greater[-1] > $lower[-1] and $version > $greater[-1]
@greater not @lower $greater[-1] > $lower[-1] $version > $greater[-1]
Truth table33%2/6
A and not(B) or C and D
A= @greaterB= not @lowerC= $greater[-1] > $lower[-1]D= $version > $greater[-1]
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
159 7 ▶ return true if @lower && (!@greater || ($lower[0] < $greater[0])) && $version < $lower[0];
Branch: 1 branch
BranchTrueFalseTotal
if @lower and !@greater || $lower[0] < $greater[0] and $version < $lower[0] 1 6 7
Condition44%4/9
@lower and !@greater || $lower[0] < $greater[0] and $version < $lower[0]
!ll&&!rl&&r
061
@lower and !@greater || $lower[0] < $greater[0]
!ll&&!rl&&r
007
not @greater or $lower[0] < $greater[0]
l!l&&r!l&&!r
070
MC/DC25%1/4
@lower and !@greater || $lower[0] < $greater[0] and $version < $lower[0]
@lower not @greater $lower[0] < $greater[0] $version < $lower[0]
Truth table33%2/6
A and not(B) or C and D
A= @lowerB= not @greaterC= $lower[0] < $greater[0]D= $version < $lower[0]
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
160 7 ▶ return true if (( List::Util::any { $version > $_ } $range->{greater}->@*)
Subroutine __ANON__: called CC=1 SCAR=0.0
161 6 ▶ && (List::Util::any { $version < $_ } $range->{lower}->@*));
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $version > $_; } , @{$$range{"greater"}}) and &List::Util::any(sub { $version < $_; } , @{$$range{"lower"}}) 4 2 6
Condition100%3/3
&List::Util::any(sub { $version > $_; } , @{$$range{"greater"}}) and &List::Util::any(sub { $version < $_; } , @{$$range{"lower"}})
!ll&&!rl&&r
114
MC/DC100%2/2
&List::Util::any(sub { $version > $_; } , @{$$range{"greater"}}) and &List::Util::any(sub { $version < $_; } , @{$$range{"lower"}})
&List::Util::any(sub { $version > $_; } , @{$$range{"greater"}}) &List::Util::any(sub { $version < $_; } , @{$$range{"lower"}})
Truth table100%3/3
A and B
A= &List::Util::any(sub { $version > $_; } , @{$$range{"greater"}})B= &List::Util::any(sub { $version < $_; } , @{$$range{"lower"}})
ABresult
0X0
100
111
162 2 return false;
163 }
164  
165 0 ▶ sub _parse_references ($references) {
Subroutine _parse_references: not called CC=7 SCAR=40.3
166 0 my @parsed;
167 0 foreach my $url (@$references) {
168 0 my $type;
169 0 ▶ if ($url =~ m{\Ahttps?://metacpan.org/.+}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[\Ahttps?://metacpan.org/.+]u) 0 0 0
170 0 $type = 'PACKAGE';
171 }
172 0 ▶ if ($url =~ m{\Ahttps?://github.com/.+?/issues/.+}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($url =~ m[\Ahttps?://github.com/.+?/issues/.+]u) { } 0 0 0
elsif ($url =~ m[\Ahttps?://github.com/.+?/pull/.+]u) { } 0 0 0
elsif ($url =~ /\blists?\b/u) { } 0 0 0
elsif ($url =~ /\bblogs?\b/u) { } 0 0 0
173 0 $type = 'REPORT'
174 }
175 elsif ($url =~ m{\Ahttps?://github.com/.+?/pull/.+}) {
176 0 $type = 'FIX'
177 }
178 elsif ($url =~ m{\blists?\b}) {
179 0 $type = 'DISCUSSION';
180 }
181 elsif ($url =~ m{\bblogs?\b}) {
182 0 $type = 'ARTICLE';
183 }
184 else {
185 0 $type = 'WEB';
186 }
187 0 push @parsed, { type => $type, url => $url };
188 }
189 0 return \@parsed;
190 }
191 }
192  
193 __END__
« Admin/Util.pm ↑ Summary Admin/Command.pm »