Skip to content

Prevent false positives #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions lib/Perl/Critic/Policy/ValuesAndExpressions/PreventSQLInjection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ Readonly::Scalar my $QUOTING_METHODS_DEFAULT => q|
Readonly::Scalar my $SAFE_FUNCTIONS_DEFAULT => q|
|;

# Default for the name of the functions that are generally safe to use (because they
# are not expected to generate SQL calls -- unless you are doing something really,
# really weird.)
Readonly::Scalar my $SAFE_CONTEXT_DEFAULT => q|
die
warn
carp
croak
confess
|;

# Regex to detect comments like ## SQL safe ($var1, $var2).
Readonly::Scalar my $SQL_SAFE_COMMENTS_REGEX => qr/
\A
Expand Down Expand Up @@ -257,6 +268,12 @@ sub supported_parameters
default_string => $SAFE_FUNCTIONS_DEFAULT,
behavior => 'string',
},
{
name => 'safe_context',
description => 'A space-separated string listing the functions that are not subject to SQL injections',
default_string => $SAFE_CONTEXT_DEFAULT,
behavior => 'string',
},
);
}

Expand Down Expand Up @@ -330,6 +347,10 @@ sub violates
return ()
if !is_sql_statement( $element );

# Skip this statement if we are in a safe context (e.g., die "string")
return ()
if $self->is_in_safe_context( $element );

# Find SQL injection vulnerabilities.
my $sql_injections = detect_sql_injections( $self, $element );

Expand Down Expand Up @@ -580,6 +601,27 @@ sub is_sql_statement
: 0;
}

=head2 is_in_safe_context()

Return a boolean indicating whether a string is used in a safe context (e.g., die "string").

my $is_in_safe_context = is_in_safe_context( $token );

=cut

sub is_in_safe_context
{
my ( $self, $token ) = @_;

return 0 if !$self->{'_safe_context_regex'};

my $sprevious_sibling = $token->sprevious_sibling;

return 0 if !defined $sprevious_sibling or $sprevious_sibling eq '';
return 0 if !$sprevious_sibling->isa('PPI::Token::Word');

return $sprevious_sibling->{content} =~ $self->{'_safe_context_regex'};
}

=head2 get_token_content()

Expand Down Expand Up @@ -824,6 +866,20 @@ sub parse_config_parameters
}
}

if ( !exists( $self->{'_safe_context_regex'} ) )
{
if ( $self->{'_safe_context'} =~ /\w/ )
{
my $regex_components = join( '|', grep { $_ =~ /\w/ } split( /,?\s+/, $self->{'_safe_context'} ) );
$self->{'_safe_context_regex'} = qr/^(?:$regex_components)$/x;
}
else
{
$self->{'_safe_context_regex'} = undef;
}
}


return;
}

Expand Down
16 changes: 16 additions & 0 deletions t/ValuesAndExpressions/PreventSQLInjection.run
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

my $string = "Hello $world";

## name Prevent false positives in non-db operations
## failures 0
## cut

die "Select returned: $error";
warn "Update returned: $error";
croak "Insert returned: $error";
carp "Delete returned: $error";
confess "Select returned: $error";

do_something() or die "Select returned: $error";
do_something() or warn "Update returned: $error";
do_something() or croak "Insert returned: $error";
do_something() or carp "Delete returned: $error";
do_something() or confess "Select returned: $error";

## name Single-quoted SQL string with an escaped variable.
## failures 0
## cut
Expand Down