CSPMiddlewareTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\AppFramework\Middleware\Security;
  25. use OC\AppFramework\Middleware\Security\CSPMiddleware;
  26. use OC\Security\CSP\ContentSecurityPolicy;
  27. use OC\Security\CSP\ContentSecurityPolicyManager;
  28. use OC\Security\CSP\ContentSecurityPolicyNonceManager;
  29. use OC\Security\CSRF\CsrfToken;
  30. use OC\Security\CSRF\CsrfTokenManager;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  33. use OCP\AppFramework\Http\Response;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. class CSPMiddlewareTest extends \Test\TestCase {
  36. /** @var CSPMiddleware|MockObject */
  37. private $middleware;
  38. /** @var Controller|MockObject */
  39. private $controller;
  40. /** @var ContentSecurityPolicyManager|MockObject */
  41. private $contentSecurityPolicyManager;
  42. /** @var CsrfTokenManager|MockObject */
  43. private $csrfTokenManager;
  44. /** @var ContentSecurityPolicyNonceManager|MockObject */
  45. private $cspNonceManager;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->controller = $this->createMock(Controller::class);
  49. $this->contentSecurityPolicyManager = $this->createMock(ContentSecurityPolicyManager::class);
  50. $this->csrfTokenManager = $this->createMock(CsrfTokenManager::class);
  51. $this->cspNonceManager = $this->createMock(ContentSecurityPolicyNonceManager::class);
  52. $this->middleware = new CSPMiddleware(
  53. $this->contentSecurityPolicyManager,
  54. $this->cspNonceManager,
  55. $this->csrfTokenManager
  56. );
  57. }
  58. public function testAfterController() {
  59. $this->cspNonceManager
  60. ->expects($this->once())
  61. ->method('browserSupportsCspV3')
  62. ->willReturn(false);
  63. $response = $this->createMock(Response::class);
  64. $defaultPolicy = new ContentSecurityPolicy();
  65. $defaultPolicy->addAllowedImageDomain('defaultpolicy');
  66. $currentPolicy = new ContentSecurityPolicy();
  67. $currentPolicy->addAllowedConnectDomain('currentPolicy');
  68. $mergedPolicy = new ContentSecurityPolicy();
  69. $mergedPolicy->addAllowedMediaDomain('mergedPolicy');
  70. $response
  71. ->expects($this->exactly(2))
  72. ->method('getContentSecurityPolicy')
  73. ->willReturn($currentPolicy);
  74. $this->contentSecurityPolicyManager
  75. ->expects($this->once())
  76. ->method('getDefaultPolicy')
  77. ->willReturn($defaultPolicy);
  78. $this->contentSecurityPolicyManager
  79. ->expects($this->once())
  80. ->method('mergePolicies')
  81. ->with($defaultPolicy, $currentPolicy)
  82. ->willReturn($mergedPolicy);
  83. $response->expects($this->once())
  84. ->method('setContentSecurityPolicy')
  85. ->with($mergedPolicy);
  86. $this->middleware->afterController($this->controller, 'test', $response);
  87. }
  88. public function testAfterControllerEmptyCSP() {
  89. $response = $this->createMock(Response::class);
  90. $emptyPolicy = new EmptyContentSecurityPolicy();
  91. $response->expects($this->any())
  92. ->method('getContentSecurityPolicy')
  93. ->willReturn($emptyPolicy);
  94. $response->expects($this->never())
  95. ->method('setContentSecurityPolicy');
  96. $this->middleware->afterController($this->controller, 'test', $response);
  97. }
  98. public function testAfterControllerWithContentSecurityPolicy3Support() {
  99. $this->cspNonceManager
  100. ->expects($this->once())
  101. ->method('browserSupportsCspV3')
  102. ->willReturn(true);
  103. $token = $this->createMock(CsrfToken::class);
  104. $token
  105. ->expects($this->once())
  106. ->method('getEncryptedValue')
  107. ->willReturn('MyEncryptedToken');
  108. $this->csrfTokenManager
  109. ->expects($this->once())
  110. ->method('getToken')
  111. ->willReturn($token);
  112. $response = $this->createMock(Response::class);
  113. $defaultPolicy = new ContentSecurityPolicy();
  114. $defaultPolicy->addAllowedImageDomain('defaultpolicy');
  115. $currentPolicy = new ContentSecurityPolicy();
  116. $currentPolicy->addAllowedConnectDomain('currentPolicy');
  117. $mergedPolicy = new ContentSecurityPolicy();
  118. $mergedPolicy->addAllowedMediaDomain('mergedPolicy');
  119. $response
  120. ->expects($this->exactly(2))
  121. ->method('getContentSecurityPolicy')
  122. ->willReturn($currentPolicy);
  123. $this->contentSecurityPolicyManager
  124. ->expects($this->once())
  125. ->method('getDefaultPolicy')
  126. ->willReturn($defaultPolicy);
  127. $this->contentSecurityPolicyManager
  128. ->expects($this->once())
  129. ->method('mergePolicies')
  130. ->with($defaultPolicy, $currentPolicy)
  131. ->willReturn($mergedPolicy);
  132. $response->expects($this->once())
  133. ->method('setContentSecurityPolicy')
  134. ->with($mergedPolicy);
  135. $this->assertEquals($response, $this->middleware->afterController($this->controller, 'test', $response));
  136. }
  137. }