SecurityHeadersMiddlewareTest.php 3.0 KB

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