Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests for nested v4 in v6
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Nov 14, 2022
commit e0d588b3905eb812ebc2e6e87c6bb1d4ad8c1c05
20 changes: 6 additions & 14 deletions lib/private/Http/Client/LocalAddressChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/
namespace OC\Http\Client;

use IPLib\Address\IPv6;
use IPLib\Factory;
use IPLib\ParseStringFlag;
use OCP\Http\Client\LocalServerException;
Expand All @@ -48,7 +49,11 @@ public function ThrowIfLocalIp(string $ip) : void {
return;
}
/* Replace by normalized form */
$ip = (string)$parsedIp;
if ($parsedIp instanceof IPv6) {
$ip = (string)($parsedIp->toIPv4() ?? $parsedIp);
} else {
$ip = (string)$parsedIp;
}

$localRanges = [
'100.64.0.0/10', // See RFC 6598
Expand All @@ -63,19 +68,6 @@ public function ThrowIfLocalIp(string $ip) : void {
$this->logger->warning("Host $ip was not connected to because it violates local access rules");
throw new LocalServerException('Host violates local access rules');
}

// Also check for IPv6 IPv4 nesting, because that's not covered by filter_var
if ((bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($ip, '.') > 0) {
$delimiter = strrpos($ip, ':'); // Get last colon
$ipv4Address = substr($ip, $delimiter + 1);

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

public function ThrowIfLocalAddress(string $uri) : void {
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/Http/Client/LocalAddressCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function dataInternalIPs() : array {
return [
['192.168.0.1'],
['fe80::200:5aee:feaa:20a2'],
['0:0:0:0:0:0:10.0.0.1'],
['0:0:0:0:0:ffff:10.0.0.1'],
['0:0:0:0:0:ffff:127.0.0.0'],
['10.0.0.1'],
['::'],
Expand All @@ -112,7 +112,7 @@ public function dataPreventLocalAddress():array {
['172.16.42.1'],
['[fdf8:f53b:82e4::53]/secret.ics'],
['[fe80::200:5aee:feaa:20a2]/secret.ics'],
['[0:0:0:0:0:0:10.0.0.1]/secret.ics'],
['[0:0:0:0:0:ffff:10.0.0.1]/secret.ics'],
['[0:0:0:0:0:ffff:127.0.0.0]/secret.ics'],
['10.0.0.1'],
['another-host.local'],
Expand Down