| 1 | package Mojolicious::Plugin::DeCSRF; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.94'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 8 | 2 | my ($self, $app, $conf) = @_; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | my $base = Mojolicious::Plugin::DeCSRF::Base->new; | |||||||||||||||||||||||||||
| 11 | 2 | ▶ | $base->{token_length} = $conf->{token_length} if $conf->{token_length}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | $base->{token_name} = $conf->{token_name} if $conf->{token_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | $base->{on_mismatch} = $conf->{on_mismatch} if $conf->{on_mismatch}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | push @{$base->urls}, $conf->{urls} if $conf->{urls}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | $app->helper(decsrf => sub { $base->c(shift) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | ▶ | $app->hook(before_dispatch => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 19 | 13 | my $c = shift; | |||||||||||||||||||||||||||
| 20 | 13 | ▶ | return $c if $base->c($c)->check; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | if ($base->on_mismatch && ref($base->on_mismatch) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$base->on_mismatch and ref $base->on_mismatch eq "CODE"
MC/DC50%1/2
$base->on_mismatch and ref $base->on_mismatch eq "CODE"
$base->on_mismatch
ref $base->on_mismatch eq "CODE"
Truth table66%2/3
A and B
A= $base->on_mismatchB= ref $base->on_mismatch eq "CODE"
| |||||||||||||||||||||||||||||
| 22 | 1 | $base->on_mismatch->($c); | |||||||||||||||||||||||||||
| 23 | } else { | ||||||||||||||||||||||||||||
| 24 | 4 | $c->render( | |||||||||||||||||||||||||||
| 25 | text => "Forbidden!", | ||||||||||||||||||||||||||||
| 26 | status => 403, | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 2 | ); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | package Mojolicious::Plugin::DeCSRF::Base; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | my $_token_checked = 1; | |||||||||||||||||||||||||||
| 38 | 2 | has c => undef; | |||||||||||||||||||||||||||
| 39 | 2 | has token_length => 4; | |||||||||||||||||||||||||||
| 40 | 2 | has token_name => 'token'; | |||||||||||||||||||||||||||
| 41 | 2 | has on_mismatch => undef; | |||||||||||||||||||||||||||
| 42 | 2 | ▶ | has urls => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 13 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 45 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 13 | my $c = $self->c; | |||||||||||||||||||||||||||
| 47 | 13 | my $token = $c->session($self->token_name); | |||||||||||||||||||||||||||
| 48 | 13 | $_token_checked = 1; | |||||||||||||||||||||||||||
| 49 | 13 | ▶ | if ($self->_match($c->req->url)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 8 | ▶ | return 0 unless ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->req->param($self->token_name) and $c->req->param($self->token_name) eq $token
MC/DC100%2/2
$c->req->param($self->token_name) and $c->req->param($self->token_name) eq $token
$c->req->param($self->token_name)
$c->req->param($self->token_name) eq $token
Truth table100%3/3
A and B
A= $c->req->param($self->token_name)B= $c->req->param($self->token_name) eq $token
| |||||||||||||||||||||||||||||
| 52 | $c->req->param($self->token_name) | ||||||||||||||||||||||||||||
| 53 | && $c->req->param($self->token_name) eq $token | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | 8 | return 1; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 10 | ▶ | sub url { | ||||||||||||||||||||||||||
|
Subroutine
url: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 60 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | my $url = shift // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // ""
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 62 | 10 | my $c = $self->c; | |||||||||||||||||||||||||||
| 63 | 10 | ▶ | if ($_token_checked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 8 | $c->session($self->token_name => $self->_token); | |||||||||||||||||||||||||||
| 65 | 8 | $_token_checked = 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 10 | ▶ | if ($self->_match($url)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 3 | return $c->url_for($url)->query([$self->token_name => $c->session($self->token_name)]); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 7 | return $c->url_for($url); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 23 | ▶ | sub _match { | ||||||||||||||||||||||||||
|
Subroutine
_match: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 74 | 23 | my ($self, $url) = @_; | |||||||||||||||||||||||||||
| 75 | 23 | ▶ | if (@{$self->urls} && $url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$self->urls;} and $url
MC/DC50%1/2
@{$self->urls;} and $url
@{$self->urls;}
$url
Truth table66%2/3
A and B
A= @{$self->urls;}B= $url
| |||||||||||||||||||||||||||||
| 76 | 17 | foreach (@{$self->urls}) { | |||||||||||||||||||||||||||
| 77 | 24 | ▶ | return 1 if $self->c->url_for($url)->path =~ m;^$_$;; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 12 | return 0; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 8 | ▶ | sub _token { | ||||||||||||||||||||||||||
|
Subroutine
_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 8 | my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(@ $ - _) ); | |||||||||||||||||||||||||||
| 85 | 8 | return join("", @chars[ map { rand @chars } ( 1 .. shift->token_length ) ]); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | __END__ | ||||||||||||||||||||||||||||