]> git.decadent.org.uk Git - maypole.git/blob - lib/CGI/Untaint/Maypole.pm
CRUD tests, FromCGI that works, test to prove it ;)
[maypole.git] / lib / CGI / Untaint / Maypole.pm
1 package CGI::Untaint::Maypole;
2
3 use strict;
4 use warnings;
5 our $VERSION = '0.01';
6 use base 'CGI::Untaint';
7 use Carp;
8
9 =head1 NAME 
10
11 CGI::Untaint::Maypole - Use instead of CGI::Untaint. Based on CGI::Untaint
12
13 =head1 SYNOPSIS
14
15   use CGI::Untaint::Maypole;
16   my $h = CGI::Untaint::Maypole->new($params);
17   $value = $h->extract(-as_printable => 'name);
18
19   if ($h->error =~ /No input for/) {
20         # caught empty input now handle it
21                 ....
22   }
23   if ($h->raw_data->{$field} eq $object->$field) {
24     # Raw data same as database data. Perhaps we should not update field
25         ...
26   }
27
28 =head1 DESCRIPTION
29
30 This patches some issues I have with CGI::Untaint. You still need it installed
31 and you install handlers the same.
32
33 1) Instead of passing the empty string to the untaint handlers and relying on
34 them to handle it to everyone's liking, it seems better 
35 to have CGI::Untaint just say "No input for field" if the field is blank.
36
37 2) It  adds the method C<raw_data> to the get back the parameters the handler
38 was created with. 
39
40 =cut
41
42
43 sub raw_data { 
44         return shift->{__data};
45 }
46
47 # offending method ripped from base and patched
48 sub _do_extract {
49         my $self = shift;
50
51         my %param = @_;
52
53         #----------------------------------------------------------------------
54         # Make sure we have a valid data handler
55         #----------------------------------------------------------------------
56         my @as = grep /^-as_/, keys %param;
57         croak "No data handler type specified"        unless @as;
58         croak "Multiple data handler types specified" unless @as == 1;
59
60         my $field      = delete $param{ $as[0] };
61         my $skip_valid = $as[0] =~ s/^(-as_)like_/$1/;
62         my $module     = $self->_load_module($as[0]);
63
64         #----------------------------------------------------------------------
65         # Do we have a sensible value? Check the default untaint for this
66         # type of variable, unless one is passed.
67         #----------------------------------------------------------------------
68
69         ################# PETER'S PATCH #####################
70         my $raw = $self->{__data}->{$field} ;
71         die "No parameter for '$field'\n" if !defined($raw);
72         die "No input for '$field'\n" if $raw eq '';
73     #####################################################
74
75
76         my $handler = $module->_new($self, $raw);
77
78         my $clean = eval { $handler->_untaint };
79         if ($@) {    # Give sensible death message
80                 die "$field ($raw) is in invalid format.\n"
81                         if $@ =~ /^Died at/;
82                 die $@;
83         }
84
85         #----------------------------------------------------------------------
86         # Are we doing a validation check?
87         #----------------------------------------------------------------------
88         unless ($skip_valid) {
89                 if (my $ref = $handler->can('is_valid')) {
90                         die "$field ($raw) is in invalid format.\n"
91                                 unless $handler->is_valid;
92                 }
93         }
94
95         return $handler->untainted;
96 }
97
98 =head1 BUGS
99
100 None known yet.
101
102 =head1 SEE ALSO
103
104 L<perlsec>. L<CGI::Untaint>.
105
106 =head1 AUTHOR
107
108 Peter Speltz.
109
110 =head1 BUGS and QUERIES
111
112 Please direct all correspondence regarding this module to:
113    bug-Maypole@rt.cpan.org
114
115 =head1 COPYRIGHT and LICENSE
116
117 Copyright (C) 2006 Peter Speltz.  All rights reserved.
118
119 This module is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.
121
122 =cut
123
124 1;