SecurityHeadersMiddlewareTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Middleware;
  16. use Cake\Http\Middleware\SecurityHeadersMiddleware;
  17. use Cake\Http\ServerRequestFactory;
  18. use Cake\TestSuite\TestCase;
  19. use Zend\Diactoros\Response;
  20. /**
  21. * Test for SecurityMiddleware
  22. */
  23. class SecurityHeadersMiddlewareTest extends TestCase
  24. {
  25. /**
  26. * Test adding the security headers
  27. *
  28. * @return void
  29. */
  30. public function testAddingSecurityHeaders()
  31. {
  32. $request = ServerRequestFactory::fromGlobals([
  33. 'REQUEST_URI' => '/',
  34. ]);
  35. $response = new Response();
  36. $next = function ($req, $res) {
  37. return $res;
  38. };
  39. $middleware = new SecurityHeadersMiddleware();
  40. $middleware
  41. ->setCrossDomainPolicy()
  42. ->setReferrerPolicy()
  43. ->setXFrameOptions()
  44. ->setXssProtection()
  45. ->noOpen()
  46. ->noSniff();
  47. $expected = [
  48. 'x-permitted-cross-domain-policies' => ['all'],
  49. 'x-xss-protection' => ['1; mode=block'],
  50. 'referrer-policy' => ['same-origin'],
  51. 'x-frame-options' => ['sameorigin'],
  52. 'x-download-options' => ['noopen'],
  53. 'x-content-type-options' => ['nosniff']
  54. ];
  55. $result = $middleware($request, $response, $next);
  56. $this->assertEquals($expected, $result->getHeaders());
  57. }
  58. /**
  59. * Testing that the URL is required when option is `allow-from`
  60. *
  61. * @return void
  62. */
  63. public function testInvalidArgumentExceptionForsetXFrameOptionsUrl()
  64. {
  65. $this->expectException(\InvalidArgumentException::class);
  66. $this->expectExceptionMessage('The 2nd arg $url can not be empty when `allow-from` is used');
  67. $middleware = new SecurityHeadersMiddleware();
  68. $middleware->setXFrameOptions('allow-from');
  69. }
  70. /**
  71. * Testing the protected checkValues() method that is used by most of the
  72. * methods in the test to avoid passing an invalid argument.
  73. *
  74. * @return void
  75. */
  76. public function testCheckValues()
  77. {
  78. $this->expectException(\InvalidArgumentException::class);
  79. $this->expectExceptionMessage('Invalid arg `INVALID-VALUE!`, use one of these: all, none, master-only, by-content-type, by-ftp-filename');
  80. $middleware = new SecurityHeadersMiddleware();
  81. $middleware->setCrossDomainPolicy('INVALID-VALUE!');
  82. }
  83. }