xref: /dokuwiki/_test/tests/inc/IpTest.php (revision a060f5a0bcf2bd42191c466160b31778110b4a6f)
1<?php
2
3namespace dokuwiki\test;
4
5use dokuwiki\Input\Input;
6use dokuwiki\Ip;
7
8class IpTest extends \DokuWikiTest {
9
10    /**
11     * The data provider for ipToNumber() tests.
12     *
13     * @return mixed[][] Returns an array of test cases.
14     */
15    public function ip_to_number_provider() : array
16    {
17        $tests = [
18            ['127.0.0.1', 4, 0x00000000, 0x7f000001],
19            ['::127.0.0.1', 6, 0x00000000, 0x7f000001],
20            ['::1', 6, 0x00000000, 0x00000001],
21            ['38AF:3033:AA39:CDE3:1A46:094C:44ED:5300', 6, 0x38AF3033AA39CDE3, 0x1A46094C44ED5300],
22            ['7FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE', 6, 9223372036854775807,0xFFFFFFFFFFFFFFFE],
23            ['0000:0000:0000:0000:0000:0000:0000:0000', 6, 0, 0],
24            ['193.53.125.7', 4, 0x00000000, 0xC1357D07],
25        ];
26
27        return $tests;
28    }
29
30    /**
31     * Test ipToNumber().
32     *
33     * @dataProvider ip_to_number_provider
34     *
35     * @param string $ip The IP address to convert.
36     * @param int    $version The IP version, either 4 or 6.
37     * @param int    $upper   The upper 64 bits of the IP.
38     * @param int    $lower   The lower 64 bits of the IP.
39     *
40     * @return void
41     */
42    public function test_ip_to_number(string $ip, int $version, int|float $upper, int|float $lower): void
43    {
44        $result = Ip::ipToNumber($ip);
45
46        // ugly hack. 32bit uses strings for large numbers
47        // also why we take type int (on x64) or float (on i386)
48        if(PHP_INT_SIZE == 4 && $version != 4) { // 32-bit arch
49          $upper = sprintf("%.0f",$upper);
50          $lower = sprintf("%.0f",$lower);
51        }
52
53        $this->assertSame($version, $result['version']);
54        $this->assertSame($upper, $result['upper']);
55        $this->assertSame($lower, $result['lower']);
56    }
57
58    /**
59     * The data provider for test_ip_in_range().
60     *
61     * @return mixed[][] Returns an array of test cases.
62     */
63    public function ip_in_range_provider(): array
64    {
65        $tests = [
66            ['192.168.11.2', '192.168.0.0/16', true],
67            ['192.168.11.2', '192.168.64.1/16', true],
68            ['192.168.11.2', '192.168.64.1/18', false],
69            ['192.168.11.2', '192.168.11.0/20', true],
70            ['127.0.0.1', '127.0.0.0/7', true],
71            ['127.0.0.1', '127.0.0.0/8', true],
72            ['127.0.0.1', '127.200.0.0/8', true],
73            ['127.0.0.1', '127.200.0.0/9', false],
74            ['127.0.0.1', '127.0.0.0/31', true],
75            ['127.0.0.1', '127.0.0.0/32', false],
76            ['127.0.0.1', '127.0.0.1/32', true],
77            ['1111:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true],
78            ['1110:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true],
79            ['1100:2222:3333:4444:5555:6666:7777:8888', '1110::/12', false],
80            ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3300::/40', true],
81            ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3200::/40', false],
82            ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/127', true],
83            ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/128', false],
84            ['1111:2222:3333:4444:5555:6666:7777:8889', '1111:2222:3333:4444:5555:6666:7777:8889/128', true],
85            ['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', true],
86            ['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abce', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', false],
87        ];
88
89        return $tests;
90    }
91
92    /**
93     * Test ipInRange().
94     *
95     * @dataProvider ip_in_range_provider
96     *
97     * @param string $ip The IP to test.
98     * @param string $range The IP range to test against.
99     * @param bool $expected The expected result from ipInRange().
100     *
101     * @return void
102     */
103    public function test_ip_in_range(string $ip, string $range, bool $expected): void
104    {
105        $result = Ip::ipInRange($ip, $range);
106
107        $this->assertSame($expected, $result);
108    }
109
110    /**
111     * Data provider for test_ip_matches().
112     *
113     * @return mixed[][] Returns an array of test cases.
114     */
115    public function ip_matches_provider(): array
116    {
117        // Tests for a CIDR range.
118        $rangeTests = $this->ip_in_range_provider();
119
120        // Tests for an exact IP match.
121        $exactTests = [
122            ['127.0.0.1', '127.0.0.1', true],
123            ['127.0.0.1', '127.0.0.0', false],
124            ['aaaa:bbbb:cccc:dddd:eeee::', 'aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', true],
125            ['aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', 'aaaa:bbbb:cccc:dddd:eeee::', true],
126            ['aaaa:bbbb:0000:0000:0000:0000:0000:0001', 'aaaa:bbbb::1', true],
127            ['aaaa:bbbb::0001', 'aaaa:bbbb::1', true],
128            ['aaaa:bbbb::0001', 'aaaa:bbbb::', false],
129            ['::ffff:127.0.0.1', '127.0.0.1', false],
130            ['::ffff:127.0.0.1', '::0:ffff:127.0.0.1', true],
131        ];
132
133
134        return array_merge($rangeTests, $exactTests);
135    }
136
137    /**
138     * Test ipMatches().
139     *
140     * @dataProvider ip_matches_provider
141     *
142     * @param string $ip        The IP to test.
143     * @param string $ipOrRange The IP or IP range to test against.
144     * @param bool   $expected  The expeced result from ipMatches().
145     *
146     * @return void
147     */
148    public function test_ip_matches(string $ip, string $ipOrRange, bool $expected): void
149    {
150        $result = Ip::ipMatches($ip, $ipOrRange);
151
152        $this->assertSame($expected, $result);
153    }
154
155    /**
156     * Data provider for proxyIsTrusted().
157     *
158     * @return mixed[][] Returns an array of test cases.
159     */
160    public function proxy_is_trusted_provider(): array
161    {
162        // The new default configuration value.
163        $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
164
165        // Adding some custom trusted proxies.
166        $custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']);
167
168        $tests = [
169            // Empty configuration.
170            ['', '127.0.0.1', false],
171
172            // Configuration with an array of  IPs/CIDRs.
173            [$default, '127.0.0.1', true],
174            [$default, '127.1.2.3', true],
175            [$default, '10.1.2.3', true],
176            [$default, '11.1.2.3', false],
177            [$default, '172.16.0.1', true],
178            [$default, '172.160.0.1', false],
179            [$default, '172.31.255.255', true],
180            [$default, '172.32.0.0', false],
181            [$default, '172.200.0.0', false],
182            [$default, '192.168.2.3', true],
183            [$default, '192.169.1.2', false],
184            [$default, '::1', true],
185            [$default, '0000:0000:0000:0000:0000:0000:0000:0001', true],
186
187            // With custom proxies set.
188            [$custom, '127.0.0.1', true],
189            [$custom, '1.2.3.4', true],
190            [$custom, '3.0.1.2', true],
191            [$custom, '1122::', true],
192            [$custom, '1122:0000:0000:0000:0000:0000:0000:0000', true],
193            [$custom, '1111:2223::', false],
194            [$custom, '1111:2222::', true],
195            [$custom, '1111:2222:3333::', true],
196            [$custom, '1111:2222:3333::1', true],
197        ];
198
199        return $tests;
200    }
201
202    /**
203     * Test proxyIsTrusted().
204     *
205     * @dataProvider proxy_is_trusted_provider
206     *
207     * @param string|string[] $config   The value for $conf[trustedproxies].
208     * @param string          $ip       The proxy IP to test.
209     * @param bool            $expected The expected result from proxyIsTrusted().
210     */
211    public function test_proxy_is_trusted($config, string $ip, bool $expected): void
212    {
213        global $conf;
214        $conf['trustedproxies'] = $config;
215
216        $result = Ip::proxyIsTrusted($ip);
217
218        $this->assertSame($expected, $result);
219    }
220
221    /**
222     * Data provider for test_forwarded_for().
223     *
224     * @return mixed[][] Returns an array of test cases.
225     */
226    public function forwarded_for_provider(): array
227    {
228        // The new default configuration value.
229        $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
230
231        // Adding some custom trusted proxies.
232        $custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']);
233
234        $tests = [
235            // Empty config value should always return empty array.
236            [[], '', '127.0.0.1', []],
237            [[], '127.0.0.1', '127.0.0.1', []],
238
239            // The new default configuration.
240            [$default, '', '127.0.0.1', []],
241            [$default, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']],
242            [$default, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']],
243            [$default, '1.2.3.4,172.16.0.1', '192.168.1.1', ['1.2.3.4', '172.16.0.1', '192.168.1.1']],
244            [$default, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']],
245            [$default, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']],
246
247            // Directly from an untrusted proxy.
248            [$default, '', '127.0.0.1', []],
249            [$default, '1.2.3.4', '11.22.33.44', []],
250            [$default, '::1', '11.22.33.44', []],
251            [$default, '::1', '::2', []],
252
253            // From a trusted proxy, but via an untrusted proxy.
254            [$default, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []],
255            [$default, '1.2.3.4,::2,172.16.0.1', '::1', []],
256
257            // A custom configuration.
258            [$custom, '', '127.0.0.1', []],
259            [$custom, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']],
260            [$custom, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']],
261            [$custom, '1.2.3.4,172.16.0.1', '192.168.1.1', ['1.2.3.4', '172.16.0.1', '192.168.1.1']],
262            [$custom, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']],
263            [$custom, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']],
264
265            // Directly from an untrusted proxy.
266            [$custom, '', '127.0.0.1', []],
267            [$custom, '1.2.3.4', '11.22.33.44', []],
268            [$custom, '::1', '11.22.33.44', []],
269            [$custom, '::1', '::2', []],
270
271            // From a trusted proxy, but via an untrusted proxy.
272            [$custom, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []],
273            [$custom, '1.2.3.4,::2,172.16.0.1', '::1', []],
274
275            // Via a custom proxy.
276            [$custom, '11.2.3.4,3.1.2.3,172.16.0.1', '192.168.1.1', ['11.2.3.4', '3.1.2.3', '172.16.0.1', '192.168.1.1']],
277            [$custom, '11.2.3.4,1122::,172.16.0.1', '3.0.0.1', ['11.2.3.4', '1122::', '172.16.0.1', '3.0.0.1']],
278            [$custom, '11.2.3.4,1122::,172.16.0.1', '1111:2222:3333::', ['11.2.3.4', '1122::', '172.16.0.1', '1111:2222:3333::']],
279        ];
280
281        return $tests;
282    }
283
284    /**
285     * Test forwardedFor().
286     *
287     * @dataProvider forwarded_for_provider
288     *
289     * @param string|string[] $config     The trustedproxies config value.
290     * @param string          $header     The X-Forwarded-For header value.
291     * @param string          $remoteAddr The TCP/IP peer address.
292     * @param array           $expected   The expected result from forwardedFor().
293     *
294     * @return void
295     */
296    public function test_forwarded_for($config, string $header, string $remoteAddr, array $expected): void
297    {
298        /* @var Input $INPUT */
299        global $INPUT, $conf;
300
301        $conf['trustedproxies'] = $config;
302        $INPUT->server->set('HTTP_X_FORWARDED_FOR', $header);
303        $INPUT->server->set('REMOTE_ADDR', $remoteAddr);
304
305        $result = Ip::forwardedFor();
306
307        $this->assertSame($expected, $result);
308    }
309
310    /**
311     * Data provider for test_is_ssl().
312     *
313     * @return mixed[][] Returns an array of test cases.
314     */
315    public function is_ssl_provider(): array
316    {
317        // The new default configuration value.
318        $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
319
320        $tests = [
321            // Running behind an SSL proxy, HTTP between server and proxy
322            // Proxy (REMOTE_ADDR) is matched by trustedproxies config
323            // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https
324            [$default, '127.0.0.1', '', 'https', true],
325
326            // Running behind an SSL proxy, HTTP between server and proxy
327            // Proxy (REMOTE_ADDR) is not matched by trustedproxies config
328            // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https
329            [[], '8.8.8.8', '', 'https', false],
330
331            // Running behind a plain HTTP proxy, HTTP between server and proxy
332            // HTTPS not set, HTTP_X_FORWARDED_PROTO set to http
333            [$default, '127.0.0.1', '', 'http', false],
334
335            // Running behind an SSL proxy, HTTP between server and proxy
336            // HTTPS set to off, HTTP_X_FORWARDED_PROTO set to https
337            [$default, '127.0.0.1', 'off', 'https', true],
338
339            // Not running behind a proxy, HTTPS server
340            // HTTPS set to on, HTTP_X_FORWARDED_PROTO not set
341            [[], '8.8.8.8', 'on', '', true],
342
343            // Not running behind a proxy, plain HTTP server
344            // HTTPS not set, HTTP_X_FORWARDED_PROTO not set
345            [[], '8.8.8.8', '', '', false],
346
347            // Not running behind a proxy, plain HTTP server
348            // HTTPS set to off, HTTP_X_FORWARDED_PROTO not set
349            [[], '8.8.8.8', 'off', '', false],
350
351            // Running behind an SSL proxy, SSL between proxy and HTTP server
352            // HTTPS set to on, HTTP_X_FORWARDED_PROTO set to https
353            [$default, '127.0.0.1', 'on', 'https', true],
354        ];
355
356        return $tests;
357    }
358
359    /**
360     * Test isSsl().
361     *
362     * @dataProvider is_ssl_provider
363     *
364     * @param string|string[] $config           The trustedproxies config value.
365     * @param string          $remoteAddr       The REMOTE_ADDR value.
366     * @param string          $https            The HTTPS value.
367     * @param string          $forwardedProto   The HTTP_X_FORWARDED_PROTO value.
368     * @param bool            $expected         The expected result from isSsl().
369     *
370     * @return void
371     */
372    public function test_is_ssl($config, string $remoteAddr, string $https, string $forwardedProto, bool $expected): void
373    {
374        /* @var Input $INPUT */
375        global $INPUT, $conf;
376
377        $conf['trustedproxies'] = $config;
378        $INPUT->server->set('REMOTE_ADDR', $remoteAddr);
379        $INPUT->server->set('HTTPS', $https);
380        $INPUT->server->set('HTTP_X_FORWARDED_PROTO', $forwardedProto);
381
382        $result = Ip::isSsl();
383
384        $this->assertSame($expected, $result);
385    }
386
387    /**
388     * Data provider for test_host_name().
389     *
390     * @return mixed[][] Returns an array of test cases.
391     */
392    public function host_name_provider(): array
393    {
394        // The new default configuration value.
395        $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'];
396
397        $tests = [
398            // X-Forwarded-Host with trusted proxy
399            [$default, '127.0.0.1', 'proxy.example.com', 'www.example.com', 'server.local', 'proxy.example.com'],
400
401            // X-Forwarded-Host with untrusted proxy (should fall back to HTTP_HOST)
402            [[], '8.8.8.8', 'proxy.example.com', 'www.example.com', 'server.local', 'www.example.com'],
403
404            // No X-Forwarded-Host, use HTTP_HOST
405            [$default, '127.0.0.1', '', 'www.example.com', 'server.local', 'www.example.com'],
406
407            // No X-Forwarded-Host or HTTP_HOST, use SERVER_NAME
408            [$default, '127.0.0.1', '', '', 'server.local', 'server.local'],
409
410            // No headers set, should fall back to system hostname
411            [$default, '127.0.0.1', '', '', '', php_uname('n')],
412        ];
413
414        return $tests;
415    }
416
417    /**
418     * Test hostName().
419     *
420     * @dataProvider host_name_provider
421     *
422     * @param string|string[] $config           The trustedproxies config value.
423     * @param string          $remoteAddr       The REMOTE_ADDR value.
424     * @param string          $forwardedHost    The HTTP_X_FORWARDED_HOST value.
425     * @param string          $httpHost         The HTTP_HOST value.
426     * @param string          $serverName       The SERVER_NAME value.
427     * @param string          $expected         The expected result from hostName().
428     *
429     * @return void
430     */
431    public function test_host_name($config, string $remoteAddr, string $forwardedHost, string $httpHost, string $serverName, string $expected): void
432    {
433        /* @var Input $INPUT */
434        global $INPUT, $conf;
435
436        $conf['trustedproxies'] = $config;
437        $INPUT->server->set('REMOTE_ADDR', $remoteAddr);
438        $INPUT->server->set('HTTP_X_FORWARDED_HOST', $forwardedHost);
439        $INPUT->server->set('HTTP_HOST', $httpHost);
440        $INPUT->server->set('SERVER_NAME', $serverName);
441
442        $result = Ip::hostName();
443
444        $this->assertSame($expected, $result);
445    }
446}
447