iTranslated by AI
The Issue of Being Unable to Log In After Updating Email Addresses in Amazon Cognito
Update 2022/05/13
I have confirmed that the solution implemented at the bottom of this article has not been working well recently. After contacting support, it was found that this code may not work correctly depending on the timing of the UpdateUserAttributes completion. If you want to solve this problem at this time, it seems more stable to create your own email address verification mechanism as described in this article.
Getting User-Locked
Hello, this is Hato. I got stuck with Cognito, so I'm sharing this.
I was implementing an email address change function in Cognito. Then I noticed the following:
- After requesting a new email address, you can log in with that new email before verifying it.
- Conversely, you cannot log in with the old email address even though verification hasn't succeeded yet.
- In other words, if you request an incorrect email address, you are stuck at that point (this is apparently called user locking).
It seems that Cognito changes the email address to the new one as soon as updateUserAttributes is executed. However, email_verified is set to false.
Ideally, we would expect the email address to be changed only after the email address verification is finished. This behavior is inconsistent. Because of this behavior, the three phenomena mentioned above occur.
Information Gathering
Searching in Japanese, I found the following two articles. I wonder why there aren't more... Many people must be hitting the same problem.
Found an Issue
This GitHub issue focuses on this problem and should provide a good overview of the information.
In conclusion, it's a problem on the Cognito service side, and no official solution has been provided. The issue above has been ongoing for over three years.
Also, here is a feedback comment from the Amazon team regarding this:
To put it briefly, they acknowledge the problem and say it's on their future TODO list. (While we were looking for a solution, they just told us to wait a bit...)
Two Solutions Proposed by Volunteers in the Issue
- A method to set up an independent verification process, as done by the person in the article I shared first.
- A slightly tricky method using Cognito custom triggers to overwrite the changes. Reference
For the first one, the article I shared initially does exactly the same thing.
I want to share the second one here. However, I should mention beforehand that there's a comment suggesting the second method has a security hole that allows users to verify without actual verification.
There might be a security hole:
@jaga provided an explanation regarding the security hole in the comments. Thanks!
Explanation of the Second Method
Prerequisite Knowledge
Cognito has a feature called triggers, which allows you to insert processes before or after signup, for example. It's like a hook.
One of these is the Custom Message trigger. This is originally provided to customize the email body (or SMS) for email address verification and such. We will use this to suppress the behavior I described earlier.
step 1: Update the email address
This is the process to change the email address on the frontend. Normally, you would only specify email, but here we specify the old email address in custom:validated_email.
const result = await Auth.updateUserAttributes(cognitoUser, {
email: NEW_EMAIL,
'custom:validated_email': CURRENTLY_VALIDATED_OLD_EMAIL,
});
Next, create a Lambda function to handle custom message events. For details on the Custom Message trigger, see here.
As mentioned earlier, the email changes and email_verified becomes false at the moment updateUserAttributes is executed. We will use the custom:validated_email we just set to revert them back.
exports.handler = (event, context, callback) => {
// Check if the user pool ID matches
if(event.userPoolId === "theSpecialUserPool") {
// Identify why was this function invoked
// Note that this becomes UpdateUserAttribute.
if(event.triggerSource === "CustomMessage_UpdateUserAttribute") {
const validated_email = event.request.userAttributes['custom:validated_email'];
const params: AdminUpdateUserAttributesRequest = {
// Revert the two values that Cognito changed automatically.
UserAttributes: [
{
Name: 'email_verified',
Value: 'true',
},
{
Name: 'email',
Value: validated_email,
},
],
UserPoolId: event.userPoolId,
Username: event.userName,
};
const result = await cognitoIdServiceProvider.adminUpdateUserAttributes(params).promise();
// adminUpdateUserAttribute is executed after email address verification. We need to prevent this Lambda from running at that time, so we avoid it here.
if (validated_email === event.request.userAttributes.email) {
throw new Error('failed to prevent sending unnecessary verification code');
}
}
}
callback(null, event);
};
step2: Verify the new email address
This is the part where the user enters the verification code. If successful, update the user information once more. This is because the email is still set to the old one in step 1, so it must be updated. At this time, enter the new email address in both email and custom:validated_email.
const result = await Auth.verifyUserAttributeSubmit(cognitoUser, 'email', code);
if (result === 'SUCCESS') {
const result2 = await Auth.updateUserAttributes(cognitoUser, {
email: NEW_EMAIL, // New email address here
'custom:validated_email': NEW_EMAIL, // and here as well.
});
}
At this point, the Lambda from step 1 will be triggered again (since it's set to fire on updateUserAttributes), but thanks to the following piece of code, nothing specific happens.
if (validated_email === event.request.userAttributes.email) {
throw new Error('failed to prevent sending unnecessary verification code');
}
step3
In the Cognito user pool settings, add custom:validated_email. In the trigger settings, specify the Lambda created in Step 1 for the Custom Message trigger.
That's it! It might be the simplest method since it doesn't require using DynamoDB or anything else.
Finally
I wonder why this has been left unaddressed for so long. I hope for a fundamental solution soon.
Discussion
有益な情報ありがとうございます。
lambdaコード中の
cognitoIdServiceProviderはなどで生成する感じでしょうか?
[追記]
上記の実装方法で解決致しました。失礼しました。
セキュリティホールは、
custom:validated_emailとemailの引数に、任意の同じアドレスをセットすると、検証メールによる検証ステップ飛ばして任意のメールアドレスに変えてしまえる(=アカウントの乗っ取りやなりすましが可能)という脆弱性になりそうですその際の挙動をコードのコメントに追記してみました
参考になれば幸いです & 自分のコメントに疑問点あれば教えて下さい!
なるほど!
新しいメールアドレス申請項目と何かしらの方法でフロントをハックしてCURRENTLY_VALIDATED_OLD_EMAILに同じ第三者のメールアドレスを指定することができれば、検証メールを相手に飛ばさずに自分のメールアドレスをその人のものにすることができるということですね。
となると、ステップ1でフロントで実行してた処理はバックエンドに移した方がよさそうですね。
ですです!それがよりセキュアかなと!
このIssueが2022/6/4にCloseされていますね。
根本的な解決がされたようです。