blib/lib/Apache/ForwardedFor.pm

filter: Statementstmt 80.0% 8 / 10 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 85.7% 12 / 14 CC 1 SCAR 0.7
CC
1
Cov
80%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1  
2 package Apache::ForwardedFor;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = sprintf "%d.%03d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
8 # use vars qw ($TRACE);
9 # $TRACE = 1;
10 }
11  
12 0 ▶use Apache::Constants qw(DECLINED);
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14  
15 sub handler {
16  
17 my $r = shift;
18  
19 my $fwd_ips = $r->header_in('X-Forwarded-For');
20 # $TRACE && warn(__PACKAGE__." bypassed - no X-Forward-For header") and
21 return DECLINED unless $fwd_ips;
22  
23 # warn(__PACKAGE__." X-Forwarded-For header: $fwd_ips") if $TRACE;
24  
25 # Block based on Remove / Add AcceptForwarder values
26 my %deny =map { $_ => 1 } $r->dir_config->get('ForwardedForDeny');
27 if (exists $deny{$r->connection->remote_ip}) {
28 # warn(__PACKAGE__." handling for IP ".$r->connection->remote_ip." refused by RemoveAcceptForwarder directive") if $TRACE;
29 return DECLINED;
30 }
31  
32 my %accept=map { $_ => 1 } $r->dir_config->get('ForwardedForAccept');
33 if (!exists $accept{$r->connection->remote_ip} && keys %accept) {
34 # warn(__PACKAGE__." handling for IP ".$r->connection->remote_ip." refused by AddAcceptForwarder directive") if $TRACE;
35 return DECLINED;
36 }
37  
38 # Extract the desired IP address
39 if (my($ip) = $fwd_ips =~ /^([\d\.]+)/) {
40 # warn(__PACKAGE__." original remote_ip: ".$r->connection->remote_ip) if $TRACE;
41 Apache->connection->remote_ip($ip);
42 # warn(__PACKAGE__." new remote_ip: ".$r->connection->remote_ip) if $TRACE;
43 } else {
44 # do nothing if no ip is in forwarded-for header
45 # warn(__PACKAGE__." remote_ip: $ip unchanged") if $TRACE;
46 }
47  
48 # Return declined to continue handling at this phase...
49 DECLINED;
50  
51 }
52  
53  
54 =head1 NAME
55  
56 Apache::ForwardedFor - Re-set remote_ip to incoming client's ip when running mod_perl behind a reverse proxy server.
57 In other words, copy the first IP from B<X-Forwarded-For> header, which was set by your reverse proxy server,
58 to the B<remote_ip> connection property.
59  
60 =head1 SYNOPSIS
61  
62 in httpd.conf
63  
64 PerlModule Apache::ForwardedFor
65 PerlPostReadRequestHandler Apache::ForwardedFor
66  
67 PerlSetVar ForwardedForAccept 192.168.1.1
68 PerlAddVar ForwardedForAccept 192.168.1.2
69  
70 =head1 DESCRIPTION
71  
72 We often want to run Apache behind a reverse proxy so that we
73 can delegate light-weight (static content) requests to a small
74 httpd and proxy heavy-weight requests (dynamic mod_perl generated
75 content) to a big httpd. This is a well known technique to overcome
76 the memory contraints of running a busy mod_perl site.
77  
78 A small problem when doing this is that our "remote_ip" for the
79 backend (mod_perl) httpd is that of the front-end proxy'ing httpd.
80 This is not a good representation of the end client's real IP
81 address - making it difficult to implement IP-based access control
82 and tracking usage through your logs.
83  
84 Before:
85  
86 +--------+ +-------------+ +----------------+
87 | Client | <-> | httpd/proxy | <-> | httpd/mod_perl |
88 +--------+ +-------------+ +----------------+
89 My IP My IP My IP
90 2.3.4.5 2.9.1.2 192.168.1.2
91 remote_ip remote_ip
92 2.3.4.5 2.9.1.2
93  
94 After:
95  
96 +--------+ +-------------+ +----------------+
97 | Client | <-> | httpd/proxy | <-> | httpd/mod_perl |
98 +--------+ +-------------+ +----------------+
99 My IP My IP My IP
100 2.3.4.5 2.9.1.2 192.168.1.2
101 remote_ip remote_ip
102 2.3.4.5 2.3.4.5
103  
104 This program takes advantage of the existance of the X-Forwarded-For
105 or header which is automatically added by software such as mod_proxy and Squid.
106 Obviously you can imagine that if a savvy user sets their own X-Forwarded-For
107 header that they could potentially be considered coming from a trusted
108 IP.
109  
110 To ensure some measure of security: 1 - make sure you can trust the
111 httpd/proxy machine (ie/ its in your organization); 2 - set this module to
112 accept X-Forwarded-For headers only from this machine.
113  
114 From my understanding of the X-Forwarded-For header - each proxy server
115 will prepend the remote_ip to this header. That means that if the request passes
116 through several proxies we want to pick up only the last proxy's change - which
117 is the first IP found in this header.
118  
119 =head1 USAGE
120  
121 At this time you simply need to load the module and add it to the
122 PerlPostReadRequestHandler phase of your mod_perl-enabled httpd.
123  
124 =head1 APACHE CONFIGURATION
125  
126 The following can be set using either the B<PerlSetVar> or B<PerlAddVar> directives.
127  
128 i.e.
129 PerlSetVar ForwardedForDeny 127.0.0.1
130 PerlAddVar ForwardedForAccept 192.168.1.1
131 PerlAddVar ForwardedForAccept 192.168.1.2
132 PerlAddVar ForwardedForAccept 192.168.1.3
133  
134 =head2 ForwardedForAccept IPaddress
135  
136 By using either the B<PerlSetVar> or B<PerlAddVar> directive you can
137 list hosts for which we will only be allowing handling of Forwarded headers from.
138  
139 That means if you put one host in this list then all non-listed hosts
140 will be blocked.
141  
142 B<Netblocks> are not supported at this time - you must supply the
143 full IP address.
144  
145 =head2 ForwardedForDeny
146  
147 By using either the B<PerlSetVar> or B<PerlAddVar> directive you can
148 list hosts for which we will be blocking handling of Forwarded headers from.
149  
150 This means that all hosts except the ones listed here will be accepted
151 for processing.
152  
153 B<Netblocks> are not supported at this time - you must supply the
154 full IP address.
155  
156 B<N.B.> - if you specify both Accept and Deny items then you effectively
157 follow the logic of Deny first, then Accept afterwards. This is virtually
158 pointless but will be more useful when/if netblock support is added.
159  
160 =head1 BUGS
161  
162 Please report your bugs and suggestions for improvement to
163 info@infonium.com ... For faster service please in
164 clude "Apache::ForwardedFor" and "bug" in your subject line.
165  
166 I have not yet found written documentation on the usage of the X-Forwarded-For
167 header. My implementation assumes that the first IP in the incoming header
168 is for your (the most recent) proxy server.
169  
170 =head1 SUPPORT
171  
172 For technical support please email to
173 info@infonium.com ... for faster service please in
174 clude "Apache::ForwardedFrom" and "help" in your subject line.
175  
176 =head1 AUTHOR
177  
178 Jay J. Lawrence - jlawrenc@cpan.org
179 Infonium Inc., Canada
180 http://www.infonium.com/perl
181  
182 =head1 COPYRIGHT
183  
184 Copyright (c) 2002 Jay J. Lawrence. All rights reserved.
185 This program is free software; you can redistribute
186 it and/or modify it under the same terms as Perl itself.
187  
188 The full text of the license can be found in the
189 LICENSE file included with this module.
190  
191 =head1 ACKNOWLEDGEMENTS
192  
193 ahosey@systhug.com - mod_extract_forwarded
194 Vivek Khera - Apache::HeavyCGI::SquidRemoteAddr
195  
196 =head1 SEE ALSO
197  
198 perl, mod_perl, mod_extract_forwarded.
199  
200 =cut
201  
202 1;
203  
204  
↑ Summary