diff options
author | Kate <26026535+provokateurin@users.noreply.github.com> | 2025-04-17 09:03:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-17 09:03:04 +0200 |
commit | f0154a75cacfa3beb06adc0440ade02fee9b8657 (patch) | |
tree | 0990eacd1ebfde1e6f8d580ccae8480c46468ce8 /lib | |
parent | f34466c99e10a5972598a4a352c25e61ad2fc306 (diff) | |
parent | 9f666c2b734f5947aaec84fb3f97d51a9d9db9b0 (diff) | |
download | nextcloud-server-f0154a75cacfa3beb06adc0440ade02fee9b8657.tar.gz nextcloud-server-f0154a75cacfa3beb06adc0440ade02fee9b8657.zip |
Merge pull request #52223 from nextcloud/feat/add-configurable-ipv6-subnet
feat(security): add configurable IPv6 subnet for BFP and throttling
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Security/Normalizer/IpAddress.php | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/private/Security/Normalizer/IpAddress.php b/lib/private/Security/Normalizer/IpAddress.php index 6ef8763f6d9..e40dc9ba96b 100644 --- a/lib/private/Security/Normalizer/IpAddress.php +++ b/lib/private/Security/Normalizer/IpAddress.php @@ -8,6 +8,8 @@ declare(strict_types=1); */ namespace OC\Security\Normalizer; +use OCP\IConfig; + /** * Class IpAddress is used for normalizing IPv4 and IPv6 addresses in security * relevant contexts in Nextcloud. @@ -24,7 +26,8 @@ class IpAddress { } /** - * Return the given subnet for an IPv6 address (48 first bits) + * Return the given subnet for an IPv6 address + * Rely on security.ipv6_normalized_subnet_size, defaults to 56 */ private function getIPv6Subnet(string $ip): string { if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1] @@ -35,10 +38,14 @@ class IpAddress { $ip = substr($ip, 0, $pos - 1); } + $config = \OCP\Server::get(IConfig::class); + $maskSize = min(64, $config->getSystemValueInt('security.ipv6_normalized_subnet_size', 56)); + $maskSize = max(32, $maskSize); + $mask = pack('VVP', (1 << 32) - 1, (1 << $maskSize - 32) - 1, 0); + $binary = \inet_pton($ip); - $mask = inet_pton('FFFF:FFFF:FFFF::'); - return inet_ntop($binary & $mask) . '/48'; + return inet_ntop($binary & $mask) . '/' . $maskSize; } /** @@ -63,7 +70,7 @@ class IpAddress { /** - * Gets either the /32 (IPv4) or the /48 (IPv6) subnet of an IP address + * Gets either the /32 (IPv4) or the /56 (default for IPv6) subnet of an IP address */ public function getSubnet(): string { if (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |