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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<?php
class common_clientIP_test extends DokuWikiTest {
/**
* @var mixed[] $configs Possible values for $conf['trustedproxy'].
*/
private $configs = [
'^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)',
['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
];
/**
* The data provider for clientIP() tests.
*
* @return mixed[][] Returns an array of test cases.
*/
public function client_ip_all_provider() : array {
// Malicious code in a header.
$bad = '<?php die("hacked"); ?>';
// Letters A, B, C, D, E will be substitued with an IPv4 or IPv6 address.
$tests = [
// A single IP with no other headers.
['A', false, '', '', false, 'A'],
['A', true, '', '', false, 'A'],
['A', false, '', '', true, 'A'],
['A', true, '', '', true, 'A'],
// A X-Real-IP header.
['A', false, 'B', '', false, 'A'],
['A', true, 'B', '', false, 'B,A'],
['A', false, 'B', '', true, 'A'],
['A', true, 'B', '', true, 'B'],
// An X-Forwarded-For header from an untrusted proxy.
['A', false, 'B', 'C', false, 'A'],
['A', true, 'B', 'C', false, 'B,A'],
['A', false, 'B', 'C', true, 'A'],
['A', true, 'B', 'C', true, 'B'],
// An X-Forwarded-For header from a trusted proxy.
['D', false, 'B', 'C', false, 'C,D'],
['D', true, 'B', 'C', false, 'B,C,D'],
['D', false, 'B', 'C', true, 'C'],
['D', true, 'B', 'C', true, 'B'],
// An X-Forwarded-For header with proxies from an untrusted proxy.
['A', false, 'B', 'C,E', false, 'A'],
['A', true, 'B', 'C,E', false, 'B,A'],
['A', false, 'B', 'C,E', true, 'A'],
['A', true, 'B', 'C,E', true, 'B'],
// An X-Forwarded-For header with untrusted proxies from a trusted proxy.
['D', false, 'B', 'C,E', false, 'D'],
['D', true, 'B', 'C,E', false, 'B,D'],
['D', false, 'B', 'C,E', true, 'D'],
['D', true, 'B', 'C,E', true, 'B'],
// An X-Forwarded-For header with an invalid proxy from a trusted proxy.
['D', false, 'B', 'C,invalid,E', false, 'D'],
['D', true, 'B', 'C,invalid,E', false, 'B,D'],
['D', false, 'B', 'C,invalid,E', true, 'D'],
['D', true, 'B', 'C,invalid,E', true, 'B'],
// Malicious X-Real-IP and X-Forwarded-For headers.
['A', false, $bad, $bad, false, 'A'],
['A', true, $bad, $bad, false, 'A'],
['A', false, $bad, $bad, true, 'A'],
['A', true, $bad, $bad, true, 'A'],
// Malicious remote address, X-Real-IP and X-Forwarded-For headers.
[$bad, false, $bad, $bad, false, '0.0.0.0'],
[$bad, true, $bad, $bad, false, '0.0.0.0'],
[$bad, false, $bad, $bad, true, '0.0.0.0'],
[$bad, true, $bad, $bad, true, '0.0.0.0'],
];
return $tests;
}
/**
* Test clientIP() with IPv6 addresses.
*
* @dataProvider client_ip_all_provider
*
* @param string $remoteAddr The TCP/IP remote IP address.
* @param bool $useRealIp True if using the X-Real-IP header is enabled in the config.
* @param string $realIp The X-Real-IP header.
* @param string $forwardedFor The X-Forwarded-For header.
* @param bool $single True to return the most likely client IP, false to return all candidates.
* @param string $expected The expected function result.
*
* @return void
*/
public function test_client_ip_v4(string $remoteAddr, bool $useRealIp, string $realIp, string $forwardedFor, bool $single, string $expected) : void {
global $conf;
$addresses = [
'A' => '123.123.123.123',
'B' => '22.22.22.22',
'C' => '33.33.33.33',
'D' => '192.168.11.1',
'E' => '44.44.44.44',
];
$_SERVER['REMOTE_ADDR'] = str_replace(array_keys($addresses), array_values($addresses), $remoteAddr);
$_SERVER['HTTP_X_REAL_IP'] = str_replace(array_keys($addresses), array_values($addresses), $realIp);
$_SERVER['HTTP_X_FORWARDED_FOR'] = str_replace(array_keys($addresses), array_values($addresses), $forwardedFor);
$conf['realip'] = $useRealIp;
foreach ($this->configs as $config) {
$conf['trustedproxy'] = $config;
$this->assertEquals(str_replace(array_keys($addresses), array_values($addresses), $expected), clientIP($single));
}
}
/**
* Test clientIP() with IPv6 addresses.
*
* @dataProvider client_ip_all_provider
*
* @param string $remoteAddr The TCP/IP remote IP address.
* @param bool $useRealIp True if using the X-Real-IP header is enabled in the config.
* @param string $realIp The X-Real-IP header.
* @param string $forwardedFor The X-Forwarded-For header.
* @param bool $single True to return the most likely client IP, false to return all candidates.
* @param string $expected The expected function result.
*
* @return void
*/
public function test_client_ip_v6(string $remoteAddr, bool $useRealIp, string $realIp, string $forwardedFor, bool $single, string $expected) : void {
global $conf;
$addresses = [
'A' => '1234:1234:1234:1234:1234:1234:1234:1234',
'B' => '22:aa:22:bb:22:cc:22:dd',
'C' => '33:aa:33:bb:33:cc:33:dd',
'D' => '::1',
'E' => '44:aa:44:bb:44:cc:44:dd',
];
$_SERVER['REMOTE_ADDR'] = str_replace(array_keys($addresses), array_values($addresses), $remoteAddr);
$_SERVER['HTTP_X_REAL_IP'] = str_replace(array_keys($addresses), array_values($addresses), $realIp);
$_SERVER['HTTP_X_FORWARDED_FOR'] = str_replace(array_keys($addresses), array_values($addresses), $forwardedFor);
$conf['realip'] = $useRealIp;
foreach ($this->configs as $config) {
$conf['trustedproxy'] = $config;
$this->assertEquals(str_replace(array_keys($addresses), array_values($addresses), $expected), clientIP($single));
}
}
}
|