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 154 155 156 157 158
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
class SecurityRoutingIntegrationTest extends AbstractWebTestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testRoutingErrorIsNotExposedForProtectedResourceWhenAnonymous(array $options)
{
$client = $this->createClient($options);
$client->request('GET', '/protected_resource');
$this->assertRedirect($client->getResponse(), '/login');
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testRoutingErrorIsExposedWhenNotProtected(array $options)
{
$client = $this->createClient($options);
$client->request('GET', '/unprotected_resource');
$this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testRoutingErrorIsNotExposedForProtectedResourceWhenLoggedInWithInsufficientRights(array $options)
{
$client = $this->createClient($options);
$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'test';
$client->submit($form);
$client->request('GET', '/highly_protected_resource');
$this->assertNotEquals(404, $client->getResponse()->getStatusCode());
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testSecurityConfigurationForSingleIPAddress(array $options)
{
$allowedClient = $this->createClient($options, ['REMOTE_ADDR' => '10.10.10.10']);
$this->ensureKernelShutdown();
$barredClient = $this->createClient($options, ['REMOTE_ADDR' => '10.10.20.10']);
$this->assertAllowed($allowedClient, '/secured-by-one-ip');
$this->assertRestricted($barredClient, '/secured-by-one-ip');
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testSecurityConfigurationForMultipleIPAddresses(array $options)
{
$allowedClientA = $this->createClient($options, ['REMOTE_ADDR' => '1.1.1.1']);
$this->ensureKernelShutdown();
$allowedClientB = $this->createClient($options, ['REMOTE_ADDR' => '2.2.2.2']);
$this->ensureKernelShutdown();
$allowedClientC = $this->createClient($options, ['REMOTE_ADDR' => '203.0.113.0']);
$this->ensureKernelShutdown();
$barredClient = $this->createClient($options, ['REMOTE_ADDR' => '192.168.1.1']);
$this->assertAllowed($allowedClientA, '/secured-by-two-ips');
$this->assertAllowed($allowedClientB, '/secured-by-two-ips');
$this->assertRestricted($allowedClientA, '/secured-by-one-real-ip');
$this->assertRestricted($allowedClientA, '/secured-by-one-real-ipv6');
$this->assertAllowed($allowedClientC, '/secured-by-one-real-ip-with-mask');
$this->assertRestricted($barredClient, '/secured-by-two-ips');
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideConfigs')]
public function testSecurityConfigurationForExpression(array $options)
{
$allowedClient = $this->createClient($options, ['HTTP_USER_AGENT' => 'Firefox 1.0']);
$this->assertAllowed($allowedClient, '/protected-via-expression');
$this->ensureKernelShutdown();
$barredClient = $this->createClient($options, []);
$this->assertRestricted($barredClient, '/protected-via-expression');
$this->ensureKernelShutdown();
$allowedClient = $this->createClient($options, []);
$allowedClient->request('GET', '/protected-via-expression');
$form = $allowedClient->followRedirect()->selectButton('login')->form();
$form['_username'] = 'johannes';
$form['_password'] = 'test';
$allowedClient->submit($form);
$this->assertRedirect($allowedClient->getResponse(), '/protected-via-expression');
$this->assertAllowed($allowedClient, '/protected-via-expression');
}
public function testInvalidIpsInAccessControl()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given value "256.357.458.559" in the "security.access_control" config option is not a valid IP address.');
$this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => 'invalid_ip_access_control.yml']);
}
public function testPublicHomepage()
{
$client = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => 'base_config.yml']);
$client->request('GET', '/en/');
$this->assertEquals(200, $client->getResponse()->getStatusCode(), (string) $client->getResponse());
$this->assertTrue($client->getResponse()->headers->getCacheControlDirective('public'));
$this->assertSame(0, self::getContainer()->get('request_tracker_subscriber')->getLastRequest()->getSession()->getUsageIndex());
}
private function assertAllowed($client, $path)
{
$client->request('GET', $path);
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
private function assertRestricted($client, $path)
{
$client->request('GET', $path);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
}
public static function provideClientOptions(): iterable
{
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'base_config.yml', 'enable_authenticator_manager' => true]];
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'routes_as_path.yml', 'enable_authenticator_manager' => true]];
}
public static function provideLegacyClientOptions()
{
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'base_config.yml', 'enable_authenticator_manager' => true]];
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'routes_as_path.yml', 'enable_authenticator_manager' => true]];
}
public static function provideConfigs(): iterable
{
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'base_config.yml']];
yield [['test_case' => 'StandardFormLogin', 'root_config' => 'routes_as_path.yml']];
}
}
|