1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2021, Lukas Reschke <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author Lukas Reschke <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
namespace OC\Http\Client; |
27
|
|
|
|
28
|
|
|
use IPLib\Address\IPv6; |
29
|
|
|
use IPLib\Factory; |
30
|
|
|
use IPLib\ParseStringFlag; |
31
|
|
|
use OCP\Http\Client\LocalServerException; |
32
|
|
|
use Psr\Log\LoggerInterface; |
33
|
|
|
use Symfony\Component\HttpFoundation\IpUtils; |
34
|
|
|
|
35
|
|
|
class LocalAddressChecker { |
36
|
|
|
private LoggerInterface $logger; |
37
|
|
|
|
38
|
|
|
public function __construct(LoggerInterface $logger) { |
39
|
|
|
$this->logger = $logger; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function throwIfLocalIp(string $ip) : void { |
43
|
|
|
$parsedIp = Factory::parseAddressString( |
44
|
|
|
$ip, |
45
|
|
|
ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED |
46
|
|
|
); |
47
|
|
|
if ($parsedIp === null) { |
48
|
|
|
/* Not an IP */ |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
/* Replace by normalized form */ |
52
|
|
|
if ($parsedIp instanceof IPv6) { |
53
|
|
|
$ip = (string)($parsedIp->toIPv4() ?? $parsedIp); |
54
|
|
|
} else { |
55
|
|
|
$ip = (string)$parsedIp; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$localRanges = [ |
59
|
|
|
'100.64.0.0/10', // See RFC 6598 |
60
|
|
|
'192.0.0.0/24', // See RFC 6890 |
61
|
|
|
]; |
62
|
|
|
if ( |
63
|
|
|
(bool)filter_var($ip, FILTER_VALIDATE_IP) && |
64
|
|
|
( |
65
|
|
|
!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) || |
66
|
|
|
IpUtils::checkIp($ip, $localRanges) |
67
|
|
|
)) { |
68
|
|
|
$this->logger->warning("Host $ip was not connected to because it violates local access rules"); |
69
|
|
|
throw new LocalServerException('Host violates local access rules'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function throwIfLocalAddress(string $uri) : void { |
74
|
|
|
$host = parse_url($uri, PHP_URL_HOST); |
75
|
|
|
if ($host === false || $host === null) { |
76
|
|
|
$this->logger->warning("Could not detect any host in $uri"); |
77
|
|
|
throw new LocalServerException('Could not detect any host'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$host = idn_to_utf8(strtolower(urldecode($host))); |
81
|
|
|
// Remove brackets from IPv6 addresses |
82
|
|
|
if (strpos($host, '[') === 0 && substr($host, -1) === ']') { |
83
|
|
|
$host = substr($host, 1, -1); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Disallow local network top-level domains from RFC 6762 |
87
|
|
|
$localTopLevelDomains = ['local','localhost','intranet','internal','private','corp','home','lan']; |
88
|
|
|
$topLevelDomain = substr((strrchr($host, '.') ?: ''), 1); |
89
|
|
|
if (in_array($topLevelDomain, $localTopLevelDomains)) { |
90
|
|
|
$this->logger->warning("Host $host was not connected to because it violates local access rules"); |
91
|
|
|
throw new LocalServerException('Host violates local access rules'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Disallow hostname only |
95
|
|
|
if (substr_count($host, '.') === 0 && !(bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
96
|
|
|
$this->logger->warning("Host $host was not connected to because it violates local access rules"); |
97
|
|
|
throw new LocalServerException('Host violates local access rules'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->throwIfLocalIp($host); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|