blib/lib/CGI/UntaintPatched.pm

filter: Statementstmt 94.2% 33 / 35 Branchbran 55.5% 10 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 82.7% 48 / 58 CC 10 SCAR 21.6
CC
10
Cov
81%
_do_extract
11.7
BEGIN
1.0
BEGIN
1.0
CRAP 8.6
↑ Summary
1 package CGI::UntaintPatched;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.01';
6 1 ▶use base 'CGI::Untaint';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 CGI::UntaintPatched - CGI::Untaint but it returns a "No input for '$field'\n" error for fields left blank on a web form.
12  
13 =head1 SYNOPSIS
14  
15 if ($h->error =~ /No input for/) {
16 # caught empty input now handle it
17 }
18  
19 See CGI::Untaint.
20  
21 =head1 DESCRIPTION
22  
23 Instead of passing the empty string to the untaint handlers, which
24 do not like it or updating them all, it seemed better
25 to have CGI::Untaint catch the field left blank exception. So it does.
26 This should be ok I see no point untainting an empty string. But i am open to suggestions and other patches.
27  
28 =cut
29  
30  
31 # offending method ripped from base and patched
32 3 ▶sub _do_extract {
Subroutine _do_extract: called CC=10 SCAR=24.6
33 3 my $self = shift;
34  
35 3 my %param = @_;
36  
37 #----------------------------------------------------------------------
38 # Make sure we have a valid data handler
39 #----------------------------------------------------------------------
40 3 my @as = grep /^-as_/, keys %param;
41 3 ▶ croak "No data handler type specified" unless @as;
Branch: 1 branch
BranchTrueFalseTotal
unless @as 0 3 3
42 3 ▶ croak "Multiple data handler types specified" unless @as == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @as == 1 0 3 3
43  
44 3 my $field = delete $param{ $as[0] };
45 3 my $skip_valid = $as[0] =~ s/^(-as_)like_/$1/;
46 3 my $module = $self->_load_module($as[0]);
47  
48 #----------------------------------------------------------------------
49 # Do we have a sensible value? Check the default untaint for this
50 # type of variable, unless one is passed.
51 #----------------------------------------------------------------------
52  
53 ################# PETER'S PATCH #####################
54 3 my $raw = $self->{__data}->{$field} ;
55 3 ▶ die "No parameter for '$field'\n" if !defined($raw);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 1 2 3
56 2 ▶ die "No input for '$field'\n" if $raw eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "" 1 1 2
57 #####################################################
58  
59  
60 # 'False' values get returned as themselves with no warnings.
61 # return $self->{__lastval} unless $self->{__lastval};
62  
63 1 my $handler = $module->_new($self, $raw);
64  
65 1 my $clean = eval { $handler->_untaint };
66 1 ▶ if ($@) { # Give sensible death message
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
67 0 ▶ die "$field ($raw) does not untaint with default pattern\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /^Died at/ 0 0 0
68 if $@ =~ /^Died at/;
69 0 die $@;
70 }
71  
72 #----------------------------------------------------------------------
73 # Are we doing a validation check?
74 #----------------------------------------------------------------------
75 1 ▶ unless ($skip_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_valid) 1 0 1
76 1 ▶ if (my $ref = $handler->can('is_valid')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $handler->can("is_valid")) 1 0 1
77 1 ▶ die "$field ($raw) does not pass the is_valid() check\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $handler->$ref 0 1 1
78 unless $handler->$ref();
79 }
80 }
81  
82 1 return $handler->untainted;
83 }
84  
85 =head1 BUGS
86  
87 None known yet.
88  
89 =head1 SEE ALSO
90  
91 L<CGI>. L<perlsec>. L<CGI::Untaint>.
92  
93 =head1 AUTHOR
94  
95 Peter Speltz but most code was ripped from CGI::Untaint.
96  
97 =head1 BUGS and QUERIES
98  
99 Please direct all correspondence regarding this module to:
100 peterspeltz@cafes.net or bug-CGI-UntaintPatched@rt.cpan.org
101  
102 =head1 COPYRIGHT and LICENSE
103  
104 Copyright (C) 2005 Peter Speltz. All rights reserved.
105  
106 This module is free software; you can redistribute it and/or modify
107 it under the same terms as Perl itself.
108  
109 =cut
110  
111 1 1;
↑ Summary