trusteddomainhelper.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Security;
  9. use OC\AppFramework\Http\Request;
  10. use OCP\IConfig;
  11. /**
  12. * Class TrustedDomain
  13. *
  14. * @package OC\Security
  15. */
  16. class TrustedDomainHelper {
  17. /** @var IConfig */
  18. private $config;
  19. /**
  20. * @param IConfig $config
  21. */
  22. function __construct(IConfig $config) {
  23. $this->config = $config;
  24. }
  25. /**
  26. * Strips a potential port from a domain (in format domain:port)
  27. * @param string $host
  28. * @return string $host without appended port
  29. */
  30. private function getDomainWithoutPort($host) {
  31. $pos = strrpos($host, ':');
  32. if ($pos !== false) {
  33. $port = substr($host, $pos + 1);
  34. if (is_numeric($port)) {
  35. $host = substr($host, 0, $pos);
  36. }
  37. }
  38. return $host;
  39. }
  40. /**
  41. * Checks whether a domain is considered as trusted from the list
  42. * of trusted domains. If no trusted domains have been configured, returns
  43. * true.
  44. * This is used to prevent Host Header Poisoning.
  45. * @param string $domainWithPort
  46. * @return bool true if the given domain is trusted or if no trusted domains
  47. * have been configured
  48. */
  49. public function isTrustedDomain($domainWithPort) {
  50. $domain = $this->getDomainWithoutPort($domainWithPort);
  51. // Read trusted domains from config
  52. $trustedList = $this->config->getSystemValue('trusted_domains', []);
  53. if(!is_array($trustedList)) {
  54. return false;
  55. }
  56. // TODO: Workaround for older instances still with port applied. Remove for ownCloud 9.
  57. if(in_array($domainWithPort, $trustedList)) {
  58. return true;
  59. }
  60. // Always allow access from localhost
  61. if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) {
  62. return true;
  63. }
  64. return in_array($domain, $trustedList);
  65. }
  66. }