TrustedDomainHelper.php 2.8 KB

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