summaryrefslogtreecommitdiffstats
path: root/t/013remote_ip.t
blob: 4bba7c95e2ceea9e746ae76c87cfc4745717ca49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use lib qw(. lib t);
use Test::More qw(no_plan);
use Bugzilla;
use Bugzilla::Util qw(remote_ip);

my $params = Bugzilla->params;
local Bugzilla->localconfig->{param_override}{inbound_proxies} = undef;

{
    local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2';
    local $ENV{REMOTE_ADDR} = '10.0.0.2';
    local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42';

    is(remote_ip(), '10.42.42.42', "from proxy 2");
}

{
    local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2';
    local $ENV{REMOTE_ADDR} = '10.0.0.1';
    local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42';

    is(remote_ip(), '10.42.42.42', "from proxy 1");
}

{
    local $params->{inbound_proxies} = '10.0.0.1,10.0.0.2';
    local $ENV{REMOTE_ADDR} = '10.0.0.3';
    local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42';

    is(remote_ip(), '10.0.0.3', "not a proxy");
}

{
    local $params->{inbound_proxies} = '*';
    local $ENV{REMOTE_ADDR} = '10.0.0.3';
    local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42,1.4.9.2';

    is(remote_ip(), '10.42.42.42', "always proxy");
}

{
    local $params->{inbound_proxies} = '';
    local $ENV{REMOTE_ADDR} = '10.9.8.7';
    local $ENV{HTTP_X_FORWARDED_FOR} = '10.42.42.42,1.4.9.2';

    is(remote_ip(), '10.9.8.7', "never proxy");
}


{
    local $params->{inbound_proxies} = '10.0.0.1,2600:cafe::cafe:ffff:bf42:4998';
    local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:4998';
    local $ENV{HTTP_X_FORWARDED_FOR} = '2600:cafe::cafe:ffff:bf42:BEEF';

    is(remote_ip(), '2600:cafe::cafe:ffff:bf42:BEEF', "from proxy ipv6");
}

{
    local $params->{inbound_proxies} = '10.0.0.1,2600:cafe::cafe:ffff:bf42:4998';
    local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:DEAD';
    local $ENV{HTTP_X_FORWARDED_FOR} = '2600:cafe::cafe:ffff:bf42:BEEF';

    is(remote_ip(), '2600:cafe::cafe:ffff:bf42:DEAD', "invalid proxy ipv6");
}


{
    local $params->{inbound_proxies} = '*';
    local $ENV{REMOTE_ADDR} = '2600:cafe::cafe:ffff:bf42:DEAD';
    local $ENV{HTTP_X_FORWARDED_FOR} = '';

    is(remote_ip(), '2600:cafe::cafe:ffff:bf42:DEAD', "always proxy ipv6");
}