LocalAddressChecker.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  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
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Http\Client;
  25. use OCP\Http\Client\LocalServerException;
  26. use Psr\Log\LoggerInterface;
  27. class LocalAddressChecker {
  28. private LoggerInterface $logger;
  29. public function __construct(LoggerInterface $logger) {
  30. $this->logger = $logger;
  31. }
  32. public function ThrowIfLocalIp(string $ip) : void {
  33. if ((bool)filter_var($ip, FILTER_VALIDATE_IP) && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  34. $this->logger->warning("Host $ip was not connected to because it violates local access rules");
  35. throw new LocalServerException('Host violates local access rules');
  36. }
  37. // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var
  38. if ((bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($ip, '.') > 0) {
  39. $delimiter = strrpos($ip, ':'); // Get last colon
  40. $ipv4Address = substr($ip, $delimiter + 1);
  41. if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  42. $this->logger->warning("Host $ip was not connected to because it violates local access rules");
  43. throw new LocalServerException('Host violates local access rules');
  44. }
  45. }
  46. }
  47. public function ThrowIfLocalAddress(string $uri) : void {
  48. $host = parse_url($uri, PHP_URL_HOST);
  49. if ($host === false || $host === null) {
  50. $this->logger->warning("Could not detect any host in $uri");
  51. throw new LocalServerException('Could not detect any host');
  52. }
  53. $host = strtolower($host);
  54. // Remove brackets from IPv6 addresses
  55. if (strpos($host, '[') === 0 && substr($host, -1) === ']') {
  56. $host = substr($host, 1, -1);
  57. }
  58. // Disallow local network top-level domains from RFC 6762
  59. $localTopLevelDomains = ['local','localhost','intranet','internal','private','corp','home','lan'];
  60. $topLevelDomain = substr((strrchr($host, '.') ?: ''), 1);
  61. if (in_array($topLevelDomain, $localTopLevelDomains)) {
  62. $this->logger->warning("Host $host was not connected to because it violates local access rules");
  63. throw new LocalServerException('Host violates local access rules');
  64. }
  65. // Disallow hostname only
  66. if (substr_count($host, '.') === 0 && !(bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
  67. $this->logger->warning("Host $host was not connected to because it violates local access rules");
  68. throw new LocalServerException('Host violates local access rules');
  69. }
  70. $this->ThrowIfLocalIp($host);
  71. }
  72. }