File: Filter.pm

package info (click to toggle)
libcpan-audit-perl 20250115.001-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 336 kB
  • sloc: perl: 905; makefile: 8
file content (112 lines) | stat: -rw-r--r-- 1,962 bytes parent folder | download | duplicates (2)
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
use v5.10;

package CPAN::Audit::Filter;
use strict;
use warnings;

our $VERSION = "1.001";

=encoding utf8

=head1 NAME

CPAN::Audit::Filter - manage the reports / CVEs to ignore

=head1 SYNOPSIS

	use CPAN::Audit::Filter;

	my $filter = CPAN::Audit::Filter->new( exclude => $array_ref );

	my $query = CPAN::Audit::Query->new(...);
	my $advisories = $query->advisories_for( $distname, $version_range );

	foreach my $advisory ( $advisories->@* ) {
		next if $filter->excludes($advisory);
		...
	}

=head1 DESCRIPTION

=head2 Class methods

=over 4

=item * new( exclude => ARRAYREF )

The values in the array ref for C<exclude> are uppercased before
they are stored.

=cut

sub new {
	my($class, %params) = @_;

	my $self = bless {}, $class;
	$params{exclude} //= [];

	my %excludes = map { uc($_) => 1 } @{ $params{exclude} };
	$self->{excludes} = \%excludes;

	$self->{ignored} = {};

	return $self;
}


=back

=head2 Instance methods

=over 4

=item * excludes( $advisory )

Returns true if this instance excludes either the ID or any of the
CVEs for ADVISORY, a hash as returned by L<CPAN::Audit::Query>. This
hash has these keys:

	id	 - a string, such as Some-Module-001
	cves - an array reference of CVE strings, such as CVE-2022-001

The values extracted from the hash are uppercased before use.

=cut

sub excludes {
	my($self, $advisory) = @_;

	return 0 unless keys %{$self->{excludes}};

	my @ids = map { uc } grep { defined } ($advisory->{id}, @{$advisory->{cves}});

	foreach my $id ( @ids ) {
		next unless $self->{excludes}{$id};
		$self->{ignored}{$id}++;
		return 1;
	}

	return 0;
}

=item * ignored_count

Return the count of the advisories that were ignored. Each ID or CVE
value only counts once.

=cut

sub ignored_count { scalar keys %{$_[0]->{ignored}} }

=back

=head1 LICENSE

Copyright (C) 2022 Graham TerMarsch

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;