| 1 | package Mojolicious::Plugin::CSRFProtect; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Mojo::Util qw/md5_sum/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Mojo::ByteStream qw/b/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub register { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | my ( $self, $app, $conf ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # On error callback | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | my $on_error; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | if ( $conf->{on_error} && ref($conf->{on_error}) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$conf->{"on_error"} and ref $conf->{"on_error"} eq "CODE"
MC/DC50%1/2
$conf->{"on_error"} and ref $conf->{"on_error"} eq "CODE"
$conf->{"on_error"}
ref $conf->{"on_error"} eq "CODE"
Truth table66%2/3
A and B
A= $conf->{"on_error"}B= ref $conf->{"on_error"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | $on_error = $conf->{on_error}; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | $on_error = sub { shift->render( status => 403, text => "Forbidden!" ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Replace "form_for" helper | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | my $original_form_for = delete $app->renderer->helpers->{form_for}; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | croak qq{Cannot find helper "form_for". Please, load plugin "TagHelpers" before} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | unless $original_form_for; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | $app->helper( | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | form_for => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ( defined $_[-1] && ref( $_[-1] ) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[-1] and ref $_[-1] eq "CODE"
MC/DC0%0/2
defined $_[-1] and ref $_[-1] eq "CODE"
defined $_[-1]
ref $_[-1] eq "CODE"
Truth table0%0/3
A and B
A= defined $_[-1]B= ref $_[-1] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $cb = $_[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $_[-1] = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $app->hidden_field( 'csrftoken' => $self->_csrftoken($c) ) . $cb->(); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return $app->$original_form_for(@_); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # Add "csrftoken" helper | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | $app->helper( csrftoken => sub { $self->_csrftoken( $_[0] ) } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Add "is_valid_csrftoken" helper | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | $app->helper( is_valid_csrftoken => sub { $self->_is_valid_csrftoken( $_[0] ) } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | # Add "jquery_ajax_csrf_protection" helper | ||||||||||||||||||||||||||||||||||||||||||
| 47 | $app->helper( | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | jquery_ajax_csrf_protection => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | my $js = '<meta name="csrftoken" content="' . $self->_csrftoken( $_[0] ) . '"/>'; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | $js .= q!<script type="text/javascript">!; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | $js .= q! jQuery(document).ajaxSend(function(e, xhr, options) { !; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | $js .= q! var token = jQuery("meta[name='csrftoken']").attr("content");!; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $js .= q! xhr.setRequestHeader("X-CSRF-Token", token);!; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | $js .= q! });</script>!; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | b($js); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # input check | ||||||||||||||||||||||||||||||||||||||||||
| 60 | $app->hook( | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 18 | ▶ | before_routes => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 18 | my ($c) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 18 | my $request_token = $c->req->param('csrftoken'); | |||||||||||||||||||||||||||||||||||||||||
| 65 | #my $is_ajax = ( $c->req->headers->header('X-Requested-With') || '' ) eq 'XMLHttpRequest'; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | ▶ | if ( $c->req->method !~ m/^(?:GET|HEAD|OPTIONS)$/ && !$self->_is_valid_csrftoken($c) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->req->method =~ /^(?:GET|HEAD|OPTIONS)$/u or $self->_is_valid_csrftoken($c)
MC/DC100%2/2
$c->req->method =~ /^(?:GET|HEAD|OPTIONS)$/u or $self->_is_valid_csrftoken($c)
$c->req->method =~ /^(?:GET|HEAD|OPTIONS)$/u
$self->_is_valid_csrftoken($c)
Truth table100%3/3
A or B
A= $c->req->method =~ /^(?:GET|HEAD|OPTIONS)$/uB= $self->_is_valid_csrftoken($c)
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 3 | my $path = $c->tx->req->url->to_abs->to_string; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | $c->app->log->debug("CSRFProtect: Wrong CSRF protection token for [$path]!"); | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 3 | $on_error->($c); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 15 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | ▶ | sub _is_valid_csrftoken { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_valid_csrftoken: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 10 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 10 | my $valid_token = $c->session('csrftoken'); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | my $form_token = $c->req->headers->header('X-CSRF-Token') || $c->param('csrftoken'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$c->req->headers->header('X-CSRF-Token') || $c->param('csrftoken')
MC/DC0%0/2
$c->req->headers->header('X-CSRF-Token') || $c->param('csrftoken')
$c->req->headers->header('X-CSRF-Token')
$c->param('csrftoken')
Truth table66%2/3
A || B
A= $c->req->headers->header('X-CSRF-Token')B= $c->param('csrftoken')
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 10 | ▶ | unless ( $valid_token && $form_token && $form_token eq $valid_token ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$valid_token and $form_token and $form_token eq $valid_token
$valid_token and $form_token
MC/DC33%1/3
$valid_token and $form_token and $form_token eq $valid_token
$valid_token
$form_token
$form_token eq $valid_token
Truth table50%2/4
A and B and C
A= $valid_tokenB= $form_tokenC= $form_token eq $valid_token
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 8 | ▶ | sub _csrftoken { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_csrftoken: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 8 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | ▶ | return $c->session('csrftoken') if $c->session('csrftoken'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | my $token = md5_sum( md5_sum( time() . {} . rand() . $$ ) ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | $c->session( 'csrftoken' => $token ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | return $token; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||||||||||