blib/lib/Mojolicious/Plugin/CSRF.pm

filter: Statementstmt 100% 103 / 103 Branchbran 64.2% 18 / 28 Conditioncond 52.6% 20 / 38 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 100% 25 / 25 Podpod 100% 1 / 1 totaltotal 78.7% 170 / 216 CC 30 SCAR 26.1
CC
30
Cov
83%
check
31.3
register
6.2
__ANON__
4.1
CRAP 13.6
↑ Summary
1 package Mojolicious::Plugin::CSRF;
2 # ABSTRACT: Cross Site Request Forgery (CSRF) "prevention" Mojolicious plugin
3  
4 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mojo::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '1.06'; # VERSION
12  
13 1 ▶sub register {
Subroutine register: called CC=6 SCAR=18.2
Pod documented
14 1 my ( $self, $app, $conf ) = @_;
15  
16 1 ▶ my $csrf = Mojolicious::Plugin::CSRF::Base->new( ( ref $conf eq 'HASH' ) ? %$conf : () );
Branch: 1 branch
BranchTrueFalseTotal
ref $conf eq 'HASH' ? : 1 0 1
17 1 ▶ $app->helper( csrf => sub { $csrf->c( $_[0] ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 $conf->{hooks} //= [
20 3 ▶ before_routes => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 3 my ($c) = @_;
22 3 $c->csrf->setup;
23 3 $c->csrf->check;
24 },
25 3 ▶ after_render => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
26 3 my ( $c, $output, $format ) = @_;
27  
28 3 ▶ if ( $format eq 'html' and $$output ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq "html" and $$output) 3 0 3
Condition33%1/3
$format eq "html" and $$output
!ll&&!rl&&r
003
MC/DC0%0/2
$format eq "html" and $$output
$format eq "html" $$output
Truth table33%1/3
A and B
A= $format eq "html"B= $$output
ABresult
0X0
100
111
29 3 my $dom = Mojo::DOM->new( Mojo::Util::decode( 'UTF-8', $$output ) );
30 3 my $forms = $dom->find('form[method="post"]');
31  
32 3 ▶ if ( $forms->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forms->size) 2 1 3
33 2 ▶ $forms->each( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 $_->append_content(
35 '<input type="hidden" ' .
36 'name="' . $c->csrf->token_name . '" ' .
37 'value="' . $c->csrf->token . '">'
38 );
39 2 } );
40 2 $$output = Mojo::Util::encode( 'UTF-8', $dom->to_string );
41 }
42 }
43 },
44 1 ▶ ];
Condition50%1/2
$conf->{"hooks"} //= ["before_routes", sub { my($c) = @_; $c->csrf->setup; $c->csrf->check; } , "after_render", sub { my($c, $output, $format) = @_; if ($format eq "html" and $$output) { my $dom = "Mojo::DOM"->new(Mojo::Util::decode("UTF-8", $$output)); my $forms = $dom->find("form[method=\"post\"]"); if ($forms->size) { $forms->each(sub { $_->append_content('<input type="hidden" name="' . $c->csrf->token_name . '" ' . "value=\"" . $c->csrf->token . '">'); } ); $$output = Mojo::Util::encode("UTF-8", $dom->to_string); } ; } ; } ]
l!l
01
MC/DC0%0/1
$conf->{"hooks"} //= ["before_routes", sub { my($c) = @_; $c->csrf->setup; $c->csrf->check; } , "after_render", sub { my($c, $output, $format) = @_; if ($format eq "html" and $$output) { my $dom = "Mojo::DOM"->new(Mojo::Util::decode("UTF-8", $$output)); my $forms = $dom->find("form[method=\"post\"]"); if ($forms->size) { $forms->each(sub { $_->append_content('<input type="hidden" name="' . $c->csrf->token_name . '" ' . "value=\"" . $c->csrf->token . '">'); } ); $$output = Mojo::Util::encode("UTF-8", $dom->to_string); } ; } ; } ]
$conf->{"hooks"}
Truth table50%1/2
A
A= $conf->{"hooks"}
Aresult
11
00
45  
46 2 $app->hook( shift @{ $conf->{hooks} }, shift @{ $conf->{hooks} } )
47 1 ▶ while ( ref $conf->{hooks} eq 'ARRAY' and @{ $conf->{hooks} } and not @{ $conf->{hooks} } % 2 );
Branch: 1 branch
BranchTrueFalseTotal
ref $conf->{"hooks"} eq "ARRAY" and @{$conf->{"hooks"};} and not @{$conf->{"hooks"};} % 2 and do { $app->hook(shift @{$$conf{"hooks"};}, shift @{$$conf{"hooks"};}) } 2 1 3
Condition66%4/6
ref $conf->{"hooks"} eq "ARRAY" and @{$conf->{"hooks"};} and not @{$conf->{"hooks"};} % 2
!ll&&!rl&&r
102
ref $conf->{"hooks"} eq "ARRAY" and @{$conf->{"hooks"};}
!ll&&!rl&&r
012
MC/DC33%1/3
ref $conf->{"hooks"} eq "ARRAY" and @{$conf->{"hooks"};} and not @{$conf->{"hooks"};} % 2
ref $conf->{"hooks"} eq "ARRAY" @{$conf->{"hooks"};} not @{$conf->{"hooks"};} % 2
Truth table50%2/4
A and B and not(C)
A= ref $conf->{"hooks"} eq "ARRAY"B= @{$conf->{"hooks"};}C= not @{$conf->{"hooks"};} % 2
ABCresult
0XX0
10X0
1100
1111
48  
49 1 return;
50 }
51  
52 package Mojolicious::Plugin::CSRF::Base;
53  
54 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 2 has c => undef;
58 1 ▶has generate_token => sub { sub { unpack( 'H*', Crypt::URandom::urandom(16) ) } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 has token_name => 'csrf_token';
60 2 has header => 'X-CSRF-Token';
61 1 ▶has methods => sub { [ qw( POST PUT DELETE PATCH ) ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 has include => undef;
63 2 has exclude => undef;
64  
65 1 ▶has on_success => sub { sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 my ($c) = @_;
67 1 $c->log->info('CSRF check success');
68 1 return 1;
69 1 } };
70  
71 1 ▶has on_failure => sub { sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 my ($c) = @_;
73 1 $c->reply->exception( 'Access Forbidden: CSRF check failure', { status => 403 } );
74 1 return 0;
75 1 } };
76  
77 3 ▶sub setup {
Subroutine setup: called CC=1 SCAR=0.0
78 3 my ($self) = @_;
79 3 $self->c->res->headers->add( $self->header => $self->token );
80 3 return $self;
81 }
82  
83 3 ▶sub check {
Subroutine check: called CC=21 SCAR=34.4
84 3 my ($self) = @_;
85  
86 3 my $path = $self->c->req->url->path->to_string;
87 3 ▶ return if ( $self->c->app->static->file($path) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->c->app->static->file($path) 0 3 3
88  
89 3 my $method = $self->c->req->method;
90 3 ▶ my @methods = @{ ( $self->methods and ref $self->methods eq 'ARRAY' ) ? $self->methods : [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->methods && ref $self->methods eq 'ARRAY' ? : 3 0 3
Condition33%1/3
$self->methods and ref $self->methods eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
$self->methods and ref $self->methods eq "ARRAY"
$self->methods ref $self->methods eq "ARRAY"
Truth table33%1/3
A and B
A= $self->methodsB= ref $self->methods eq "ARRAY"
ABresult
0X0
100
111
91 3 ▶ @methods = 'any' unless @methods;
Branch: 1 branch
BranchTrueFalseTotal
unless @methods 0 3 3
92 3 ▶ return unless ( grep { uc $_ eq $method or lc $_ eq 'any' } @methods );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {uc $_ eq $method or lc $_ eq "any";} @methods 1 2 3
Condition66%2/3
uc $_ eq $method || lc $_ eq 'any'
l!l&&r!l&&!r
2010
MC/DC50%1/2
uc $_ eq $method || lc $_ eq 'any'
uc $_ eq $method lc $_ eq 'any'
Truth table66%2/3
A || B
A= uc $_ eq $methodB= lc $_ eq 'any'
ABresult
1X1
011
000
93  
94 2 ▶ if ( $self->include and ref $self->include eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->include and ref $self->include eq "ARRAY") 2 0 2
Condition33%1/3
$self->include and ref $self->include eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
$self->include and ref $self->include eq "ARRAY"
$self->include ref $self->include eq "ARRAY"
Truth table33%1/3
A and B
A= $self->includeB= ref $self->include eq "ARRAY"
ABresult
0X0
100
111
95 2 my $include = 0;
96 2 for ( @{ $self->include } ) {
97 2 ▶ if ( $path =~ $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$_/u) 2 0 2
98 2 $include = 1;
99 2 last;
100 }
101 }
102 2 ▶ return unless $include;
Branch: 1 branch
BranchTrueFalseTotal
unless $include 0 2 2
103 }
104  
105 2 ▶ if ( $self->exclude and ref $self->exclude eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exclude and ref $self->exclude eq "ARRAY") 2 0 2
Condition33%1/3
$self->exclude and ref $self->exclude eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
$self->exclude and ref $self->exclude eq "ARRAY"
$self->exclude ref $self->exclude eq "ARRAY"
Truth table33%1/3
A and B
A= $self->excludeB= ref $self->exclude eq "ARRAY"
ABresult
0X0
100
111
106 2 for ( @{ $self->exclude } ) {
107 2 ▶ return if ( $path =~ $_ );
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$_/u 0 2 2
108 }
109 }
110  
111 2 my $session = $self->c->session( $self->token_name );
112 2 my $param = $self->c->param( $self->token_name );
113 2 my $header = $self->c->req->headers->header( $self->header );
114 2 ▶ my $on = 'on_' . ( (
Branch: 1 branch
BranchTrueFalseTotal
!($session && ($param && $param eq $session || $header && $header eq $session)) ? : 1 1 2
Condition58%7/12
$session && ($param && $param eq $session || $header && $header eq $session)
!ll&&!rl&&r
011
$param and $param eq $session or $header and $header eq $session
l!l&&r!l&&!r
101
$param and $param eq $session
!ll&&!rl&&r
101
$header and $header eq $session
!ll&&!rl&&r
100
MC/DC20%1/5
$session && ($param && $param eq $session || $header && $header eq $session)
$session $param $param eq $session $header $header eq $session
Truth table25%2/8
A && B and C or D and E
A= $sessionB= $paramC= $param eq $sessionD= $headerE= $header eq $session
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
115 not $session or
116 not (
117 $param and $param eq $session or
118 $header and $header eq $session
119 )
120 ) ? 'failure' : 'success' );
121  
122 2 return $self->$on->( $self->c );
123 }
124  
125 7 ▶sub token {
Subroutine token: called CC=2 SCAR=7.1
126 7 my ($self) = @_;
127 return
128 7 ▶ $self->c->session( $self->token_name ) ||
Condition66%2/3
$self->c->session($self->token_name) || $self->c->session($self->token_name, $self->generate_token->())->session($self->token_name)
l!l&&r!l&&!r
610
MC/DC0%0/2
$self->c->session($self->token_name) || $self->c->session($self->token_name, $self->generate_token->())->session($self->token_name)
$self->c->session($self->token_name) $self->c->session($self->token_name, $self->generate_token->())->session($self->token_name)
Truth table66%2/3
A || B
A= $self->c->session($self->token_name)B= $self->c->session($self->token_name, $self->generate_token->())->session($self->token_name)
ABresult
1X1
011
000
129 $self->c->session( $self->token_name => $self->generate_token->() )->session( $self->token_name );
130 }
131  
132 2 ▶sub url_for {
Subroutine url_for: called CC=1 SCAR=0.0
133 2 my ( $self, @params ) = @_;
134 2 return $self->c->url_for(@params)->query( { $self->token_name => $self->token } );
135 }
136  
137 1 ▶sub delete_token {
Subroutine delete_token: called CC=1 SCAR=0.0
138 1 my ($self) = @_;
139 1 delete $self->c->session->{ $self->token_name };
140 1 return $self;
141 }
142  
143 2 1;
144  
145 __END__
↑ Summary