Skip to content
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
fix(loginflow): Fix type error when password could not be decrypted
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed May 2, 2025
commit 9009ef1a4cc64f49baf9633056f7cdc809868b91
14 changes: 9 additions & 5 deletions core/Service/LoginFlowV2Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ public function poll(string $pollToken): LoginFlowV2Credentials {
try {
// Decrypt the apptoken
$privateKey = $this->crypto->decrypt($data->getPrivateKey(), $pollToken);
$appPassword = $this->decryptPassword($data->getAppPassword(), $privateKey);
} catch (\Exception $e) {
} catch (\Exception) {
throw new LoginFlowV2NotFoundException('Apptoken could not be decrypted');
}

$appPassword = $this->decryptPassword($data->getAppPassword(), $privateKey);
if ($appPassword === null) {
throw new LoginFlowV2NotFoundException('Apptoken could not be decrypted');
}

Expand Down Expand Up @@ -251,10 +255,10 @@ private function encryptPassword(string $password, string $publicKey): string {
return $encryptedPassword;
}

private function decryptPassword(string $encryptedPassword, string $privateKey): string {
private function decryptPassword(string $encryptedPassword, string $privateKey): ?string {
$encryptedPassword = base64_decode($encryptedPassword);
openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);
$success = openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING);

return $password;
return $success ? $password : null;
}
}
33 changes: 31 additions & 2 deletions tests/Core/Service/LoginFlowV2ServiceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,14 @@ private function getOpenSSLEncryptedPublicAndPrivateKey(string $appPassword): ar
/*
* Tests for poll
*/

public function testPollApptokenCouldNotBeDecrypted(): void {
public function testPollPrivateKeyCouldNotBeDecrypted(): void {
$this->expectException(LoginFlowV2NotFoundException::class);
$this->expectExceptionMessage('Apptoken could not be decrypted');

$this->crypto->expects($this->once())
->method('decrypt')
->willThrowException(new \Exception('HMAC mismatch'));

/*
* Cannot be mocked, because functions like getLoginName are magic functions.
* To be able to set internal properties, we have to use the real class here.
Expand All @@ -150,6 +153,32 @@ public function testPollApptokenCouldNotBeDecrypted(): void {
$this->subjectUnderTest->poll('');
}

public function testPollApptokenCouldNotBeDecrypted(): void {
$this->expectException(LoginFlowV2NotFoundException::class);
$this->expectExceptionMessage('Apptoken could not be decrypted');

/*
* Cannot be mocked, because functions like getLoginName are magic functions.
* To be able to set internal properties, we have to use the real class here.
*/
[$encrypted, $privateKey,] = $this->getOpenSSLEncryptedPublicAndPrivateKey('test');
$loginFlowV2 = new LoginFlowV2();
$loginFlowV2->setLoginName('test');
$loginFlowV2->setServer('test');
$loginFlowV2->setAppPassword('broken#' . $encrypted);
$loginFlowV2->setPrivateKey('encrypted(test)');

$this->crypto->expects($this->once())
->method('decrypt')
->willReturn($privateKey);

$this->mapper->expects($this->once())
->method('getByPollToken')
->willReturn($loginFlowV2);

$this->subjectUnderTest->poll('test');
}

public function testPollInvalidToken(): void {
$this->expectException(LoginFlowV2NotFoundException::class);
$this->expectExceptionMessage('Invalid token');
Expand Down
Loading