AddSessionDomainConstraintPassTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
  13. use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSessionDomainConstraintPass;
  14. use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\HttpFoundation\Request;
  17. class AddSessionDomainConstraintPassTest extends TestCase
  18. {
  19. public function testSessionCookie()
  20. {
  21. $container = $this->createContainer(array('cookie_domain' => '.symfony.com.', 'cookie_secure' => true));
  22. $utils = $container->get('security.http_utils');
  23. $request = Request::create('/', 'get');
  24. $this->assertTrue($utils->createRedirectResponse($request, 'https://symfony.com/blog')->isRedirect('https://symfony.com/blog'));
  25. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.symfony.com/blog')->isRedirect('https://www.symfony.com/blog'));
  26. $this->assertTrue($utils->createRedirectResponse($request, 'https://localhost/foo')->isRedirect('https://localhost/foo'));
  27. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.localhost/foo')->isRedirect('http://localhost/'));
  28. $this->assertTrue($utils->createRedirectResponse($request, 'http://symfony.com/blog')->isRedirect('http://localhost/'));
  29. $this->assertTrue($utils->createRedirectResponse($request, 'http://pirate.com/foo')->isRedirect('http://localhost/'));
  30. }
  31. public function testSessionNoDomain()
  32. {
  33. $container = $this->createContainer(array('cookie_secure' => true));
  34. $utils = $container->get('security.http_utils');
  35. $request = Request::create('/', 'get');
  36. $this->assertTrue($utils->createRedirectResponse($request, 'https://symfony.com/blog')->isRedirect('http://localhost/'));
  37. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.symfony.com/blog')->isRedirect('http://localhost/'));
  38. $this->assertTrue($utils->createRedirectResponse($request, 'https://localhost/foo')->isRedirect('https://localhost/foo'));
  39. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.localhost/foo')->isRedirect('http://localhost/'));
  40. $this->assertTrue($utils->createRedirectResponse($request, 'http://symfony.com/blog')->isRedirect('http://localhost/'));
  41. $this->assertTrue($utils->createRedirectResponse($request, 'http://pirate.com/foo')->isRedirect('http://localhost/'));
  42. }
  43. public function testSessionNoSecure()
  44. {
  45. $container = $this->createContainer(array('cookie_domain' => '.symfony.com.'));
  46. $utils = $container->get('security.http_utils');
  47. $request = Request::create('/', 'get');
  48. $this->assertTrue($utils->createRedirectResponse($request, 'https://symfony.com/blog')->isRedirect('https://symfony.com/blog'));
  49. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.symfony.com/blog')->isRedirect('https://www.symfony.com/blog'));
  50. $this->assertTrue($utils->createRedirectResponse($request, 'https://localhost/foo')->isRedirect('https://localhost/foo'));
  51. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.localhost/foo')->isRedirect('http://localhost/'));
  52. $this->assertTrue($utils->createRedirectResponse($request, 'http://symfony.com/blog')->isRedirect('http://symfony.com/blog'));
  53. $this->assertTrue($utils->createRedirectResponse($request, 'http://pirate.com/foo')->isRedirect('http://localhost/'));
  54. }
  55. public function testSessionNoSecureAndNoDomain()
  56. {
  57. $container = $this->createContainer(array());
  58. $utils = $container->get('security.http_utils');
  59. $request = Request::create('/', 'get');
  60. $this->assertTrue($utils->createRedirectResponse($request, 'https://symfony.com/blog')->isRedirect('http://localhost/'));
  61. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.symfony.com/blog')->isRedirect('http://localhost/'));
  62. $this->assertTrue($utils->createRedirectResponse($request, 'https://localhost/foo')->isRedirect('https://localhost/foo'));
  63. $this->assertTrue($utils->createRedirectResponse($request, 'http://localhost/foo')->isRedirect('http://localhost/foo'));
  64. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.localhost/foo')->isRedirect('http://localhost/'));
  65. $this->assertTrue($utils->createRedirectResponse($request, 'http://symfony.com/blog')->isRedirect('http://localhost/'));
  66. $this->assertTrue($utils->createRedirectResponse($request, 'http://pirate.com/foo')->isRedirect('http://localhost/'));
  67. }
  68. public function testNoSession()
  69. {
  70. $container = $this->createContainer(null);
  71. $utils = $container->get('security.http_utils');
  72. $request = Request::create('/', 'get');
  73. $this->assertTrue($utils->createRedirectResponse($request, 'https://symfony.com/blog')->isRedirect('https://symfony.com/blog'));
  74. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.symfony.com/blog')->isRedirect('https://www.symfony.com/blog'));
  75. $this->assertTrue($utils->createRedirectResponse($request, 'https://localhost/foo')->isRedirect('https://localhost/foo'));
  76. $this->assertTrue($utils->createRedirectResponse($request, 'https://www.localhost/foo')->isRedirect('https://www.localhost/foo'));
  77. $this->assertTrue($utils->createRedirectResponse($request, 'http://symfony.com/blog')->isRedirect('http://symfony.com/blog'));
  78. $this->assertTrue($utils->createRedirectResponse($request, 'http://pirate.com/foo')->isRedirect('http://pirate.com/foo'));
  79. }
  80. private function createContainer($sessionStorageOptions)
  81. {
  82. $container = new ContainerBuilder();
  83. $container->setParameter('kernel.cache_dir', __DIR__);
  84. $container->setParameter('kernel.charset', 'UTF-8');
  85. $container->setParameter('kernel.container_class', 'cc');
  86. $container->setParameter('kernel.debug', true);
  87. $container->setParameter('kernel.root_dir', __DIR__);
  88. $container->setParameter('kernel.secret', __DIR__);
  89. if (null !== $sessionStorageOptions) {
  90. $container->setParameter('session.storage.options', $sessionStorageOptions);
  91. }
  92. $container->setParameter('request_listener.http_port', 80);
  93. $container->setParameter('request_listener.https_port', 443);
  94. $config = array(
  95. 'security' => array(
  96. 'providers' => array('some_provider' => array('id' => 'foo')),
  97. 'firewalls' => array('some_firewall' => array('security' => false)),
  98. ),
  99. );
  100. $ext = new FrameworkExtension();
  101. $ext->load(array(), $container);
  102. $ext = new SecurityExtension();
  103. $ext->load($config, $container);
  104. $pass = new AddSessionDomainConstraintPass();
  105. $pass->process($container);
  106. return $container;
  107. }
  108. }