CVSS/Base.pm

filter: Statementstmt 79.2% 134 / 169 Branchbran 60.7% 34 / 56 Conditioncond 70.0% 14 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 56.6% 30 / 53 Podpod 73.6% 28 / 38 totaltotal 70.4% 245 / 348 CC 44 SCAR 21.1
CC
44
Cov
74%
TO_JSON
30.4
to_vector_string
11.3
new
7.4
CRAP 8.2
« 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.14';
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 sub import {
18  
19 7 ▶ my $class = shift;
Subroutine import: called CC=2 SCAR=6.9
20  
21 7 my $ATTRIBUTES = $class->ATTRIBUTES;
22  
23 7 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 158 my $metric = $ATTRIBUTES->{$method};
30  
31 # Long method name
32 158 *{"${class}::${method}"} = sub {
33 16 ▶ @_ > 1 ? $_[0]->_metric_name_to_value($metric, $_[1]) : $_[0]->_metric_value_to_name($metric);
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 8 8 16
34 158 };
35  
36 # Create metric alias
37 158 ▶ *{"${class}::${metric}"} = sub { $_[0]->M($metric) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
38  
39 }
40  
41 }
42  
43 sub new {
44  
45 2446 ▶ my ($class, %params) = @_;
Subroutine new: called CC=7 SCAR=20.0
Pod undocumented
46  
47 2446 ▶ $params{metrics} //= {};
Condition100%2/2
$params{"metrics"} //= {}
l!l
24451
MC/DC100%1/1
$params{"metrics"} //= {}
$params{"metrics"}
Truth table100%2/2
A
A= $params{"metrics"}
Aresult
11
00
48 2446 ▶ $params{scores} //= {};
Condition50%1/2
$params{"scores"} //= {}
l!l
02446
MC/DC0%0/1
$params{"scores"} //= {}
$params{"scores"}
Truth table50%1/2
A
A= $params{"scores"}
Aresult
11
00
49 2446 ▶ $params{vector_string} //= undef;
Condition100%2/2
$params{"vector_string"} //= undef
l!l
24451
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 2446 my $self = bless {%params}, $class;
52  
53 2446 ▶ 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 2446 2446
54 0 Carp::croak 'Invalid CVSS version';
55 }
56  
57 2446 ▶ if ($self->{vector_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"vector_string"}) 2445 1 2446
58  
59 2445 DEBUG and say STDERR sprintf('-- Validate vector string: %s', $self->VECTOR_STRING_REGEX);
60  
61 2445 ▶ if ($self->{vector_string} !~ $self->VECTOR_STRING_REGEX) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"vector_string"} =~ $self->VECTOR_STRING_REGEX) 0 2445 2445
62 0 Carp::croak 'Invalid CVSS vector string';
63 }
64  
65 2445 $self->calculate_score;
66  
67 }
68  
69 2446 return $self;
70  
71 }
72  
73 sub from_vector_string {
74  
75 2443 ▶ my ($class, $vector_string) = @_;
Subroutine from_vector_string: called CC=2 SCAR=6.9
Pod undocumented
76  
77 2443 my %metrics = split /[\/:]/, $vector_string;
78 2443 ▶ my $version = delete $metrics{CVSS} || '2.0';
Condition100%2/2
delete $metrics{'CVSS'} || '2.0'
l!l
2075368
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 2443 DEBUG and say STDERR "-- Vector String: $vector_string";
81 2443 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 ▶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 sub _metric_name_to_value {
95 8 ▶ my ($self, $metric, $name) = @_;
Subroutine _metric_name_to_value: called CC=1 SCAR=0.0
96 8 $name =~ s/\s/_/g;
97 8 $self->metrics->{$metric} = $self->METRIC_NAMES->{$metric}->{names}->{$name};
98 8 return $self;
99 }
100  
101 sub _metric_value_to_name {
102 8 ▶ my ($self, $metric) = @_;
Subroutine _metric_value_to_name: called CC=1 SCAR=0.0
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") 0 5501 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 0 1 1
108 153080 ▶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 2450 ▶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 sub metric_group_is_set {
139  
140 4038 ▶ my ($self, $type) = @_;
Subroutine metric_group_is_set: called CC=4 SCAR=15.2
Pod documented
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 sub metric {
149 0 ▶ my ($self, $metric) = @_;
Subroutine metric: not called CC=1 SCAR=6.9
Pod documented
150 0 my $value = $self->M($metric);
151  
152 0 return $self->METRIC_NAMES->{$metric}->{values}->{$value};
153 }
154  
155 95222 ▶sub M { $_[0]->metrics->{$_[1]} }
Subroutine M: called CC=1 SCAR=0.0
Pod documented
156  
157 sub score_to_severity {
158  
159 4 ▶ my ($self, $score) = @_;
Subroutine score_to_severity: called CC=5 SCAR=16.1
Pod documented
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 10 my $range = $SCORE_SEVERITY->{$_};
167 10 ▶ if ($score >= $range->{min} && $score <= $range->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score >= $range->{"min"} and $score <= $range->{"max"}) 2 8 10
Condition100%3/3
$score >= $range->{"min"} and $score <= $range->{"max"}
!ll&&!rl&&r
262
MC/DC100%2/2
$score >= $range->{"min"} and $score <= $range->{"max"}
$score >= $range->{"min"} $score <= $range->{"max"}
Truth table100%3/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 sub to_vector_string {
183  
184 2444 ▶ my ($self) = @_;
Subroutine to_vector_string: called CC=11 SCAR=24.2
Pod documented
185  
186 2444 my $metrics = $self->metrics;
187 2444 my @vectors = ();
188  
189 2444 ▶ if ($self->version > 2.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version > 2) 2076 368 2444
190 2076 push @vectors, sprintf('CVSS:%s', $self->version);
191 }
192  
193 2444 foreach my $metric (@{$self->METRIC_GROUPS->{base}}) {
194 19551 ▶ return if (!$metrics->{$metric});
Branch: 1 branch
BranchTrueFalseTotal
unless $metrics->{$metric} 0 19551 19551
195 19551 push @vectors, sprintf('%s:%s', $metric, $metrics->{$metric});
196 }
197  
198 2444 my @other_metrics = ();
199  
200 2444 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{threat} || []}; # CVSS 4.0
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"threat"} or [] 2199 245 2444
201 2444 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{temporal} || []}; # CVSS 2.0-3.x
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"temporal"} or [] 245 2199 2444
202 2444 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{environmental} || []}; # CVSS 2.0-3.x-4.0
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"environmental"} or [] 0 2444 2444
203 2444 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{supplemental} || []}; # CVSS 4.0
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"supplemental"} or [] 2199 245 2444
204  
205 2444 foreach my $metric (@other_metrics) {
206 33723 ▶ 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 33723 33723
Condition66%2/3
defined $metrics->{$metric} and $metrics->{$metric} ne $self->NOT_DEFINED_VALUE
!ll&&!rl&&r
368333550
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 2444 return join '/', @vectors;
212  
213 }
214  
215 sub TO_JSON {
216  
217 1 ▶ my ($self) = @_;
Subroutine TO_JSON: called CC=16 SCAR=34.2
Pod documented
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
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"threat"} or [] 1 0 1
245 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{temporal} || []}; # CVSS 2.0-3.x
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"temporal"} or [] 0 1 1
246 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{environmental} || []}; # CVSS 2.0-3.x-4.0
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"environmental"} or [] 0 1 1
247 1 ▶ push @other_metrics, @{$self->METRIC_GROUPS->{supplemental} || []}; # CVSS 4.0
Branch: 1 branch
BranchTrueFalseTotal
$self->METRIC_GROUPS->{"supplemental"} or [] 1 0 1
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 »