You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrustedDomainHelper.php 3.1KB

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