FeaturePolicyMiddleware.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\AppFramework\Middleware\Security;
  8. use OC\Security\FeaturePolicy\FeaturePolicy;
  9. use OC\Security\FeaturePolicy\FeaturePolicyManager;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\EmptyFeaturePolicy;
  12. use OCP\AppFramework\Http\Response;
  13. use OCP\AppFramework\Middleware;
  14. class FeaturePolicyMiddleware extends Middleware {
  15. /** @var FeaturePolicyManager */
  16. private $policyManager;
  17. public function __construct(FeaturePolicyManager $policyManager) {
  18. $this->policyManager = $policyManager;
  19. }
  20. /**
  21. * Performs the default FeaturePolicy modifications that may be injected by other
  22. * applications
  23. *
  24. * @param Controller $controller
  25. * @param string $methodName
  26. * @param Response $response
  27. * @return Response
  28. */
  29. public function afterController($controller, $methodName, Response $response): Response {
  30. $policy = !is_null($response->getFeaturePolicy()) ? $response->getFeaturePolicy() : new FeaturePolicy();
  31. if (get_class($policy) === EmptyFeaturePolicy::class) {
  32. return $response;
  33. }
  34. $defaultPolicy = $this->policyManager->getDefaultPolicy();
  35. $defaultPolicy = $this->policyManager->mergePolicies($defaultPolicy, $policy);
  36. $response->setFeaturePolicy($defaultPolicy);
  37. return $response;
  38. }
  39. }