| 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
| |||||||||||||||||||||||||||||
| 16 | 10 | ▶ | my @trusted_proxy = map { Net::Netmask->new($_) } ref($trusted_proxy) ? @{ $trusted_proxy } : ($trusted_proxy); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 27 | 10 | ($header = uc $header) =~ tr/-/_/; | |||||||||||||||||||||||||||
| 28 | 10 | ▶ | $header = "HTTP_$header" unless $header =~ /^(?:HTTP|CONTENT|COOKIE)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | my (@remote, @trusted_proxy); | |||||||||||||||||||||||||||
| 32 | 10 | ▶ | @remote = $env->{$header} =~ /([^,\s]+)/g if exists $env->{$header}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 10 | ▶ | @trusted_proxy = @{ $self->trusted_proxy } if $self->trusted_proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 10 | ▶ | if (@remote and @trusted_proxy) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@remote and @trusted_proxy
MC/DC50%1/2
@remote and @trusted_proxy
@remote
@trusted_proxy
Truth table66%2/3
A and B
A= @remoteB= @trusted_proxy
| |||||||||||||||||||||||||||||
| 36 | 8 | my @unconfirmed = (@remote, $env->{REMOTE_ADDR}); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | while (my $addr = pop @unconfirmed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 15 | ▶ | $env->{REMOTE_ADDR} = $addr, last unless $has_matched; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 8 | ▶ | if (@unconfirmed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||