1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use IO::Interactive qw(is_interactive);
use CPAN::Audit;
our $VERSION = "1.503";
__PACKAGE__->run( @ARGV ) unless caller;
# The exit code indicates the number of advisories, up to this max
# since we have a limited number of exit codes.
use constant ADVISORY_COUNT_MAX => 62;
use constant EXIT_NORMAL => 0;
use constant EXIT_ZERO => 0;
use constant EXIT_USAGE => 2;
use constant EXIT_BASE => 64;
my $output_table;
BEGIN {
$output_table = {
text => \&format_text,
dumper => \&format_dump,
json => \&format_json,
default => \&format_text,
};
}
sub format_advisory {
my ($advisory) = @_;
my $s = " __BOLD__* $advisory->{id}__RESET__\n";
$s .= " $advisory->{description}\n";
if ( $advisory->{affected_versions} ) {
my @v = ref $advisory->{affected_versions} ? @{$advisory->{affected_versions}} : $advisory->{affected_versions};
my $first = shift @v;
$s .= " Affected range: $first\n";
$s .= " $_\n" for @v;
}
if ( $advisory->{fixed_versions} ) {
my @v = ref $advisory->{fixed_versions} ? @{$advisory->{fixed_versions}} : $advisory->{fixed_versions};
my $first = shift @v;
$first //= '';
$s .= " Fixed range: $first\n";
$s .= " $_\n" for @v;
}
if ( $advisory->{cves} ) {
$s .= "\n CVEs: ";
$s .= join ', ', @{ $advisory->{cves} };
$s .= "\n";
}
if ( $advisory->{references} ) {
$s .= "\n References:\n";
foreach my $reference ( @{ $advisory->{references} || [] } ) {
$s .= " $reference\n";
}
}
$s .= "\n";
return $s;
}
use Data::Dumper;
sub dumper { Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump }
sub format_dump {
my( $result ) = @_;
return dumper($result);
}
sub format_json {
state $rc = require JSON;
my( $result ) = @_;
return JSON::encode_json($result);
}
sub format_text {
my( $result, $opts ) = @_;
my $s = '';
foreach my $distname ( keys %{ $result->{dists} } ) {
my $advisories = $result->{dists}{$distname}{advisories};
$s .= sprintf("__RED__%s (%s %s) has %d advisor%s__RESET__\n",
$distname,
($result->{meta}{command} eq 'installed' ? 'have' : 'requires'),
$result->{dists}{$distname}{version},
scalar(@$advisories),
(scalar(@$advisories) == 1 ? 'y' : 'ies'),
);
foreach my $advisory ( @$advisories ) {
$s .= format_advisory( $advisory );
}
}
$s .= "\n" if length $s;
if ( $opts->{'no-color'} or $opts->{'ascii'} ) {
$s =~ s{__BOLD__}{}g;
$s =~ s{__GREEN__}{}g;
$s =~ s{__RED__}{}g;
$s =~ s{__RESET__}{}g;
}
else {
$s =~ s{__BOLD__}{\e[39;1m}g;
$s =~ s{__GREEN__}{\e[32m}g;
$s =~ s{__RED__}{\e[31m}g;
$s =~ s{__RESET__}{\e[0m}g;
$s .= "\e[0m" if length $s;
}
return $s;
}
sub output_version {
my( $class, $exit_code ) = @_;
print <<"HERE";
$0 version $VERSION using:
\tCPAN::Audit @{[ CPAN::Audit->VERSION ]}
\tCPAN::Audit::DB @{[ CPAN::Audit::DB->VERSION // '<not installed>' ]}
\tCPANSA::DB @{[ ( eval { require CPANSA::DB } && CPANSA::DB->VERSION) // '<not installed>' ]}
HERE
exit($exit_code);
}
sub run {
my( $class, @args ) = @_;
my( $opts ) = $class->process_options( \@args );
unless( ! $opts->{interactive} ) {
$opts->{ascii} = 1;
$opts->{no_color} //= 1;
}
$class->usage(EXIT_NORMAL) if $opts->{help};
$class->output_version(EXIT_NORMAL) if $opts->{version};
if( $opts->{fresh_check} ) {
require CPAN::Audit::FreshnessCheck;
CPAN::Audit::FreshnessCheck->import
}
my $command = shift @args;
$class->usage(EXIT_USAGE) unless defined $command;
my %extra = (
interactive => is_interactive(),
);
my $audit = CPAN::Audit->new( %$opts, %extra );
my $result = $audit->command( $command, @args );
if( @{ $result->{errors} } > 0 ) {
my $message = join "\n", map "Error: $_", @{ $result->{errors} };
unless( $opts->{'no-color'} ) {
$message = "\e[31m" . $message . "\e[0m"
}
print STDERR $message;
exit 255;
}
my( $output_type ) = grep { $opts->{$_} } qw(json);
my $sub = $output_table->{$output_type // 'default'};
my $output = $sub->( $result, $opts );
if( $command eq 'show' ) {
$output =~ s/\A.*\n//;
}
print $output;
my $advisory_count = $result->{meta}{total_advisories};
$advisory_count = ADVISORY_COUNT_MAX if $advisory_count > ADVISORY_COUNT_MAX;
my $exit_code = do {
if( $opts->{exit_zero} ) { EXIT_ZERO }
elsif( $advisory_count == 0 ) { EXIT_NORMAL }
else { EXIT_BASE + $advisory_count }
};
exit( $exit_code );
}
sub process_options {
my( $class, $args ) = @_;
require Getopt::Long;
my $options = {};
my %params = ();
my $params = {
'ascii' => \$params{ascii},
'f|fresh' => \$params{fresh_check},
'help|h' => \$params{help},
'json' => \$params{json},
'no-color' => \$params{no_color},
'no-corelist' => \$params{no_corelist},
'perl' => \$params{include_perl},
'quiet|q' => \$params{quiet},
'verbose|v' => \$params{verbose},
'version' => \$params{version},
'exclude=s@' => \$params{exclude},
'exclude-file=s@' => \$params{exclude_file},
'modules=s@' => \$params{modules},
'exit-zero' => \$params{exit_zero},
};
my $ret = Getopt::Long::GetOptionsFromArray( $args, $options, %$params )
or $class->usage(EXIT_USAGE);
$params{quiet} = 1 if $params{json};
\%params;
}
sub usage {
require Pod::Usage;
require FindBin;
my( $class, $exit_code ) = @_;
no warnings qw(once);
Pod::Usage::pod2usage( -input => $FindBin::Bin . "/" . $FindBin::Script );
print <<'HERE';
NAME
cpan-audit - Audit CPAN modules
SYNOPSIS
cpan-audit [command] [options]
Commands:
module [version range] audit module with optional version range (all by default)
modules [version range] audit module list with optional version range (all by default)
dist|release [version range] audit distribution with optional version range (all by default)
deps [directory] audit dependencies from the directory (. by default)
installed audit all installed modules
show [advisory id] show information about specific advisory
Options:
--ascii use ascii output
--fresh|f check the database for freshness (CPAN::Audit::FreshnessCheck)
--help|h show the help message and exit
--no-color switch off colors
--no-corelist ignore modules bundled with perl version
--perl include perl advisories
--quiet be quiet (overrules --verbose)
--verbose be verbose (off if --quiet in effect)
--version show the version and exit
--exit-zero always exit with 0 even if advisories are reported
--exclude <str> exclude/ignore the specified advisory/cve (multiple)
--exclude-file <file> read exclude/ignore patterns from file
--json output JSON
Examples:
cpan-audit dist Catalyst-Runtime
cpan-audit dist Catalyst-Runtime 7.0
cpan-audit dist Catalyst-Runtime '>5.48'
cpan-audit module Catalyst 7.0
cpan-audit modules "Catalyst;7.0" "Mojolicious;>8.40,<9.20"
cpan-audit deps .
cpan-audit deps /path/to/distribution
cpan-audit installed
cpan-audit installed local/
cpan-audit installed local/ --exclude CVE-2011-4116
cpan-audit installed local/ --exclude CVE-2011-4116 --exclude CVE-2011-123
cpan-audit installed local/ --exclude-file ignored-cves.txt
cpan-audit installed --json
cpan-audit installed --json --exit-zero
cpan-audit show CPANSA-Mojolicious-2018-03
DESCRIPTION
"cpan-audit" is a command line application that checks the modules or
distributions for known vulnerabilities. It is using its internal
database that is automatically generated from a hand-picked database
<https://github.com/briandfoy/cpan-security-advisory>.
"cpan-audit" does not connect to anything, that is why it is important
to keep it up to date. Every update of the internal database is released
as a new version. Ensure that you have the latest database by updating
CPAN::Audit frequently; the database can change daily. You can use
enable a warning for a possibly out-of-date database by adding
"--fresh", which warns if the database version is older than a month:
% cpan-audit --fresh ...
% cpan-audit -f ...
% env CPAN_AUDIT_FRESH_DAYS=7 cpan-audit -f ...
Finding dependencies
"cpan-audit" can automatically detect dependencies from the following
sources:
"Carton"
Parses cpanfile.snapshot file and checks the distribution versions.
cpanfile
Parses cpanfile taking into account the required versions.
It is assumed that if the required version of the module is less than a
version of a release with a known vulnerability fix, then the module is
considered affected.
JSON data
If you request JSON output, the data looks like
{
"meta" : {
... meta information ...
"dists": {
"<distribution1>": {
... distribution info ...
}
}
"errors" : [
... list of errors - if any ...
]
}
Meta information
The meta data contains information about the run of "cpan-audit".
{
"args": [
"Mojo::File",
"Mojo::UserAgent",
"LWP::UserAgent"
],
"cpan_audit": {
"version": "20230601.002"
},
"total_advisories": 19,
"command": "modules"
}
These information are shown
* cpan_audit
The version of "cpan_audit" that is used for the audit
* command
The command of "cpan_audit" that was run
* args
Arguments for the command
* total_advisories
Number of found advisories
Distribution information
For each distribution where at least one advisory was found, the JSON
looks like:
"Dist-Name": {
"queried_modules": [
"Queried::Namespace"
],
"version": "Any",
"advisories": [
{
... advisory data as in the audit database ...
},
... more advisories ...
]
},
The advisory data is basically the data from the database. So this
depends on what is known for the given advisory.
The distribution information contains:
* version
The version (range) that is checked for advisories. If there's no
version specified, all versions are checked and the version is
report as "Any".
* queried_modules
The actual namespaces queried, either from the command line or
another source, such as a cpanfile.
* advisories
A list of all vulnerabilities found for the version range
Exit values
In prior versions, "cpan-audit" exited with the number of advisories it
found. Starting with 1.001, if there are advisories found, "cpan-audit"
exits with 64 added to that number. The maximum number of reported
advisories is 62, since values over 126 are spoken for.
If the option "--exit-zero" is set "cpan-audit" exits always with a
normal exit code (0). This allows to use "cpan-audit" in build
environments together with bash exit mode activated ("set -e").
* 0 - no advisories found
* 2 - problem with program invocation, such as bad switches or values
* 64+n - advisories found. Subtract 64 to get the advisory count, up
to 62 advisories
* 255 - unspecified program error
LICENSE
Copyright (C) Viacheslav Tykhanovskyi.
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.HERE
HERE
exit( $exit_code );
}
__END__
=head1 NAME
cpan-audit - Audit CPAN modules
=head1 SYNOPSIS
cpan-audit [command] [options]
Commands:
module [version range] audit module with optional version range (all by default)
modules [version range] audit module list with optional version range (all by default)
dist|release [version range] audit distribution with optional version range (all by default)
deps [directory] audit dependencies from the directory (. by default)
installed audit all installed modules
show [advisory id] show information about specific advisory
Options:
--ascii use ascii output
--fresh|f check the database for freshness (CPAN::Audit::FreshnessCheck)
--help|h show the help message and exit
--no-color switch off colors
--no-corelist ignore modules bundled with perl version
--perl include perl advisories
--quiet be quiet (overrules --verbose)
--verbose be verbose (off if --quiet in effect)
--version show the version and exit
--exit-zero always exit with 0 even if advisories are reported
--exclude <str> exclude/ignore the specified advisory/cve (multiple)
--exclude-file <file> read exclude/ignore patterns from file
--json output JSON
Examples:
cpan-audit dist Catalyst-Runtime
cpan-audit dist Catalyst-Runtime 7.0
cpan-audit dist Catalyst-Runtime '>5.48'
cpan-audit module Catalyst 7.0
cpan-audit modules "Catalyst;7.0" "Mojolicious;>8.40,<9.20"
cpan-audit deps .
cpan-audit deps /path/to/distribution
cpan-audit installed
cpan-audit installed local/
cpan-audit installed local/ --exclude CVE-2011-4116
cpan-audit installed local/ --exclude CVE-2011-4116 --exclude CVE-2011-123
cpan-audit installed local/ --exclude-file ignored-cves.txt
cpan-audit installed --json
cpan-audit installed --json --exit-zero
cpan-audit show CPANSA-Mojolicious-2018-03
=head1 DESCRIPTION
C<cpan-audit> is a command line application that checks the modules or
distributions for known vulnerabilities. It is using its internal
database that is automatically generated from a hand-picked database
L<https://github.com/briandfoy/cpan-security-advisory>.
C<cpan-audit> does not connect to anything, that is why it is
important to keep it up to date. Every update of the internal database
is released as a new version. Ensure that you have the latest database
by updating L<CPAN::Audit> frequently; the database can change daily.
You can use enable a warning for a possibly out-of-date database by
adding C<--fresh>, which warns if the database version is older
than a month:
% cpan-audit --fresh ...
% cpan-audit -f ...
% env CPAN_AUDIT_FRESH_DAYS=7 cpan-audit -f ...
=head2 Finding dependencies
C<cpan-audit> can automatically detect dependencies from the following
sources:
=over
=item C<Carton>
Parses F<cpanfile.snapshot> file and checks the distribution versions.
=item F<cpanfile>
Parses F<cpanfile> taking into account the required versions.
=back
It is assumed that if the required version of the module is less than
a version of a release with a known vulnerability fix, then the module
is considered affected.
=head2 JSON data
If you request JSON output, the data looks like:
{
"meta" : {
... meta information ...
"dists": {
"<distribution1>": {
... distribution info ...
}
}
"errors" : [
... list of errors - if any ...
]
}
=head3 Meta information
The meta data contains information about the run of C<cpan-audit>.
{
"args": [
"Mojo::File",
"Mojo::UserAgent",
"LWP::UserAgent"
],
"cpan_audit": {
"version": "20230601.002"
},
"total_advisories": 19,
"command": "modules"
}
These information are shown:
=over 4
=item * cpan_audit
The version of C<cpan_audit> that is used for the audit
=item * command
The command of C<cpan_audit> that was run
=item * args
Arguments for the command
=item * total_advisories
Number of found advisories
=back
=head3 Distribution information
For each distribution where at least one advisory was found, the JSON
looks like:
"Dist-Name": {
"queried_modules": [
"Queried::Namespace"
],
"version": "Any",
"advisories": [
{
... advisory data as in the audit database ...
},
... more advisories ...
]
},
The advisory data is basically the data from the database. So this depends
on what is known for the given advisory.
The distribution information contains:
=over 4
=item * version
The version (range) that is checked for advisories. If there's no
version specified, all versions are checked and the version is report
as "Any".
=item * queried_modules
The actual namespaces queried, either from the command line or another
source, such as a F<cpanfile>.
=item * advisories
A list of all vulnerabilities found for the version range
=back
=head2 Exit values
In prior versions, C<cpan-audit> exited with the number of advisories
it found. Starting with 1.001, if there are advisories found, C<cpan-audit>
exits with 64 added to that number. The maximum number of reported advisories
is 62, since values over 126 are spoken for.
If the option C<--exit-zero> is set C<cpan-audit> exits always with a normal
exit code (0). This allows you to use C<cpan-audit> in build environments together
with bash exit mode activated (C<set -e>).
=over 4
=item * 0 - no advisories found
=item * 2 - problem with program invocation, such as bad switches or values
=item * 64+n - advisories found. Subtract 64 to get the advisory count, up to 62 advisories
=item * 255 - unspecified program error
=back
=head1 LICENSE
Copyright (C) Viacheslav Tykhanovskyi.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|