TrustedDomainHelper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Security;
  25. use OC\AppFramework\Http\Request;
  26. use OCP\IConfig;
  27. /**
  28. * Class TrustedDomain
  29. *
  30. * @package OC\Security
  31. */
  32. class TrustedDomainHelper {
  33. /** @var IConfig */
  34. private $config;
  35. /**
  36. * @param IConfig $config
  37. */
  38. 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($host) {
  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. * Checks whether a domain is considered as trusted from the list
  58. * of trusted domains. If no trusted domains have been configured, returns
  59. * true.
  60. * This is used to prevent Host Header Poisoning.
  61. * @param string $domainWithPort
  62. * @return bool true if the given domain is trusted or if no trusted domains
  63. * have been configured
  64. */
  65. public function isTrustedDomain($domainWithPort) {
  66. $domain = $this->getDomainWithoutPort($domainWithPort);
  67. // Read trusted domains from config
  68. $trustedList = $this->config->getSystemValue('trusted_domains', []);
  69. if (!is_array($trustedList)) {
  70. return false;
  71. }
  72. // Always allow access from localhost
  73. if (preg_match(Request::REGEX_LOCALHOST, $domain) === 1) {
  74. return true;
  75. }
  76. // Reject misformed domains in any case
  77. if (strpos($domain,'-') === 0 || strpos($domain,'..') !== false) {
  78. return false;
  79. }
  80. // Match, allowing for * wildcards
  81. foreach ($trustedList as $trusted) {
  82. if (gettype($trusted) !== 'string') {
  83. break;
  84. }
  85. $regex = '/^' . join('[-\.a-zA-Z0-9]*', array_map(function($v) { return preg_quote($v, '/'); }, explode('*', $trusted))) . '$/';
  86. if (preg_match($regex, $domain) || preg_match($regex, $domainWithPort)) {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. }