View source
<?php
namespace Drupal\Tests\Core\StackMiddleware;
use Drupal\Core\Site\Settings;
use Drupal\Core\StackMiddleware\ReverseProxyMiddleware;
use Drupal\Tests\Traits\ExpectDeprecationTrait;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
class ReverseProxyMiddlewareTest extends UnitTestCase {
use ExpectDeprecationTrait;
protected $mockHttpKernel;
protected function setUp() {
$this->mockHttpKernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
}
public function testNoProxy() {
$settings = new Settings([]);
$this
->assertEquals(0, $settings
->get('reverse_proxy'));
$middleware = new ReverseProxyMiddleware($this->mockHttpKernel, $settings);
$request = $this
->getMockBuilder('Symfony\\Component\\HttpFoundation\\Request')
->setMethods([
'setTrustedProxies',
])
->getMock();
$request
->expects($this
->never())
->method('setTrustedProxies');
$middleware
->handle($request);
}
public function testReverseProxyEnabled($provided_settings, $expected_trusted_header_set) {
$settings = new Settings([
'reverse_proxy' => 1,
] + $provided_settings);
$this
->trustedHeadersAreSet($settings, $expected_trusted_header_set);
}
public function reverseProxyEnabledProvider() {
return [
'Proxy with default trusted headers' => [
[
'reverse_proxy_addresses' => [
'127.0.0.2',
'127.0.0.3',
],
],
Request::HEADER_FORWARDED | Request::HEADER_X_FORWARDED_ALL,
],
'Proxy with AWS trusted headers' => [
[
'reverse_proxy_addresses' => [
'127.0.0.2',
'127.0.0.3',
],
'reverse_proxy_trusted_headers' => Request::HEADER_X_FORWARDED_AWS_ELB,
],
Request::HEADER_X_FORWARDED_AWS_ELB,
],
'Proxy with custom trusted headers' => [
[
'reverse_proxy_addresses' => [
'127.0.0.2',
'127.0.0.3',
],
'reverse_proxy_trusted_headers' => Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST,
],
Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST,
],
];
}
public function testReverseProxyEnabledLegacy($provided_settings, $expected_trusted_header_set, array $expected_deprecations) {
if (!method_exists(Request::class, 'setTrustedHeaderName')) {
$this
->markTestSkipped('The method \\Symfony\\Component\\HttpFoundation\\Request::setTrustedHeaderName() does not exist therefore testing on Symfony 4 or greater.');
}
$this
->expectedDeprecations($expected_deprecations);
$settings = new Settings([
'reverse_proxy' => 1,
] + $provided_settings);
$this
->trustedHeadersAreSet($settings, $expected_trusted_header_set);
}
public function reverseProxyEnabledProviderLegacy() {
return [
'Proxy with deprecated custom headers' => [
[
'reverse_proxy_addresses' => [
'127.0.0.2',
'127.0.0.3',
],
'reverse_proxy_host_header' => NULL,
'reverse_proxy_forwarded_header' => NULL,
],
Request::HEADER_X_FORWARDED_AWS_ELB,
[
'The \'reverse_proxy_host_header\' setting in settings.php is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the \'reverse_proxy_trusted_headers\' setting instead. See https://www.drupal.org/node/3030558',
'The \'reverse_proxy_forwarded_header\' setting in settings.php is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the \'reverse_proxy_trusted_headers\' setting instead. See https://www.drupal.org/node/3030558',
'The "Symfony\\Component\\HttpFoundation\\Request::setTrustedHeaderName()" method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the $trustedHeaderSet argument of the Request::setTrustedProxies() method instead.',
],
],
'Proxy with deprecated custom header' => [
[
'reverse_proxy_addresses' => [
'127.0.0.2',
'127.0.0.3',
],
'reverse_proxy_forwarded_header' => NULL,
],
Request::HEADER_X_FORWARDED_ALL,
[
'The \'reverse_proxy_forwarded_header\' setting in settings.php is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use the \'reverse_proxy_trusted_headers\' setting instead. See https://www.drupal.org/node/3030558',
'The "Symfony\\Component\\HttpFoundation\\Request::setTrustedHeaderName()" method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the $trustedHeaderSet argument of the Request::setTrustedProxies() method instead.',
],
],
];
}
protected function trustedHeadersAreSet(Settings $settings, $expected_trusted_header_set) {
$middleware = new ReverseProxyMiddleware($this->mockHttpKernel, $settings);
$request = new Request();
$middleware
->handle($request);
$this
->assertSame($settings
->get('reverse_proxy_addresses'), $request
->getTrustedProxies());
$this
->assertSame($expected_trusted_header_set, $request
->getTrustedHeaderSet());
}
}