From 147d52391340056388a1c6c0d0fb0c6401c92a31 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 28a9acc48f859..5f1ef59068e1c 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 19b7545599fe8b920042590ef2b9608ac7b74730 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 0378fc6c76c1f..e1b85674ed7c0 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -429,7 +429,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() { @@ -451,7 +451,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() { @@ -470,7 +470,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() { @@ -490,7 +490,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() { @@ -509,7 +509,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() { @@ -539,7 +539,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() { @@ -575,7 +575,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() { @@ -603,7 +603,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() {