ContentSecurityPolicyManager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Security\CSP;
  27. use OCP\AppFramework\Http\ContentSecurityPolicy;
  28. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\Security\CSP\AddContentSecurityPolicyEvent;
  31. use OCP\Security\IContentSecurityPolicyManager;
  32. class ContentSecurityPolicyManager implements IContentSecurityPolicyManager {
  33. /** @var ContentSecurityPolicy[] */
  34. private $policies = [];
  35. /** @var IEventDispatcher */
  36. private $dispatcher;
  37. public function __construct(IEventDispatcher $dispatcher) {
  38. $this->dispatcher = $dispatcher;
  39. }
  40. /** {@inheritdoc} */
  41. public function addDefaultPolicy(EmptyContentSecurityPolicy $policy) {
  42. $this->policies[] = $policy;
  43. }
  44. /**
  45. * Get the configured default policy. This is not in the public namespace
  46. * as it is only supposed to be used by core itself.
  47. *
  48. * @return ContentSecurityPolicy
  49. */
  50. public function getDefaultPolicy(): ContentSecurityPolicy {
  51. $event = new AddContentSecurityPolicyEvent($this);
  52. $this->dispatcher->dispatchTyped($event);
  53. $defaultPolicy = new \OC\Security\CSP\ContentSecurityPolicy();
  54. foreach ($this->policies as $policy) {
  55. $defaultPolicy = $this->mergePolicies($defaultPolicy, $policy);
  56. }
  57. return $defaultPolicy;
  58. }
  59. /**
  60. * Merges the first given policy with the second one
  61. *
  62. * @param ContentSecurityPolicy $defaultPolicy
  63. * @param EmptyContentSecurityPolicy $originalPolicy
  64. * @return ContentSecurityPolicy
  65. */
  66. public function mergePolicies(ContentSecurityPolicy $defaultPolicy,
  67. EmptyContentSecurityPolicy $originalPolicy): ContentSecurityPolicy {
  68. foreach ((object)(array)$originalPolicy as $name => $value) {
  69. $setter = 'set'.ucfirst($name);
  70. if (\is_array($value)) {
  71. $getter = 'get'.ucfirst($name);
  72. $currentValues = \is_array($defaultPolicy->$getter()) ? $defaultPolicy->$getter() : [];
  73. $defaultPolicy->$setter(array_values(array_unique(array_merge($currentValues, $value))));
  74. } elseif (\is_bool($value)) {
  75. $getter = 'is'.ucfirst($name);
  76. $currentValue = $defaultPolicy->$getter();
  77. // true wins over false
  78. if ($value > $currentValue) {
  79. $defaultPolicy->$setter($value);
  80. }
  81. }
  82. }
  83. return $defaultPolicy;
  84. }
  85. }