CSPMiddleware.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\AppFramework\Middleware\Security;
  26. use OC\Security\CSP\ContentSecurityPolicyManager;
  27. use OC\Security\CSP\ContentSecurityPolicyNonceManager;
  28. use OC\Security\CSRF\CsrfTokenManager;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http\ContentSecurityPolicy;
  31. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  32. use OCP\AppFramework\Http\Response;
  33. use OCP\AppFramework\Middleware;
  34. class CSPMiddleware extends Middleware {
  35. /** @var ContentSecurityPolicyManager */
  36. private $contentSecurityPolicyManager;
  37. /** @var ContentSecurityPolicyNonceManager */
  38. private $cspNonceManager;
  39. /** @var CsrfTokenManager */
  40. private $csrfTokenManager;
  41. public function __construct(ContentSecurityPolicyManager $policyManager,
  42. ContentSecurityPolicyNonceManager $cspNonceManager,
  43. CsrfTokenManager $csrfTokenManager) {
  44. $this->contentSecurityPolicyManager = $policyManager;
  45. $this->cspNonceManager = $cspNonceManager;
  46. $this->csrfTokenManager = $csrfTokenManager;
  47. }
  48. /**
  49. * Performs the default CSP modifications that may be injected by other
  50. * applications
  51. *
  52. * @param Controller $controller
  53. * @param string $methodName
  54. * @param Response $response
  55. * @return Response
  56. */
  57. public function afterController($controller, $methodName, Response $response): Response {
  58. $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy();
  59. if (get_class($policy) === EmptyContentSecurityPolicy::class) {
  60. return $response;
  61. }
  62. $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy();
  63. $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy);
  64. if ($this->cspNonceManager->browserSupportsCspV3()) {
  65. $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue());
  66. }
  67. $response->setContentSecurityPolicy($defaultPolicy);
  68. return $response;
  69. }
  70. }