25
25
*/
26
26
namespace OC \Http \Client ;
27
27
28
+ use IPLib \Address \IPv6 ;
29
+ use IPLib \Factory ;
30
+ use IPLib \ParseStringFlag ;
28
31
use OCP \Http \Client \LocalServerException ;
29
32
use Psr \Log \LoggerInterface ;
30
33
use Symfony \Component \HttpFoundation \IpUtils ;
@@ -37,6 +40,21 @@ public function __construct(LoggerInterface $logger) {
37
40
}
38
41
39
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
+
40
58
$ localRanges = [
41
59
'100.64.0.0/10 ' , // See RFC 6598
42
60
'192.0.0.0/24 ' , // See RFC 6890
@@ -50,19 +68,6 @@ public function ThrowIfLocalIp(string $ip) : void {
50
68
$ this ->logger ->warning ("Host $ ip was not connected to because it violates local access rules " );
51
69
throw new LocalServerException ('Host violates local access rules ' );
52
70
}
53
-
54
- // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var
55
- if ((bool )filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 ) && substr_count ($ ip , '. ' ) > 0 ) {
56
- $ delimiter = strrpos ($ ip , ': ' ); // Get last colon
57
- $ ipv4Address = substr ($ ip , $ delimiter + 1 );
58
-
59
- if (
60
- !filter_var ($ ipv4Address , FILTER_VALIDATE_IP , FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ||
61
- IpUtils::checkIp ($ ip , $ localRanges )) {
62
- $ this ->logger ->warning ("Host $ ip was not connected to because it violates local access rules " );
63
- throw new LocalServerException ('Host violates local access rules ' );
64
- }
65
- }
66
71
}
67
72
68
73
public function ThrowIfLocalAddress (string $ uri ) : void {
@@ -72,7 +77,7 @@ public function ThrowIfLocalAddress(string $uri) : void {
72
77
throw new LocalServerException ('Could not detect any host ' );
73
78
}
74
79
75
- $ host = strtolower ($ host );
80
+ $ host = idn_to_utf8 ( strtolower (urldecode ( $ host)) );
76
81
// Remove brackets from IPv6 addresses
77
82
if (strpos ($ host , '[ ' ) === 0 && substr ($ host , -1 ) === '] ' ) {
78
83
$ host = substr ($ host , 1 , -1 );
0 commit comments