EmptyFeaturePolicy.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 OCP\AppFramework\Http;
  8. /**
  9. * Class EmptyFeaturePolicy is a simple helper which allows applications
  10. * to modify the FeaturePolicy sent by Nextcloud. Per default the policy
  11. * is forbidding everything.
  12. *
  13. * As alternative with sane exemptions look at FeaturePolicy
  14. *
  15. * @see \OCP\AppFramework\Http\FeaturePolicy
  16. * @since 17.0.0
  17. */
  18. class EmptyFeaturePolicy {
  19. /** @var string[] of allowed domains to autoplay media */
  20. protected $autoplayDomains = null;
  21. /** @var string[] of allowed domains that can access the camera */
  22. protected $cameraDomains = null;
  23. /** @var string[] of allowed domains that can use fullscreen */
  24. protected $fullscreenDomains = null;
  25. /** @var string[] of allowed domains that can use the geolocation of the device */
  26. protected $geolocationDomains = null;
  27. /** @var string[] of allowed domains that can use the microphone */
  28. protected $microphoneDomains = null;
  29. /** @var string[] of allowed domains that can use the payment API */
  30. protected $paymentDomains = null;
  31. /**
  32. * Allows to use autoplay from a specific domain. Use * to allow from all domains.
  33. *
  34. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  35. * @return $this
  36. * @since 17.0.0
  37. */
  38. public function addAllowedAutoplayDomain(string $domain): self {
  39. $this->autoplayDomains[] = $domain;
  40. return $this;
  41. }
  42. /**
  43. * Allows to use the camera on a specific domain. Use * to allow from all domains
  44. *
  45. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  46. * @return $this
  47. * @since 17.0.0
  48. */
  49. public function addAllowedCameraDomain(string $domain): self {
  50. $this->cameraDomains[] = $domain;
  51. return $this;
  52. }
  53. /**
  54. * Allows the full screen functionality to be used on a specific domain. Use * to allow from all domains
  55. *
  56. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  57. * @return $this
  58. * @since 17.0.0
  59. */
  60. public function addAllowedFullScreenDomain(string $domain): self {
  61. $this->fullscreenDomains[] = $domain;
  62. return $this;
  63. }
  64. /**
  65. * Allows to use the geolocation on a specific domain. Use * to allow from all domains
  66. *
  67. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  68. * @return $this
  69. * @since 17.0.0
  70. */
  71. public function addAllowedGeoLocationDomain(string $domain): self {
  72. $this->geolocationDomains[] = $domain;
  73. return $this;
  74. }
  75. /**
  76. * Allows to use the microphone on a specific domain. Use * to allow from all domains
  77. *
  78. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  79. * @return $this
  80. * @since 17.0.0
  81. */
  82. public function addAllowedMicrophoneDomain(string $domain): self {
  83. $this->microphoneDomains[] = $domain;
  84. return $this;
  85. }
  86. /**
  87. * Allows to use the payment API on a specific domain. Use * to allow from all domains
  88. *
  89. * @param string $domain Domain to whitelist. Any passed value needs to be properly sanitized.
  90. * @return $this
  91. * @since 17.0.0
  92. */
  93. public function addAllowedPaymentDomain(string $domain): self {
  94. $this->paymentDomains[] = $domain;
  95. return $this;
  96. }
  97. /**
  98. * Get the generated Feature-Policy as a string
  99. *
  100. * @return string
  101. * @since 17.0.0
  102. */
  103. public function buildPolicy(): string {
  104. $policy = '';
  105. if (empty($this->autoplayDomains)) {
  106. $policy .= "autoplay 'none';";
  107. } else {
  108. $policy .= 'autoplay ' . implode(' ', $this->autoplayDomains);
  109. $policy .= ';';
  110. }
  111. if (empty($this->cameraDomains)) {
  112. $policy .= "camera 'none';";
  113. } else {
  114. $policy .= 'camera ' . implode(' ', $this->cameraDomains);
  115. $policy .= ';';
  116. }
  117. if (empty($this->fullscreenDomains)) {
  118. $policy .= "fullscreen 'none';";
  119. } else {
  120. $policy .= 'fullscreen ' . implode(' ', $this->fullscreenDomains);
  121. $policy .= ';';
  122. }
  123. if (empty($this->geolocationDomains)) {
  124. $policy .= "geolocation 'none';";
  125. } else {
  126. $policy .= 'geolocation ' . implode(' ', $this->geolocationDomains);
  127. $policy .= ';';
  128. }
  129. if (empty($this->microphoneDomains)) {
  130. $policy .= "microphone 'none';";
  131. } else {
  132. $policy .= 'microphone ' . implode(' ', $this->microphoneDomains);
  133. $policy .= ';';
  134. }
  135. if (empty($this->paymentDomains)) {
  136. $policy .= "payment 'none';";
  137. } else {
  138. $policy .= 'payment ' . implode(' ', $this->paymentDomains);
  139. $policy .= ';';
  140. }
  141. return rtrim($policy, ';');
  142. }
  143. }