script/cpan-audit

filter: Statementstmt 85.8% 133 / 155 Branchbran 58.0% 29 / 50 Conditioncond 44.4% 8 / 18 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 91.3% 21 / 23 Podpod n/a totaltotal 75.2% 192 / 255 CC 38 SCAR 23.3
CC
38
Cov
76%
run
22.0
format_advisory
12.9
format_text
12.0
CRAP 10.3
« lib/CPAN/Audit.pm ↑ Summary blib/lib/CPAN/Audit/Installed.pm »
1 #!/usr/bin/env perl
2 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use open qw(:std :encoding(UTF-8));
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use IO::Interactive qw(is_interactive);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use CPAN::Audit;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = "1.503";
12  
13 11 ▶__PACKAGE__->run( @ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 11 0 11
14  
15 # The exit code indicates the number of advisories, up to this max
16 # since we have a limited number of exit codes.
17 11 ▶use constant ADVISORY_COUNT_MAX => 62;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 ▶use constant EXIT_NORMAL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use constant EXIT_ZERO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use constant EXIT_USAGE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use constant EXIT_BASE => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 my $output_table;
25 11 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 $output_table = {
27 text => \&format_text,
28 dumper => \&format_dump,
29 json => \&format_json,
30 default => \&format_text,
31 };
32 }
33  
34 8 ▶sub format_advisory {
Subroutine format_advisory: called CC=12 SCAR=25.5
35 8 my ($advisory) = @_;
36 8 my $s = " __BOLD__* $advisory->{id}__RESET__\n";
37 8 $s .= " $advisory->{description}\n";
38  
39 8 ▶ if ( $advisory->{affected_versions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($advisory->{"affected_versions"}) 8 0 8
40 8 ▶ my @v = ref $advisory->{affected_versions} ? @{$advisory->{affected_versions}} : $advisory->{affected_versions};
Branch: 1 branch
BranchTrueFalseTotal
ref $advisory->{'affected_versions'} ? : 8 0 8
41 8 my $first = shift @v;
42 8 $s .= " Affected range: $first\n";
43 8 $s .= " $_\n" for @v;
44 }
45  
46 8 ▶ if ( $advisory->{fixed_versions} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($advisory->{"fixed_versions"}) 8 0 8
47 8 ▶ my @v = ref $advisory->{fixed_versions} ? @{$advisory->{fixed_versions}} : $advisory->{fixed_versions};
Branch: 1 branch
BranchTrueFalseTotal
ref $advisory->{'fixed_versions'} ? : 8 0 8
48 8 my $first = shift @v;
49 8 ▶ $first //= '';
Condition50%1/2
$first //= ""
l!l
08
MC/DC0%0/1
$first //= ""
$first
Truth table50%1/2
A
A= $first
Aresult
11
00
50 8 $s .= " Fixed range: $first\n";
51 8 $s .= " $_\n" for @v;
52 }
53  
54 8 ▶ if ( $advisory->{cves} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($advisory->{"cves"}) 8 0 8
55 8 $s .= "\n CVEs: ";
56 8 $s .= join ', ', @{ $advisory->{cves} };
57 8 $s .= "\n";
58 }
59  
60 8 ▶ if ( $advisory->{references} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($advisory->{"references"}) 8 0 8
61 8 $s .= "\n References:\n";
62 8 ▶ foreach my $reference ( @{ $advisory->{references} || [] } ) {
Condition50%1/2
$advisory->{'references'} || []
l!l
80
MC/DC0%0/1
$advisory->{'references'} || []
$advisory->{'references'}
Truth table50%1/2
A
A= $advisory->{'references'}
Aresult
11
00
63 20 $s .= " $reference\n";
64 }
65 }
66  
67 8 $s .= "\n";
68 8 return $s;
69 }
70  
71 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 0 ▶sub dumper { Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump }
Subroutine dumper: not called CC=1 SCAR=6.9
73 0 ▶sub format_dump {
Subroutine format_dump: not called CC=1 SCAR=6.9
74 0 my( $result ) = @_;
75 0 return dumper($result);
76 }
77  
78 6 ▶sub format_json {
Subroutine format_json: called CC=1 SCAR=0.0
79 6 state $rc = require JSON;
80 6 my( $result ) = @_;
81 6 return JSON::encode_json($result);
82 }
83  
84 2 ▶sub format_text {
Subroutine format_text: called CC=9 SCAR=24.8
85 2 my( $result, $opts ) = @_;
86 2 my $s = '';
87  
88 2 foreach my $distname ( keys %{ $result->{dists} } ) {
89 8 my $advisories = $result->{dists}{$distname}{advisories};
90 $s .= sprintf("__RED__%s (%s %s) has %d advisor%s__RESET__\n",
91 $distname,
92 ($result->{meta}{command} eq 'installed' ? 'have' : 'requires'),
93 $result->{dists}{$distname}{version},
94 8 ▶ scalar(@$advisories),
Branch: 2 branches
BranchTrueFalseTotal
$result->{'meta'}{'command'} eq 'installed' ? : 8 0 8
scalar @$advisories == 1 ? : 8 0 8
95 (scalar(@$advisories) == 1 ? 'y' : 'ies'),
96 );
97  
98 8 foreach my $advisory ( @$advisories ) {
99 8 $s .= format_advisory( $advisory );
100 }
101 }
102  
103 2 ▶ $s .= "\n" if length $s;
Branch: 1 branch
BranchTrueFalseTotal
if length $s 2 0 2
104  
105 2 ▶ if ( $opts->{'no-color'} or $opts->{'ascii'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'no-color'} or $opts->{'ascii'}) { } 0 2 2
Condition33%1/3
$opts->{"no-color"} or $opts->{"ascii"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts->{"no-color"} or $opts->{"ascii"}
$opts->{"no-color"} $opts->{"ascii"}
Truth table33%1/3
A or B
A= $opts->{"no-color"}B= $opts->{"ascii"}
ABresult
1X1
011
000
106 0 $s =~ s{__BOLD__}{}g;
107 0 $s =~ s{__GREEN__}{}g;
108 0 $s =~ s{__RED__}{}g;
109 0 $s =~ s{__RESET__}{}g;
110 }
111 else {
112 2 $s =~ s{__BOLD__}{\e[39;1m}g;
113 2 $s =~ s{__GREEN__}{\e[32m}g;
114 2 $s =~ s{__RED__}{\e[31m}g;
115 2 $s =~ s{__RESET__}{\e[0m}g;
116  
117 2 ▶ $s .= "\e[0m" if length $s;
Branch: 1 branch
BranchTrueFalseTotal
if length $s 2 0 2
118 }
119  
120 2 return $s;
121 }
122  
123 1 ▶sub output_version {
Subroutine output_version: called CC=4 SCAR=14.8
124 1 my( $class, $exit_code ) = @_;
125  
126 1 print <<"HERE";
127 $0 version $VERSION using:
128 1 \tCPAN::Audit @{[ CPAN::Audit->VERSION ]}
129 1 ▶\tCPAN::Audit::DB @{[ CPAN::Audit::DB->VERSION // '<not installed>' ]} (deprecated)
Condition50%1/2
"CPAN::Audit::DB"->VERSION // "<not installed>"
l!l
01
MC/DC0%0/1
"CPAN::Audit::DB"->VERSION // "<not installed>"
"CPAN::Audit::DB"->VERSION
Truth table50%1/2
A
A= "CPAN::Audit::DB"->VERSION
Aresult
11
00
130 1 ▶\tCPANSA::DB @{[ ( eval { require CPANSA::DB } && CPANSA::DB->VERSION) // '<not installed>' ]}
Condition40%2/5
eval { do { require CPANSA::DB } } && "CPANSA::DB"->VERSION // "<not installed>"
l!l
10
eval { do { require CPANSA::DB } } and "CPANSA::DB"->VERSION
!ll&&!rl&&r
001
MC/DC0%0/2
eval { do { require CPANSA::DB } } && "CPANSA::DB"->VERSION // "<not installed>"
eval { do { require CPANSA::DB } } "CPANSA::DB"->VERSION
Truth table33%1/3
A and B
A= eval { do { require CPANSA::DB } }B= "CPANSA::DB"->VERSION
ABresult
111
0X0
100
131  
132 HERE
133  
134 1 exit($exit_code);
135 }
136  
137 11 ▶sub run {
Subroutine run: called CC=14 SCAR=30.9
138 11 my( $class, @args ) = @_;
139  
140 11 my( $opts ) = $class->process_options( \@args );
141 11 ▶ unless( ! $opts->{interactive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"interactive"}) 0 11 11
142 0 $opts->{ascii} = 1;
143 0 ▶ $opts->{no_color} //= 1;
Condition0%0/2
$opts->{"no_color"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"no_color"} //= 1
$opts->{"no_color"}
Truth table0%0/2
A
A= $opts->{"no_color"}
Aresult
11
00
144 }
145  
146 11 ▶ $class->usage(EXIT_NORMAL) if $opts->{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"help"} 1 10 11
147 10 ▶ $class->output_version(EXIT_NORMAL) if $opts->{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"version"} 1 9 10
148  
149 9 ▶ if( $opts->{fresh_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"fresh_check"}) 0 9 9
150 0 require CPAN::Audit::FreshnessCheck;
151 0 CPAN::Audit::FreshnessCheck->import
152 }
153  
154 9 my $command = shift @args;
155 9 ▶ $class->usage(EXIT_USAGE) unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 1 8 9
156  
157 8 my %extra = (
158 interactive => is_interactive(),
159 );
160  
161 8 my $audit = CPAN::Audit->new( %$opts, %extra );
162  
163 8 my $result = $audit->command( $command, @args );
164  
165 8 ▶ if( @{ $result->{errors} } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$result->{"errors"};} > 0) 0 8 8
166 0 my $message = join "\n", map "Error: $_", @{ $result->{errors} };
167 0 ▶ unless( $opts->{'no-color'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"no-color"}) 0 0 0
168 0 $message = "\e[31m" . $message . "\e[0m"
169 }
170 0 print STDERR $message;
171 0 exit 255;
172 }
173  
174 8 my( $output_type ) = grep { $opts->{$_} } qw(json);
175 8 ▶ my $sub = $output_table->{$output_type // 'default'};
Condition100%2/2
$output_type // "default"
l!l
62
MC/DC100%1/1
$output_type // "default"
$output_type
Truth table100%2/2
A
A= $output_type
Aresult
11
00
176  
177 8 my $output = $sub->( $result, $opts );
178 8 ▶ if( $command eq 'show' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "show") 0 8 8
179 0 $output =~ s/\A.*\n//;
180 }
181  
182 8 print $output;
183  
184 8 my $advisory_count = $result->{meta}{total_advisories};
185 8 ▶ $advisory_count = ADVISORY_COUNT_MAX if $advisory_count > ADVISORY_COUNT_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $advisory_count > 62 0 8 8
186  
187 8 my $exit_code = do {
188 8 ▶ if( $opts->{exit_zero} ) { EXIT_ZERO }
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'exit_zero'}) { } 1 7 8
elsif ($advisory_count == 0) { } 0 7 7
189 0 elsif( $advisory_count == 0 ) { EXIT_NORMAL }
190 7 else { EXIT_BASE + $advisory_count }
191 };
192  
193 8 exit( $exit_code );
194 }
195  
196 11 ▶sub process_options {
Subroutine process_options: called CC=3 SCAR=11.0
197 11 my( $class, $args ) = @_;
198 11 require Getopt::Long;
199  
200 11 my $options = {};
201  
202 11 my %params = ();
203 my $params = {
204 'ascii' => \$params{ascii},
205 'f|fresh' => \$params{fresh_check},
206 'help|h' => \$params{help},
207 'json' => \$params{json},
208 'no-color' => \$params{no_color},
209 'no-corelist' => \$params{no_corelist},
210 'perl' => \$params{include_perl},
211 'quiet|q' => \$params{quiet},
212 'verbose|v' => \$params{verbose},
213 'version' => \$params{version},
214 'exclude=s@' => \$params{exclude},
215 'exclude-file=s@' => \$params{exclude_file},
216 'modules=s@' => \$params{modules},
217 'exit-zero' => \$params{exit_zero},
218 11 };
219  
220 11 ▶ my $ret = Getopt::Long::GetOptionsFromArray( $args, $options, %$params )
Branch: 1 branch
BranchTrueFalseTotal
my $ret = &Getopt::Long::GetOptionsFromArray($args, $options, %$params) or $class->usage(2) 11 0 11
221 or $class->usage(EXIT_USAGE);
222  
223 11 ▶ $params{quiet} = 1 if $params{json};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"json"} 6 5 11
224  
225 11 \%params;
226 }
227  
228 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.1
229 2 require Pod::Usage;
230 2 require FindBin;
231  
232 2 my( $class, $exit_code ) = @_;
233 11 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
234 2 Pod::Usage::pod2usage( -input => $FindBin::Bin . "/" . $FindBin::Script );
235 0 print <<'HERE';
236 NAME
237 cpan-audit - Audit CPAN modules
238  
239 SYNOPSIS
240 cpan-audit [command] [options]
241  
242 Commands:
243  
244 module [version range] audit module with optional version range (all by default)
245 modules [version range] audit module list with optional version range (all by default)
246 dist|release [version range] audit distribution with optional version range (all by default)
247 deps [directory] audit dependencies from the directory (. by default)
248 installed audit all installed modules
249 show [advisory id] show information about specific advisory
250  
251 Options:
252  
253 --ascii use ascii output
254 --fresh|f check the database for freshness (CPAN::Audit::FreshnessCheck)
255 --help|h show the help message and exit
256 --no-color switch off colors
257 --no-corelist ignore modules bundled with perl version
258 --perl include perl advisories
259 --quiet be quiet (overrules --verbose)
260 --verbose be verbose (off if --quiet in effect)
261 --version show the version and exit
262 --exit-zero always exit with 0 even if advisories are reported
263 --exclude <str> exclude/ignore the specified advisory/cve (multiple)
264 --exclude-file <file> read exclude/ignore patterns from file
265 --json output JSON
266  
267 Examples:
268  
269 cpan-audit dist Catalyst-Runtime
270 cpan-audit dist Catalyst-Runtime 7.0
271 cpan-audit dist Catalyst-Runtime '>5.48'
272  
273 cpan-audit module Catalyst 7.0
274  
275 cpan-audit modules "Catalyst;7.0" "Mojolicious;>8.40,<9.20"
276  
277 cpan-audit deps .
278 cpan-audit deps /path/to/distribution
279  
280 cpan-audit installed
281 cpan-audit installed local/
282 cpan-audit installed local/ --exclude CVE-2011-4116
283 cpan-audit installed local/ --exclude CVE-2011-4116 --exclude CVE-2011-123
284 cpan-audit installed local/ --exclude-file ignored-cves.txt
285  
286 cpan-audit installed --json
287  
288 cpan-audit installed --json --exit-zero
289  
290 cpan-audit show CPANSA-Mojolicious-2018-03
291  
292 DESCRIPTION
293 "cpan-audit" is a command line application that checks the modules or
294 distributions for known vulnerabilities. It is using its internal
295 database that is automatically generated from a hand-picked database
296 <https://github.com/briandfoy/cpan-security-advisory>.
297  
298 "cpan-audit" does not connect to anything, that is why it is important
299 to keep it up to date. Every update of the internal database is released
300 as a new version. Ensure that you have the latest database by updating
301 CPAN::Audit frequently; the database can change daily. You can use
302 enable a warning for a possibly out-of-date database by adding
303 "--fresh", which warns if the database version is older than a month:
304  
305 % cpan-audit --fresh ...
306 % cpan-audit -f ...
307  
308 % env CPAN_AUDIT_FRESH_DAYS=7 cpan-audit -f ...
309  
310 Finding dependencies
311 "cpan-audit" can automatically detect dependencies from the following
312 sources:
313  
314 "Carton"
315 Parses cpanfile.snapshot file and checks the distribution versions.
316  
317 cpanfile
318 Parses cpanfile taking into account the required versions.
319  
320 It is assumed that if the required version of the module is less than a
321 version of a release with a known vulnerability fix, then the module is
322 considered affected.
323  
324 JSON data
325 If you request JSON output, the data looks like
326  
327 {
328 "meta" : {
329 ... meta information ...
330 "dists": {
331 "<distribution1>": {
332 ... distribution info ...
333 }
334 }
335 "errors" : [
336 ... list of errors - if any ...
337 ]
338 }
339  
340 Meta information
341 The meta data contains information about the run of "cpan-audit".
342  
343 {
344 "args": [
345 "Mojo::File",
346 "Mojo::UserAgent",
347 "LWP::UserAgent"
348 ],
349 "cpan_audit": {
350 "version": "20230601.002"
351 },
352 "total_advisories": 19,
353 "command": "modules"
354 }
355  
356 These information are shown
357  
358 * cpan_audit
359  
360 The version of "cpan_audit" that is used for the audit
361  
362 * command
363  
364 The command of "cpan_audit" that was run
365  
366 * args
367  
368 Arguments for the command
369  
370 * total_advisories
371  
372 Number of found advisories
373  
374 Distribution information
375 For each distribution where at least one advisory was found, the JSON
376 looks like:
377  
378 "Dist-Name": {
379 "queried_modules": [
380 "Queried::Namespace"
381 ],
382 "version": "Any",
383 "advisories": [
384 {
385 ... advisory data as in the audit database ...
386 },
387 ... more advisories ...
388 ]
389 },
390  
391 The advisory data is basically the data from the database. So this
392 depends on what is known for the given advisory.
393  
394 The distribution information contains:
395  
396 * version
397  
398 The version (range) that is checked for advisories. If there's no
399 version specified, all versions are checked and the version is
400 report as "Any".
401  
402 * queried_modules
403  
404 The actual namespaces queried, either from the command line or
405 another source, such as a cpanfile.
406  
407 * advisories
408  
409 A list of all vulnerabilities found for the version range
410  
411 Exit values
412 In prior versions, "cpan-audit" exited with the number of advisories it
413 found. Starting with 1.001, if there are advisories found, "cpan-audit"
414 exits with 64 added to that number. The maximum number of reported
415 advisories is 62, since values over 126 are spoken for.
416  
417 If the option "--exit-zero" is set "cpan-audit" exits always with a
418 normal exit code (0). This allows to use "cpan-audit" in build
419 environments together with bash exit mode activated ("set -e").
420  
421 * 0 - no advisories found
422  
423 * 2 - problem with program invocation, such as bad switches or values
424  
425 * 64+n - advisories found. Subtract 64 to get the advisory count, up
426 to 62 advisories
427  
428 * 255 - unspecified program error
429  
430 LICENSE
431 Copyright (C) Viacheslav Tykhanovskyi.
432  
433 This library is free software; you can redistribute it and/or modify it
434 under the same terms as Perl itself.HERE
435 HERE
436  
437 0 exit( $exit_code );
438 }
439  
440 __END__
« lib/CPAN/Audit.pm ↑ Summary blib/lib/CPAN/Audit/Installed.pm »