Skip to content

Commit 8df095f

Browse files
AltahrimAndyScherzinger
authored andcommitted
feat: don't count failed CSRF as failed login attempt
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 52718fe commit 8df095f

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

core/Controller/LoginController.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private function setPasswordResetInitialState(?string $username): void {
232232
$this->canResetPassword($passwordLink, $user)
233233
);
234234
}
235-
235+
236236
/**
237237
* Sets the initial state of whether or not a user is allowed to login with their email
238238
* initial state is passed in the array of 1 for email allowed and 0 for not allowed
@@ -326,7 +326,8 @@ public function tryLogin(Chain $loginChain,
326326
$user,
327327
$user,
328328
$redirect_url,
329-
self::LOGIN_MSG_CSRFCHECKFAILED
329+
self::LOGIN_MSG_CSRFCHECKFAILED,
330+
false,
330331
);
331332
}
332333

@@ -376,7 +377,12 @@ public function tryLogin(Chain $loginChain,
376377
* @return RedirectResponse
377378
*/
378379
private function createLoginFailedResponse(
379-
$user, $originalUser, $redirect_url, string $loginMessage) {
380+
$user,
381+
$originalUser,
382+
$redirect_url,
383+
string $loginMessage,
384+
bool $throttle = true,
385+
) {
380386
// Read current user and append if possible we need to
381387
// return the unmodified user otherwise we will leak the login name
382388
$args = $user !== null ? ['user' => $originalUser, 'direct' => 1] : [];
@@ -386,7 +392,9 @@ private function createLoginFailedResponse(
386392
$response = new RedirectResponse(
387393
$this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)
388394
);
389-
$response->throttle(['user' => substr($user, 0, 64)]);
395+
if ($throttle) {
396+
$response->throttle(['user' => substr($user, 0, 64)]);
397+
}
390398
$this->session->set('loginMessages', [
391399
[$loginMessage], []
392400
]);

tests/Core/Controller/LoginControllerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,6 @@ public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn(): void {
544544
$response = $this->loginController->tryLogin($loginChain, 'Jane', $password, $originalUrl);
545545

546546
$expected = new RedirectResponse('');
547-
$expected->throttle(['user' => 'Jane']);
548547
$this->assertEquals($expected, $response);
549548
}
550549

0 commit comments

Comments
 (0)