88 */
99namespace OC \Security \Normalizer ;
1010
11+ use OCP \IConfig ;
12+
1113/**
1214 * Class IpAddress is used for normalizing IPv4 and IPv6 addresses in security
1315 * relevant contexts in Nextcloud.
@@ -24,7 +26,8 @@ public function __construct(
2426 }
2527
2628 /**
27- * Return the given subnet for an IPv6 address (48 first bits)
29+ * Return the given subnet for an IPv6 address
30+ * Rely on security.ipv6_normalized_subnet_size, defaults to 56
2831 */
2932 private function getIPv6Subnet (string $ ip ): string {
3033 if ($ ip [0 ] === '[ ' && $ ip [-1 ] === '] ' ) { // If IP is with brackets, for example [::1]
@@ -35,10 +38,14 @@ private function getIPv6Subnet(string $ip): string {
3538 $ ip = substr ($ ip , 0 , $ pos - 1 );
3639 }
3740
41+ $ config = \OCP \Server::get (IConfig::class);
42+ $ maskSize = min (64 , $ config ->getSystemValueInt ('security.ipv6_normalized_subnet_size ' , 56 ));
43+ $ maskSize = max (32 , $ maskSize );
44+ $ mask = pack ('VVP ' , (1 << 32 ) - 1 , (1 << $ maskSize - 32 ) - 1 , 0 );
45+
3846 $ binary = \inet_pton ($ ip );
39- $ mask = inet_pton ('FFFF:FFFF:FFFF:: ' );
4047
41- return inet_ntop ($ binary & $ mask ) . '/48 ' ;
48+ return inet_ntop ($ binary & $ mask ) . '/ ' . $ maskSize ;
4249 }
4350
4451 /**
@@ -63,7 +70,7 @@ private function getEmbeddedIpv4(string $ipv6): ?string {
6370
6471
6572 /**
66- * Gets either the /32 (IPv4) or the /48 ( IPv6) subnet of an IP address
73+ * Gets either the /32 (IPv4) or the /56 (default for IPv6) subnet of an IP address
6774 */
6875 public function getSubnet (): string {
6976 if (filter_var ($ this ->ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 )) {
0 commit comments