From 7ee81b65557c84c764a9014a0442c9915c82a9a8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 15 May 2023 09:21:07 +0200 Subject: [PATCH 1/2] fix(lostpassword): Also rate limit the setPassword endpoint Signed-off-by: Joas Schilling --- core/Controller/LostController.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index 51ab8d85a6eb7..127c6310f6b3e 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -201,7 +201,7 @@ public function email(string $user): JSONResponse { } $user = trim($user); - + \OCP\Util::emitHook( '\OCA\Files_Sharing\API\Server2Server', 'preLoginNameUsedAsUserName', @@ -225,8 +225,10 @@ public function email(string $user): JSONResponse { /** * @PublicPage + * @BruteForceProtection(action=passwordResetEmail) + * @AnonRateThrottle(limit=10, period=300) */ - public function setPassword(string $token, string $userId, string $password, bool $proceed): array { + public function setPassword(string $token, string $userId, string $password, bool $proceed): JSONResponse { if ($this->encryptionManager->isEnabled() && !$proceed) { $encryptionModules = $this->encryptionManager->getEncryptionModules(); foreach ($encryptionModules as $module) { @@ -234,7 +236,7 @@ public function setPassword(string $token, string $userId, string $password, boo $instance = call_user_func($module['callback']); // this way we can find out whether per-user keys are used or a system wide encryption key if ($instance->needDetailedAccessList()) { - return $this->error('', ['encryption' => true]); + return new JSONResponse($this->error('', ['encryption' => true])); } } } @@ -262,12 +264,16 @@ public function setPassword(string $token, string $userId, string $password, boo $this->config->deleteUserValue($userId, 'core', 'lostpassword'); @\OC::$server->getUserSession()->unsetMagicInCookie(); } catch (HintException $e) { - return $this->error($e->getHint()); + $response = new JSONResponse($this->error($e->getHint())); + $response->throttle(); + return $response; } catch (Exception $e) { - return $this->error($e->getMessage()); + $response = new JSONResponse($this->error($e->getMessage())); + $response->throttle(); + return $response; } - return $this->success(['user' => $userId]); + return new JSONResponse($this->success(['user' => $userId])); } /** From 33385d7ecb892a6674caf18647e5f2a6394d70b9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 15 May 2023 16:12:14 +0200 Subject: [PATCH 2/2] fix(tests): Adjust unit tests Signed-off-by: Joas Schilling --- tests/Core/Controller/LostControllerTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 1481a1e46d469..84ec450943ea6 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -449,7 +449,7 @@ public function testSetPasswordUnsuccessful() { $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); $expectedResponse = ['status' => 'error', 'msg' => '']; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSetPasswordSuccessful() { @@ -477,7 +477,7 @@ public function testSetPasswordSuccessful() { $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true); $expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success']; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSetPasswordExpiredToken() { @@ -496,7 +496,7 @@ public function testSetPasswordExpiredToken() { 'status' => 'error', 'msg' => 'Could not reset password because the token is expired', ]; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSetPasswordInvalidDataInDb() { @@ -516,7 +516,7 @@ public function testSetPasswordInvalidDataInDb() { 'status' => 'error', 'msg' => 'Could not reset password because the token is invalid', ]; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testIsSetPasswordWithoutTokenFailing() { @@ -535,7 +535,7 @@ public function testIsSetPasswordWithoutTokenFailing() { 'status' => 'error', 'msg' => 'Could not reset password because the token is invalid' ]; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSetPasswordForDisabledUser() { @@ -565,7 +565,7 @@ public function testSetPasswordForDisabledUser() { 'status' => 'error', 'msg' => 'Could not reset password because the token is invalid' ]; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSendEmailNoEmail() { @@ -601,7 +601,7 @@ public function testSetPasswordEncryptionDontProceedPerUserKey() { }]]); $response = $this->lostController->setPassword('myToken', 'user', 'newpass', false); $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true]; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testSetPasswordDontProceedMasterKey() { @@ -629,7 +629,7 @@ public function testSetPasswordDontProceedMasterKey() { $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', false); $expectedResponse = ['user' => 'ValidTokenUser', 'status' => 'success']; - $this->assertSame($expectedResponse, $response); + $this->assertSame($expectedResponse, $response->getData()); } public function testTwoUsersWithSameEmail() {