OC_Response.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author J0WI <J0WI@users.noreply.github.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. class OC_Response {
  31. /**
  32. * Sets the content disposition header (with possible workarounds)
  33. * @param string $filename file name
  34. * @param string $type disposition type, either 'attachment' or 'inline'
  35. */
  36. public static function setContentDispositionHeader($filename, $type = 'attachment') {
  37. if (\OC::$server->getRequest()->isUserAgent(
  38. [
  39. \OC\AppFramework\Http\Request::USER_AGENT_IE,
  40. \OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  41. \OC\AppFramework\Http\Request::USER_AGENT_FREEBOX,
  42. ])) {
  43. header('Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode($filename) . '"');
  44. } else {
  45. header('Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode($filename)
  46. . '; filename="' . rawurlencode($filename) . '"');
  47. }
  48. }
  49. /**
  50. * Sets the content length header (with possible workarounds)
  51. * @param string|int|float $length Length to be sent
  52. */
  53. public static function setContentLengthHeader($length) {
  54. header('Content-Length: '.$length);
  55. }
  56. /**
  57. * This function adds some security related headers to all requests served via base.php
  58. * The implementation of this function has to happen here to ensure that all third-party
  59. * components (e.g. SabreDAV) also benefit from this headers.
  60. */
  61. public static function addSecurityHeaders() {
  62. /**
  63. * FIXME: Content Security Policy for legacy ownCloud components. This
  64. * can be removed once \OCP\AppFramework\Http\Response from the AppFramework
  65. * is used everywhere.
  66. * @see \OCP\AppFramework\Http\Response::getHeaders
  67. */
  68. $policy = 'default-src \'self\'; '
  69. . 'script-src \'self\' \'nonce-'.\OC::$server->getContentSecurityPolicyNonceManager()->getNonce().'\'; '
  70. . 'style-src \'self\' \'unsafe-inline\'; '
  71. . 'frame-src *; '
  72. . 'img-src * data: blob:; '
  73. . 'font-src \'self\' data:; '
  74. . 'media-src *; '
  75. . 'connect-src *; '
  76. . 'object-src \'none\'; '
  77. . 'base-uri \'self\'; ';
  78. header('Content-Security-Policy:' . $policy);
  79. // Send fallback headers for installations that don't have the possibility to send
  80. // custom headers on the webserver side
  81. if (getenv('modHeadersAvailable') !== 'true') {
  82. header('Referrer-Policy: no-referrer'); // https://www.w3.org/TR/referrer-policy/
  83. header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE
  84. header('X-Frame-Options: SAMEORIGIN'); // Disallow iFraming from other domains
  85. header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html
  86. header('X-Robots-Tag: none'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
  87. header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
  88. }
  89. }
  90. }