| 1 | package Perl::Critic::Policy::Security::RandBytesFromHash; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: flag common anti-patterns for generating random bytes | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use v5.24; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use List::Util qw( any ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Perl::Critic::Utils qw( :severities :classification :ppi ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use PPI 1.281; # signatures support | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Readonly 2.01; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Ref::Util qw( is_plain_arrayref ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # RECOMMEND PREREQ: Ref::Util::XS | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = 'v0.1.3'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | Readonly my $DESC => 'random bytes generated using a hash'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | Readonly my $EXPL => 'A hash seeded with poor sources of entropy is still a poor source of entropy, use system entropy instead.'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 21 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 25 | ▶ | sub default_severity { $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub default_themes { return qw( security cpansec ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 21 | ▶ | sub applies_to { 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | Readonly my $DIGEST_REGEX => qr/\A ( | |||||||||||||||||||||||||||||||||||||||||
| 34 | ( \w+:: )* | ||||||||||||||||||||||||||||||||||||||||||
| 35 | ( md[2456] | sha( 1 | 224 | 256 ) | digest_data | (hex|b64)?digest(_hash)? | join ) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ( _ ( hex | b64u? | base64 | sum | bytes ) )? | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ) \z/anx; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 99 | ▶ | sub violates ( $self, $elem, $ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 99 | ▶ | if ( $elem =~ $DIGEST_REGEX && ( is_function_call($elem) || is_method_call($elem) ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$elem =~ /$DIGEST_REGEX/u and is_function_call($elem) || is_method_call($elem)
is_function_call($elem) or is_method_call($elem)
MC/DC33%1/3
$elem =~ /$DIGEST_REGEX/u and is_function_call($elem) || is_method_call($elem)
$elem =~ /$DIGEST_REGEX/u
is_function_call($elem)
is_method_call($elem)
Truth table75%3/4
A and B or C
A= $elem =~ /$DIGEST_REGEX/uB= is_function_call($elem)C= is_method_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 27 | my @args = parse_arg_list($elem); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 27 | ▶ | if ( $self->_is_bad_seed_source( \@args ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 25 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 74 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 157 | ▶ | sub _is_bad_seed_source( $self, $elem ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_bad_seed_source: called CC=12 SCAR=25.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 157 | ▶ | if ( is_plain_arrayref($elem) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 63 | ▶ | return any { $self->_is_bad_seed_source($_) } $elem->@*; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 94 | ▶ | return 0 if $elem->isa("PPI::Token::Whitespace"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 85 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$elem =~ /\A ( (CORE::)?rand | (Time::HiRes::)? (time|gettimeofday|localtime|gmtime|clock_gettime) | refaddr ) \z/anx and is_perl_builtin_with_optional_argument($elem) || is_function_call($elem)
is_perl_builtin_with_optional_argument($elem) or is_function_call($elem)
MC/DC33%1/3
$elem =~ /\A ( (CORE::)?rand | (Time::HiRes::)? (time|gettimeofday|localtime|gmtime|clock_gettime) | refaddr ) \z/anx and is_perl_builtin_with_optional_argument($elem) || is_function_call($elem)
$elem =~ /\A ( (CORE::)?rand | (Time::HiRes::)? (time|gettimeofday|localtime|gmtime|clock_gettime) | refaddr ) \z/anx
is_perl_builtin_with_optional_argument($elem)
is_function_call($elem)
Truth table75%3/4
A and B or C
A= $elem =~ /\A ( (CORE::)?rand | (Time::HiRes::)? (time|gettimeofday|localtime|gmtime|clock_gettime) | refaddr ) \z/anxB= is_perl_builtin_with_optional_argument($elem)C= is_function_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | if $elem =~ /\A ( (CORE::)?rand | (Time::HiRes::)? (time|gettimeofday|localtime|gmtime|clock_gettime) | refaddr ) \z/anx | ||||||||||||||||||||||||||||||||||||||||||
| 65 | && ( is_perl_builtin_with_optional_argument($elem) | ||||||||||||||||||||||||||||||||||||||||||
| 66 | || is_function_call($elem) ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 66 | ▶ | return 1 if $elem eq '$$' && is_perl_global($elem); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$elem eq "\$\$" and is_perl_global($elem)
MC/DC50%1/2
$elem eq "\$\$" and is_perl_global($elem)
$elem eq "\$\$"
is_perl_global($elem)
Truth table66%2/3
A and B
A= $elem eq "\$\$"B= is_perl_global($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 62 | ▶ | return 1 if $elem =~ /\A \$ (PID|PROCESS_ID) \z/anx && $elem->isa("PPI::Token::Symbol"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$elem =~ /\A \$ (PID|PROCESS_ID) \z/anx and $elem->isa("PPI::Token::Symbol")
MC/DC50%1/2
$elem =~ /\A \$ (PID|PROCESS_ID) \z/anx and $elem->isa("PPI::Token::Symbol")
$elem =~ /\A \$ (PID|PROCESS_ID) \z/anx
$elem->isa("PPI::Token::Symbol")
Truth table66%2/3
A and B
A= $elem =~ /\A \$ (PID|PROCESS_ID) \z/anxB= $elem->isa("PPI::Token::Symbol")
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 60 | ▶ | if ( $elem->isa("PPI::Structure") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 14 | ▶ | return any { $self->_is_bad_seed_source($_) } $elem->children | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | elsif ( $elem->isa("PPI::Statement") ) { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | ▶ | return any { $self->_is_bad_seed_source($_) } $elem->children | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 36 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||||||||||||||||