blib/lib/Plack/Middleware/RealIP.pm

filter: Statementstmt 100% 47 / 47 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 92.8% 78 / 84 CC 14 SCAR 20.3
CC
14
Cov
93%
call
12.1
prepare_app
3.0
BEGIN
1.0
CRAP 7.6
↑ Summary
1 package Plack::Middleware::RealIP;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = 0.04;
6 2 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Plack::Util::Accessor qw( header trusted_proxy );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=11.0
Pod documented
12 10 my $self = shift;
13 10 local $Net::Netmask::SHORTNET_DEFAULT = 1;
14  
15 10 ▶ if (my $trusted_proxy = $self->trusted_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trusted_proxy = $self->trusted_proxy) 10 0 10
16 10 ▶ my @trusted_proxy = map { Net::Netmask->new($_) } ref($trusted_proxy) ? @{ $trusted_proxy } : ($trusted_proxy);
Branch: 1 branch
BranchTrueFalseTotal
ref $trusted_proxy ? : 4 6 10
17 10 $self->trusted_proxy(\@trusted_proxy);
18 }
19 }
20  
21 10 ▶sub call {
Subroutine call: called CC=12 SCAR=25.0
Pod documented
22 10 my $self = shift;
23 10 my $env = shift;
24  
25 10 my $header;
26 10 ▶ if ($header = $self->header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header = $self->header) 10 0 10
27 10 ($header = uc $header) =~ tr/-/_/;
28 10 ▶ $header = "HTTP_$header" unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/ 10 0 10
29 }
30  
31 10 my (@remote, @trusted_proxy);
32 10 ▶ @remote = $env->{$header} =~ /([^,\s]+)/g if exists $env->{$header};
Branch: 1 branch
BranchTrueFalseTotal
if exists $env->{$header} 8 2 10
33 10 ▶ @trusted_proxy = @{ $self->trusted_proxy } if $self->trusted_proxy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->trusted_proxy 10 0 10
34  
35 10 ▶ if (@remote and @trusted_proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remote and @trusted_proxy) 8 2 10
Condition66%2/3
@remote and @trusted_proxy
!ll&&!rl&&r
208
MC/DC50%1/2
@remote and @trusted_proxy
@remote @trusted_proxy
Truth table66%2/3
A and B
A= @remoteB= @trusted_proxy
ABresult
0X0
100
111
36 8 my @unconfirmed = (@remote, $env->{REMOTE_ADDR});
37  
38 8 ▶ while (my $addr = pop @unconfirmed) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $addr = pop @unconfirmed) 15 1 16
39 15 my $has_matched = 0;
40 15 foreach my $netmask (@trusted_proxy) {
41 26 ▶ $has_matched++, last if $netmask->match($addr);
Branch: 1 branch
BranchTrueFalseTotal
if $netmask->match($addr) 8 18 26
42 }
43 15 ▶ $env->{REMOTE_ADDR} = $addr, last unless $has_matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_matched 7 8 15
44 }
45  
46 8 ▶ if (@unconfirmed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unconfirmed) { } 4 4 8
47 4 $env->{$header} = join(', ', @unconfirmed);
48 } else {
49 4 delete $env->{$header};
50 }
51 }
52  
53 10 return $self->app->($env);
54 }
55  
56 2 1;
57  
58 =head1 NAME
59  
60 Plack::Middleware::RealIP - Override client IP with header value provided by proxy/load balancer
61  
62 =head1 SYNOPSIS
63  
64 enable 'Plack::Middleware::RealIP',
65 header => 'X-Forwarded-For',
66 trusted_proxy => [qw(192.168.1.0/24 192.168.2.1)];
67  
68 =head1 DESCRIPTION
69  
70 Plack::Middleware::RealIP is loose port of the Apache module
71 mod_remoteip. It overrides C<REMOTE_ADDR> with the IP address advertised
72 in the request header configured with C<header>.
73  
74 When multiple, comma delimited IP addresses are listed in the header
75 value, they are processed from right to left. The first untrusted IP
76 address found, based on C<trusted_proxy>, stops the processing and is
77 set to be C<REMOTE_ADDR>. The header field is updated to this remaining
78 list of unconfirmed IP addresses, or if all IP addresses were trusted,
79 this header is removed from the request altogether.
80  
81 =head1 CONFIGURATION
82  
83 =head2 header
84  
85 Sets a request header to trust as the client IP, e.g. X-Client-IP
86  
87 =head2 trusted_proxy
88  
89 A list of IP addresses or subnet blocks which are trusted to provide IP header.
90  
91 =head1 AUTHOR
92  
93 Sherwin Daganato E<lt>sherwin@daganato.comE<gt>
94  
95 Most of the logic is based on L<Plack::Middleware::XForwardedFor> by Graham Barr
96  
97 =head1 CONTRIBUTORS
98  
99 jmaslak: Joelle Maslak <jmaslak@antelope.net>
100  
101 =head1 LICENSE
102  
103 This library is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself.
105  
106 =head1 SEE ALSO
107  
108 L<mod_remoteip|http://httpd.apache.org/docs/trunk/mod/mod_remoteip.html>
109  
110 L<Plack::Middleware::XForwardedFor>
111  
112 L<Plack::Middleware::ReverseProxy>
113  
114 L<Net::Netmask>
115  
116 =cut
↑ Summary