Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lostpassword): Also rate limit the setPassword endpoint #38267

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function email(string $user): JSONResponse {
}

$user = trim($user);

\OCP\Util::emitHook(
'\OCA\Files_Sharing\API\Server2Server',
'preLoginNameUsedAsUserName',
Expand All @@ -225,16 +225,18 @@ 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) {
/** @var IEncryptionModule $instance */
$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]));
}
}
}
Expand Down Expand Up @@ -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]));
}

/**
Expand Down
16 changes: 8 additions & 8 deletions tests/Core/Controller/LostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down