Skip to content

Commit 624f82f

Browse files
come-ncbackportbot-nextcloud[bot]
authored andcommitted
Add rate limiting on lost password emails
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent f5de979 commit 624f82f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

core/Controller/LostController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
use OCP\IUser;
5454
use OCP\IUserManager;
5555
use OCP\Mail\IMailer;
56-
use OCP\Security\VerificationToken\InvalidTokenException;
5756
use OCP\Security\VerificationToken\IVerificationToken;
5857
use function array_filter;
5958
use function count;
@@ -294,6 +293,12 @@ protected function sendEmail($input) {
294293
throw new ResetPasswordException('Could not send reset e-mail since there is no email for username ' . $input);
295294
}
296295

296+
try {
297+
$this->limiter->registerUserRequest('lostpasswordemail', 5, 1800, $user);
298+
} catch (RateLimitExceededException $e) {
299+
throw new ResetPasswordException('Could not send reset e-mail, 5 of them were already sent in the last 30 minutes', 0, $e);
300+
}
301+
297302
// Generate the token. It is stored encrypted in the database with the
298303
// secret being the users' email address appended with the system secret.
299304
// This makes the token automatically invalidate once the user changes

lib/private/Security/RateLimiting/Limiter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(IBackend $backend) {
4545
/**
4646
* @param string $methodIdentifier
4747
* @param string $userIdentifier
48-
* @param int $period
48+
* @param int $period in seconds
4949
* @param int $limit
5050
* @throws RateLimitExceededException
5151
*/
@@ -66,7 +66,7 @@ private function register(string $methodIdentifier,
6666
*
6767
* @param string $identifier
6868
* @param int $anonLimit
69-
* @param int $anonPeriod
69+
* @param int $anonPeriod in seconds
7070
* @param string $ip
7171
* @throws RateLimitExceededException
7272
*/
@@ -85,7 +85,7 @@ public function registerAnonRequest(string $identifier,
8585
*
8686
* @param string $identifier
8787
* @param int $userLimit
88-
* @param int $userPeriod
88+
* @param int $userPeriod in seconds
8989
* @param IUser $user
9090
* @throws RateLimitExceededException
9191
*/

0 commit comments

Comments
 (0)