EmptyFeaturePolicy.php 5.2 KB

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