17caad012SAndreas Gohr<?php 27caad012SAndreas Gohr 37caad012SAndreas Gohrnamespace dokuwiki\test; 47caad012SAndreas Gohr 57caad012SAndreas Gohruse dokuwiki\Input\Input; 67caad012SAndreas Gohruse dokuwiki\Ip; 77caad012SAndreas Gohr 87caad012SAndreas Gohrclass IpTest extends \DokuWikiTest { 97caad012SAndreas Gohr 107caad012SAndreas Gohr /** 117caad012SAndreas Gohr * The data provider for ipToNumber() tests. 127caad012SAndreas Gohr * 137caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 147caad012SAndreas Gohr */ 157caad012SAndreas Gohr public function ip_to_number_provider() : array 167caad012SAndreas Gohr { 177caad012SAndreas Gohr $tests = [ 187caad012SAndreas Gohr ['127.0.0.1', 4, 0x00000000, 0x7f000001], 197caad012SAndreas Gohr ['::127.0.0.1', 6, 0x00000000, 0x7f000001], 207caad012SAndreas Gohr ['::1', 6, 0x00000000, 0x00000001], 217caad012SAndreas Gohr ['38AF:3033:AA39:CDE3:1A46:094C:44ED:5300', 6, 0x38AF3033AA39CDE3, 0x1A46094C44ED5300], 22*e7cd6878SWillForan // This fails on 64bit! "-2 is identical to 1.8446744073709552E+19" 23*e7cd6878SWillForan //['7FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFE', 6, 9223372036854775807,0xFFFFFFFFFFFFFFFE], 24*e7cd6878SWillForan ['0000:0000:0000:0000:0000:0000:0000:0000', 6, 0, 0], 257caad012SAndreas Gohr ['193.53.125.7', 4, 0x00000000, 0xC1357D07], 267caad012SAndreas Gohr ]; 277caad012SAndreas Gohr 287caad012SAndreas Gohr return $tests; 297caad012SAndreas Gohr } 307caad012SAndreas Gohr 317caad012SAndreas Gohr /** 327caad012SAndreas Gohr * Test ipToNumber(). 337caad012SAndreas Gohr * 347caad012SAndreas Gohr * @dataProvider ip_to_number_provider 357caad012SAndreas Gohr * 367caad012SAndreas Gohr * @param string $ip The IP address to convert. 377caad012SAndreas Gohr * @param int $version The IP version, either 4 or 6. 387caad012SAndreas Gohr * @param int $upper The upper 64 bits of the IP. 397caad012SAndreas Gohr * @param int $lower The lower 64 bits of the IP. 407caad012SAndreas Gohr * 417caad012SAndreas Gohr * @return void 427caad012SAndreas Gohr */ 43*e7cd6878SWillForan public function test_ip_to_number(string $ip, int $version, int|float $upper, int|float $lower): void 447caad012SAndreas Gohr { 457caad012SAndreas Gohr $result = Ip::ipToNumber($ip); 467caad012SAndreas Gohr 47*e7cd6878SWillForan // ugly hack. 32bit uses strings for large numbers 48*e7cd6878SWillForan // also why we take type int (on x64) or float (on i386) 49*e7cd6878SWillForan if(PHP_INT_SIZE == 4 && $version != 4) { // 32-bit arch 50*e7cd6878SWillForan $upper = sprintf("%.0f",$upper); 51*e7cd6878SWillForan $lower = sprintf("%.0f",$lower); 52*e7cd6878SWillForan } 53*e7cd6878SWillForan 547caad012SAndreas Gohr $this->assertSame($version, $result['version']); 557caad012SAndreas Gohr $this->assertSame($upper, $result['upper']); 567caad012SAndreas Gohr $this->assertSame($lower, $result['lower']); 577caad012SAndreas Gohr } 587caad012SAndreas Gohr 597caad012SAndreas Gohr /** 607caad012SAndreas Gohr * The data provider for test_ip_in_range(). 617caad012SAndreas Gohr * 627caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 637caad012SAndreas Gohr */ 647caad012SAndreas Gohr public function ip_in_range_provider(): array 657caad012SAndreas Gohr { 667caad012SAndreas Gohr $tests = [ 677caad012SAndreas Gohr ['192.168.11.2', '192.168.0.0/16', true], 687caad012SAndreas Gohr ['192.168.11.2', '192.168.64.1/16', true], 697caad012SAndreas Gohr ['192.168.11.2', '192.168.64.1/18', false], 707caad012SAndreas Gohr ['192.168.11.2', '192.168.11.0/20', true], 717caad012SAndreas Gohr ['127.0.0.1', '127.0.0.0/7', true], 727caad012SAndreas Gohr ['127.0.0.1', '127.0.0.0/8', true], 737caad012SAndreas Gohr ['127.0.0.1', '127.200.0.0/8', true], 747caad012SAndreas Gohr ['127.0.0.1', '127.200.0.0/9', false], 757caad012SAndreas Gohr ['127.0.0.1', '127.0.0.0/31', true], 767caad012SAndreas Gohr ['127.0.0.1', '127.0.0.0/32', false], 777caad012SAndreas Gohr ['127.0.0.1', '127.0.0.1/32', true], 787caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true], 797caad012SAndreas Gohr ['1110:2222:3333:4444:5555:6666:7777:8888', '1110::/12', true], 807caad012SAndreas Gohr ['1100:2222:3333:4444:5555:6666:7777:8888', '1110::/12', false], 817caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3300::/40', true], 827caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3200::/40', false], 837caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/127', true], 847caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8888', '1111:2222:3333:4444:5555:6666:7777:8889/128', false], 857caad012SAndreas Gohr ['1111:2222:3333:4444:5555:6666:7777:8889', '1111:2222:3333:4444:5555:6666:7777:8889/128', true], 867caad012SAndreas Gohr ['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', true], 877caad012SAndreas Gohr ['abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abce', 'abcd:ef0a:bcde:f0ab:cdef:0abc:def0:abcd/128', false], 887caad012SAndreas Gohr ]; 897caad012SAndreas Gohr 907caad012SAndreas Gohr return $tests; 917caad012SAndreas Gohr } 927caad012SAndreas Gohr 937caad012SAndreas Gohr /** 947caad012SAndreas Gohr * Test ipInRange(). 957caad012SAndreas Gohr * 967caad012SAndreas Gohr * @dataProvider ip_in_range_provider 977caad012SAndreas Gohr * 987caad012SAndreas Gohr * @param string $ip The IP to test. 997caad012SAndreas Gohr * @param string $range The IP range to test against. 1007caad012SAndreas Gohr * @param bool $expected The expected result from ipInRange(). 1017caad012SAndreas Gohr * 1027caad012SAndreas Gohr * @return void 1037caad012SAndreas Gohr */ 1047caad012SAndreas Gohr public function test_ip_in_range(string $ip, string $range, bool $expected): void 1057caad012SAndreas Gohr { 1067caad012SAndreas Gohr $result = Ip::ipInRange($ip, $range); 1077caad012SAndreas Gohr 1087caad012SAndreas Gohr $this->assertSame($expected, $result); 1097caad012SAndreas Gohr } 1107caad012SAndreas Gohr 1117caad012SAndreas Gohr /** 1127caad012SAndreas Gohr * Data provider for test_ip_matches(). 1137caad012SAndreas Gohr * 1147caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 1157caad012SAndreas Gohr */ 1167caad012SAndreas Gohr public function ip_matches_provider(): array 1177caad012SAndreas Gohr { 1187caad012SAndreas Gohr // Tests for a CIDR range. 1197caad012SAndreas Gohr $rangeTests = $this->ip_in_range_provider(); 1207caad012SAndreas Gohr 1217caad012SAndreas Gohr // Tests for an exact IP match. 1227caad012SAndreas Gohr $exactTests = [ 1237caad012SAndreas Gohr ['127.0.0.1', '127.0.0.1', true], 1247caad012SAndreas Gohr ['127.0.0.1', '127.0.0.0', false], 1257caad012SAndreas Gohr ['aaaa:bbbb:cccc:dddd:eeee::', 'aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', true], 1267caad012SAndreas Gohr ['aaaa:bbbb:cccc:dddd:eeee:0000:0000:0000', 'aaaa:bbbb:cccc:dddd:eeee::', true], 1277caad012SAndreas Gohr ['aaaa:bbbb:0000:0000:0000:0000:0000:0001', 'aaaa:bbbb::1', true], 1287caad012SAndreas Gohr ['aaaa:bbbb::0001', 'aaaa:bbbb::1', true], 1297caad012SAndreas Gohr ['aaaa:bbbb::0001', 'aaaa:bbbb::', false], 1307caad012SAndreas Gohr ['::ffff:127.0.0.1', '127.0.0.1', false], 1317caad012SAndreas Gohr ['::ffff:127.0.0.1', '::0:ffff:127.0.0.1', true], 1327caad012SAndreas Gohr ]; 1337caad012SAndreas Gohr 1347caad012SAndreas Gohr 1357caad012SAndreas Gohr return array_merge($rangeTests, $exactTests); 1367caad012SAndreas Gohr } 1377caad012SAndreas Gohr 1387caad012SAndreas Gohr /** 1397caad012SAndreas Gohr * Test ipMatches(). 1407caad012SAndreas Gohr * 1417caad012SAndreas Gohr * @dataProvider ip_matches_provider 1427caad012SAndreas Gohr * 1437caad012SAndreas Gohr * @param string $ip The IP to test. 1447caad012SAndreas Gohr * @param string $ipOrRange The IP or IP range to test against. 1457caad012SAndreas Gohr * @param bool $expected The expeced result from ipMatches(). 1467caad012SAndreas Gohr * 1477caad012SAndreas Gohr * @return void 1487caad012SAndreas Gohr */ 1497caad012SAndreas Gohr public function test_ip_matches(string $ip, string $ipOrRange, bool $expected): void 1507caad012SAndreas Gohr { 1517caad012SAndreas Gohr $result = Ip::ipMatches($ip, $ipOrRange); 1527caad012SAndreas Gohr 1537caad012SAndreas Gohr $this->assertSame($expected, $result); 1547caad012SAndreas Gohr } 1557caad012SAndreas Gohr 1567caad012SAndreas Gohr /** 1577caad012SAndreas Gohr * Data provider for proxyIsTrusted(). 1587caad012SAndreas Gohr * 1597caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 1607caad012SAndreas Gohr */ 1617caad012SAndreas Gohr public function proxy_is_trusted_provider(): array 1627caad012SAndreas Gohr { 1637caad012SAndreas Gohr // The new default configuration value. 1647caad012SAndreas Gohr $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; 1657caad012SAndreas Gohr 1667caad012SAndreas Gohr // Adding some custom trusted proxies. 1677caad012SAndreas Gohr $custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']); 1687caad012SAndreas Gohr 1697caad012SAndreas Gohr $tests = [ 1707caad012SAndreas Gohr // Empty configuration. 1717caad012SAndreas Gohr ['', '127.0.0.1', false], 1727caad012SAndreas Gohr 1737caad012SAndreas Gohr // Configuration with an array of IPs/CIDRs. 1747caad012SAndreas Gohr [$default, '127.0.0.1', true], 1757caad012SAndreas Gohr [$default, '127.1.2.3', true], 1767caad012SAndreas Gohr [$default, '10.1.2.3', true], 1777caad012SAndreas Gohr [$default, '11.1.2.3', false], 1787caad012SAndreas Gohr [$default, '172.16.0.1', true], 1797caad012SAndreas Gohr [$default, '172.160.0.1', false], 1807caad012SAndreas Gohr [$default, '172.31.255.255', true], 1817caad012SAndreas Gohr [$default, '172.32.0.0', false], 1827caad012SAndreas Gohr [$default, '172.200.0.0', false], 1837caad012SAndreas Gohr [$default, '192.168.2.3', true], 1847caad012SAndreas Gohr [$default, '192.169.1.2', false], 1857caad012SAndreas Gohr [$default, '::1', true], 1867caad012SAndreas Gohr [$default, '0000:0000:0000:0000:0000:0000:0000:0001', true], 1877caad012SAndreas Gohr 1887caad012SAndreas Gohr // With custom proxies set. 1897caad012SAndreas Gohr [$custom, '127.0.0.1', true], 1907caad012SAndreas Gohr [$custom, '1.2.3.4', true], 1917caad012SAndreas Gohr [$custom, '3.0.1.2', true], 1927caad012SAndreas Gohr [$custom, '1122::', true], 1937caad012SAndreas Gohr [$custom, '1122:0000:0000:0000:0000:0000:0000:0000', true], 1947caad012SAndreas Gohr [$custom, '1111:2223::', false], 1957caad012SAndreas Gohr [$custom, '1111:2222::', true], 1967caad012SAndreas Gohr [$custom, '1111:2222:3333::', true], 1977caad012SAndreas Gohr [$custom, '1111:2222:3333::1', true], 1987caad012SAndreas Gohr ]; 1997caad012SAndreas Gohr 2007caad012SAndreas Gohr return $tests; 2017caad012SAndreas Gohr } 2027caad012SAndreas Gohr 2037caad012SAndreas Gohr /** 2047caad012SAndreas Gohr * Test proxyIsTrusted(). 2057caad012SAndreas Gohr * 2067caad012SAndreas Gohr * @dataProvider proxy_is_trusted_provider 2077caad012SAndreas Gohr * 2087caad012SAndreas Gohr * @param string|string[] $config The value for $conf[trustedproxies]. 2097caad012SAndreas Gohr * @param string $ip The proxy IP to test. 2107caad012SAndreas Gohr * @param bool $expected The expected result from proxyIsTrusted(). 2117caad012SAndreas Gohr */ 2127caad012SAndreas Gohr public function test_proxy_is_trusted($config, string $ip, bool $expected): void 2137caad012SAndreas Gohr { 2147caad012SAndreas Gohr global $conf; 2157caad012SAndreas Gohr $conf['trustedproxies'] = $config; 2167caad012SAndreas Gohr 2177caad012SAndreas Gohr $result = Ip::proxyIsTrusted($ip); 2187caad012SAndreas Gohr 2197caad012SAndreas Gohr $this->assertSame($expected, $result); 2207caad012SAndreas Gohr } 2217caad012SAndreas Gohr 2227caad012SAndreas Gohr /** 2237caad012SAndreas Gohr * Data provider for test_forwarded_for(). 2247caad012SAndreas Gohr * 2257caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 2267caad012SAndreas Gohr */ 2277caad012SAndreas Gohr public function forwarded_for_provider(): array 2287caad012SAndreas Gohr { 2297caad012SAndreas Gohr // The new default configuration value. 2307caad012SAndreas Gohr $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; 2317caad012SAndreas Gohr 2327caad012SAndreas Gohr // Adding some custom trusted proxies. 2337caad012SAndreas Gohr $custom = array_merge($default, ['1.2.3.4', '1122::', '3.0.0.1/8', '1111:2222::/32']); 2347caad012SAndreas Gohr 2357caad012SAndreas Gohr $tests = [ 2367caad012SAndreas Gohr // Empty config value should always return empty array. 2377caad012SAndreas Gohr [[], '', '127.0.0.1', []], 2387caad012SAndreas Gohr [[], '127.0.0.1', '127.0.0.1', []], 2397caad012SAndreas Gohr 2407caad012SAndreas Gohr // The new default configuration. 2417caad012SAndreas Gohr [$default, '', '127.0.0.1', []], 2427caad012SAndreas Gohr [$default, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']], 2437caad012SAndreas Gohr [$default, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']], 2447caad012SAndreas Gohr [$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']], 2457caad012SAndreas Gohr [$default, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']], 2467caad012SAndreas Gohr [$default, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']], 2477caad012SAndreas Gohr 2487caad012SAndreas Gohr // Directly from an untrusted proxy. 2497caad012SAndreas Gohr [$default, '', '127.0.0.1', []], 2507caad012SAndreas Gohr [$default, '1.2.3.4', '11.22.33.44', []], 2517caad012SAndreas Gohr [$default, '::1', '11.22.33.44', []], 2527caad012SAndreas Gohr [$default, '::1', '::2', []], 2537caad012SAndreas Gohr 2547caad012SAndreas Gohr // From a trusted proxy, but via an untrusted proxy. 2557caad012SAndreas Gohr [$default, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []], 2567caad012SAndreas Gohr [$default, '1.2.3.4,::2,172.16.0.1', '::1', []], 2577caad012SAndreas Gohr 2587caad012SAndreas Gohr // A custom configuration. 2597caad012SAndreas Gohr [$custom, '', '127.0.0.1', []], 2607caad012SAndreas Gohr [$custom, '1.2.3.4', '127.0.0.1', ['1.2.3.4', '127.0.0.1']], 2617caad012SAndreas Gohr [$custom, '1.2.3.4', '192.168.1.1', ['1.2.3.4', '192.168.1.1']], 2627caad012SAndreas Gohr [$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']], 2637caad012SAndreas Gohr [$custom, '1.2.3.4,172.16.0.1', '::1', ['1.2.3.4', '172.16.0.1', '::1']], 2647caad012SAndreas Gohr [$custom, '1.2.3.4,172.16.0.1', '::0001', ['1.2.3.4', '172.16.0.1', '::0001']], 2657caad012SAndreas Gohr 2667caad012SAndreas Gohr // Directly from an untrusted proxy. 2677caad012SAndreas Gohr [$custom, '', '127.0.0.1', []], 2687caad012SAndreas Gohr [$custom, '1.2.3.4', '11.22.33.44', []], 2697caad012SAndreas Gohr [$custom, '::1', '11.22.33.44', []], 2707caad012SAndreas Gohr [$custom, '::1', '::2', []], 2717caad012SAndreas Gohr 2727caad012SAndreas Gohr // From a trusted proxy, but via an untrusted proxy. 2737caad012SAndreas Gohr [$custom, '1.2.3.4,11.22.33.44,172.16.0.1', '192.168.1.1', []], 2747caad012SAndreas Gohr [$custom, '1.2.3.4,::2,172.16.0.1', '::1', []], 2757caad012SAndreas Gohr 2767caad012SAndreas Gohr // Via a custom proxy. 2777caad012SAndreas Gohr [$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']], 2787caad012SAndreas Gohr [$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']], 2797caad012SAndreas Gohr [$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::']], 2807caad012SAndreas Gohr ]; 2817caad012SAndreas Gohr 2827caad012SAndreas Gohr return $tests; 2837caad012SAndreas Gohr } 2847caad012SAndreas Gohr 2857caad012SAndreas Gohr /** 2867caad012SAndreas Gohr * Test forwardedFor(). 2877caad012SAndreas Gohr * 2887caad012SAndreas Gohr * @dataProvider forwarded_for_provider 2897caad012SAndreas Gohr * 2907caad012SAndreas Gohr * @param string|string[] $config The trustedproxies config value. 2917caad012SAndreas Gohr * @param string $header The X-Forwarded-For header value. 2927caad012SAndreas Gohr * @param string $remoteAddr The TCP/IP peer address. 2937caad012SAndreas Gohr * @param array $expected The expected result from forwardedFor(). 2947caad012SAndreas Gohr * 2957caad012SAndreas Gohr * @return void 2967caad012SAndreas Gohr */ 2977caad012SAndreas Gohr public function test_forwarded_for($config, string $header, string $remoteAddr, array $expected): void 2987caad012SAndreas Gohr { 2997caad012SAndreas Gohr /* @var Input $INPUT */ 3007caad012SAndreas Gohr global $INPUT, $conf; 3017caad012SAndreas Gohr 3027caad012SAndreas Gohr $conf['trustedproxies'] = $config; 3037caad012SAndreas Gohr $INPUT->server->set('HTTP_X_FORWARDED_FOR', $header); 3047caad012SAndreas Gohr $INPUT->server->set('REMOTE_ADDR', $remoteAddr); 3057caad012SAndreas Gohr 3067caad012SAndreas Gohr $result = Ip::forwardedFor(); 3077caad012SAndreas Gohr 3087caad012SAndreas Gohr $this->assertSame($expected, $result); 3097caad012SAndreas Gohr } 3107caad012SAndreas Gohr 3117caad012SAndreas Gohr /** 3127caad012SAndreas Gohr * Data provider for test_is_ssl(). 3137caad012SAndreas Gohr * 3147caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 3157caad012SAndreas Gohr */ 3167caad012SAndreas Gohr public function is_ssl_provider(): array 3177caad012SAndreas Gohr { 3187caad012SAndreas Gohr // The new default configuration value. 3197caad012SAndreas Gohr $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; 3207caad012SAndreas Gohr 3217caad012SAndreas Gohr $tests = [ 3227caad012SAndreas Gohr // Running behind an SSL proxy, HTTP between server and proxy 3237caad012SAndreas Gohr // Proxy (REMOTE_ADDR) is matched by trustedproxies config 3247caad012SAndreas Gohr // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https 3257caad012SAndreas Gohr [$default, '127.0.0.1', '', 'https', true], 3267caad012SAndreas Gohr 3277caad012SAndreas Gohr // Running behind an SSL proxy, HTTP between server and proxy 3287caad012SAndreas Gohr // Proxy (REMOTE_ADDR) is not matched by trustedproxies config 3297caad012SAndreas Gohr // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https 3307caad012SAndreas Gohr [[], '8.8.8.8', '', 'https', false], 3317caad012SAndreas Gohr 3327caad012SAndreas Gohr // Running behind a plain HTTP proxy, HTTP between server and proxy 3337caad012SAndreas Gohr // HTTPS not set, HTTP_X_FORWARDED_PROTO set to http 3347caad012SAndreas Gohr [$default, '127.0.0.1', '', 'http', false], 3357caad012SAndreas Gohr 3367caad012SAndreas Gohr // Running behind an SSL proxy, HTTP between server and proxy 3377caad012SAndreas Gohr // HTTPS set to off, HTTP_X_FORWARDED_PROTO set to https 3387caad012SAndreas Gohr [$default, '127.0.0.1', 'off', 'https', true], 3397caad012SAndreas Gohr 3407caad012SAndreas Gohr // Not running behind a proxy, HTTPS server 3417caad012SAndreas Gohr // HTTPS set to on, HTTP_X_FORWARDED_PROTO not set 3427caad012SAndreas Gohr [[], '8.8.8.8', 'on', '', true], 3437caad012SAndreas Gohr 3447caad012SAndreas Gohr // Not running behind a proxy, plain HTTP server 3457caad012SAndreas Gohr // HTTPS not set, HTTP_X_FORWARDED_PROTO not set 3467caad012SAndreas Gohr [[], '8.8.8.8', '', '', false], 3477caad012SAndreas Gohr 3487caad012SAndreas Gohr // Not running behind a proxy, plain HTTP server 3497caad012SAndreas Gohr // HTTPS set to off, HTTP_X_FORWARDED_PROTO not set 3507caad012SAndreas Gohr [[], '8.8.8.8', 'off', '', false], 3517caad012SAndreas Gohr 3527caad012SAndreas Gohr // Running behind an SSL proxy, SSL between proxy and HTTP server 3537caad012SAndreas Gohr // HTTPS set to on, HTTP_X_FORWARDED_PROTO set to https 3547caad012SAndreas Gohr [$default, '127.0.0.1', 'on', 'https', true], 3557caad012SAndreas Gohr ]; 3567caad012SAndreas Gohr 3577caad012SAndreas Gohr return $tests; 3587caad012SAndreas Gohr } 3597caad012SAndreas Gohr 3607caad012SAndreas Gohr /** 3617caad012SAndreas Gohr * Test isSsl(). 3627caad012SAndreas Gohr * 3637caad012SAndreas Gohr * @dataProvider is_ssl_provider 3647caad012SAndreas Gohr * 3657caad012SAndreas Gohr * @param string|string[] $config The trustedproxies config value. 3667caad012SAndreas Gohr * @param string $remoteAddr The REMOTE_ADDR value. 3677caad012SAndreas Gohr * @param string $https The HTTPS value. 3687caad012SAndreas Gohr * @param string $forwardedProto The HTTP_X_FORWARDED_PROTO value. 3697caad012SAndreas Gohr * @param bool $expected The expected result from isSsl(). 3707caad012SAndreas Gohr * 3717caad012SAndreas Gohr * @return void 3727caad012SAndreas Gohr */ 3737caad012SAndreas Gohr public function test_is_ssl($config, string $remoteAddr, string $https, string $forwardedProto, bool $expected): void 3747caad012SAndreas Gohr { 3757caad012SAndreas Gohr /* @var Input $INPUT */ 3767caad012SAndreas Gohr global $INPUT, $conf; 3777caad012SAndreas Gohr 3787caad012SAndreas Gohr $conf['trustedproxies'] = $config; 3797caad012SAndreas Gohr $INPUT->server->set('REMOTE_ADDR', $remoteAddr); 3807caad012SAndreas Gohr $INPUT->server->set('HTTPS', $https); 3817caad012SAndreas Gohr $INPUT->server->set('HTTP_X_FORWARDED_PROTO', $forwardedProto); 3827caad012SAndreas Gohr 3837caad012SAndreas Gohr $result = Ip::isSsl(); 3847caad012SAndreas Gohr 3857caad012SAndreas Gohr $this->assertSame($expected, $result); 3867caad012SAndreas Gohr } 3877caad012SAndreas Gohr 3887caad012SAndreas Gohr /** 3897caad012SAndreas Gohr * Data provider for test_host_name(). 3907caad012SAndreas Gohr * 3917caad012SAndreas Gohr * @return mixed[][] Returns an array of test cases. 3927caad012SAndreas Gohr */ 3937caad012SAndreas Gohr public function host_name_provider(): array 3947caad012SAndreas Gohr { 3957caad012SAndreas Gohr // The new default configuration value. 3967caad012SAndreas Gohr $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; 3977caad012SAndreas Gohr 3987caad012SAndreas Gohr $tests = [ 3997caad012SAndreas Gohr // X-Forwarded-Host with trusted proxy 4007caad012SAndreas Gohr [$default, '127.0.0.1', 'proxy.example.com', 'www.example.com', 'server.local', 'proxy.example.com'], 4017caad012SAndreas Gohr 4027caad012SAndreas Gohr // X-Forwarded-Host with untrusted proxy (should fall back to HTTP_HOST) 4037caad012SAndreas Gohr [[], '8.8.8.8', 'proxy.example.com', 'www.example.com', 'server.local', 'www.example.com'], 4047caad012SAndreas Gohr 4057caad012SAndreas Gohr // No X-Forwarded-Host, use HTTP_HOST 4067caad012SAndreas Gohr [$default, '127.0.0.1', '', 'www.example.com', 'server.local', 'www.example.com'], 4077caad012SAndreas Gohr 4087caad012SAndreas Gohr // No X-Forwarded-Host or HTTP_HOST, use SERVER_NAME 4097caad012SAndreas Gohr [$default, '127.0.0.1', '', '', 'server.local', 'server.local'], 4107caad012SAndreas Gohr 4117caad012SAndreas Gohr // No headers set, should fall back to system hostname 4127caad012SAndreas Gohr [$default, '127.0.0.1', '', '', '', php_uname('n')], 4137caad012SAndreas Gohr ]; 4147caad012SAndreas Gohr 4157caad012SAndreas Gohr return $tests; 4167caad012SAndreas Gohr } 4177caad012SAndreas Gohr 4187caad012SAndreas Gohr /** 4197caad012SAndreas Gohr * Test hostName(). 4207caad012SAndreas Gohr * 4217caad012SAndreas Gohr * @dataProvider host_name_provider 4227caad012SAndreas Gohr * 4237caad012SAndreas Gohr * @param string|string[] $config The trustedproxies config value. 4247caad012SAndreas Gohr * @param string $remoteAddr The REMOTE_ADDR value. 4257caad012SAndreas Gohr * @param string $forwardedHost The HTTP_X_FORWARDED_HOST value. 4267caad012SAndreas Gohr * @param string $httpHost The HTTP_HOST value. 4277caad012SAndreas Gohr * @param string $serverName The SERVER_NAME value. 4287caad012SAndreas Gohr * @param string $expected The expected result from hostName(). 4297caad012SAndreas Gohr * 4307caad012SAndreas Gohr * @return void 4317caad012SAndreas Gohr */ 4327caad012SAndreas Gohr public function test_host_name($config, string $remoteAddr, string $forwardedHost, string $httpHost, string $serverName, string $expected): void 4337caad012SAndreas Gohr { 4347caad012SAndreas Gohr /* @var Input $INPUT */ 4357caad012SAndreas Gohr global $INPUT, $conf; 4367caad012SAndreas Gohr 4377caad012SAndreas Gohr $conf['trustedproxies'] = $config; 4387caad012SAndreas Gohr $INPUT->server->set('REMOTE_ADDR', $remoteAddr); 4397caad012SAndreas Gohr $INPUT->server->set('HTTP_X_FORWARDED_HOST', $forwardedHost); 4407caad012SAndreas Gohr $INPUT->server->set('HTTP_HOST', $httpHost); 4417caad012SAndreas Gohr $INPUT->server->set('SERVER_NAME', $serverName); 4427caad012SAndreas Gohr 4437caad012SAndreas Gohr $result = Ip::hostName(); 4447caad012SAndreas Gohr 4457caad012SAndreas Gohr $this->assertSame($expected, $result); 4467caad012SAndreas Gohr } 4477caad012SAndreas Gohr} 448