Skip to content

reset _sqlsafe when scanning multiple documents; added tests #22

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
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ my $builder = $class->new(
'Test::Deep' => 0,
'Test::FailWarnings' => 0,
'Test::More' => 0,
'Test::Perl::Critic' => 0,
'Test::Perl::Critic::Policy' => 0,
},
requires =>
Expand Down
14 changes: 14 additions & 0 deletions lib/Perl/Critic/Policy/ValuesAndExpressions/PreventSQLInjection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ sub applies_to
);
}

=head2 prepare_to_scan_document()

Sets up policy ($self) for each new document before scanning.

my $bool = $policy->prepare_to_scan_document();

=cut

sub prepare_to_scan_document {
my ( $self, $doc ) = @_;
delete $self->{'_sqlsafe'};
return $TRUE;
}


=head2 violates()

Expand Down
17 changes: 17 additions & 0 deletions t/45-multi-docs.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!perl

use strict;
use warnings;

use Test::FailWarnings -allow_deps => 1;
use Test::More tests => 2;
use Test::Perl::Critic;
use File::Spec;

Test::Perl::Critic->import('-single-policy' => 'ValuesAndExpressions::PreventSQLInjection');

my $testfile1 = File::Spec->catfile( 't', 'files', 'test10.pl' );
my $testfile2 = File::Spec->catfile( 't', 'files', 'test11.pl' );

all_critic_ok($testfile1, $testfile2 );

8 changes: 8 additions & 0 deletions t/files/test10.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/perl

use strict;
use warnings;

my $str = "just a placeholder";

1;
8 changes: 8 additions & 0 deletions t/files/test11.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/perl

use strict;
use warnings;

my $username = "user1";
my $sql = "select * from users WHERE user = $username"; ## SQL safe($username)