TrustedDomainHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Johannes Ernst <jernst@indiecomputing.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Security;
  26. use OC\AppFramework\Http\Request;
  27. use OCP\IConfig;
  28. /**
  29. * Class TrustedDomain
  30. *
  31. * @package OC\Security
  32. */
  33. class TrustedDomainHelper {
  34. /** @var IConfig */
  35. private $config;
  36. /**
  37. * @param IConfig $config
  38. */
  39. public function __construct(IConfig $config) {
  40. $this->config = $config;
  41. }
  42. /**
  43. * Strips a potential port from a domain (in format domain:port)
  44. * @param string $host
  45. * @return string $host without appended port
  46. */
  47. private function getDomainWithoutPort($host) {
  48. $pos = strrpos($host, ':');
  49. if ($pos !== false) {
  50. $port = substr($host, $pos + 1);
  51. if (is_numeric($port)) {
  52. $host = substr($host, 0, $pos);
  53. }
  54. }
  55. return $host;
  56. }
  57. /**
  58. * Checks whether a domain is considered as trusted from the list
  59. * of trusted domains. If no trusted domains have been configured, returns
  60. * true.
  61. * This is used to prevent Host Header Poisoning.
  62. * @param string $domainWithPort
  63. * @return bool true if the given domain is trusted or if no trusted domains
  64. * have been configured
  65. */
  66. public function isTrustedDomain($domainWithPort) {
  67. // overwritehost is always trusted
  68. if ($this->config->getSystemValue('overwritehost') !== '') {
  69. return true;
  70. }
  71. $domain = $this->getDomainWithoutPort($domainWithPort);
  72. // Read trusted domains from config
  73. $trustedList = $this->config->getSystemValue('trusted_domains', []);
  74. if (!is_array($trustedList)) {
  75. return false;
  76. }
  77. // Always allow access from localhost
  78. if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) {
  79. return true;
  80. }
  81. // Reject misformed domains in any case
  82. if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) {
  83. return false;
  84. }
  85. // Match, allowing for * wildcards
  86. foreach ($trustedList as $trusted) {
  87. if (gettype($trusted) !== 'string') {
  88. break;
  89. }
  90. $regex = '/^' . implode('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/';
  91. if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. }