|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +namespace dokuwiki\test; |
| 4 | + |
| 5 | +use dokuwiki\Input\Input; |
3 | 6 | use dokuwiki\Ip; |
4 | 7 |
|
5 | | -class ip_test extends DokuWikiTest { |
| 8 | +class IpTest extends \DokuWikiTest { |
6 | 9 |
|
7 | 10 | /** |
8 | 11 | * The data provider for ipToNumber() tests. |
@@ -294,4 +297,141 @@ public function test_forwarded_for($config, string $header, string $remoteAddr, |
294 | 297 |
|
295 | 298 | $this->assertSame($expected, $result); |
296 | 299 | } |
| 300 | + |
| 301 | + /** |
| 302 | + * Data provider for test_is_ssl(). |
| 303 | + * |
| 304 | + * @return mixed[][] Returns an array of test cases. |
| 305 | + */ |
| 306 | + public function is_ssl_provider(): array |
| 307 | + { |
| 308 | + // The new default configuration value. |
| 309 | + $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; |
| 310 | + |
| 311 | + $tests = [ |
| 312 | + // Running behind an SSL proxy, HTTP between server and proxy |
| 313 | + // Proxy (REMOTE_ADDR) is matched by trustedproxies config |
| 314 | + // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https |
| 315 | + [$default, '127.0.0.1', '', 'https', true], |
| 316 | + |
| 317 | + // Running behind an SSL proxy, HTTP between server and proxy |
| 318 | + // Proxy (REMOTE_ADDR) is not matched by trustedproxies config |
| 319 | + // HTTPS not set, HTTP_X_FORWARDED_PROTO set to https |
| 320 | + [[], '8.8.8.8', '', 'https', false], |
| 321 | + |
| 322 | + // Running behind a plain HTTP proxy, HTTP between server and proxy |
| 323 | + // HTTPS not set, HTTP_X_FORWARDED_PROTO set to http |
| 324 | + [$default, '127.0.0.1', '', 'http', false], |
| 325 | + |
| 326 | + // Running behind an SSL proxy, HTTP between server and proxy |
| 327 | + // HTTPS set to off, HTTP_X_FORWARDED_PROTO set to https |
| 328 | + [$default, '127.0.0.1', 'off', 'https', true], |
| 329 | + |
| 330 | + // Not running behind a proxy, HTTPS server |
| 331 | + // HTTPS set to on, HTTP_X_FORWARDED_PROTO not set |
| 332 | + [[], '8.8.8.8', 'on', '', true], |
| 333 | + |
| 334 | + // Not running behind a proxy, plain HTTP server |
| 335 | + // HTTPS not set, HTTP_X_FORWARDED_PROTO not set |
| 336 | + [[], '8.8.8.8', '', '', false], |
| 337 | + |
| 338 | + // Not running behind a proxy, plain HTTP server |
| 339 | + // HTTPS set to off, HTTP_X_FORWARDED_PROTO not set |
| 340 | + [[], '8.8.8.8', 'off', '', false], |
| 341 | + |
| 342 | + // Running behind an SSL proxy, SSL between proxy and HTTP server |
| 343 | + // HTTPS set to on, HTTP_X_FORWARDED_PROTO set to https |
| 344 | + [$default, '127.0.0.1', 'on', 'https', true], |
| 345 | + ]; |
| 346 | + |
| 347 | + return $tests; |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Test isSsl(). |
| 352 | + * |
| 353 | + * @dataProvider is_ssl_provider |
| 354 | + * |
| 355 | + * @param string|string[] $config The trustedproxies config value. |
| 356 | + * @param string $remoteAddr The REMOTE_ADDR value. |
| 357 | + * @param string $https The HTTPS value. |
| 358 | + * @param string $forwardedProto The HTTP_X_FORWARDED_PROTO value. |
| 359 | + * @param bool $expected The expected result from isSsl(). |
| 360 | + * |
| 361 | + * @return void |
| 362 | + */ |
| 363 | + public function test_is_ssl($config, string $remoteAddr, string $https, string $forwardedProto, bool $expected): void |
| 364 | + { |
| 365 | + /* @var Input $INPUT */ |
| 366 | + global $INPUT, $conf; |
| 367 | + |
| 368 | + $conf['trustedproxies'] = $config; |
| 369 | + $INPUT->server->set('REMOTE_ADDR', $remoteAddr); |
| 370 | + $INPUT->server->set('HTTPS', $https); |
| 371 | + $INPUT->server->set('HTTP_X_FORWARDED_PROTO', $forwardedProto); |
| 372 | + |
| 373 | + $result = Ip::isSsl(); |
| 374 | + |
| 375 | + $this->assertSame($expected, $result); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Data provider for test_host_name(). |
| 380 | + * |
| 381 | + * @return mixed[][] Returns an array of test cases. |
| 382 | + */ |
| 383 | + public function host_name_provider(): array |
| 384 | + { |
| 385 | + // The new default configuration value. |
| 386 | + $default = ['::1', 'fe80::/10', '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']; |
| 387 | + |
| 388 | + $tests = [ |
| 389 | + // X-Forwarded-Host with trusted proxy |
| 390 | + [$default, '127.0.0.1', 'proxy.example.com', 'www.example.com', 'server.local', 'proxy.example.com'], |
| 391 | + |
| 392 | + // X-Forwarded-Host with untrusted proxy (should fall back to HTTP_HOST) |
| 393 | + [[], '8.8.8.8', 'proxy.example.com', 'www.example.com', 'server.local', 'www.example.com'], |
| 394 | + |
| 395 | + // No X-Forwarded-Host, use HTTP_HOST |
| 396 | + [$default, '127.0.0.1', '', 'www.example.com', 'server.local', 'www.example.com'], |
| 397 | + |
| 398 | + // No X-Forwarded-Host or HTTP_HOST, use SERVER_NAME |
| 399 | + [$default, '127.0.0.1', '', '', 'server.local', 'server.local'], |
| 400 | + |
| 401 | + // No headers set, should fall back to system hostname |
| 402 | + [$default, '127.0.0.1', '', '', '', php_uname('n')], |
| 403 | + ]; |
| 404 | + |
| 405 | + return $tests; |
| 406 | + } |
| 407 | + |
| 408 | + /** |
| 409 | + * Test hostName(). |
| 410 | + * |
| 411 | + * @dataProvider host_name_provider |
| 412 | + * |
| 413 | + * @param string|string[] $config The trustedproxies config value. |
| 414 | + * @param string $remoteAddr The REMOTE_ADDR value. |
| 415 | + * @param string $forwardedHost The HTTP_X_FORWARDED_HOST value. |
| 416 | + * @param string $httpHost The HTTP_HOST value. |
| 417 | + * @param string $serverName The SERVER_NAME value. |
| 418 | + * @param string $expected The expected result from hostName(). |
| 419 | + * |
| 420 | + * @return void |
| 421 | + */ |
| 422 | + public function test_host_name($config, string $remoteAddr, string $forwardedHost, string $httpHost, string $serverName, string $expected): void |
| 423 | + { |
| 424 | + /* @var Input $INPUT */ |
| 425 | + global $INPUT, $conf; |
| 426 | + |
| 427 | + $conf['trustedproxies'] = $config; |
| 428 | + $INPUT->server->set('REMOTE_ADDR', $remoteAddr); |
| 429 | + $INPUT->server->set('HTTP_X_FORWARDED_HOST', $forwardedHost); |
| 430 | + $INPUT->server->set('HTTP_HOST', $httpHost); |
| 431 | + $INPUT->server->set('SERVER_NAME', $serverName); |
| 432 | + |
| 433 | + $result = Ip::hostName(); |
| 434 | + |
| 435 | + $this->assertSame($expected, $result); |
| 436 | + } |
297 | 437 | } |
0 commit comments