blib/lib/Net/NVD.pm

filter: Statementstmt 100% 39 / 39 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 91.1% 62 / 68 SCAR 22.1
CC
9
Cov
89%
_build_url_params
6.2
_build_user_agent
2.1
get
2.1
CRAP 9.1
↑ Summary
1 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.0.2';
11  
12 2 package Net::NVD {
13 ▶ sub new ($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
14 2 return bless { ua => _build_user_agent($args{api_key}) }, $class;
15 }
16  
17 ▶ sub get ($self, $cve_id) {
Subroutine get: called CC=2 SCAR=7.2
Pod documented
18 1 my ($single) = $self->search( cve_id => $cve_id );
19 1 ▶ return $single ? $single->{cve} : ();
Branch: 1 branch
BranchTrueFalseTotal
$single ? : 1 0 1
20 }
21  
22 ▶ sub search ($self, %params) {
Subroutine search: called CC=2 SCAR=7.2
Pod documented
23 2 my $res = $self->{ua}->request('GET', 'https://services.nvd.nist.gov/rest/json/cves/2.0?' . _build_url_params($self->{ua}, %params));
24 2 ▶ return $res->{success} ? (JSON::decode_json($res->{content}))[0]{vulnerabilities}->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
$res->{'success'} ? : 2 0 2
25 }
26  
27 ▶ sub _build_user_agent($api_key) {
Subroutine _build_user_agent: called CC=2 SCAR=7.6
28 2 ▶ return HTTP::Tiny->new(
Branch: 1 branch
BranchTrueFalseTotal
$api_key ? : 0 2 2
29 agent => __PACKAGE__ . '/' . $VERSION,
30 verify_SSL => 1,
31 ($api_key ? (default_headers => { apiKey => $api_key }) : ()),
32 );
33 }
34  
35 ▶ sub _build_url_params ($ua, %params) {
Subroutine _build_url_params: called CC=6 SCAR=18.2
36 2 my $iso8061 = qr{\A\d{4}\-\d{2}\-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:[\+\-]\d{2}:\d{2})?\z};
37 2 my %translation = (
38 cpe_name => { name => 'cpeName' , validation => qr{\Acpe:2.3(\:[^*:]+){4}(\:[^:]+){7}\z} },
39 cve_id => { name => 'cveId' , validation => qr{\ACVE\-[0-9]{4}\-[0-9]+\z} },
40 cvssv2_metrics => { name => 'cvssV2Metrics' , validation => qr{.} },
41 cvssV2Severity => { name => 'cvssV2Severity' , validation => qr{\A(?:LOW|MEDIUM|HIGH)\z} },
42 cvssv3_metrics => { name => 'cvssV3Metrics' , validation => qr{.} },
43 cvssv3_severity => { name => 'cvssV3Severity' , validation => qr{\A(?:LOW|MEDIUM|HIGH|CRITICAL)\z} },
44 cwe_id => { name => 'cweId' , validation => qr{\ACWE\-\d+\z} },
45 keyword_search => { name => 'keywordSearch' , validation => qr{\A.+\z} },
46 last_mod_start_date => { name => 'lastModStartDate' , validation => $iso8061 },
47 last_mod_end_date => { name => 'lastModEndDate' , validation => $iso8061 },
48 pub_start_date => { name => 'pubStartDate' , validation => $iso8061 },
49 pub_end_date => { name => 'pubEndDate' , validation => $iso8061 },
50 results_per_page => { name => 'resultsPerPage' , validation => qr{\A\d+\z} },
51 start_index => { name => 'startIndex' , validation => qr{\A\d+\z} },
52 source_identifier => { name => 'sourceIdentifier' , validation => qr{.} },
53 version_end => { name => 'versionEnd' , validation => qr{.} },
54 version_end_type => { name => 'versionEndType' , validation => qr{\A(?:including|excluding)\z} },
55 version_start => { name => 'versionStart' , validation => qr{.} },
56 version_start_type => { name => 'versionStartType' , validation => qr{\A(?:including|excluding)\z} },
57 virtual_match_string => { name => 'virtualMatchString', validation => qr{.} },
58 has_cert_alerts => { name => 'hasCertAlerts' , boolean => 1 },
59 has_cert_notes => { name => 'hasCertNotes' , boolean => 1 },
60 has_kev => { name => 'hasKev' , boolean => 1 },
61 has_oval => { name => 'hasOval' , boolean => 1 },
62 is_vulnerable => { name => 'isVulnerable' , boolean => 1 },
63 keyword_exact_match => { name => 'keywordExactMatch' , boolean => 1 },
64 no_rejected => { name => 'noRejected' , boolean => 1 },
65 );
66  
67 2 my @params;
68 my %translated;
69 2 foreach my $p (keys %params) {
70 4 ▶ Carp::croak("'$p' is not a valid search parameter") unless exists $translation{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $translation{$p} 0 4 4
71 4 ▶ if ($translation{$p}{boolean}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation{$p}{'boolean'}) { } 1 3 4
72 1 ▶ push @params, $translation{$p}{name} if delete $params{$p};
Branch: 1 branch
BranchTrueFalseTotal
if delete $params{$p} 1 0 1
73 }
74 else {
75 3 ▶ Carp::croak("invalid value '$params{$p}' for '$p'") unless $params{$p} =~ $translation{$p}{validation};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{$p} =~ /$translation{$p}{"validation"}/u 0 3 3
76 3 $translated{$translation{$p}{name}} = $params{$p};
77 }
78 }
79 2 return join('&', @params, $ua->www_form_urlencode(\%translated));
80 }
81 };
82  
83 2 1;
84 __END__
↑ Summary