-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ratelimit): Allow to bypass rate-limit from bruteforce allowlist
Signed-off-by: Joas Schilling <[email protected]>
- Loading branch information
1 parent
56c4565
commit c0bd1f0
Showing
9 changed files
with
247 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OC\Security\Ip; | ||
|
||
use OCP\IAppConfig; | ||
use OCP\Security\Ip\IFactory; | ||
|
||
class BruteforceAllowList { | ||
/** @var array<string, bool> */ | ||
protected array $ipIsAllowListed = []; | ||
|
||
public function __construct( | ||
private readonly IAppConfig $appConfig, | ||
private readonly IFactory $factory, | ||
) { | ||
} | ||
|
||
/** | ||
* Check if the IP is allowed to bypass bruteforce protection | ||
*/ | ||
public function isBypassListed(string $ip): bool { | ||
if (isset($this->ipIsAllowListed[$ip])) { | ||
return $this->ipIsAllowListed[$ip]; | ||
} | ||
|
||
try { | ||
$address = $this->factory->addressFromString($ip); | ||
} catch (\InvalidArgumentException) { | ||
$this->ipIsAllowListed[$ip] = false; | ||
return false; | ||
} | ||
|
||
$keys = $this->appConfig->getKeys('bruteForce'); | ||
$keys = array_filter($keys, static fn ($key): bool => str_starts_with($key, 'whitelist_')); | ||
|
||
foreach ($keys as $key) { | ||
$rangeString = $this->appConfig->getValueString('bruteForce', $key); | ||
try { | ||
$range = $this->factory->rangeFromString($rangeString); | ||
} catch (\InvalidArgumentException) { | ||
continue; | ||
} | ||
|
||
$allowed = $range->contains($address); | ||
if ($allowed) { | ||
$this->ipIsAllowListed[$ip] = true; | ||
return true; | ||
} | ||
} | ||
|
||
$this->ipIsAllowListed[$ip] = false; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.