lib/CGI/Application/Plugin/ValidateQuery.pm

filter: Statementstmt 94.8% 73 / 77 Branchbran 84.6% 22 / 26 Conditioncond 73.3% 11 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 12 / 12 Podpod 50.0% 2 / 4 totaltotal 87.4% 125 / 143 CC 21 SCAR 19.7
CC
21
Cov
90%
_validate
12.1
validate_query_config
8.5
validate_query
2.0
CRAP 7.1
↑ Summary
1 package CGI::Application::Plugin::ValidateQuery;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 CGI::Application::Plugin::ValidateQuery - lightweight query validation for CGI::Application
14  
15 =head1 VERSION
16  
17 Version 1.0.5
18  
19 =cut
20  
21 7 our $VERSION = '1.0.5';
22  
23 7 our @EXPORT_OK = qw(
24 validate_query_config
25 validate_app_params
26 validate_query
27 validate_query_error_mode
28 );
29 7 push @EXPORT_OK, @Params::Validate::EXPORT_OK;
30 our %EXPORT_TAGS = (
31 all => \@EXPORT_OK,
32 types => $Params::Validate::EXPORT_TAGS{types}
33 7 );
34  
35 7 local $Params::Validate::NO_VALIDATION = 0;
36  
37 14 ▶sub validate_query_config {
Subroutine validate_query_config: called CC=8 SCAR=21.4
Pod undocumented
38 14 my $self = shift;
39  
40 14 my $opts = {@_};
41  
42 14 $opts = {map {uc $_ => $opts->{$_}} keys %$opts};
43  
44 # for now, default checking all params. First config arg is legacy.
45 14 ▶ if ( defined $opts->{EXTRA_FIELDS_OPTIONAL} or defined $opts->{ALLOW_EXTRA} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'EXTRA_FIELDS_OPTIONAL'} or defined $opts->{'ALLOW_EXTRA'}) { } 0 14 14
Condition33%1/3
defined $opts->{"EXTRA_FIELDS_OPTIONAL"} or defined $opts->{"ALLOW_EXTRA"}
l!l&&r!l&&!r
0014
MC/DC0%0/2
defined $opts->{"EXTRA_FIELDS_OPTIONAL"} or defined $opts->{"ALLOW_EXTRA"}
defined $opts->{"EXTRA_FIELDS_OPTIONAL"} defined $opts->{"ALLOW_EXTRA"}
Truth table33%1/3
A or B
A= defined $opts->{"EXTRA_FIELDS_OPTIONAL"}B= defined $opts->{"ALLOW_EXTRA"}
ABresult
1X1
011
000
46 0 delete $opts->{EXTRA_FIELDS_OPTIONAL};
47 0 delete $opts->{ALLOW_EXTRA};
48 0 $self->{__CAP_VALQUERY_ALLOW_EXTRA} = 1;
49 } else {
50 14 $self->{__CAP_VALQUERY_ALLOW_EXTRA} = 0;
51 }
52  
53 $self->{__CAP_VALQUERY_ERROR_MODE} = defined $opts->{ERROR_MODE} ?
54 14 ▶ delete $opts->{ERROR_MODE} : 'validate_query_error_mode';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'ERROR_MODE'} ? : 7 7 14
55  
56 $self->{__CAP_VALQUERY_LOG_LEVEL} = defined $opts->{LOG_LEVEL} ?
57 14 ▶ delete $opts->{LOG_LEVEL} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'LOG_LEVEL'} ? : 4 10 14
58  
59 croak 'log_level given but no logging interface exists.'
60 14 ▶ if $self->{__CAP_VALQUERY_LOG_LEVEL} && !$self->can('log');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"__CAP_VALQUERY_LOG_LEVEL"} and not $self->can("log") 1 13 14
Condition100%3/3
$self->{"__CAP_VALQUERY_LOG_LEVEL"} and not $self->can("log")
!ll&&!rl&&r
1031
MC/DC100%2/2
$self->{"__CAP_VALQUERY_LOG_LEVEL"} and not $self->can("log")
$self->{"__CAP_VALQUERY_LOG_LEVEL"} not $self->can("log")
Truth table100%3/3
A and not(B)
A= $self->{"__CAP_VALQUERY_LOG_LEVEL"}B= not $self->can("log")
ABresult
0X0
100
111
61  
62 2 croak 'Invalid option(s) ('.join(', ', keys %{$opts}).') passed to'
63 13 ▶ .'validate_query_config' if %{$opts};
Branch: 1 branch
BranchTrueFalseTotal
if %{$opts;} 2 11 13
64 }
65  
66 3 ▶sub validate_app_params {
Subroutine validate_app_params: called CC=2 SCAR=7.0
Pod documented
67 3 my $self = shift;
68  
69 3 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
70  
71 3 my $query_props = {@_};
72  
73 3 $query_props->{allow_extra} = 1;
74 3 $query_props->{app_params} = 1;
75  
76 3 return _validate($self, $query_props);
77 }
78  
79 14 ▶sub validate_query {
Subroutine validate_query: called CC=2 SCAR=7.1
Pod documented
80 14 my $self = shift;
81  
82 14 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 14 14
83  
84 14 return _validate($self, {@_});
85 }
86  
87  
88 17 ▶sub _validate {
Subroutine _validate: called CC=12 SCAR=24.9
89 17 my $self = shift;
90 17 my $query_props = shift;
91  
92 my $log_level = delete $query_props->{log_level}
93 17 ▶ || $self->{__CAP_VALQUERY_LOG_LEVEL};
Condition66%2/3
delete $query_props->{'log_level'} || $self->{'__CAP_VALQUERY_LOG_LEVEL'}
l!l&&r!l&&!r
0215
MC/DC50%1/2
delete $query_props->{'log_level'} || $self->{'__CAP_VALQUERY_LOG_LEVEL'}
delete $query_props->{'log_level'} $self->{'__CAP_VALQUERY_LOG_LEVEL'}
Truth table66%2/3
A || B
A= delete $query_props->{'log_level'}B= $self->{'__CAP_VALQUERY_LOG_LEVEL'}
ABresult
1X1
011
000
94  
95 my $allow_extra = delete($query_props->{extra_fields_optional})
96 || delete($query_props->{allow_extra})
97 17 ▶ || $self->{__CAP_ALLOW_EXTRA};
Condition83%5/6
delete $query_props->{'extra_fields_optional'} || delete $query_props->{'allow_extra'} || $self->{'__CAP_ALLOW_EXTRA'}
l!l&&r!l&&!r
5012
delete $query_props->{"extra_fields_optional"} or delete $query_props->{"allow_extra"}
l!l&&r!l&&!r
1412
MC/DC66%2/3
delete $query_props->{'extra_fields_optional'} || delete $query_props->{'allow_extra'} || $self->{'__CAP_ALLOW_EXTRA'}
delete $query_props->{"extra_fields_optional"} delete $query_props->{"allow_extra"} $self->{'__CAP_ALLOW_EXTRA'}
Truth table75%3/4
A or B || C
A= delete $query_props->{"extra_fields_optional"}B= delete $query_props->{"allow_extra"}C= $self->{'__CAP_ALLOW_EXTRA'}
ABCresult
1XX1
01X1
0011
0000
98  
99 17 my $app_params = delete $query_props->{app_params};
100  
101 17 ▶ my $param_obj = $app_params ? $self : $self->query;
Branch: 1 branch
BranchTrueFalseTotal
$app_params ? : 3 14 17
102  
103 # filter query_props to support quick regex syntax
104 # turns
105 # key => qr/$regex/
106 # into
107 # key => { regex => qr/$regex/ }
108 17 for my $key (keys %$query_props) {
109 77 my $val = $query_props->{$key};
110 77 ▶ if ( ref $val eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "Regexp") 0 77 77
111 0 $query_props->{$key} = { regex => $val, type => SCALAR };
112 }
113 }
114  
115 17 my %validated;
116 17 eval {
117 17 my @vars_array;
118 17 for my $p ($param_obj->param) {
119 82 my @values = $param_obj->param($p);
120 82 ▶ push @vars_array, ($p, scalar @values > 1 ? \@values : $values[0]);
Branch: 1 branch
BranchTrueFalseTotal
scalar @values > 1 ? : 12 70 82
121 }
122 17 %validated = validate_with(
123 params => \@vars_array,
124 spec => $query_props,
125 allow_extra => $allow_extra
126 );
127 };
128 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 7 10 17
129 7 my $log_msg = "Query Validation Failed: $@";
130 7 ▶ $self->log->$log_level($log_msg) if $log_level;
Branch: 1 branch
BranchTrueFalseTotal
if $log_level 1 6 7
131 7 $self->error_mode($self->{__CAP_VALQUERY_ERROR_MODE});
132  
133 7 croak $log_msg;
134 }
135  
136 # Account for default values, and use the expanded -name / -value
137 # syntax for CGI to ensure proper handling of multivalued fields.
138 my $sub = $app_params
139 9 ▶ ? sub { my $p = shift; $param_obj->param($p, $validated{$p}) }
Subroutine __ANON__: called CC=1 SCAR=0.0
140 10 ▶ : sub { my $p = shift; $param_obj->param(-name=>$p, -value=>$validated{$p}) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$app_params ? : 2 8 10
141  
142 10 map { $sub->($_) } keys %validated;
143  
144 10 return %validated;
145 }
146  
147 1 ▶sub validate_query_error_mode {
Subroutine validate_query_error_mode: called CC=1 SCAR=0.0
Pod undocumented
148 1 my $self = shift;
149 1 return "<html><head><title>Request not understood</title></head><body>The
150 request submitted could not be understood.</body></html>";
151 }
152  
153 7 1;
154  
155 __END__
↑ Summary