Skip to content

Commit f96c929

Browse files
come-ncbackportbot[bot]
authored andcommitted
fix(2fa): Add missing BruteForceProtection attribute
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent d2f3985 commit f96c929

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
@@ -11,6 +11,7 @@
1111
use OC\Authentication\TwoFactorAuth\Manager;
1212
use OC_User;
1313
use OCP\AppFramework\Controller;
14+
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
1415
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
1516
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1617
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
@@ -144,6 +145,7 @@ public function showChallenge(string $challengeProviderId, ?string $redirect_url
144145
#[FrontpageRoute(verb: 'POST', url: '/login/challenge/{challengeProviderId}')]
145146
#[TwoFactorSetUpDoneRequired]
146147
#[UserRateLimit(limit: 5, period: 100)]
148+
#[BruteForceProtection(action: 'solveChallenge')]
147149
public function solveChallenge(string $challengeProviderId, string $challenge, ?string $redirect_url = null): RedirectResponse {
148150
$user = $this->userSession->getUser();
149151
$provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
@@ -171,10 +173,12 @@ public function solveChallenge(string $challengeProviderId, string $challenge, ?
171173
$uid = $user->getUID();
172174
$this->logger->warning("Two-factor challenge failed: $uid (Remote IP: $ip)");
173175
$this->session->set('two_factor_auth_error', true);
174-
return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
176+
$response = new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
175177
'challengeProviderId' => $provider->getId(),
176178
'redirect_url' => $redirect_url,
177179
]));
180+
$response->throttle(['user' => $uid, 'provider' => $challengeProviderId]);
181+
return $response;
178182
}
179183

180184
#[NoAdminRequired]

tests/Core/Controller/TwoFactorChallengeControllerTest.php

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

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

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

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

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

@@ -325,6 +328,7 @@ public function testSolveChallengeTwoFactorException(): void {
325328
->willReturn('myprovider');
326329

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

0 commit comments

Comments
 (0)