OC_Response.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. class OC_Response {
  8. /**
  9. * Sets the content disposition header (with possible workarounds)
  10. * @param string $filename file name
  11. * @param string $type disposition type, either 'attachment' or 'inline'
  12. */
  13. public static function setContentDispositionHeader($filename, $type = 'attachment') {
  14. if (\OC::$server->getRequest()->isUserAgent(
  15. [
  16. \OC\AppFramework\Http\Request::USER_AGENT_IE,
  17. \OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  18. \OC\AppFramework\Http\Request::USER_AGENT_FREEBOX,
  19. ])) {
  20. header('Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode($filename) . '"');
  21. } else {
  22. header('Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode($filename)
  23. . '; filename="' . rawurlencode($filename) . '"');
  24. }
  25. }
  26. /**
  27. * Sets the content length header (with possible workarounds)
  28. * @param string|int|float $length Length to be sent
  29. */
  30. public static function setContentLengthHeader($length) {
  31. if (PHP_INT_SIZE === 4) {
  32. if ($length > PHP_INT_MAX && stripos(PHP_SAPI, 'apache') === 0) {
  33. // Apache PHP SAPI casts Content-Length headers to PHP integers.
  34. // This enforces a limit of PHP_INT_MAX (2147483647 on 32-bit
  35. // platforms). So, if the length is greater than PHP_INT_MAX,
  36. // we just do not send a Content-Length header to prevent
  37. // bodies from being received incompletely.
  38. return;
  39. }
  40. // Convert signed integer or float to unsigned base-10 string.
  41. $lfh = new \OC\LargeFileHelper;
  42. $length = $lfh->formatUnsignedInteger($length);
  43. }
  44. header('Content-Length: '.$length);
  45. }
  46. /**
  47. * This function adds some security related headers to all requests served via base.php
  48. * The implementation of this function has to happen here to ensure that all third-party
  49. * components (e.g. SabreDAV) also benefit from this headers.
  50. */
  51. public static function addSecurityHeaders() {
  52. /**
  53. * FIXME: Content Security Policy for legacy ownCloud components. This
  54. * can be removed once \OCP\AppFramework\Http\Response from the AppFramework
  55. * is used everywhere.
  56. * @see \OCP\AppFramework\Http\Response::getHeaders
  57. */
  58. $policy = 'default-src \'self\'; '
  59. . 'script-src \'self\' \'nonce-'.\OC::$server->getContentSecurityPolicyNonceManager()->getNonce().'\'; '
  60. . 'style-src \'self\' \'unsafe-inline\'; '
  61. . 'frame-src *; '
  62. . 'img-src * data: blob:; '
  63. . 'font-src \'self\' data:; '
  64. . 'media-src *; '
  65. . 'connect-src *; '
  66. . 'object-src \'none\'; '
  67. . 'base-uri \'self\'; ';
  68. header('Content-Security-Policy:' . $policy);
  69. // Send fallback headers for installations that don't have the possibility to send
  70. // custom headers on the webserver side
  71. if (getenv('modHeadersAvailable') !== 'true') {
  72. header('Referrer-Policy: no-referrer'); // https://www.w3.org/TR/referrer-policy/
  73. header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE
  74. header('X-Frame-Options: SAMEORIGIN'); // Disallow iFraming from other domains
  75. header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html
  76. header('X-Robots-Tag: noindex, nofollow'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag
  77. header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters
  78. }
  79. }
  80. }