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.

110 lines
3.0 KiB

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