Skip to content

Commit

Permalink
a few more CSRF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 4, 2013
1 parent 490e48b commit 59ca3a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -745,11 +745,11 @@ L<Mojolicious::Validator::Validation/"error">.

=head2 Cross-site request forgery

Cross-site request forgery is a very common attack on web applications that
trick your logged in users to submit forms they did not intend to send. All
you have to do to protect your users from this, is to add an additional hidden
field to your forms with L<Mojolicious::Plugin::TagHelpers/"csrf_field"> and
validate it with L<Mojolicious::Validator::Validation/"csrf_protect">.
CSRF is a very common attack on web applications that trick your logged in
users to submit forms they did not intend to send. All you have to do to
protect your users from this, is to add an additional hidden field to your
forms with L<Mojolicious::Plugin::TagHelpers/"csrf_field"> and validate it
with L<Mojolicious::Validator::Validation/"csrf_protect">.

use Mojolicious::Lite;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/TagHelpers.pm
Expand Up @@ -314,7 +314,7 @@ picked up and shown as default.
%= csrf_field
Generate hidden input element with CSRF token from
Generate hidden input element with
L<Mojolicious::Plugin::DefaultHelpers/"csrf_token">.
<input name="csrf_token" type="hidden" value="fa6a08..." />
Expand Down
9 changes: 6 additions & 3 deletions t/mojolicious/validation_lite_app.t
Expand Up @@ -175,7 +175,8 @@ $t->post_ok('/' => form => {foo => 'no'})->status_is(200)

# Missing CSRF token
$t->get_ok('/forgery' => form => {foo => 'bar'})->status_is(200)
->content_like(qr/Wrong or missing CSRF token!/);
->content_like(qr/Wrong or missing CSRF token!/)
->element_exists('[value=bar]');

# Correct CSRF token
my $token
Expand All @@ -191,11 +192,13 @@ $t->post_ok('/forgery' => {'X-CSRF-Token' => $token} => form => {foo => 'bar'})

# Wrong CSRF token (header)
$t->post_ok('/forgery' => {'X-CSRF-Token' => 'abc'} => form => {foo => 'bar'})
->status_is(200)->content_like(qr/Wrong or missing CSRF token!/);
->status_is(200)->content_like(qr/Wrong or missing CSRF token!/)
->element_exists('[value=bar]');

# Missing CSRF token again
$t->post_ok('/forgery' => form => {foo => 'bar'})->status_is(200)
->content_like(qr/Wrong or missing CSRF token!/);
->content_like(qr/Wrong or missing CSRF token!/)
->element_exists('[value=bar]');

# Failed validation for all fields (with custom helper)
$t->app->helper(
Expand Down

0 comments on commit 59ca3a4

Please sign in to comment.