TrustedDomainHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Johannes Ernst <jernst@indiecomputing.com>
  6. * @author Johannes Koenig <mail@jokoenig.de>
  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) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/i';
  93. if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) {
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. }