blib/lib/Net/CVE.pm

filter: Statementstmt 96.0% 146 / 152 Branchbran 64.8% 48 / 74 Conditioncond 52.9% 18 / 34 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 100% 22 / 22 Podpod 90.0% 9 / 10 totaltotal 78.5% 245 / 312 CC 56 SCAR 23.3
CC
56
Cov
82%
summary
21.4
diag
18.1
get
10.0
CRAP 10.3
↑ Summary
1 #!/usr/bin/perl
2  
3 package Net::CVE;
4  
5 6 ▶use 5.014002;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = "0.010";
9  
10 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # https://cveawg.mitre.org/api/cve/CVE-2022-26928
16 # But that is likely to change to cve.org
17  
18 1 ▶sub Version { $VERSION }
Subroutine Version: called CC=1 SCAR=0.0
Pod undocumented
19  
20 21 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
21 21 my $class = shift;
22 21 my %r = (
23 url => "https://cveawg.mitre.org/api/cve",
24 ua => undef,
25 lang => "en",
26 data => {},
27 diag => undef,
28 );
29 21 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 15 21
30 6 ▶ if (@_ == 1 && ref $_[0] eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 1 5 6
elsif (@_ == 2) { } 5 0 5
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
501
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
31 1 $r{$_} = $_[0]{$_} for keys %{$_[0]};
32 }
33 elsif (@_ == 2) {
34 5 my %p = @_;
35 5 $r{$_} = $p{$_} for keys %p;
36 }
37 }
38 21 bless \%r => $class;
39 } # new
40  
41 5 ▶sub diag {
Subroutine diag: called CC=7 SCAR=28.9
Pod documented
42 5 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 5 0 5
43 5 ▶ ref $self or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $self or return 5 0 5
44 5 ▶ my $d = $self->{diag} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $self->{"diag"} or return 5 0 5
45 5 ▶ unless (defined wantarray) { # void context
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 5 5
46 0 my $act = $d->{action};
47 warn "$act: ",
48 0 (join " " => grep { length } $d->{status}, $d->{reason}), "\n";
49 0 $act =~ s/./ /g;
50 0 ▶ warn "$act source = $d->{source}\n" if $d->{source};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"source"} 0 0 0
51 0 ▶ warn "$act usage: $d->{usage}\n" if $d->{usage};
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"usage"} 0 0 0
52 }
53 5 return $d;
54 } # diag
55  
56 21 ▶sub get {
Subroutine get: called CC=10 SCAR=23.0
Pod documented
57 21 my ($self, $cve) = @_;
58 21 ▶ ref $self or $self = __PACKAGE__->new ();
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = __PACKAGE__->new 19 2 21
59 21 $self->{data} = {};
60 21 $self->{diag} = undef;
61 21 ▶ $cve or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$cve or return $self 20 1 21
62 20 $cve =~ s/^(?=[0-9])/CVE-/;
63 20 ▶ if ($cve =~ m/^CVE-[0-9]{4}-([0-9]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cve =~ /^CVE-[0-9]{4}-([0-9]+)$/u) { } 5 15 20
elsif (-s $cve) { } 14 1 15
64 5 ▶ $self->{ua} //= HTTP::Tiny->new ();
Condition33%1/3
$self->{"ua"} //= "HTTP::Tiny"->new
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->{"ua"} //= "HTTP::Tiny"->new
$self->{"ua"} "HTTP::Tiny"->new
Truth table33%1/3
A //= B
A= $self->{"ua"}B= "HTTP::Tiny"->new
ABresult
1X1
011
000
65 5 my $url = join "/" => $self->{url}, $cve;
66 5 my $r = $self->{ua}->get ($url);
67 5 ▶ unless ($r->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"success"}) 1 4 5
68 # if pseudo-HTTP status code 599 and reason "Internal Exception"
69 # the content field will contain the text of the error
70 1 my $status = $r->{status};
71 2 ▶ my $reason = join ": " => grep { length }
Branch: 1 branch
BranchTrueFalseTotal
$status =~ /^5[0-9][0-9]$/u ? : 1 0 1
72 1 $r->{reason}, $status =~ m/^5[0-9][0-9]$/ ? $r->{content} : "";
73 $self->{diag} = {
74 1 status => $status,
75 reason => $reason,
76 action => "get",
77 source => $url,
78 usage => undef,
79 };
80 1 return $self;
81 }
82 4 $self->{data} = decode_json ($r->{content});
83 }
84 elsif (-s $cve) {
85 14 my $fh;
86 14 ▶ unless (open $fh, "<:encoding(utf-8)", $cve) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<:encoding(utf-8)", $cve) 2 12 14
87 $self->{diag} = {
88 2 status => 0 + $!,
89 reason => "$!",
90 action => "get",
91 source => $cve,
92 usage => 'get ("cve-2022-26928.json")',
93 };
94 2 return $self;
95 }
96 12 ▶ unless (eval { $self->{data} = decode_json (do { local $/; <$fh> }); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->{"data"} = &decode_json(scalar do { local $/; readline $fh }); 1 } }) 2 10 12
97 $self->{diag} = {
98 2 status => -2,
99 reason => $@ =~ s{\s+at\s+\S+\s+line\s+\d+.*}{}rs,
100 action => "decode_json",
101 source => $cve,
102 usage => undef,
103 };
104 2 return $self;
105 }
106 10 close $fh;
107 }
108 else {
109 #warn "Invalid CVE format: '$cve' - expected format CVE-2023-12345\n";
110 $self->{diag} = {
111 1 status => -1,
112 reason => "Invalid CVE format: '$cve'",
113 action => "get",
114 source => "tag",
115 usage => 'get ("CVE-2022-26928")',
116 };
117 1 return $self;
118 }
119 14 $self;
120 } # get
121  
122 8 ▶sub data {
Subroutine data: called CC=3 SCAR=11.0
Pod documented
123 8 my $self = shift;
124 8 ▶ ref $self or $self = __PACKAGE__->new ();
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = __PACKAGE__->new 7 1 8
125 8 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 2 6 8
126 8 $self->{data};
127 } # data
128  
129 10 ▶sub summary {
Subroutine summary: called CC=19 SCAR=30.6
Pod documented
130 10 my $self = shift;
131 10 ▶ ref $self or $self = __PACKAGE__->new ();
Branch: 1 branch
BranchTrueFalseTotal
ref $self or $self = __PACKAGE__->new 9 1 10
132 10 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 2 8 10
133 10 ▶ my $j = $self->{data} or croak "summary only available after get";
Branch: 1 branch
BranchTrueFalseTotal
my $j = $self->{"data"} or croak("summary only available after get") 10 0 10
134 10 ▶ my $cna = $j->{containers}{cna} or return +{};
Branch: 1 branch
BranchTrueFalseTotal
my $cna = $j->{"containers"}{"cna"} or return {} 10 0 10
135 #y $weak = ... weaknesses [{ description [{ lang => "en", value => "CWE-123"}]
136  
137 10 ▶ my $severity = join ", " => grep { length } map { $_->{cvssV3_1}{baseSeverity} } @{$cna->{metrics} || []};
Condition50%1/2
$cna->{'metrics'} || []
l!l
100
MC/DC0%0/1
$cna->{'metrics'} || []
$cna->{'metrics'}
Truth table50%1/2
A
A= $cna->{'metrics'}
Aresult
11
00
138 10 ▶ my $score = join ", " => grep { length } map { $_->{cvssV3_1}{baseScore} } @{$cna->{metrics} || []};
Condition50%1/2
$cna->{'metrics'} || []
l!l
100
MC/DC0%0/1
$cna->{'metrics'} || []
$cna->{'metrics'}
Truth table50%1/2
A
A= $cna->{'metrics'}
Aresult
11
00
139  
140 10 my %desc;
141 10 ▶ for (@{$cna->{descriptions} || []}) {
Condition50%1/2
$cna->{'descriptions'} || []
l!l
100
MC/DC0%0/1
$cna->{'descriptions'} || []
$cna->{'descriptions'}
Truth table50%1/2
A
A= $cna->{'descriptions'}
Aresult
11
00
142 30 ▶ my $d = $_->{value} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $_->{"value"} or next 30 0 30
143 30 push @{$desc{$_->{lang}}} => $d;
144 }
145 10 my @lang = sort keys %desc;
146 10 ▶ my $lang = first { m/\b $self->{lang} /ix } @lang;
Subroutine __ANON__: called CC=1 SCAR=0.0
147 10 ▶ $lang //= first { m/\b en /ix } @lang;
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$lang //= &first(sub { /\b en /iux; } , @lang)
l!l&&r!l&&!r
910
MC/DC0%0/2
$lang //= &first(sub { /\b en /iux; } , @lang)
$lang &first(sub { /\b en /iux; } , @lang)
Truth table66%2/3
A //= B
A= $langB= &first(sub { /\b en /iux; } , @lang)
ABresult
1X1
011
000
148 10 ▶ $lang //= $lang[0];
Condition33%1/3
$lang //= $lang[0]
l!l&&r!l&&!r
1000
MC/DC0%0/2
$lang //= $lang[0]
$lang $lang[0]
Truth table33%1/3
A //= B
A= $langB= $lang[0]
ABresult
1X1
011
000
149 10 my $desc = join "\n" => @{$desc{$lang}};
150  
151 10 my %problem;
152 10 ▶ for (map { @{$_->{descriptions} || []} } @{$cna->{problemTypes} || []}) {
Condition50%2/4
$_->{'descriptions'} || []
l!l
100
$cna->{'problemTypes'} || []
l!l
100
MC/DC0%0/1
$_->{'descriptions'} || []
$_->{'descriptions'}
MC/DC0%0/1
$cna->{'problemTypes'} || []
$cna->{'problemTypes'}
Truth table50%1/2
A
A= $_->{'descriptions'}
Aresult
11
00
Truth table50%1/2
A
A= $cna->{'problemTypes'}
Aresult
11
00
153 22 ▶ my $d = $_->{description} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $d = $_->{"description"} or next 22 0 22
154 22 push @{$problem{$_->{lang}}} => $d;
155 }
156 10 @lang = sort keys %problem;
157 10 ▶ $lang = first { m/\b $self->{lang} /ix } @lang;
Subroutine __ANON__: called CC=1 SCAR=0.0
158 10 ▶ $lang //= first { m/\b en /ix } @lang;
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$lang //= &first(sub { /\b en /iux; } , @lang)
l!l&&r!l&&!r
802
MC/DC50%1/2
$lang //= &first(sub { /\b en /iux; } , @lang)
$lang &first(sub { /\b en /iux; } , @lang)
Truth table66%2/3
A //= B
A= $langB= &first(sub { /\b en /iux; } , @lang)
ABresult
1X1
011
000
159 10 ▶ $lang //= $lang[0];
Condition66%2/3
$lang //= $lang[0]
l!l&&r!l&&!r
820
MC/DC0%0/2
$lang //= $lang[0]
$lang $lang[0]
Truth table66%2/3
A //= B
A= $langB= $lang[0]
ABresult
1X1
011
000
160 10 ▶ my $problem = defined $lang ? join "\n" => @{$problem{$lang}} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $lang ? : 10 0 10
161  
162 { id => $j->{cveMetadata}{cveId},
163 date => $j->{cveMetadata}{datePublished},
164 10 description => $desc,
165 severity => lc $severity,
166 score => $score,
167 problem => $problem,
168 product => [ $self->product ],
169 vendor => [ $self->vendor ],
170 platforms => [ $self->platforms ],
171 status => $self->status,
172 };
173 } # summary
174  
175 10 ▶sub status {
Subroutine status: called CC=3 SCAR=11.4
Pod documented
176 10 my $self = shift;
177 10 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 0 10 10
178 10 ▶ my $j = $self->{data} or croak "status only available after get";
Branch: 1 branch
BranchTrueFalseTotal
my $j = $self->{"data"} or croak("status only available after get") 10 0 10
179 10 $j->{cveMetadata}{state};
180 } # status
181  
182 20 ▶sub _affected_tag {
Subroutine _affected_tag: called CC=5 SCAR=16.8
183 20 my ($self, $tag) = @_;
184 20 ▶ my $j = $self->{data} or croak "$tag only available after get";
Branch: 1 branch
BranchTrueFalseTotal
my $j = $self->{"data"} or croak("$tag only available after get") 20 0 20
185 20 ▶ my $cna = $j->{containers}{cna} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cna = $j->{"containers"}{"cna"} or return 20 0 20
186 20 ▶ my %v = map { $_->{$tag} => 1 } @{$cna->{affected} || []};
Condition50%1/2
$cna->{'affected'} || []
l!l
200
MC/DC0%0/1
$cna->{'affected'} || []
$cna->{'affected'}
Truth table50%1/2
A
A= $cna->{'affected'}
Aresult
11
00
187 20 my @v = sort keys %v;
188 20 ▶ return wantarray ? @v : join ", " => @v;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 20 0 20
189 } # _affected_tag
190  
191 10 ▶sub _affected_tag_a {
Subroutine _affected_tag_a: called CC=6 SCAR=18.7
192 10 my ($self, $tag) = @_;
193 10 ▶ my $j = $self->{data} or croak "$tag only available after get";
Branch: 1 branch
BranchTrueFalseTotal
my $j = $self->{"data"} or croak("$tag only available after get") 10 0 10
194 10 ▶ my $cna = $j->{containers}{cna} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cna = $j->{"containers"}{"cna"} or return 10 0 10
195 139 ▶ my %v = map { $_ => 1 } map { @{$_ || []} } map { $_->{$tag} }
Condition50%1/2
$_ || []
l!l
760
MC/DC0%0/1
$_ || []
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
196 10 ▶ @{$cna->{affected} || []};
Condition50%1/2
$cna->{'affected'} || []
l!l
100
MC/DC0%0/1
$cna->{'affected'} || []
$cna->{'affected'}
Truth table50%1/2
A
A= $cna->{'affected'}
Aresult
11
00
197 10 my @v = sort keys %v;
198 10 ▶ return wantarray ? @v : join ", " => @v;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 0 10
199 } # _affected_tag_a
200  
201 10 ▶sub platforms {
Subroutine platforms: called CC=2 SCAR=7.1
Pod documented
202 10 my $self = shift;
203 10 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 0 10 10
204 10 $self->_affected_tag_a ("platforms");
205 } # platforms
206  
207 10 ▶sub vendor {
Subroutine vendor: called CC=2 SCAR=7.1
Pod documented
208 10 my $self = shift;
209 10 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 0 10 10
210 10 $self->_affected_tag ("vendor");
211 } # vendor
212  
213 10 ▶sub product {
Subroutine product: called CC=2 SCAR=7.1
Pod documented
214 10 my $self = shift;
215 10 ▶ @_ and $self->get (@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->get(@_) 0 10 10
216 10 $self->_affected_tag ("product");
217 } # vendor
218  
219 6 1;
220  
221 __END__
↑ Summary