blib/lib/Mojolicious/Plugin/WithCSRFProtection.pm

filter: Statementstmt 82.7% 24 / 29 Branchbran 50.0% 7 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 62.7% 37 / 59 CC 10 SCAR 26.3
CC
10
Cov
63%
__ANON__
22.9
__ANON__
2.1
__ANON__
2.0
CRAP 13.8
↑ Summary
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
BranchTrueFalseTotal
$c->render_maybe("bad_csrf") or $c->render("text", "Failed CSRF check") 1 8 9
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
BranchTrueFalseTotal
if ($Mojolicious::VERSION >= 9.48) { } 15 0 15
29 15 ▶ if ( $c->validation->csrf_protect->has_error('csrf_token') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->validation->csrf_protect->has_error("csrf_token")) 9 6 15
30 9 ▶ $c->reply->bad_csrf unless $c->stash->{'mojo.finished'};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash->{"mojo.finished"} 9 0 9
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')
l!l&&r!l&&!r
000
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')
ABresult
1X1
011
000
36 || $c->param('csrf_token');
37  
38 0 ▶ unless ( $csrf && $csrf eq $c->csrf_token ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($csrf and $csrf eq $c->csrf_token) 0 0 0
Condition0%0/3
$csrf and $csrf eq $c->csrf_token
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
39 0 ▶ $c->reply->bad_csrf unless $c->stash->{'mojo.finished'};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash->{"mojo.finished"} 0 0 0
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
BranchTrueFalseTotal
if ($Mojolicious::VERSION >= 9) { } 1 0 1
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__
↑ Summary