blib/lib/Mojolicious/Plugin/SecureOnly.pm

filter: Statementstmt 96.5% 28 / 29 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 80.0% 4 / 5 Podpod 50.0% 1 / 2 totaltotal 81.1% 56 / 69 CC 13 SCAR 20.6
CC
13
Cov
87%
__ANON__
11.2
detect_proxy
2.0
register
2.0
CRAP 7.8
↑ Summary
1 package Mojolicious::Plugin::SecureOnly;
2 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = '0.03';
5  
6 0 ▶has 'conf' => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
7  
8 3 ▶sub register {
Subroutine register: called CC=2 SCAR=7.0
Pod documented
9 3 my ($self, $app, $conf) = @_;
10  
11 3 ▶ $self->conf({%$conf, %{$app->config('SecureOnly')||{}}});
Condition50%1/2
$app->config('SecureOnly') || {}
l!l
03
MC/DC0%0/1
$app->config('SecureOnly') || {}
$app->config('SecureOnly')
Truth table50%1/2
A
A= $app->config('SecureOnly')
Aresult
11
00
12  
13 8 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: called CC=11 SCAR=24.1
14 8 my $c = shift;
15  
16 8 ▶ if ( $self->conf->{not_modes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"not_modes"}) 2 6 8
17 2 ▶ return if grep { $_ eq $app->mode } @{$self->conf->{not_modes}||[]};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $app->mode;} @{$self->conf->{"not_modes"} or [];} 1 1 2
Condition50%1/2
$self->conf->{'not_modes'} || []
l!l
20
MC/DC0%0/1
$self->conf->{'not_modes'} || []
$self->conf->{'not_modes'}
Truth table50%1/2
A
A= $self->conf->{'not_modes'}
Aresult
11
00
18 }
19 7 ▶ if ( $self->conf->{modes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{"modes"}) 2 5 7
20 2 ▶ return unless grep { $_ eq $app->mode } @{$self->conf->{modes}||[]};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $app->mode;} @{$self->conf->{"modes"} or [];} 1 1 2
Condition50%1/2
$self->conf->{'modes'} || []
l!l
20
MC/DC0%0/1
$self->conf->{'modes'} || []
$self->conf->{'modes'}
Truth table50%1/2
A
A= $self->conf->{'modes'}
Aresult
11
00
21 }
22  
23 6 ▶ return if $c->req->is_secure;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->is_secure 0 6 6
24 6 ▶ return $app->log->warn('SecureOnly disabled; Reverse Proxy support not enabled in Mojolicious, see http://mojolicious.org/perldoc/Mojo/Server#reverse_proxy')
Branch: 1 branch
BranchTrueFalseTotal
if not $c->tx->req->reverse_proxy and detect_proxy($c) 1 5 6
Condition100%3/3
not $c->tx->req->reverse_proxy and detect_proxy($c)
!ll&&!rl&&r
411
MC/DC100%2/2
not $c->tx->req->reverse_proxy and detect_proxy($c)
not $c->tx->req->reverse_proxy detect_proxy($c)
Truth table100%3/3
not(A) and B
A= not $c->tx->req->reverse_proxyB= detect_proxy($c)
ABresult
0X0
100
111
25 if !$c->tx->req->reverse_proxy && detect_proxy($c);
26  
27 5 my $url = $c->req->url->to_abs;
28 5 $url->scheme('https');
29 5 ▶ $url->port($self->conf->{secureport}) if $self->conf->{secureport};
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"secureport"} 0 5 5
30 5 $c->app->log->debug("SecureOnly enabled; Request for insecure resource, redirecting to $url");
31 5 $c->redirect_to($url);
32 3 });
33 }
34  
35 2 ▶sub detect_proxy {
Subroutine detect_proxy: called CC=2 SCAR=7.1
Pod undocumented
36 2 my $c = shift;
37 2 ▶ return $c->tx->req->headers->header('X-Forwarded-For') || $c->tx->req->headers->header('X-Forwarded-Proto')
Condition66%2/3
$c->tx->req->headers->header('X-Forwarded-For') || $c->tx->req->headers->header('X-Forwarded-Proto')
l!l&&r!l&&!r
101
MC/DC50%1/2
$c->tx->req->headers->header('X-Forwarded-For') || $c->tx->req->headers->header('X-Forwarded-Proto')
$c->tx->req->headers->header('X-Forwarded-For') $c->tx->req->headers->header('X-Forwarded-Proto')
Truth table66%2/3
A || B
A= $c->tx->req->headers->header('X-Forwarded-For')B= $c->tx->req->headers->header('X-Forwarded-Proto')
ABresult
1X1
011
000
38 }
39  
40 3 1;
41  
42 __END__
↑ Summary