Skip to content

Add a config option to class a variable as safe. #3

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 4 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
34 changes: 24 additions & 10 deletions .github/workflows/dzil-build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:
- "*"
schedule:
- cron: "15 4 * * 0" # Every Sunday morning
workflow_dispatch:

jobs:
build-job:
Expand All @@ -18,15 +19,15 @@ jobs:
container:
image: perldocker/perl-tester:5.32
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Run Tests
env:
AUTHOR_TESTING: 1
AUTOMATED_TESTING: 1
EXTENDED_TESTING: 1
RELEASE_TESTING: 1
run: auto-build-and-test-dist
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: build_dir
path: build_dir
Expand All @@ -37,8 +38,8 @@ jobs:
container:
image: perldocker/perl-tester:5.32
steps:
- uses: actions/checkout@v2 # codecov wants to be inside a Git repository
- uses: actions/download-artifact@v2
- uses: actions/checkout@v4 # codecov wants to be inside a Git repository
- uses: actions/download-artifact@v4
with:
name: build_dir
path: .
Expand All @@ -54,7 +55,7 @@ jobs:
matrix:
os: [ubuntu-20.04]
perl-version:
- "5.8"
# - "5.8" Won't build
- "5.10"
- "5.12"
- "5.14"
Expand All @@ -67,13 +68,17 @@ jobs:
- "5.28"
- "5.30"
- "5.32"
- "5.34"
- "5.36"
- "5.38"
- "5.40"
name: perl ${{ matrix.perl-version }} on ${{ matrix.os }}
steps:
- name: set up perl
uses: shogo82148/actions-setup-perl@v1
with:
perl-version: ${{ matrix.perl-version }}
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: build_dir
path: .
Expand All @@ -94,7 +99,7 @@ jobs:
matrix:
os: [macos-latest]
perl-version:
- "5.8"
# - "5.8"
- "5.10"
- "5.12"
- "5.14"
Expand All @@ -107,13 +112,17 @@ jobs:
- "5.28"
- "5.30"
- "5.32"
- "5.34"
- "5.36"
- "5.38"
- "5.40"
name: perl ${{ matrix.perl-version }} on ${{ matrix.os }}
steps:
- name: set up perl
uses: shogo82148/actions-setup-perl@v1
with:
perl-version: ${{ matrix.perl-version }}
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: build_dir
path: .
Expand All @@ -135,22 +144,27 @@ jobs:
os: [windows-latest]
perl-version:
- "5.14"
- "5.16"
# - "5.16" Doesn't work
- "5.18"
- "5.20"
- "5.22"
- "5.24"
- "5.26"
- "5.28"
- "5.30"
- "5.32"
# - "5.34"
- "5.36"
- "5.38"
# - "5.40"
name: perl ${{ matrix.perl-version }} on ${{ matrix.os }}
steps:
- name: set up perl
uses: shogo82148/actions-setup-perl@v1
with:
perl-version: ${{ matrix.perl-version }}
distribution: strawberry # this option only used on windows
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: build_dir
path: .
Expand Down
2 changes: 2 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Revision history for Perl-Critic-Policy-ValuesAndExpressions-PreventSQLInjection
{{$NEXT}}

2.000001 2021-03-05 15:39:11Z
- Bump minimum PPI version to 1.222 (GH#1) (Olaf Alders)

2.000000 2021-03-04 22:59:36Z
Expand Down
19 changes: 16 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"runtime" : {
"requires" : {
"Carp" : "0",
"PPI" : "1.222",
"PPIx::QuoteLike" : "0.015",
"Perl::Critic::Policy" : "0",
"Perl::Critic::Utils" : "0",
Expand All @@ -76,7 +77,7 @@
"requires" : {
"ExtUtils::MakeMaker" : "0",
"File::Spec" : "0",
"PPI" : "0",
"PPI" : "1.222",
"Test::FailWarnings" : "0",
"Test::More" : "0",
"Test::Perl::Critic" : "0",
Expand All @@ -97,7 +98,7 @@
"web" : "https://github.com/oalders/Perl-Critic-Policy-ValuesAndExpressions-PreventSQLInjection"
}
},
"version" : "2.000000",
"version" : "2.000001",
"x_Dist_Zilla" : {
"perl" : {
"version" : "5.030002"
Expand Down Expand Up @@ -655,7 +656,7 @@
"branch" : null,
"changelog" : "Changes",
"signed" : 0,
"tag" : "v2.000000",
"tag" : "v2.000001",
"tag_format" : "v%V",
"tag_message" : "v%V"
},
Expand Down Expand Up @@ -749,6 +750,17 @@
"name" : "StaticInstall",
"version" : "0.012"
},
{
"class" : "Dist::Zilla::Plugin::Prereqs",
"config" : {
"Dist::Zilla::Plugin::Prereqs" : {
"phase" : "runtime",
"type" : "requires"
}
},
"name" : "RuntimeRequires",
"version" : "6.017"
},
{
"class" : "Dist::Zilla::Plugin::FinderCode",
"name" : ":InstallModules",
Expand Down Expand Up @@ -812,6 +824,7 @@
"Charlie Garrison <[email protected]>",
"Guillaume Aubert <[email protected]>",
"Nelson Ferraz <[email protected]>",
"Olaf Alders <[email protected]>",
"Olaf Alders <[email protected]>",
"Victor <[email protected]>"
],
Expand Down
7 changes: 4 additions & 3 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ my %WriteMakefileArgs = (
"NAME" => "Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection",
"PREREQ_PM" => {
"Carp" => 0,
"PPI" => "1.222",
"PPIx::QuoteLike" => "0.015",
"Perl::Critic::Policy" => 0,
"Perl::Critic::Utils" => 0,
Expand All @@ -31,13 +32,13 @@ my %WriteMakefileArgs = (
"TEST_REQUIRES" => {
"ExtUtils::MakeMaker" => 0,
"File::Spec" => 0,
"PPI" => 0,
"PPI" => "1.222",
"Test::FailWarnings" => 0,
"Test::More" => 0,
"Test::Perl::Critic" => 0,
"Test::Perl::Critic::Policy" => 0
},
"VERSION" => "2.000001",
"VERSION" => "2.000002",
"test" => {
"TESTS" => "t/*.t"
}
Expand All @@ -48,7 +49,7 @@ my %FallbackPrereqs = (
"Carp" => 0,
"ExtUtils::MakeMaker" => 0,
"File::Spec" => 0,
"PPI" => 0,
"PPI" => "1.222",
"PPIx::QuoteLike" => "0.015",
"Perl::Critic::Policy" => 0,
"Perl::Critic::Utils" => 0,
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Perl::Critic::Policy::ValuesAndExpressions::PreventSQLInjection - Prevent SQL in

# VERSION

version 2.000000
version 2.000002

# DESCRIPTION

Expand Down Expand Up @@ -62,6 +62,12 @@ For example, to declare `quote_function()` and

By default, no functions are considered safe.


## safe_variables

A space separated string of variables we know are safe.


## prefer\_upper\_case\_keywords

A boolean indicating whether you'd prefer to detect only SELECT, INSERT, UPDATE
Expand Down
3 changes: 2 additions & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Do not edit this file directly. To change prereqs, edit the `dist.ini` file.

requires "Carp" => "0";
requires "PPI" => "1.222";
requires "PPIx::QuoteLike" => "0.015";
requires "Perl::Critic::Policy" => "0";
requires "Perl::Critic::Utils" => "0";
Expand All @@ -16,7 +17,7 @@ requires "warnings" => "0";
on 'test' => sub {
requires "ExtUtils::MakeMaker" => "0";
requires "File::Spec" => "0";
requires "PPI" => "0";
requires "PPI" => "1.222";
requires "Test::FailWarnings" => "0";
requires "Test::More" => "0";
requires "Test::Perl::Critic" => "0";
Expand Down
27 changes: 23 additions & 4 deletions lib/Perl/Critic/Policy/ValuesAndExpressions/PreventSQLInjection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use 5.006001;
use strict;
use warnings;

our $VERSION = '2.000001';
our $VERSION = '2.000002';

use base 'Perl::Critic::Policy';

Expand Down Expand Up @@ -215,6 +215,11 @@ Readonly::Scalar my $QUOTING_METHODS_DEFAULT => q|
Readonly::Scalar my $SAFE_FUNCTIONS_DEFAULT => q|
|;

# Default for the name of the variables that are safe to
# concatenate to SQL strings.
Readonly::Scalar my $SAFE_VARIABLES_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.)
Expand Down Expand Up @@ -281,6 +286,13 @@ sub supported_parameters {
default_string => '0',
behavior => 'boolean',
},
{
name => 'safe_variables',
description =>
'A space-separated string listing the variables that are a safely quoted value',
default_string => $SAFE_VARIABLES_DEFAULT,
behavior => 'string',
},
);
}

Expand Down Expand Up @@ -791,10 +803,13 @@ sub get_safe_elements {

# If there's nothing in the cache for that line, return immediately.
return {}
if !exists( $self->{'_sqlsafe'}->{$line_number} );
if !exists( $self->{'_sqlsafe'}->{$line_number} ) && !exists( $self->{_safe_variables});

# Return a hash of safe element names.
return { map { $_ => 1 } @{ $self->{'_sqlsafe'}->{$line_number} } };
my %hash = map { $_ => 1 } @{ $self->{'_sqlsafe'}->{$line_number} };
# Return a hash of safe element names.
map {$hash{$_} = 1 } split( /[,\s]+/, $self->{'_safe_variables'} );
return \%hash;
}

=head2 parse_comments()
Expand Down Expand Up @@ -889,7 +904,11 @@ sub parse_config_parameters {
$self->{'_safe_context_regex'} = undef;
}
}

# Strip surrounding quotes.
if ( exists( $self->{'_safe_variables'} ) ) {
$self->{'_safe_variables'} =~ s/^['" ]+//;
$self->{'_safe_variables'} =~ s/['" ]+$//;
}
return;
}

Expand Down