Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

use OC\Authentication\TwoFactorAuth\Manager;
use OC\Core\Exception\ResetPasswordException;
use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
Expand Down Expand Up @@ -91,6 +93,8 @@ class LostController extends Controller {
private $initialStateService;
/** @var IVerificationToken */
private $verificationToken;
/** @var Limiter */
private $limiter;

public function __construct(
$appName,
Expand All @@ -106,7 +110,8 @@ public function __construct(
ILogger $logger,
Manager $twoFactorManager,
IInitialStateService $initialStateService,
IVerificationToken $verificationToken
IVerificationToken $verificationToken,
Limiter $limiter
) {
parent::__construct($appName, $request);
$this->urlGenerator = $urlGenerator;
Expand All @@ -121,6 +126,7 @@ public function __construct(
$this->twoFactorManager = $twoFactorManager;
$this->initialStateService = $initialStateService;
$this->verificationToken = $verificationToken;
$this->limiter = $limiter;
}

/**
Expand Down Expand Up @@ -294,6 +300,12 @@ protected function sendEmail($input) {
throw new ResetPasswordException('Could not send reset e-mail since there is no email for username ' . $input);
}

try {
$this->limiter->registerUserRequest('lostpasswordemail', 5, 1800, $user);
} catch (RateLimitExceededException $e) {
throw new ResetPasswordException('Could not send reset e-mail, 5 of them were already sent in the last 30 minutes', 0, $e);
}

// Generate the token. It is stored encrypted in the database with the
// secret being the users' email address appended with the system secret.
// This makes the token automatically invalidate once the user changes
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Security/RateLimiting/Limiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(IBackend $backend) {
/**
* @param string $methodIdentifier
* @param string $userIdentifier
* @param int $period
* @param int $period in seconds
* @param int $limit
* @throws RateLimitExceededException
*/
Expand All @@ -66,7 +66,7 @@ private function register(string $methodIdentifier,
*
* @param string $identifier
* @param int $anonLimit
* @param int $anonPeriod
* @param int $anonPeriod in seconds
* @param string $ip
* @throws RateLimitExceededException
*/
Expand All @@ -85,7 +85,7 @@ public function registerAnonRequest(string $identifier,
*
* @param string $identifier
* @param int $userLimit
* @param int $userPeriod
* @param int $userPeriod in seconds
* @param IUser $user
* @throws RateLimitExceededException
*/
Expand Down
9 changes: 7 additions & 2 deletions tests/Core/Controller/LostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use OC\Authentication\TwoFactorAuth\Manager;
use OC\Core\Controller\LostController;
use OC\Mail\Message;
use OC\Security\RateLimiting\Limiter;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults;
Expand All @@ -41,14 +42,14 @@
use OCP\Mail\IMailer;
use OCP\Security\VerificationToken\InvalidTokenException;
use OCP\Security\VerificationToken\IVerificationToken;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Class LostControllerTest
*
* @package OC\Core\Controller
*/
class LostControllerTest extends \Test\TestCase {

/** @var LostController */
private $lostController;
/** @var IUser */
Expand Down Expand Up @@ -77,6 +78,8 @@ class LostControllerTest extends \Test\TestCase {
private $initialStateService;
/** @var IVerificationToken|\PHPUnit\Framework\MockObject\MockObject */
private $verificationToken;
/** @var Limiter|MockObject */
private $limiter;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -129,6 +132,7 @@ protected function setUp(): void {
$this->twofactorManager = $this->createMock(Manager::class);
$this->initialStateService = $this->createMock(IInitialStateService::class);
$this->verificationToken = $this->createMock(IVerificationToken::class);
$this->limiter = $this->createMock(Limiter::class);
$this->lostController = new LostController(
'Core',
$this->request,
Expand All @@ -143,7 +147,8 @@ protected function setUp(): void {
$this->logger,
$this->twofactorManager,
$this->initialStateService,
$this->verificationToken
$this->verificationToken,
$this->limiter
);
}

Expand Down