blib/lib/Plack/Middleware/Greylist.pm

filter: Statementstmt 99.0% 107 / 108 Branchbran 76.0% 38 / 50 Conditioncond 69.4% 25 / 36 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 84.1% 197 / 234 CC 42 SCAR 29.3
CC
42
Cov
88%
prepare_app
33.0
call
14.0
__ANON__
2.1
CRAP 18.8
↑ Summary
1 package Plack::Middleware::Greylist;
2  
3 # ABSTRACT: throttle requests with different rates based on net blocks
4  
5 # RECOMMEND PREREQ: Cache::FastMmap 1.52
6 # RECOMMEND PREREQ: Ref::Util::XS
7  
8 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use HTTP::Status qw/ HTTP_FORBIDDEN HTTP_TOO_MANY_REQUESTS /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use List::Util 1.29 qw/ pairs /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Module::Load qw/ load /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Net::IP::LPM;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Plack::Util::Accessor qw/ default_rate rules cache file _match greylist retry_after cache_config callback /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use Ref::Util qw/ is_plain_arrayref is_coderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Time::Seconds qw/ ONE_MINUTE /;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶use experimental qw/ postderef signatures /;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 our $VERSION = 'v0.8.2';
25  
26  
27 5 ▶sub prepare_app($self) {
Subroutine prepare_app: called CC=27 SCAR=35.0
Pod documented
28  
29 5 ▶ $self->default_rate(-1) unless defined $self->default_rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->default_rate 0 5 5
30  
31 5 ▶ die "default_rate must be a positive integer" unless $self->default_rate =~ /^[1-9][0-9]*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->default_rate =~ /^[1-9][0-9]*$/u 0 5 5
32  
33 5 my $config = $self->cache_config;
34 5 ▶ $self->cache_config( $config //= {} ) unless defined $config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 5 5
Condition0%0/2
$config //= {}
l!l
00
MC/DC0%0/1
$config //= {}
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
35  
36 5 ▶ $config->{init_file} //= 0;
Condition50%1/2
$config->{"init_file"} //= 0
l!l
50
MC/DC0%0/1
$config->{"init_file"} //= 0
$config->{"init_file"}
Truth table50%1/2
A
A= $config->{"init_file"}
Aresult
11
00
37 5 ▶ $config->{unlink_on_exit} //= !$config->{init_file};
Condition50%1/2
$config->{"unlink_on_exit"} //= !$config->{"init_file"}
l!l
50
MC/DC0%0/1
$config->{"unlink_on_exit"} //= !$config->{"init_file"}
$config->{"unlink_on_exit"}
Truth table50%1/2
A
A= $config->{"unlink_on_exit"}
Aresult
11
00
38 5 ▶ $config->{serializer} //= '';
Condition50%1/2
$config->{"serializer"} //= ""
l!l
05
MC/DC0%0/1
$config->{"serializer"} //= ""
$config->{"serializer"}
Truth table50%1/2
A
A= $config->{"serializer"}
Aresult
11
00
39 5 ▶ my $expiry = $config->{expire_time} //= ONE_MINUTE;
Condition100%2/2
$config->{"expire_time"} //= 60
l!l
32
MC/DC100%1/1
$config->{"expire_time"} //= 60
$config->{"expire_time"}
Truth table100%2/2
A
A= $config->{"expire_time"}
Aresult
11
00
40  
41 5 ▶ $self->retry_after( $config->{expire_time} + 1 ) unless defined $self->retry_after;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->retry_after 3 2 5
42 5 ▶ die "retry_after must be a positive integer greater than $expiry seconds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->retry_after =~ /^[1-9][0-9]*$/u and $self->retry_after > $expiry 0 5 5
Condition33%1/3
$self->retry_after =~ /^[1-9][0-9]*$/u and $self->retry_after > $expiry
!ll&&!rl&&r
005
MC/DC0%0/2
$self->retry_after =~ /^[1-9][0-9]*$/u and $self->retry_after > $expiry
$self->retry_after =~ /^[1-9][0-9]*$/u $self->retry_after > $expiry
Truth table33%1/3
A and B
A= $self->retry_after =~ /^[1-9][0-9]*$/uB= $self->retry_after > $expiry
ABresult
0X0
100
111
43 unless $self->retry_after =~ /^[1-9][0-9]*$/ && $self->retry_after > $expiry;
44  
45 5 ▶ unless ( $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->cache) 5 0 5
46  
47 5 ▶ my $file = $self->file // $config->{share_file};
Condition66%2/3
$self->file // $config->{"share_file"}
l!l&&r!l&&!r
320
MC/DC0%0/2
$self->file // $config->{"share_file"}
$self->file $config->{"share_file"}
Truth table66%2/3
A // B
A= $self->fileB= $config->{"share_file"}
ABresult
1X1
011
000
48 5 ▶ die "No cache was set" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 5 5
49 5 $config->{share_file} = "$file";
50  
51 5 load Cache::FastMmap;
52 5 ▶ die "Cache::FastMmap version 1.52 or newer is required" if Cache::FastMmap->VERSION < 1.52;
Branch: 1 branch
BranchTrueFalseTotal
if "Cache::FastMmap"->VERSION < "1.52" 0 5 5
53  
54 5 my $cache = Cache::FastMmap->new(%$config);
55  
56 $self->cache(
57 89 ▶ sub($ip) {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 return $cache->get_and_set(
59 $ip,
60 89 ▶ sub( $, $count, $opts ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
61 89 ▶ $count //= 0;
Condition100%2/2
$count //= 0
l!l
7415
MC/DC100%1/1
$count //= 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
62 89 return ( $count + 1, { expire_on => $opts->{expire_on} } );
63 }
64 89 );
65 }
66 5 );
67  
68 }
69  
70 5 my $match = Net::IP::LPM->new;
71  
72 5 ▶ $self->_match( sub($ip) { $match->lookup($ip) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
73  
74 5 my @blocks;
75  
76 5 ▶ if ( my $greylist = $self->greylist ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $greylist = $self->greylist) 5 0 5
77 5 push @blocks, ( $greylist->%* );
78 }
79  
80 5 $self->rules( my $rules = {} );
81  
82 5 my %codes = ( whitelist => -1, allowed => -1, blacklist => 0, rejected => 0, norobots => 0 );
83 5 my %types = ( ip => '', netblock => 1 );
84  
85 5 for my $line ( pairs @blocks ) {
86  
87 15 my ( $block, $rule ) = $line->@*;
88 15 ▶ $rule = [ split /\s+/, $rule ] unless is_plain_arrayref($rule);
Branch: 1 branch
BranchTrueFalseTotal
unless is_plain_arrayref($rule) 8 7 15
89  
90 15 my ( $rate, $type ) = $rule->@*;
91  
92 15 ▶ $type //= "ip";
Condition100%2/2
$type //= "ip"
l!l
96
MC/DC100%1/1
$type //= "ip"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
93 15 ▶ my $mask = $types{$type} // $type;
Condition66%2/3
$types{$type} // $type
l!l&&r!l&&!r
1140
MC/DC0%0/2
$types{$type} // $type
$types{$type} $type
Truth table66%2/3
A // B
A= $types{$type}B= $type
ABresult
1X1
011
000
94 15 ▶ $mask = $block if $mask eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if $mask eq 1 3 12 15
95  
96 15 ▶ $rate //= "rejected";
Condition50%1/2
$rate //= "rejected"
l!l
150
MC/DC0%0/1
$rate //= "rejected"
$rate
Truth table50%1/2
A
A= $rate
Aresult
11
00
97 15 ▶ if ( exists $codes{$rate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $codes{$rate}) 4 11 15
98 4 ▶ $mask = $rate if $mask eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $mask eq "" 4 0 4
99 4 $rate = $codes{$rate};
100 }
101  
102 15 $rules->{$block} = [ $rate, $mask ];
103 15 $match->add( $block => $block );
104 }
105  
106 5 ▶ if ( my $fn = $self->callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $self->callback) { } 1 4 5
107 1 ▶ die "callback must be a code reference" unless is_coderef($fn);
Branch: 1 branch
BranchTrueFalseTotal
unless is_coderef($fn) 0 1 1
108 }
109 else {
110  
111 $self->callback(
112 15 ▶ sub($info) {
Subroutine __ANON__: called CC=2 SCAR=7.2
113 15 my $env = $info->{env};
114 15 my $msg = $info->{message};
115 15 ▶ if ( my $log = $env->{'psgix.logger'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $env->{'psgix.logger'}) { } 15 0 15
116 15 $log->( { message => $msg, level => 'warn' } );
117 }
118 else {
119 0 $env->{'psgi.errors'}->print($msg);
120 }
121 15 return 1;
122 }
123 4 );
124 }
125  
126 }
127  
128 101 ▶sub call( $self, $env ) {
Subroutine call: called CC=14 SCAR=26.4
Pod documented
129  
130 101 my $ip = $env->{REMOTE_ADDR};
131 101 my $name = $self->_match->($ip);
132 101 ▶ my $rule = $name ? $self->rules->{$name} : [ $self->default_rate ];
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 84 17 101
133  
134 101 my $rate = $rule->[0];
135  
136 101 ▶ if ( $rate == 0 && $rule->[1] && $rule->[1] eq "norobots" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate == 0 and $rule->[1] and $rule->[1] eq "norobots") 3 98 101
Condition100%6/6
$rate == 0 and $rule->[1] and $rule->[1] eq "norobots"
!ll&&!rl&&r
9623
$rate == 0 and $rule->[1]
!ll&&!rl&&r
9425
MC/DC100%3/3
$rate == 0 and $rule->[1] and $rule->[1] eq "norobots"
$rate == 0 $rule->[1] $rule->[1] eq "norobots"
Truth table100%4/4
A and B and C
A= $rate == 0B= $rule->[1]C= $rule->[1] eq "norobots"
ABCresult
0XX0
10X0
1100
1111
137 3 ▶ if ( $env->{PATH_INFO} eq "/robots.txt" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"PATH_INFO"} eq "/robots.txt") 2 1 3
138 2 $rate = ONE_MINUTE; # one request/second
139 }
140 }
141  
142 101 ▶ if ( $rate >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate >= 0) 89 12 101
143  
144 89 my $limit = $rate == 0;
145  
146 89 ▶ my ($hits) = $self->cache->( $rule->[1] || $ip );
Condition66%2/3
$rule->[1] || $ip
l!l&&r!l&&!r
43460
MC/DC0%0/2
$rule->[1] || $ip
$rule->[1] $ip
Truth table66%2/3
A || B
A= $rule->[1]B= $ip
ABresult
1X1
011
000
147 89 ▶ $limit = $hits > $rate ? $hits : 0;
Branch: 1 branch
BranchTrueFalseTotal
$hits > $rate ? : 17 72 89
148  
149 89 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 17 72 89
150  
151 17 ▶ my $block = $name || "default";
Condition100%2/2
$name || 'default'
l!l
152
MC/DC100%1/1
$name || 'default'
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
152  
153 17 ▶ if ( my $fn = $self->callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $self->callback) 17 0 17
154 17 ▶ $fn->(
Branch: 1 branch
BranchTrueFalseTotal
&$fn({"env", $env, "ip", $ip, "hits", $limit, "rate", $rate, "block", $block, "message", "Rate limiting $ip after $limit/$rate for $block"}) or return $self->app->($env) 16 1 17
155 {
156 env => $env,
157 ip => $ip,
158 hits => $limit,
159 rate => $rate,
160 block => $block,
161 message => "Rate limiting ${ip} after ${limit}/${rate} for ${block}",
162 }
163 ) or return $self->app->($env);
164 }
165  
166 16 ▶ if ( $rate == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rate == 0) { } 5 11 16
167  
168 5 return [ HTTP_FORBIDDEN, [], ["Forbbidden"] ];
169  
170 }
171 else {
172  
173 return [
174 11 HTTP_TOO_MANY_REQUESTS,
175 [
176 "Retry-After" => $self->retry_after,
177 ],
178 ["Too Many Requests"]
179 ];
180  
181 }
182 }
183  
184 }
185  
186 84 return $self->app->($env);
187 }
188  
189  
190 5 1;
191  
192 __END__
↑ Summary