| 1 | package CatalystX::RequestRole::StrictParams; | ||
| 2 | BEGIN { | ||
| 3 | $CatalystX::RequestRole::StrictParams::VERSION = '0.02'; | ||
| 4 | } | ||
| 5 | use Moose::Role; | ||
| 6 | use Carp qw/croak/; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | CatalystX::RequestRole::StrictParams - Insist users specify HTTP method for form parameters | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | version 0.02 | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Insist users specify HTTP method for form parameters | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | package MyApp; | ||
| 23 | |||
| 24 | use base 'Catalyst'; | ||
| 25 | use Catalyst; | ||
| 26 | use CatalystX::RoleApplicator; | ||
| 27 | |||
| 28 | __PACKAGE__->apply_request_class_roles('CatalystX::RequestRole::StrictParams'); | ||
| 29 | |||
| 30 | =head1 EXPLANATION | ||
| 31 | |||
| 32 | Perl wrappers around the CGI protocol frequently make it too easy to write | ||
| 33 | exploitable code by conflating C<GET> and C<POST> parameters. Implementers | ||
| 34 | instead should be considering whether a given request is retrieving (I<GET>) | ||
| 35 | or modifying (I<POST>) data. | ||
| 36 | |||
| 37 | This role removes access to C<params>, C<parameters> and C<param> from | ||
| 38 | Catalyst request objects, forcing users to use C<body_parameters> and | ||
| 39 | C<query_parameters> instead. | ||
| 40 | |||
| 41 | =head1 WARNING | ||
| 42 | |||
| 43 | L<Cross-site Scripting|https://en.wikipedia.org/wiki/Cross-site_scripting> | ||
| 44 | vulnerabilities are easy to introduce, and often subtle. While using this | ||
| 45 | module reduces the threat surface a little, it in no way provides general | ||
| 46 | protection from all (or maybe even most) attacks. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | # See: perldoc Carp | ||
| 51 | our @CARP_NOT; | ||
| 52 | |||
| 53 | # Methods we're intending to knock out | ||
| 54 | our @targets = qw/param parameters params/; | ||
| 55 | |||
| 56 | for my $target (@targets) { | ||
| 57 | before $target => sub { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | # Catalyst::Engine may call this as part of setup, and we want to | ||
| 61 | # let it... We'll check for that by seeing if we can find | ||
| 62 | # Catalyst::Engine in the callstack. We're as likely to find | ||
| 63 | # Class::MOP::Method::Wrapped first, which we ignore. | ||
| 64 | |||
| 65 | # Search the callers for the first class that isn't | ||
| 66 | # Class::MOP::Method::Wrapped | ||
| 67 | my $stack = 0; | ||
| 68 | my @caller = caller( $stack++ ); | ||
| 69 | @caller = caller( $stack++ ) while | ||
| 70 | $caller[0] eq 'Class::MOP::Method::Wrapped'; | ||
| 71 | my $package = $caller[0]; | ||
| 72 | |||
| 73 | # Don't allow the call unless the caller is Catalyst::Engine, which | ||
| 74 | # implies this is happening at request preparation state. | ||
| 75 | if (! $package->isa('Catalyst::Engine') ) { | ||
| 76 | local @CARP_NOT = qw/Class::MOP::Method::Wrapped/; | ||
| 77 | croak | ||
| 78 | "'$target' encourages insecure code; please use either " . | ||
| 79 | "body_parameters or query_parameters instead. For more " . | ||
| 80 | "details: perldoc CatalystX::RequestRole::StrictParams"; | ||
| 81 | } | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head1 SPONSORED BY | ||
| 86 | |||
| 87 | Initial development sponsored by NET-A-PORTER | ||
| 88 | L<http://www.net-a-porter.com/>, through their generous open-source support. | ||
| 89 | |||
| 90 | =head1 AUTHOR | ||
| 91 | |||
| 92 | Peter Sergeant - C<pete@clueball.com> | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | 1; |