forked from hidek/Catalyst-Plugin-HTML-Scrubber
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support scrubbing $c->req->data from C::Action::REST
If Catalyst::Action::REST / Catalyst::Controller::REST is in use, the request object will have a `data()` method for deserialised data as added by the Catalyst::TraitFor::Request::REST role which ought to be scrubbed too. To support this, (a) the scrubbing needs to happen later in the request flow - now `hooking dispatch()` instead of `prepare_parameters()` (b) to avoid the data not being read if the request body had already been read by `$c->req->body_data`, the fix in this PR is needed: perl-catalyst/catalyst-runtime/pull/186 Until such time, dirtily monkey-patch the `seek()` in.
- Loading branch information
Showing
3 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use FindBin qw($Bin); | ||
use lib "$Bin/lib"; | ||
|
||
use Test::More; | ||
|
||
|
||
eval 'use Catalyst::Controller::REST'; | ||
plan skip_all => 'Catalyst::Controller::REST not available, skip REST tests' if $@; | ||
|
||
use Catalyst::Test 'MyApp05'; | ||
use HTTP::Request::Common; | ||
use HTTP::Status; | ||
|
||
{ | ||
# Test that data in a JSON body POSTed gets scrubbed too | ||
my $json_body = <<JSON; | ||
{ | ||
"foo": "Top-level <img src=foo.jpg title=fun>", | ||
"baz":{ | ||
"one":"Second-level <img src=test.jpg>" | ||
}, | ||
"arr": [ | ||
"one test <img src=arrtest1.jpg>", | ||
"two <script>window.alert('XSS!');</script>" | ||
], | ||
"some_html": "Leave <b>this</b> alone: <img src=allowed.gif>" | ||
} | ||
JSON | ||
my $req = POST('/foo', | ||
Content_Type => 'application/json', Content => $json_body | ||
); | ||
diag("REQUEST: " . $req->as_string); | ||
my ($res, $c) = ctx_request($req); | ||
is($res->code, RC_OK, 'response ok'); | ||
is( | ||
$c->req->data->{foo}, | ||
'Top-level ', # note trailing space where img was removed | ||
'Top level body param scrubbed', | ||
); | ||
is( | ||
$c->req->data->{baz}{one}, | ||
'Second-level ', | ||
'Second level body param scrubbed', | ||
); | ||
is( | ||
$c->req->data->{arr}[0], | ||
'one test ', | ||
'Second level array contents scrubbbed', | ||
); | ||
is( | ||
$c->req->data->{some_html}, | ||
'Leave <b>this</b> alone: <img src=allowed.gif>', | ||
'Body data param matching ignore_params left alone', | ||
); | ||
} | ||
|
||
done_testing(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package MyApp05; | ||
|
||
use Moose; | ||
use namespace::autoclean; | ||
|
||
use Catalyst qw/HTML::Scrubber/; | ||
|
||
extends 'Catalyst'; | ||
|
||
__PACKAGE__->config( | ||
name => 'MyApp03', | ||
scrubber => { | ||
|
||
auto => 1, | ||
|
||
ignore_params => [ qr/_html$/, 'ignored_param' ], | ||
|
||
# params for HTML::Scrubber | ||
params => [ | ||
allow => [qw/br hr b/], | ||
], | ||
} | ||
); | ||
|
||
|
||
|
||
__PACKAGE__->setup(); | ||
1; | ||
|