Skip to content

Commit 92b0691

Browse files
Merge pull request #54974 from nextcloud/backport/54653/stable30
[stable30] fix: Avoid internal error when logging in with the wrong account to verify email address
2 parents 0c51823 + 4d777f7 commit 92b0691

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

apps/provisioning_api/lib/Controller/VerificationController.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCA\Provisioning_API\Controller;
1111

12-
use InvalidArgumentException;
1312
use OC\Security\Crypto;
1413
use OCP\Accounts\IAccountManager;
1514
use OCP\AppFramework\Controller;
@@ -18,6 +17,7 @@
1817
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1918
use OCP\AppFramework\Http\Attribute\OpenAPI;
2019
use OCP\AppFramework\Http\TemplateResponse;
20+
use OCP\HintException;
2121
use OCP\IL10N;
2222
use OCP\IRequest;
2323
use OCP\IUserManager;
@@ -66,11 +66,21 @@ public function __construct(
6666
#[NoAdminRequired]
6767
#[NoCSRFRequired]
6868
public function showVerifyMail(string $token, string $userId, string $key): TemplateResponse {
69-
if ($this->userSession->getUser()->getUID() !== $userId) {
70-
// not a public page, hence getUser() must return an IUser
71-
throw new InvalidArgumentException('Logged in account is not mail address owner');
69+
try {
70+
if ($this->userSession->getUser()?->getUID() !== $userId) {
71+
// not a public page, hence getUser() must return an IUser
72+
throw new HintException(
73+
'Logged in account is not mail address owner',
74+
$this->l10n->t('Logged in account is not mail address owner'),
75+
);
76+
}
77+
$email = $this->crypto->decrypt($key);
78+
} catch (HintException $e) {
79+
return new TemplateResponse(
80+
'core', 'error', [
81+
'errors' => [['error' => $e->getHint()]]
82+
], TemplateResponse::RENDER_AS_GUEST);
7283
}
73-
$email = $this->crypto->decrypt($key);
7484

7585
return new TemplateResponse(
7686
'core', 'confirmation', [
@@ -88,8 +98,11 @@ public function showVerifyMail(string $token, string $userId, string $key): Temp
8898
public function verifyMail(string $token, string $userId, string $key): TemplateResponse {
8999
$throttle = false;
90100
try {
91-
if ($this->userSession->getUser()->getUID() !== $userId) {
92-
throw new InvalidArgumentException('Logged in account is not mail address owner');
101+
if ($this->userSession->getUser()?->getUID() !== $userId) {
102+
throw new HintException(
103+
'Logged in account is not mail address owner',
104+
$this->l10n->t('Logged in account is not mail address owner'),
105+
);
93106
}
94107
$email = $this->crypto->decrypt($key);
95108
$ref = \substr(hash('sha256', $email), 0, 8);
@@ -102,7 +115,10 @@ public function verifyMail(string $token, string $userId, string $key): Template
102115
->getPropertyByValue($email);
103116

104117
if ($emailProperty === null) {
105-
throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
118+
throw new HintException(
119+
'Email was already removed from account and cannot be confirmed anymore.',
120+
$this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'),
121+
);
106122
}
107123
$emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
108124
$this->accountManager->updateAccount($userAccount);
@@ -114,8 +130,8 @@ public function verifyMail(string $token, string $userId, string $key): Template
114130
$throttle = true;
115131
$error = $this->l10n->t('Could not verify mail because the token is invalid.');
116132
}
117-
} catch (InvalidArgumentException $e) {
118-
$error = $e->getMessage();
133+
} catch (HintException $e) {
134+
$error = $e->getHint();
119135
} catch (\Exception $e) {
120136
$error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
121137
}

0 commit comments

Comments
 (0)