2727
2828use OCP \Http \Client \LocalServerException ;
2929use Psr \Log \LoggerInterface ;
30+ use Symfony \Component \HttpFoundation \IpUtils ;
3031
3132class 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 }
0 commit comments