| 1 | package Mojolicious::Plugin::WithCSRFProtection; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Mojolicious plugin providing CSRF protection at the routing level | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.7
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 2 | my ( $self, $app ) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | my $routes = $app->routes; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | $app->helper( | ||||||||||||||||||||||||||||
| 15 | 9 | ▶ | 'reply.bad_csrf' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 16 | 9 | my ($c) = @_; | |||||||||||||||||||||||||||
| 17 | 9 | $c->res->code(403); | |||||||||||||||||||||||||||
| 18 | 9 | ▶ | $c->render_maybe('bad_csrf') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | or $c->render( text => 'Failed CSRF check' ); | ||||||||||||||||||||||||||||
| 20 | 9 | return; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | 2 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $routes->add_condition( | ||||||||||||||||||||||||||||
| 25 | 15 | ▶ | with_csrf_protection => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=31.3
| |||||||||||||||||||||||||||||
| 26 | 15 | my ( $route, $c ) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 15 | ▶ | if ( $Mojolicious::VERSION >= 9.48 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 15 | ▶ | if ( $c->validation->csrf_protect->has_error('csrf_token') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 9 | ▶ | $c->reply->bad_csrf unless $c->stash->{'mojo.finished'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 9 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my $csrf = $c->req->headers->header('X-CSRF-Token') | ||||||||||||||||||||||||||
|
Condition0%0/3
$c->req->headers->header('X-CSRF-Token') || $c->param('csrf_token')
MC/DC0%0/2
$c->req->headers->header('X-CSRF-Token') || $c->param('csrf_token')
$c->req->headers->header('X-CSRF-Token')
$c->param('csrf_token')
Truth table0%0/3
A || B
A= $c->req->headers->header('X-CSRF-Token')B= $c->param('csrf_token')
| |||||||||||||||||||||||||||||
| 36 | || $c->param('csrf_token'); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | unless ( $csrf && $csrf eq $c->csrf_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$csrf and $csrf eq $c->csrf_token
MC/DC0%0/2
$csrf and $csrf eq $c->csrf_token
$csrf
$csrf eq $c->csrf_token
Truth table0%0/3
A and B
A= $csrfB= $csrf eq $c->csrf_token
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $c->reply->bad_csrf unless $c->stash->{'mojo.finished'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | return 1; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 2 | ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | $routes->add_shortcut( | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | with_csrf_protection => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 50 | 1 | my ($route) = @_; | |||||||||||||||||||||||||||
| 51 | 1 | ▶ | if ( $Mojolicious::VERSION >= 9 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | return $route->requires( with_csrf_protection => 1 ); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||
| 55 | 0 | return $route->over( with_csrf_protection => 1 ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 2 | ); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | return; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | 1; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||