++ed by:
RRWO TIMB VSESPB

3 PAUSE user(s)
1 non-PAUSE user(s).

Guillaume Aubert

NAME

Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection - Prevent SQL injection in interpolated strings.

VERSION

Version 1.2.0

AFFILIATION

This is a standalone policy not part of a larger PerlCritic Policies group.

DESCRIPTION

When building SQL statements manually instead of using an ORM, any input must be quoted or passed using placeholders to prevent the introduction of SQL injection vectors. This policy attempts to detect the most common sources of SQL injection in manually crafted SQL statements, by detecting the use of variables inside interpolated strings that look like SQL statements.

In other words, this policy searches for code such as:

        my $sql = "SELECT * FROM $table WHERE field = $value";

But would leave alone:

        my $string = "Hello $world";

CONFIGURATION

There is no configuration option available for this policy.

MARKING VARIABLES AS SAFE

You can disable this policy on a particular string with the usual PerlCritic syntax:

        my $sql = "SELECT * FROM table WHERE field = $value"; ## no critic (PreventSQLInjection)

This is however not recommended, even if you know that $value is safe because it was previously quoted with something such as:

        my $value = $dbh->quote( $user_value );

The risk there is that someone will later modify your code and introduce unsafe variables by accident, which will then not get reported. To prevent this, this module has a special ## SQL safe ($var1, $var2, ...) syntax which allows whitelisting specific variables:

        my $sql = "SELECT * FROM table WHERE field = $value"; ## SQL safe($value)

That said, you should always convert your code to use placeholders instead where possible.

Note: this policy supports both comma-separated and space-separated lists to describe safe variables. In other words, ## SQL safe ($var1, $var2, ...) and ## SQL safe ($var1 $var2 ...) are strictly equivalent.

LIMITATIONS

There are many sources of SQL injection flaws, and this module comes with no guarantee whatsoever. It focuses on the most obvious flaws, but you should still learn more about SQL injection techniques to manually detect more advanced issues.

Possible future improvements for this module:

  • Detect use of sprintf()

    This should probably be considered a violation:

            my $sql = sprintf(
                    'SELECT * FROM %s',
                    $table
            );
  • Detect use of $dbh->quote() and $dbh->quote_identifier()

    The following code will incorrectly trigger a violation:

            my $sql = 'SELECT * FROM ' . $dbh->quote_identifier( $test );

    This is however quite a difficult problem, as the quote call can be hidden by several levels of indirection.

FUNCTIONS

supported_parameters()

Return an array with information about the parameters supported.

        my @supported_parameters = $policy->supported_parameters();

default_severity()

Return the default severify for this policy.

        my $default_severity = $policy->default_severity();

default_themes()

Return the default themes this policy is included in.

        my $default_themes = $policy->default_themes();

applies_to()

Return the class of elements this policy applies to.

        my $class = $policy->applies_to();

violates()

Check an element for violations against this policy.

        my $policy->violates(
                $element,
                $document,
        );

get_complete_variable()

Retrieve a complete variable starting with a PPI::Token::Symbol object, and indicate if the variable has used a quoting method to make it safe to use directly in SQL strings.

        my ( $variable, $is_quoted ) = get_complete_variable( $token );

For example, if you have $variable->{test}->[0] in your code, PPI will identify $variable as a PPI::Token::Symbol, and calling this function on that token will return the whole "$variable->{test}->[0]" string.

is_sql_statement()

Return a boolean indicating whether a string is potentially the beginning of a SQL statement.

        my $is_sql_statement = is_sql_statement( $token );

get_token_content()

Return the text content of a PPI token.

        my $content = get_token_content( $token );

analyze_sql_injections()

Analyze a token and returns an arrayref of variables that are potential SQL injection vectors.

        my $sql_injection_vector_names = analyze_sql_injections(
                $policy, # this object
                $token,
        );

extract_variables()

Extract variable names from a string.

        my $variables = extract_variables( $string );

get_safe_variables()

Return a hashref with safe variable names as the keys.

        my $safe_variables = get_safe_variables(
                $self,
                $line_number,
        );

parse_comments()

Parse the comments for the current document and identify variables marked as SQL safe.

        parse_comments(
                $self,
                $ppi_document,
        );

BUGS

Please report any bugs or feature requests through the web interface at https://github.com/guillaumeaubert/Perl-Critic-Policy-ValuesAndExpressions-PreventSQLInjection/issues. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection

You can also look for information at:

AUTHOR

Guillaume Aubert, <aubertg at cpan.org>.

COPYRIGHT & LICENSE

Copyright 2013 Guillaume Aubert.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/




Hosting generously
sponsored by Bytemark