Skip to content

Commit ef408a7

Browse files
authored
Merge pull request #63741 from nextcloud/backport/63740/stable31
[stable31] fix(2fa): Add missing BruteForceProtection attribute
2 parents ac695d9 + a99c34c commit ef408a7

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;
@@ -161,6 +162,7 @@ public function showChallenge($challengeProviderId, $redirect_url) {
161162
#[NoCSRFRequired]
162163
#[UseSession]
163164
#[FrontpageRoute(verb: 'POST', url: '/login/challenge/{challengeProviderId}')]
165+
#[BruteForceProtection(action: 'solveChallenge')]
164166
public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
165167
$user = $this->userSession->getUser();
166168
$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
@@ -188,10 +190,12 @@ public function solveChallenge($challengeProviderId, $challenge, $redirect_url =
188190
$uid = $user->getUID();
189191
$this->logger->warning("Two-factor challenge failed: $uid (Remote IP: $ip)");
190192
$this->session->set('two_factor_auth_error', true);
191-
return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
193+
$response = new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
192194
'challengeProviderId' => $provider->getId(),
193195
'redirect_url' => $redirect_url,
194196
]));
197+
$response->throttle(['user' => $uid, 'provider' => $challengeProviderId]);
198+
return $response;
195199
}
196200

197201
#[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

@@ -320,6 +323,7 @@ public function testSolveChallengeTwoFactorException(): void {
320323
->willReturn('myprovider');
321324

322325
$expected = new RedirectResponse('files/index/url');
326+
$expected->throttle(['user' => 'myuser', 'provider' => 'myprovider']);
323327
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
324328
}
325329

0 commit comments

Comments
 (0)