Skip to content

Commit 7615536

Browse files
authored
Merge pull request #33031 from nextcloud/fix/improve-local-ip-detection
Improve local IP detection
2 parents 9f77aba + c5ffd7c commit 7615536

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/private/Http/Client/LocalAddressChecker.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use OCP\Http\Client\LocalServerException;
2929
use Psr\Log\LoggerInterface;
30+
use Symfony\Component\HttpFoundation\IpUtils;
3031

3132
class LocalAddressChecker {
3233
private LoggerInterface $logger;
@@ -36,7 +37,16 @@ public function __construct(LoggerInterface $logger) {
3637
}
3738

3839
public function ThrowIfLocalIp(string $ip) : void {
39-
if ((bool)filter_var($ip, FILTER_VALIDATE_IP) && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
40+
$localRanges = [
41+
'100.64.0.0/10', // See RFC 6598
42+
'192.0.0.0/24', // See RFC 6890
43+
];
44+
if (
45+
(bool)filter_var($ip, FILTER_VALIDATE_IP) &&
46+
(
47+
!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) ||
48+
IpUtils::checkIp($ip, $localRanges)
49+
)) {
4050
$this->logger->warning("Host $ip was not connected to because it violates local access rules");
4151
throw new LocalServerException('Host violates local access rules');
4252
}
@@ -46,7 +56,9 @@ public function ThrowIfLocalIp(string $ip) : void {
4656
$delimiter = strrpos($ip, ':'); // Get last colon
4757
$ipv4Address = substr($ip, $delimiter + 1);
4858

49-
if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
59+
if (
60+
!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) ||
61+
IpUtils::checkIp($ip, $localRanges)) {
5062
$this->logger->warning("Host $ip was not connected to because it violates local access rules");
5163
throw new LocalServerException('Host violates local access rules');
5264
}

tests/lib/Http/Client/LocalAddressCheckerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public function dataInternalIPs() : array {
9696
['10.0.0.1'],
9797
['::'],
9898
['::1'],
99+
['100.100.100.200'],
100+
['192.0.0.1'],
99101
];
100102
}
101103

@@ -116,6 +118,9 @@ public function dataPreventLocalAddress():array {
116118
['another-host.local'],
117119
['service.localhost'],
118120
['!@#$'], // test invalid url
121+
['100.100.100.200'],
122+
['192.0.0.1'],
123+
['randomdomain.internal'],
119124
];
120125
}
121126

0 commit comments

Comments
 (0)