++ed by:

2 PAUSE users
1 non-PAUSE user.

Guillaume Aubert


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


Version 1.2.0


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


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";


There is no configuration option available for this policy.


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.


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',
  • 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.



Return an array with information about the parameters supported.

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


Return the default severify for this policy.

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


Return the default themes this policy is included in.

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


Return the class of elements this policy applies to.

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


Check an element for violations against this policy.

        my $policy->violates(


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.


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

        my $is_sql_statement = is_sql_statement( $token );


Return the text content of a PPI token.

        my $content = get_token_content( $token );


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


Extract variable names from a string.

        my $variables = extract_variables( $string );


Return a hashref with safe variable names as the keys.

        my $safe_variables = get_safe_variables(


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



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.


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

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

You can also look for information at:


Guillaume Aubert, <aubertg at cpan.org>.


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/