blib/lib/Plack/Middleware/Access.pm

filter: Statementstmt 91.4% 64 / 70 Branchbran 77.7% 28 / 36 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 82.4% 113 / 137 CC 23 SCAR 22.2
CC
23
Cov
83%
prepare_app
17.9
__ANON__
5.3
allow
4.0
CRAP 9.2
↑ Summary
1 package Plack::Middleware::Access;
2 #ABSTRACT: Restrict access depending on remote ip or other parameters
3 2 $Plack::Middleware::Access::VERSION = '0.4';
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Plack::Util::Accessor qw(rules deny_page);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶sub prepare_app {
Subroutine prepare_app: called CC=15 SCAR=28.8
Pod documented
15 9 my $self = shift;
16  
17 9 ▶ if (!ref $self->deny_page) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $self->deny_page) { } 8 1 9
elsif (ref $self->deny_page ne 'CODE') { } 0 1 1
18 8 ▶ my $msg = defined $self->deny_page ?
Branch: 1 branch
BranchTrueFalseTotal
defined $self->deny_page ? : 1 7 8
19 $self->deny_page : 'Forbidden';
20 5 ▶ $self->deny_page(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 5 [403, [ 'Content-Type' =>'text/plain',
22 'Content-Length' => length $msg ], [ $msg ] ];
23 8 });
24 } elsif (ref $self->deny_page ne 'CODE') {
25 0 croak "deny_page must be a CODEREF";
26 }
27  
28 9 ▶ if (!defined($self->rules)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $self->rules) { } 1 8 9
elsif (ref $self->rules ne 'ARRAY') { } 0 8 8
elsif (@{$self->rules;} % 2 != 0) { } 0 8 8
29 1 $self->rules([]);
30 } elsif( ref($self->rules) ne 'ARRAY' ) {
31 0 croak "rules must be an ARRAYREF";
32 8 } elsif (@{ $self->rules } % 2 != 0) {
33 0 croak "rules must contain an even number of params";
34 }
35  
36 9 my @rules = ();
37  
38 9 ▶ foreach (my $i = 0; $i < @{ $self->rules }; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->rules;}) 12 9 21
39 12 my $allowing = $self->rules->[$i];
40 12 my $rule = $self->rules->[$i + 1];
41  
42 12 ▶ if ($allowing !~ /^(allow|deny)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowing =~ /^(allow|deny)$/) 0 12 12
43 0 croak "first argument of each rule must be 'allow' or 'deny'";
44 }
45  
46 12 ▶ if (!defined($rule)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule) 0 12 12
47 0 croak "rule argument must be defined";
48 }
49  
50 12 ▶ $allowing = ($allowing eq 'allow') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowing eq 'allow' ? : 4 8 12
51 12 my $check = $rule;
52  
53 12 ▶ if ($rule eq 'all') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule eq 'all') { } 3 9 12
elsif ($rule =~ /[A-Z]$/i) { } 2 7 9
elsif (ref $rule ne 'CODE') { } 5 2 7
54 3 ▶ $check = sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
55 } elsif ($rule =~ /[A-Z]$/i) {
56 2 ▶ $check = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
57 2 my $host = $_[0]->{REMOTE_HOST};
58 2 ▶ return unless defined $host; # skip rule
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 2 2
59 2 return $host =~ qr/^(.*\.)?\Q${rule}\E$/;
60 2 };
61 } elsif ( ref($rule) ne 'CODE' ) {
62 5 ▶ my $netip = Net::IP->new($rule) or
Branch: 1 branch
BranchTrueFalseTotal
my $netip = "Net::IP"->new($rule) or die "not supported type of rule argument [$rule] or bad ip: " . Net::IP::Error() 5 0 5
63 die "not supported type of rule argument [$rule] or bad ip: " . Net::IP::Error();
64 4 ▶ $check = sub {
Subroutine __ANON__: called CC=4 SCAR=16.6
65 4 my $addr = $_[0]->{REMOTE_ADDR};
66 4 my $ip;
67 4 ▶ if (defined($addr) && ($ip = Net::IP->new($addr))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $addr and $ip = 'Net::IP'->new($addr)) { } 4 0 4
Condition33%1/3
defined $addr and $ip = "Net::IP"->new($addr)
!ll&&!rl&&r
004
MC/DC0%0/2
defined $addr and $ip = "Net::IP"->new($addr)
defined $addr $ip = "Net::IP"->new($addr)
Truth table33%1/3
A and B
A= defined $addrB= $ip = "Net::IP"->new($addr)
ABresult
0X0
100
111
68 4 my $overlaps = $netip->overlaps($ip);
69 4 ▶ return $overlaps == $IP_B_IN_A_OVERLAP || $overlaps == $IP_IDENTICAL;
Condition33%1/3
$overlaps == $Plack::Middleware::Access::IP_B_IN_A_OVERLAP || $overlaps == $Plack::Middleware::Access::IP_IDENTICAL
l!l&&r!l&&!r
040
MC/DC0%0/2
$overlaps == $Plack::Middleware::Access::IP_B_IN_A_OVERLAP || $overlaps == $Plack::Middleware::Access::IP_IDENTICAL
$overlaps == $Plack::Middleware::Access::IP_B_IN_A_OVERLAP $overlaps == $Plack::Middleware::Access::IP_IDENTICAL
Truth table33%1/3
A || B
A= $overlaps == $Plack::Middleware::Access::IP_B_IN_A_OVERLAPB= $overlaps == $Plack::Middleware::Access::IP_IDENTICAL
ABresult
1X1
011
000
70 } else {
71 0 return undef;
72 }
73 5 };
74 }
75  
76 12 push @rules, [ $check => $allowing ];
77 }
78  
79 9 $self->rules(\@rules);
80 }
81  
82 9 ▶sub allow {
Subroutine allow: called CC=4 SCAR=13.9
Pod documented
83 9 my ($self, $env) = @_;
84  
85 9 foreach my $rule (@{ $self->rules }) {
86 9 my ($check, $allow) = @{$rule};
87 9 my $result = $check->($env);
88 9 ▶ if (defined $result && $result) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and $result) 8 1 9
Condition66%2/3
defined $result and $result
!ll&&!rl&&r
018
MC/DC50%1/2
defined $result and $result
defined $result $result
Truth table66%2/3
A and B
A= defined $resultB= $result
ABresult
0X0
100
111
89 8 return $allow;
90 }
91 }
92  
93 1 return 1;
94 }
95  
96 9 ▶sub call {
Subroutine call: called CC=2 SCAR=6.9
Pod documented
97 9 my ($self, $env) = @_;
98  
99 9 ▶ return $self->allow($env)
Branch: 1 branch
BranchTrueFalseTotal
$self->allow($env) ? : 3 6 9
100 ? $self->app->($env) : $self->deny_page->($env);
101 }
102  
103 2 1;
104  
105 __END__
↑ Summary