blib/lib/Plack/Middleware/XForwardedFor.pm

filter: Statementstmt 100% 40 / 40 Branchbran 85.7% 12 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 93.9% 62 / 66 CC 10 SCAR 15.9
CC
10
Cov
95%
call
8.1
prepare_app
3.0
BEGIN
1.0
CRAP 4.9
↑ Summary
1 package Plack::Middleware::XForwardedFor;
2 # ABSTRACT: Plack middleware to handle X-Forwarded-For headers
3 1 $Plack::Middleware::XForwardedFor::VERSION = '0.172050';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Plack::Util::Accessor qw(trust);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::IP qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶sub prepare_app {
Subroutine prepare_app: called CC=3 SCAR=11.0
Pod documented
11 10 my $self = shift;
12  
13 10 ▶ if (my $trust = $self->trust) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trust = $self->trust) 8 2 10
14 8 ▶ my @trust = map { Net::IP->new($_) } ref($trust) ? @$trust : ($trust);
Branch: 1 branch
BranchTrueFalseTotal
ref $trust ? : 4 4 8
15 8 $self->trust(\@trust);
16 }
17 }
18  
19 10 ▶sub call {
Subroutine call: called CC=8 SCAR=20.9
Pod documented
20 10 my ($self, $env) = @_;
21  
22 my @forward =
23 16 ▶ map { s/^::ffff://; $_ }
Condition50%1/2
$env->{'HTTP_X_FORWARDED_FOR'} || ''
l!l
100
MC/DC0%0/1
$env->{'HTTP_X_FORWARDED_FOR'} || ''
$env->{'HTTP_X_FORWARDED_FOR'}
Truth table50%1/2
A
A= $env->{'HTTP_X_FORWARDED_FOR'}
Aresult
11
00
24 10 (split(/,\s*/, ($env->{HTTP_X_FORWARDED_FOR} || '')));
25  
26 10 ▶ if (@forward) {
Branch: 1 branch
BranchTrueFalseTotal
if (@forward) 10 0 10
27 10 my $addr = $env->{REMOTE_ADDR};
28 10 $addr =~ s/^::ffff://;
29  
30 10 ▶ if (my $trust = $self->trust) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $trust = $self->trust) { } 8 2 10
31 ADDR: {
32 8 ▶ if (my $next = pop @forward) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = pop @forward) 13 5 18
33 13 foreach my $netmask (@$trust) {
34 17 ▶ my $ip = Net::IP->new($addr) or redo ADDR;
Branch: 1 branch
BranchTrueFalseTotal
my $ip = "Net::IP"->new($addr) or redo ADDR 17 0 17
35 17 ▶ if ($netmask->overlaps($ip)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($netmask->overlaps($ip)) 10 7 17
36 10 $addr = $next;
37 10 redo ADDR;
38 }
39 }
40 }
41 }
42 }
43 else { # trust everything, so use first in list
44 2 $addr = shift @forward;
45 }
46 10 $env->{REMOTE_ADDR} = $addr;
47 }
48  
49 10 $self->app->($env);
50 }
51  
52 1 1;
53  
54 =head1 NAME
55  
56 Plack::Middleware::XForwardedFor - Plack middleware to handle X-Forwarded-For headers
57  
58 =head1 VERSION
59  
60 version 0.172050
61  
62 =head1 SYNOPSIS
63  
64 builder {
65 enable "Plack::Middleware::XForwardedFor",
66 trust => [qw(127.0.0.1/8)];
67 };
68  
69 =head1 DESCRIPTION
70  
71 C<Plack::Middleware::XForwardedFor> will look for C<X-Forwarded-For>
72 header in the incoming request and change C<REMOTE_ADDR> to the
73 real client IP
74  
75 =head1 PARAMETERS
76  
77 =over
78  
79 =item trust
80  
81 If not specified then all addressed are trusted and C<REMOTE_ADDR> will be set to the
82 first IP in the C<X-Forwarded-For> header.
83  
84 If given, it should be a list of IPs or Netmasks that can be trusted. Starting with the IP
85 of the client in C<REMOTE_ADDR> then the IPs in the C<X-Forwarded-For> header from right to left.
86 The first untrusted IP found is set to be C<REMOTE_ADDR>
87  
88 =back
89  
90 =head1 SEE ALSO
91  
92 L<Plack::Middleware>, L<Net::IP>
93  
94 =head1 AUTHOR
95  
96 Graham Barr <gbarr@cpan.org>
97  
98 =head1 COPYRIGHT AND LICENSE
99  
100 This software is copyright (c) 2010 by Graham Barr.
101  
102 This is free software; you can redistribute it and/or modify it under
103 the same terms as the Perl 5 programming language system itself.
104  
105 =cut
↑ Summary