Skip to content

Commit 43f2183

Browse files
authored
Merge pull request #63817 from nextcloud/backport/63744/stable27
[stable27] fix(2fa): Add missing BruteForceProtection attribute
2 parents a910389 + 6efdd52 commit 43f2183

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
@@ -28,6 +28,7 @@
2828
use OC\Authentication\TwoFactorAuth\Manager;
2929
use OC_User;
3030
use OCP\AppFramework\Controller;
31+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
3132
use OCP\AppFramework\Http\Attribute\UseSession;
3233
use OCP\AppFramework\Http\RedirectResponse;
3334
use OCP\AppFramework\Http\StandaloneTemplateResponse;
@@ -172,6 +173,7 @@ public function showChallenge($challengeProviderId, $redirect_url) {
172173
* @return RedirectResponse
173174
*/
174175
#[UseSession]
176+
#[BruteForceProtection(action: 'solveChallenge')]
175177
public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
176178
$user = $this->userSession->getUser();
177179
$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
@@ -199,10 +201,12 @@ public function solveChallenge($challengeProviderId, $challenge, $redirect_url =
199201
$uid = $user->getUID();
200202
$this->logger->warning("Two-factor challenge failed: $uid (Remote IP: $ip)");
201203
$this->session->set('two_factor_auth_error', true);
202-
return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
204+
$response = new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
203205
'challengeProviderId' => $provider->getId(),
204206
'redirect_url' => $redirect_url,
205207
]));
208+
$response->throttle(['user' => $uid, 'provider' => $challengeProviderId]);
209+
return $response;
206210
}
207211

208212
/**

tests/Core/Controller/TwoFactorChallengeControllerTest.php

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

268268
public function testSolveInvalidChallenge() {
269269
$user = $this->createMock(IUser::class);
270+
$user->method('getUID')->willReturn('myuser');
270271
$provider = $this->createMock(IProvider::class);
271272

272273
$this->userSession->expects($this->once())
@@ -296,11 +297,13 @@ public function testSolveInvalidChallenge() {
296297
->willReturn('myprovider');
297298

298299
$expected = new RedirectResponse('files/index/url');
300+
$expected->throttle(['user' => 'myuser', 'provider' => 'myprovider']);
299301
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
300302
}
301303

302304
public function testSolveChallengeTwoFactorException() {
303305
$user = $this->createMock(IUser::class);
306+
$user->method('getUID')->willReturn('myuser');
304307
$provider = $this->createMock(IProvider::class);
305308
$exception = new TwoFactorException("2FA failed");
306309

@@ -334,6 +337,7 @@ public function testSolveChallengeTwoFactorException() {
334337
->willReturn('myprovider');
335338

336339
$expected = new RedirectResponse('files/index/url');
340+
$expected->throttle(['user' => 'myuser', 'provider' => 'myprovider']);
337341
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
338342
}
339343

0 commit comments

Comments
 (0)