|
| 1 | +#!/usr/bin/perl |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +# |
| 6 | +# This Source Code Form is "Incompatible With Secondary Licenses", as |
| 7 | +# defined by the Mozilla Public License, v. 2.0. |
| 8 | + |
| 9 | +use strict; |
| 10 | +use lib qw(. lib t); |
| 11 | +use Test::More qw(no_plan); |
| 12 | +use Bugzilla; |
| 13 | +use Bugzilla::Util qw(remote_ip); |
| 14 | + |
| 15 | +my $params = Bugzilla->params; |
| 16 | + |
| 17 | +{ |
| 18 | + local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2'; |
| 19 | + local $ENV{REMOTE_ADDR} = '10.0.0.2'; |
| 20 | + local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42'; |
| 21 | + |
| 22 | + is(remote_ip(), '10.42.42.42', "from proxy 2"); |
| 23 | +} |
| 24 | + |
| 25 | +{ |
| 26 | + local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2'; |
| 27 | + local $ENV{REMOTE_ADDR} = '10.0.0.1'; |
| 28 | + local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42'; |
| 29 | + |
| 30 | + is(remote_ip(), '10.42.42.42', "from proxy 1"); |
| 31 | +} |
| 32 | + |
| 33 | +{ |
| 34 | + local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2'; |
| 35 | + local $ENV{REMOTE_ADDR} = '10.0.0.3'; |
| 36 | + local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42'; |
| 37 | + |
| 38 | + is(remote_ip(), '10.0.0.3', "not a proxy"); |
| 39 | +} |
| 40 | + |
| 41 | +{ |
| 42 | + local $params->{inbound_proxies} = '*'; |
| 43 | + local $ENV{REMOTE_ADDR} = '10.0.0.3'; |
| 44 | + local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42,1.4.9.2'; |
| 45 | + |
| 46 | + is(remote_ip(), '10.42.42.42', "always proxy"); |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + local $params->{inbound_proxies} = ''; |
| 51 | + local $ENV{REMOTE_ADDR} = '10.9.8.7'; |
| 52 | + local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42,1.4.9.2'; |
| 53 | + |
| 54 | + is(remote_ip(), '10.9.8.7', "never proxy"); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +{ |
| 59 | + local $params->{inbound_proxies} = '10.0.0.1,2600:cafe::cafe:ffff:bf42:4998'; |
| 60 | + local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:4998'; |
| 61 | + local $ENV{HTTP_X_FORWARDED_FOR} = '2600:cafe::cafe:ffff:bf42:BEEF'; |
| 62 | + |
| 63 | + is(remote_ip(), '2600:cafe::cafe:ffff:bf42:BEEF', "from proxy ipv6"); |
| 64 | +} |
| 65 | + |
| 66 | +{ |
| 67 | + local $params->{inbound_proxies} = '10.0.0.1,2600:cafe::cafe:ffff:bf42:4998'; |
| 68 | + local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:DEAD'; |
| 69 | + local $ENV{HTTP_X_FORWARDED_FOR} = '2600:cafe::cafe:ffff:bf42:BEEF'; |
| 70 | + |
| 71 | + is(remote_ip(), '2600:cafe::cafe:ffff:bf42:DEAD', "invalid proxy ipv6"); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +{ |
| 76 | + local $params->{inbound_proxies} = '*'; |
| 77 | + local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:DEAD'; |
| 78 | + local $ENV{HTTP_X_FORWARDED_FOR} = ''; |
| 79 | + |
| 80 | + is(remote_ip(), '2600:cafe::cafe:ffff:bf42:DEAD', "always proxy ipv6"); |
| 81 | +} |
0 commit comments