| 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
| |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | my $msg = defined $self->deny_page ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 0 | croak "first argument of each rule must be 'allow' or 'deny'"; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 12 | ▶ | if (!defined($rule)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | croak "rule argument must be defined"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 12 | ▶ | $allowing = ($allowing eq 'allow') ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 12 | my $check = $rule; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 12 | ▶ | if ($rule eq 'all') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $addr and $ip = "Net::IP"->new($addr)
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)
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $result and $result
MC/DC50%1/2
defined $result and $result
defined $result
$result
Truth table66%2/3
A and B
A= defined $resultB= $result
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 100 | ? $self->app->($env) : $self->deny_page->($env); | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 2 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||