Foo
'; + my $req = POST('/', [foo_html => $value]); + my ($res, $c) = ctx_request($req); + is($res->code, RC_OK, 'response ok'); + is( + $c->req->param('foo_html'), + $value, + 'HTML left alone in ignored (by regex) field', + ); +} +{ + diag "HTML left alone in ignored field - by name"; + my $value = 'Foo
'; + my $req = POST('/', [ignored_param => $value]); + my ($res, $c) = ctx_request($req); + is($res->code, RC_OK, 'response ok'); + is( + $c->req->param('ignored_param'), + $value, + 'HTML left alone in ignored (by name) field', + ); +} + +{ + # Test that data in a JSON body POSTed gets scrubbed too + my $json_body = <
"
+ },
+ "arr": [
+ "one test
",
+ "two "
+ ],
+ "some_html": "Leave this alone:
"
+}
+JSON
+ my $req = POST('/',
+ Content_Type => 'application/json', Content => $json_body
+ );
+ my ($res, $c) = ctx_request($req);
+ is($res->code, RC_OK, 'response ok');
+ is(
+ $c->req->body_data->{foo},
+ 'Top-level ', # note trailing space where img was removed
+ 'Top level body param scrubbed',
+ );
+ is(
+ $c->req->body_data->{baz}{one},
+ 'Second-level ',
+ 'Second level body param scrubbed',
+ );
+ is(
+ $c->req->body_data->{arr}[0],
+ 'one test ',
+ 'Second level array contents scrubbbed',
+ );
+ is(
+ $c->req->body_data->{some_html},
+ 'Leave this alone:
',
+ 'Body data param matching ignore_params left alone',
+ );
}
done_testing();
diff --git a/t/05_ignore_params.t b/t/05_ignore_params.t
deleted file mode 100644
index b28061f..0000000
--- a/t/05_ignore_params.t
+++ /dev/null
@@ -1,53 +0,0 @@
-use strict;
-use warnings;
-
-use FindBin qw($Bin);
-use lib "$Bin/lib";
-
-use Catalyst::Test 'MyApp05';
-use HTTP::Request::Common;
-use HTTP::Status;
-use Test::More;
-
-{
- diag "Simple request with no params";
- my $req = GET('/');
- my ($res, $c) = ctx_request($req);
- ok($res->code == RC_OK, 'response ok');
- is($res->content, 'index', 'content ok');
-}
-{
- diag "Request wth one param, nothing to strip";
- my $req = POST('/', [foo => 'bar']);
- my ($res, $c) = ctx_request($req);
- ok($res->code == RC_OK, 'response ok');
- is($c->req->param('foo'), 'bar', 'parameter ok');
-}
-{
- diag "Request with XSS attempt gets stripped";
- my $req = POST('/', [foo => 'bar']);
- my ($res, $c) = ctx_request($req);
- ok($res->code == RC_OK, 'response ok');
- is($c->req->param('foo'), 'bar', 'XSS was stripped');
-}
-{
- diag "HTML left alone in ignored field - by regex match";
- my $value = 'Foo
'; - my $req = POST('/', [foo_html => $value]); - my ($res, $c) = ctx_request($req); - ok($res->code == RC_OK, 'response ok'); - is($c->req->param('foo_html'), $value, 'HTML left alone in ignored field'); -} -{ - diag "HTML left alone in ignored field - by name"; - my $value = 'Foo
'; - my $req = POST('/', [ignored_param => $value]); - my ($res, $c) = ctx_request($req); - ok($res->code == RC_OK, 'response ok'); - is($c->req->param('ignored_param'), $value, 'HTML left alone in ignored field'); -} - - - -done_testing(); - diff --git a/t/05_rest.t b/t/05_rest.t new file mode 100644 index 0000000..e680a47 --- /dev/null +++ b/t/05_rest.t @@ -0,0 +1,60 @@ +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 = <
"
+ },
+ "arr": [
+ "one test
",
+ "two "
+ ],
+ "some_html": "Leave this alone:
"
+}
+JSON
+ my $req = POST('/foo',
+ Content_Type => 'application/json', Content => $json_body
+ );
+ 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 this alone:
',
+ 'Body data param matching ignore_params left alone',
+ );
+}
+
+done_testing();
+
diff --git a/t/lib/MyApp03.pm b/t/lib/MyApp03.pm
index 5ba1683..615f94a 100644
--- a/t/lib/MyApp03.pm
+++ b/t/lib/MyApp03.pm
@@ -9,8 +9,17 @@ extends 'Catalyst';
__PACKAGE__->config(
name => 'MyApp03',
- scrubber => [allow => [qw/br hr b/],]
+ scrubber => {
+ auto => 1,
+
+ ignore_params => [ qr/_html$/, 'ignored_param' ],
+
+ # params for HTML::Scrubber
+ params => [
+ allow => [qw/br hr b/],
+ ],
+ }
);
__PACKAGE__->setup();
diff --git a/t/lib/MyApp05.pm b/t/lib/MyApp05.pm
index ffac01c..3131524 100644
--- a/t/lib/MyApp05.pm
+++ b/t/lib/MyApp05.pm
@@ -10,13 +10,20 @@ extends 'Catalyst';
__PACKAGE__->config(
name => 'MyApp03',
scrubber => {
- ignore_params => [
- qr/_html$/,
- 'ignored_param',
+
+ auto => 1,
+
+ ignore_params => [ qr/_html$/, 'ignored_param' ],
+
+ # params for HTML::Scrubber
+ params => [
+ allow => [qw/br hr b/],
],
- },
+ }
);
-__PACKAGE__->setup();
+
+
+__PACKAGE__->setup();
1;
diff --git a/t/lib/MyApp05/Controller/Root.pm b/t/lib/MyApp05/Controller/Root.pm
index 2a1feeb..937f72c 100644
--- a/t/lib/MyApp05/Controller/Root.pm
+++ b/t/lib/MyApp05/Controller/Root.pm
@@ -3,15 +3,30 @@ package MyApp05::Controller::Root;
use Moose;
use namespace::autoclean;
-BEGIN { extends 'Catalyst::Controller'; }
+BEGIN { extends 'Catalyst::Controller::REST' }
-__PACKAGE__->config(namespace => '');
+__PACKAGE__->config(
+ namespace => '',
+);
-sub index : Path : Args(0) {
+# default to avoid "No default action defined"
+sub foo : Path : ActionClass('REST') { }
+
+sub foo_GET {
my ($self, $c) = @_;
$c->res->body('index');
}
+sub foo_POST {
+ my ($self, $c) = @_;
+ $c->res->body('POST received');
+}
+
+sub index {
+ my ($self, $c) = @_;
+ $c->res->body("DEFAULT");
+}
+
1;