Skip to content

Commit c2a4d8d

Browse files
committed
fix(2fa): Add missing BruteForceProtection attribute
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent df86f04 commit c2a4d8d

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

core/Controller/TwoFactorChallengeController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Authentication\TwoFactorAuth\Manager;
1111
use OC_User;
1212
use OCP\AppFramework\Controller;
13+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
1314
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
1415
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1516
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
@@ -162,6 +163,7 @@ public function showChallenge($challengeProviderId, $redirect_url) {
162163
#[UseSession]
163164
#[FrontpageRoute(verb: 'POST', url: '/login/challenge/{challengeProviderId}')]
164165
#[UserRateLimit(limit: 5, period: 100)]
166+
#[BruteForceProtection(action: 'solveChallenge')]
165167
public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
166168
$user = $this->userSession->getUser();
167169
$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
@@ -189,10 +191,12 @@ public function solveChallenge($challengeProviderId, $challenge, $redirect_url =
189191
$uid = $user->getUID();
190192
$this->logger->warning("Two-factor challenge failed: $uid (Remote IP: $ip)");
191193
$this->session->set('two_factor_auth_error', true);
192-
return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
194+
$response = new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
193195
'challengeProviderId' => $provider->getId(),
194196
'redirect_url' => $redirect_url,
195197
]));
198+
$response->throttle(['user' => $uid, 'provider' => $challengeProviderId]);
199+
return $response;
196200
}
197201

198202
#[NoAdminRequired]

tests/Core/Controller/TwoFactorChallengeControllerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ public function testSolveChallengeInvalidProvider(): void {
253253

254254
public function testSolveInvalidChallenge(): void {
255255
$user = $this->createMock(IUser::class);
256+
$user->method('getUID')->willReturn('myuser');
256257
$provider = $this->createMock(IProvider::class);
257258

258259
$this->userSession->expects($this->once())
@@ -282,11 +283,13 @@ public function testSolveInvalidChallenge(): void {
282283
->willReturn('myprovider');
283284

284285
$expected = new RedirectResponse('files/index/url');
286+
$expected->throttle(['user' => 'myuser', 'provider' => 'myprovider']);
285287
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
286288
}
287289

288290
public function testSolveChallengeTwoFactorException(): void {
289291
$user = $this->createMock(IUser::class);
292+
$user->method('getUID')->willReturn('myuser');
290293
$provider = $this->createMock(IProvider::class);
291294
$exception = new TwoFactorException('2FA failed');
292295

@@ -324,6 +327,7 @@ public function testSolveChallengeTwoFactorException(): void {
324327
->willReturn('myprovider');
325328

326329
$expected = new RedirectResponse('files/index/url');
330+
$expected->throttle(['user' => 'myuser', 'provider' => 'myprovider']);
327331
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
328332
}
329333

0 commit comments

Comments
 (0)