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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. use OCP\Security\ITrustedDomainHelper;
  32. class TrustedDomainHelper implements ITrustedDomainHelper {
  33. /** @var IConfig */
  34. private $config;
  35. /**
  36. * @param IConfig $config
  37. */
  38. public function __construct(IConfig $config) {
  39. $this->config = $config;
  40. }
  41. /**
  42. * Strips a potential port from a domain (in format domain:port)
  43. * @param string $host
  44. * @return string $host without appended port
  45. */
  46. private function getDomainWithoutPort(string $host): string {
  47. $pos = strrpos($host, ':');
  48. if ($pos !== false) {
  49. $port = substr($host, $pos + 1);
  50. if (is_numeric($port)) {
  51. $host = substr($host, 0, $pos);
  52. }
  53. }
  54. return $host;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function isTrustedUrl(string $url): bool {
  60. $parsedUrl = parse_url($url);
  61. if (empty($parsedUrl['host'])) {
  62. return false;
  63. }
  64. if (isset($parsedUrl['port']) && $parsedUrl['port']) {
  65. return $this->isTrustedDomain($parsedUrl['host'] . ':' . $parsedUrl['port']);
  66. }
  67. return $this->isTrustedDomain($parsedUrl['host']);
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function isTrustedDomain(string $domainWithPort): bool {
  73. // overwritehost is always trusted
  74. if ($this->config->getSystemValue('overwritehost') !== '') {
  75. return true;
  76. }
  77. $domain = $this->getDomainWithoutPort($domainWithPort);
  78. // Read trusted domains from config
  79. $trustedList = $this->config->getSystemValue('trusted_domains', []);
  80. if (!is_array($trustedList)) {
  81. return false;
  82. }
  83. // Always allow access from localhost
  84. if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) {
  85. return true;
  86. }
  87. // Reject misformed domains in any case
  88. if (strpos($domain, '-') === 0 || strpos($domain, '..') !== false) {
  89. return false;
  90. }
  91. // Match, allowing for * wildcards
  92. foreach ($trustedList as $trusted) {
  93. if (gettype($trusted) !== 'string') {
  94. break;
  95. }
  96. $regex = '/^' . implode('[-\.a-zA-Z0-9]*', array_map(function ($v) {
  97. return preg_quote($v, '/');
  98. }, explode('*', $trusted))) . '$/i';
  99. if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. }