From 83b910942536686ef61202c22a2d8d31fe8d480d 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 | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index 90fddb825eede..ef388dbd41011 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -247,11 +247,13 @@ public function email($user) { /** * @PublicPage + * @BruteForceProtection(action=passwordResetEmail) + * @AnonRateThrottle(limit=10, period=300) * @param string $token * @param string $userId * @param string $password * @param boolean $proceed - * @return array + * @return JSONResponse */ public function setPassword($token, $userId, $password, $proceed) { if ($this->encryptionManager->isEnabled() && !$proceed) { @@ -261,7 +263,7 @@ public function setPassword($token, $userId, $password, $proceed) { $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])); } } } @@ -283,12 +285,16 @@ public function setPassword($token, $userId, $password, $proceed) { $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 8edb6b3e65597666cc3a6b862eb0c076df4f8fe1 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 6eab4722d12d9..0f64504c58faf 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -430,7 +430,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() { @@ -452,7 +452,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() { @@ -471,7 +471,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() { @@ -491,7 +491,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() { @@ -510,7 +510,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() { @@ -540,7 +540,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() { @@ -576,7 +576,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() { @@ -604,7 +604,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() {