From a338decf9019c1729a5ea44d4afffd34d83f9279 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sun, 16 Jun 2024 22:05:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=89=EF=B8=8F=20fix:=20email=20address=20e?= =?UTF-8?q?ncoding=20in=20verification=20link=20(#3085)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to #3084 Implements URL encoding for email addresses in verification links and decodes them upon verification. - **Encode email addresses** in `sendVerificationEmail` and `resendVerificationEmail` functions using `encodeURIComponent` to ensure special characters like `+` are correctly handled in the verification link. - **Decode email addresses** in the `verifyEmail` function using `decodeURIComponent` to accurately retrieve and validate the email address from the verification link against the database. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/danny-avila/LibreChat/issues/3084?shareId=9c32df30-4156-4082-a3eb-fff54eaba5b3). --- api/server/services/AuthService.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/server/services/AuthService.js b/api/server/services/AuthService.js index b7ab344884..06dd0d0e72 100644 --- a/api/server/services/AuthService.js +++ b/api/server/services/AuthService.js @@ -62,7 +62,7 @@ const sendVerificationEmail = async (user) => { let verifyToken = crypto.randomBytes(32).toString('hex'); const hash = bcrypt.hashSync(verifyToken, 10); - const verificationLink = `${domains.client}/verify?token=${verifyToken}&email=${user.email}`; + const verificationLink = `${domains.client}/verify?token=${verifyToken}&email=${encodeURIComponent(user.email)}`; await sendEmail({ email: user.email, subject: 'Verify your email', @@ -91,7 +91,7 @@ const sendVerificationEmail = async (user) => { */ const verifyEmail = async (req) => { const { email, token } = req.body; - let emailVerificationData = await Token.findOne({ email }); + let emailVerificationData = await Token.findOne({ email: decodeURIComponent(email) }); if (!emailVerificationData) { logger.warn(`[verifyEmail] [No email verification data found] [Email: ${email}]`); @@ -363,7 +363,7 @@ const resendVerificationEmail = async (req) => { let verifyToken = crypto.randomBytes(32).toString('hex'); const hash = bcrypt.hashSync(verifyToken, 10); - const verificationLink = `${domains.client}/verify?token=${verifyToken}&email=${user.email}`; + const verificationLink = `${domains.client}/verify?token=${verifyToken}&email=${encodeURIComponent(user.email)}`; await sendEmail({ email: user.email,