1
0

RemoteHostValidator.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security;
  8. use OC\Net\HostnameClassifier;
  9. use OC\Net\IpAddressClassifier;
  10. use OCP\IConfig;
  11. use OCP\Security\IRemoteHostValidator;
  12. use Psr\Log\LoggerInterface;
  13. use function strtolower;
  14. use function substr;
  15. use function urldecode;
  16. /**
  17. * @internal
  18. */
  19. final class RemoteHostValidator implements IRemoteHostValidator {
  20. public function __construct(
  21. private IConfig $config,
  22. private HostnameClassifier $hostnameClassifier,
  23. private IpAddressClassifier $ipAddressClassifier,
  24. private LoggerInterface $logger,
  25. ) {
  26. }
  27. public function isValid(string $host): bool {
  28. if ($this->config->getSystemValueBool('allow_local_remote_servers', false)) {
  29. return true;
  30. }
  31. $host = idn_to_utf8(strtolower(urldecode($host)));
  32. if ($host === false) {
  33. return false;
  34. }
  35. // Remove brackets from IPv6 addresses
  36. if (str_starts_with($host, '[') && str_ends_with($host, ']')) {
  37. $host = substr($host, 1, -1);
  38. }
  39. if ($this->hostnameClassifier->isLocalHostname($host)
  40. || $this->ipAddressClassifier->isLocalAddress($host)) {
  41. $this->logger->warning("Host $host was not connected to because it violates local access rules");
  42. return false;
  43. }
  44. return true;
  45. }
  46. }