CVSS/Base.pm

filter: Statementstmt 79.2% 134 / 169 Branchbran 55.0% 22 / 40 Conditioncond 63.8% 23 / 36 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 56.6% 30 / 53 Podpod 73.6% 28 / 38 totaltotal 68.2% 243 / 356 CC 44 SCAR 21.1
CC
44
Cov
73%
TO_JSON
30.4
to_vector_string
11.3
new
7.4
CRAP 8.3
« CVSS/v2.pm ↑ Summary CVSS.pm »
1 package CVSS::Base;
2  
3 6 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 our $VERSION = '1.15';
11 6 $VERSION =~ tr/_//d; ## no critic
12  
13 6 ▶use overload '""' => \&to_string, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use constant DEBUG => $ENV{CVSS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
18  
19 13 my $class = shift;
20  
21 13 my $ATTRIBUTES = $class->ATTRIBUTES;
22  
23 13 for my $method (keys %{$ATTRIBUTES}) {
24  
25 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 294 my $metric = $ATTRIBUTES->{$method};
30  
31 # Long method name
32 294 ▶ *{"${class}::${method}"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 16 ▶ @_ > 1 ? $_[0]->_metric_name_to_value($metric, $_[1]) : $_[0]->_metric_value_to_name($metric);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 8 8 16
34 294 };
35  
36 # Create metric alias
37 0 ▶ *{"${class}::${metric}"} = sub { $_[0]->M($metric) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
38  
39 }
40  
41 }
42  
43 4255 ▶sub new {
Subroutine new: called CC=7 SCAR=20.0
Pod undocumented
44  
45 4255 my ($class, %params) = @_;
46  
47 4255 ▶ $params{metrics} //= {};
Condition100%2/2
$params{"metrics"} //= {}
l!l
42541
MC/DC100%1/1
$params{"metrics"} //= {}
$params{"metrics"}
Truth table100%2/2
A
A= $params{"metrics"}
Aresult
11
00
48 4255 ▶ $params{scores} //= {};
Condition50%1/2
$params{"scores"} //= {}
l!l
04255
MC/DC0%0/1
$params{"scores"} //= {}
$params{"scores"}
Truth table50%1/2
A
A= $params{"scores"}
Aresult
11
00
49 4255 ▶ $params{vector_string} //= undef;
Condition100%2/2
$params{"vector_string"} //= undef
l!l
42541
MC/DC100%1/1
$params{"vector_string"} //= undef
$params{"vector_string"}
Truth table100%2/2
A
A= $params{"vector_string"}
Aresult
11
00
50  
51 4255 my $self = bless {%params}, $class;
52  
53 4255 ▶ if ($self->version !~ /(2.0|3.[0-1]|4.0)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->version =~ /(2.0|3.[0-1]|4.0)/) 0 4255 4255
54 0 Carp::croak 'Invalid CVSS version';
55 }
56  
57 4255 ▶ if ($self->{vector_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"vector_string"}) 4254 1 4255
58  
59 4254 DEBUG and say STDERR sprintf('-- Validate vector string: %s', $self->VECTOR_STRING_REGEX);
60  
61 4254 ▶ if ($self->{vector_string} !~ $self->VECTOR_STRING_REGEX) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"vector_string"} =~ $self->VECTOR_STRING_REGEX) 0 4254 4254
62 0 Carp::croak 'Invalid CVSS vector string';
63 }
64  
65 4254 $self->calculate_score;
66  
67 }
68  
69 4255 return $self;
70  
71 }
72  
73 4252 ▶sub from_vector_string {
Subroutine from_vector_string: called CC=2 SCAR=6.9
Pod undocumented
74  
75 4252 my ($class, $vector_string) = @_;
76  
77 4252 my %metrics = split /[\/:]/, $vector_string;
78 4252 ▶ my $version = delete $metrics{CVSS} || '2.0';
Condition100%2/2
delete $metrics{'CVSS'} || '2.0'
l!l
3884368
MC/DC100%1/1
delete $metrics{'CVSS'} || '2.0'
delete $metrics{'CVSS'}
Truth table100%2/2
A
A= delete $metrics{'CVSS'}
Aresult
11
00
79  
80 4252 DEBUG and say STDERR "-- Vector String: $vector_string";
81 4252 return $class->new(version => $version, metrics => \%metrics, vector_string => $vector_string);
82  
83 }
84  
85 0 ▶sub ATTRIBUTES { {} }
Subroutine ATTRIBUTES: not called CC=1 SCAR=6.9
Pod documented
86 0 ▶sub SCORE_SEVERITY { {} }
Subroutine SCORE_SEVERITY: not called CC=1 SCAR=6.9
Pod documented
87 0 ▶sub NOT_DEFINED_VALUE { }
Subroutine NOT_DEFINED_VALUE: not called CC=1 SCAR=0.0
Pod documented
88 0 ▶sub VECTOR_STRING_REGEX {qw{}}
Subroutine VECTOR_STRING_REGEX: not called CC=1 SCAR=6.9
Pod documented
89 0 ▶sub METRIC_GROUPS { {} }
Subroutine METRIC_GROUPS: not called CC=1 SCAR=6.9
Pod documented
90 0 ▶sub METRIC_NAMES { {} }
Subroutine METRIC_NAMES: not called CC=1 SCAR=6.9
Pod documented
91 0 ▶sub METRIC_VALUES { {} }
Subroutine METRIC_VALUES: not called CC=1 SCAR=6.9
Pod documented
92  
93  
94 8 ▶sub _metric_name_to_value {
Subroutine _metric_name_to_value: called CC=1 SCAR=0.0
95 8 my ($self, $metric, $name) = @_;
96 8 $name =~ s/\s/_/g;
97 8 $self->metrics->{$metric} = $self->METRIC_NAMES->{$metric}->{names}->{$name};
98 8 return $self;
99 }
100  
101 8 ▶sub _metric_value_to_name {
Subroutine _metric_value_to_name: called CC=1 SCAR=0.0
102 8 my ($self, $metric) = @_;
103 8 $self->METRIC_NAMES->{$metric}->{values}->{$self->metrics->{$metric}};
104 }
105  
106 5501 ▶sub version { shift->{version} || Carp::croak 'Missing CVSS version' }
Subroutine version: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"version"} or Carp::croak("Missing CVSS version") 5501 0 5501
107 1 ▶sub vector_string { $_[0]->{vector_string} || $_[0]->to_vector_string }
Subroutine vector_string: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"vector_string"} or $_[0]->to_vector_string 1 0 1
108 363352 ▶sub metrics { shift->{metrics} }
Subroutine metrics: called CC=1 SCAR=0.0
Pod documented
109 0 ▶sub scores { shift->{scores} }
Subroutine scores: not called CC=1 SCAR=6.9
Pod documented
110  
111  
112 # Scores & severities
113 4259 ▶sub base_score { shift->{scores}->{base} }
Subroutine base_score: called CC=1 SCAR=0.0
Pod documented
114 2 ▶sub base_severity { $_[0]->score_to_severity($_[0]->base_score) }
Subroutine base_severity: called CC=1 SCAR=0.0
Pod documented
115  
116 # CVSS 2.0/3.x scores & severities
117 2 ▶sub temporal_score { shift->{scores}->{temporal} }
Subroutine temporal_score: called CC=1 SCAR=0.0
Pod documented
118 1 ▶sub temporal_severity { $_[0]->score_to_severity($_[0]->temporal_score) }
Subroutine temporal_severity: called CC=1 SCAR=0.0
Pod documented
119 2 ▶sub environmental_score { shift->{scores}->{environmental} }
Subroutine environmental_score: called CC=1 SCAR=0.0
Pod documented
120 1 ▶sub environmental_severity { $_[0]->score_to_severity($_[0]->environmental_score) }
Subroutine environmental_severity: called CC=1 SCAR=0.0
Pod documented
121  
122 # Extra 2.0/3.x scores
123 0 ▶sub exploitability_score { shift->{scores}->{exploitability} }
Subroutine exploitability_score: not called CC=1 SCAR=6.9
Pod documented
124 0 ▶sub impact_score { shift->{scores}->{impact} }
Subroutine impact_score: not called CC=1 SCAR=6.9
Pod documented
125 0 ▶sub modified_impact_score { shift->{scores}->{modified_impact} }
Subroutine modified_impact_score: not called CC=1 SCAR=6.9
Pod documented
126  
127  
128 # JSON-style alias
129 0 ▶sub vectorString { shift->vector_string }
Subroutine vectorString: not called CC=1 SCAR=6.9
Pod undocumented
130 0 ▶sub baseScore { shift->base_score }
Subroutine baseScore: not called CC=1 SCAR=6.9
Pod undocumented
131 0 ▶sub baseSeverity { shift->base_severity }
Subroutine baseSeverity: not called CC=1 SCAR=6.9
Pod undocumented
132 0 ▶sub temporalScore { shift->temporal_score }
Subroutine temporalScore: not called CC=1 SCAR=6.9
Pod undocumented
133 0 ▶sub temporalSeverity { shift->temporal_severity }
Subroutine temporalSeverity: not called CC=1 SCAR=6.9
Pod undocumented
134 0 ▶sub environmentalScore { shift->environmental_score }
Subroutine environmentalScore: not called CC=1 SCAR=6.9
Pod undocumented
135 0 ▶sub environmentalSeverity { shift->environmental_severity }
Subroutine environmentalSeverity: not called CC=1 SCAR=6.9
Pod undocumented
136  
137  
138 4038 ▶sub metric_group_is_set {
Subroutine metric_group_is_set: called CC=4 SCAR=15.2
Pod documented
139  
140 4038 my ($self, $type) = @_;
141  
142 4038 for (@{$self->METRIC_GROUPS->{$type}}) {
143 26794 ▶ return 1 if ($self->M($_) && $self->M($_) ne $self->NOT_DEFINED_VALUE);
Branch: 1 branch
BranchTrueFalseTotal
if $self->M($_) and $self->M($_) ne $self->NOT_DEFINED_VALUE 0 26794 26794
Condition33%1/3
$self->M($_) and $self->M($_) ne $self->NOT_DEFINED_VALUE
!ll&&!rl&&r
0267940
MC/DC0%0/2
$self->M($_) and $self->M($_) ne $self->NOT_DEFINED_VALUE
$self->M($_) $self->M($_) ne $self->NOT_DEFINED_VALUE
Truth table33%1/3
A and B
A= $self->M($_)B= $self->M($_) ne $self->NOT_DEFINED_VALUE
ABresult
0X0
100
111
144 }
145  
146 }
147  
148 0 ▶sub metric {
Subroutine metric: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self, $metric) = @_;
150 0 my $value = $self->M($metric);
151  
152 0 return $self->METRIC_NAMES->{$metric}->{values}->{$value};
153 }
154  
155 243988 ▶sub M { $_[0]->metrics->{$_[1]} }
Subroutine M: called CC=1 SCAR=0.0
Pod documented
156  
157 4 ▶sub score_to_severity {
Subroutine score_to_severity: called CC=5 SCAR=16.8
Pod documented
158  
159 4 my ($self, $score) = @_;
160  
161 4 ▶ return unless (!!$score);
Branch: 1 branch
BranchTrueFalseTotal
if not $score 2 2 4
162  
163 2 my $SCORE_SEVERITY = $self->SCORE_SEVERITY;
164  
165 2 foreach (keys %{$SCORE_SEVERITY}) {
166 2 my $range = $SCORE_SEVERITY->{$_};
167 2 ▶ if ($score >= $range->{min} && $score <= $range->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score >= $range->{"min"} and $score <= $range->{"max"}) 2 0 2
Condition33%1/3
$score >= $range->{"min"} and $score <= $range->{"max"}
!ll&&!rl&&r
002
MC/DC0%0/2
$score >= $range->{"min"} and $score <= $range->{"max"}
$score >= $range->{"min"} $score <= $range->{"max"}
Truth table33%1/3
A and B
A= $score >= $range->{"min"}B= $score <= $range->{"max"}
ABresult
0X0
100
111
168 2 return $_;
169 }
170 }
171  
172 0 Carp::croak 'Unknown severity';
173  
174 }
175  
176 0 ▶sub calculate_score { Carp::croak sprintf('%s->calculate_score() is not implemented in subclass', ref(shift)) }
Subroutine calculate_score: not called CC=1 SCAR=6.9
Pod documented
177  
178 0 ▶sub to_xml { Carp::croak sprintf('%s->to_xml() is not implemented in subclass', ref(shift)) }
Subroutine to_xml: not called CC=1 SCAR=6.9
Pod documented
179  
180 0 ▶sub to_string { shift->to_vector_string }
Subroutine to_string: not called CC=1 SCAR=6.9
Pod undocumented
181  
182 4253 ▶sub to_vector_string {
Subroutine to_vector_string: called CC=11 SCAR=24.2
Pod documented
183  
184 4253 my ($self) = @_;
185  
186 4253 my $metrics = $self->metrics;
187 4253 my @vectors = ();
188  
189 4253 ▶ if ($self->version > 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version > 2) 3885 368 4253
190 3885 push @vectors, sprintf('CVSS:%s', $self->version);
191 }
192  
193 4253 foreach my $metric (@{$self->METRIC_GROUPS->{base}}) {
194 39450 ▶ return if (!$metrics->{$metric});
Branch: 1 branch
BranchTrueFalseTotal
unless $metrics->{$metric} 0 39450 39450
195 39450 push @vectors, sprintf('%s:%s', $metric, $metrics->{$metric});
196 }
197  
198 4253 my @other_metrics = ();
199  
200 4253 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{threat} || []}; # CVSS 4.0
Condition100%2/2
$self->METRIC_GROUPS->{'threat'} || []
l!l
20542199
MC/DC100%1/1
$self->METRIC_GROUPS->{'threat'} || []
$self->METRIC_GROUPS->{'threat'}
Truth table100%2/2
A
A= $self->METRIC_GROUPS->{'threat'}
Aresult
11
00
201 4253 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{temporal} || []}; # CVSS 2.0-3.x
Condition100%2/2
$self->METRIC_GROUPS->{'temporal'} || []
l!l
21992054
MC/DC100%1/1
$self->METRIC_GROUPS->{'temporal'} || []
$self->METRIC_GROUPS->{'temporal'}
Truth table100%2/2
A
A= $self->METRIC_GROUPS->{'temporal'}
Aresult
11
00
202 4253 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{environmental} || []}; # CVSS 2.0-3.x-4.0
Condition50%1/2
$self->METRIC_GROUPS->{'environmental'} || []
l!l
42530
MC/DC0%0/1
$self->METRIC_GROUPS->{'environmental'} || []
$self->METRIC_GROUPS->{'environmental'}
Truth table50%1/2
A
A= $self->METRIC_GROUPS->{'environmental'}
Aresult
11
00
203 4253 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{supplemental} || []}; # CVSS 4.0
Condition100%2/2
$self->METRIC_GROUPS->{'supplemental'} || []
l!l
20542199
MC/DC100%1/1
$self->METRIC_GROUPS->{'supplemental'} || []
$self->METRIC_GROUPS->{'supplemental'}
Truth table100%2/2
A
A= $self->METRIC_GROUPS->{'supplemental'}
Aresult
11
00
204  
205 4253 foreach my $metric (@other_metrics) {
206 71712 ▶ if (defined $metrics->{$metric} && $metrics->{$metric} ne $self->NOT_DEFINED_VALUE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE) 0 71712 71712
Condition66%2/3
defined $metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
!ll&&!rl&&r
368713440
MC/DC0%0/2
defined $metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
defined $metrics->{$metric} $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
Truth table66%2/3
A and B
A= defined $metrics->{$metric}B= $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
ABresult
0X0
100
111
207 0 push @vectors, sprintf('%s:%s', $metric, $metrics->{$metric});
208 }
209 }
210  
211 4253 return join '/', @vectors;
212  
213 }
214  
215 1 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=16 SCAR=34.2
Pod documented
216  
217 1 my ($self) = @_;
218  
219 # Required JSON fields:
220 # CVSS == v2.0: version, vectorString and baseScore
221 # CVSS >= v3.0: version, vectorString, baseScore and baseSeverity
222  
223 1 ▶ $self->calculate_score unless ($self->base_score);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->base_score 0 1 1
224  
225 1 my $json = {
226 version => sprintf('%.1f', $self->version),
227 vectorString => $self->vector_string,
228 baseScore => $self->base_score
229 };
230  
231 1 ▶ if ($self->version > 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version > 2) 1 0 1
232 1 $json->{baseSeverity} = $self->base_severity;
233 }
234  
235 1 my $metrics = $self->metrics;
236 1 my %attributes = reverse(%{$self->ATTRIBUTES});
237  
238 1 foreach my $metric (@{$self->METRIC_GROUPS->{base}}) {
239 8 $json->{$attributes{$metric}} = $self->METRIC_NAMES->{$metric}->{values}->{$metrics->{$metric}};
240 }
241  
242 1 my @other_metrics = ();
243  
244 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{threat} || []}; # CVSS 4.0
Condition50%1/2
$self->METRIC_GROUPS->{'threat'} || []
l!l
01
MC/DC0%0/1
$self->METRIC_GROUPS->{'threat'} || []
$self->METRIC_GROUPS->{'threat'}
Truth table50%1/2
A
A= $self->METRIC_GROUPS->{'threat'}
Aresult
11
00
245 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{temporal} || []}; # CVSS 2.0-3.x
Condition50%1/2
$self->METRIC_GROUPS->{'temporal'} || []
l!l
10
MC/DC0%0/1
$self->METRIC_GROUPS->{'temporal'} || []
$self->METRIC_GROUPS->{'temporal'}
Truth table50%1/2
A
A= $self->METRIC_GROUPS->{'temporal'}
Aresult
11
00
246 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{environmental} || []}; # CVSS 2.0-3.x-4.0
Condition50%1/2
$self->METRIC_GROUPS->{'environmental'} || []
l!l
10
MC/DC0%0/1
$self->METRIC_GROUPS->{'environmental'} || []
$self->METRIC_GROUPS->{'environmental'}
Truth table50%1/2
A
A= $self->METRIC_GROUPS->{'environmental'}
Aresult
11
00
247 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{supplemental} || []}; # CVSS 4.0
Condition50%1/2
$self->METRIC_GROUPS->{'supplemental'} || []
l!l
01
MC/DC0%0/1
$self->METRIC_GROUPS->{'supplemental'} || []
$self->METRIC_GROUPS->{'supplemental'}
Truth table50%1/2
A
A= $self->METRIC_GROUPS->{'supplemental'}
Aresult
11
00
248  
249 1 foreach my $metric (@other_metrics) {
250 14 ▶ if ($metrics->{$metric} && $metrics->{$metric} ne $self->NOT_DEFINED_VALUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE) 0 14 14
Condition33%1/3
$metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
!ll&&!rl&&r
0140
MC/DC0%0/2
$metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
$metrics->{$metric} $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
Truth table33%1/3
A and B
A= $metrics->{$metric}B= $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
ABresult
0X0
100
111
251 0 $json->{$attributes{$metric}} = $self->METRIC_NAMES->{$metric}->{values}->{$metrics->{$metric}};
252 }
253 }
254  
255 1 ▶ if ($self->version <= 3.1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version <= "3.1") 1 0 1
256  
257 1 ▶ if ($self->metric_group_is_set('temporal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metric_group_is_set("temporal")) 0 1 1
258  
259 0 $json->{temporalScore} = $self->temporal_score;
260  
261 0 ▶ if ($self->version != 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version != 2) 0 0 0
262 0 $json->{temporalSeverity} = $self->temporal_severity;
263 }
264  
265 }
266  
267 1 ▶ if ($self->metric_group_is_set('environmental')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->metric_group_is_set("environmental")) 0 1 1
268  
269 0 $json->{environmentalScore} = $self->environmental_score;
270  
271 0 ▶ if ($self->version != 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version != 2) 0 0 0
272 0 $json->{environmentalSeverity} = $self->environmental_severity;
273 }
274  
275 }
276  
277 }
278  
279 # CVSS 4.0 ???
280  
281 # environmentalScore
282 # environmentalSeverity
283 # threatScore
284 # threatSeverity
285  
286 1 return $json;
287  
288 }
289  
290 6 1;
291 __END__
« CVSS/v2.pm ↑ Summary CVSS.pm »