🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(auth): clear stale email codes before sending a new one (#18767)
Steffen Reuther77bd7cb5e449
diff --git a/web/src/__tests__/server/nextauth-email-callback.servertest.ts b/web/src/__tests__/server/nextauth-email-callback.servertest.tsindex b2694aaa6b..b17e3f7f9a 100644--- a/web/src/__tests__/server/nextauth-email-callback.servertest.ts+++ b/web/src/__tests__/server/nextauth-email-callback.servertest.ts@@ -127,6 +127,45 @@ describe("NextAuth signIn callback for the email provider", () => {expect(verifyTurnstileToken).not.toHaveBeenCalled();});+ it("clears the account's earlier codes and all expired codes before a new code is sent", async () => {+ const { email } = await createUser({ password: OLD_PASSWORD });+ const earlierToken = uniqueOtp();+ await insertOtp({ email, token: earlierToken });+ const expiredEmail = `expired-${randomUUID()}@example.com`;+ const expiredToken = uniqueOtp();+ await insertOtp({+ email: expiredEmail,+ token: expiredToken,+ expires: new Date(Date.now() - 1000),+ });+ const otherEmail = `other-${randomUUID()}@example.com`;+ const otherToken = uniqueOtp();+ await insertOtp({ email: otherEmail, token: otherToken });++ const authOptions = await getAuthOptions(undefined, {+ turnstileToken: "reset-token",+ });+ await expect(+ authOptions.callbacks!.signIn!({+ user: { id: email, email },+ account: {+ provider: "email",+ type: "email",+ providerAccountId: email,+ },+ email: { verificationRequest: true },+ }),+ ).resolves.toBe(true);++ await expect(findOtp({ email, token: earlierToken })).resolves.toBeNull();+ await expect(+ findOtp({ email: expiredEmail, token: expiredToken }),+ ).resolves.toBeNull();+ await expect(+ findOtp({ email: otherEmail, token: otherToken }),+ ).resolves.not.toBeNull();+ });+it("rejects the reset-email request when captcha verification fails", async () => {vi.mocked(verifyTurnstileToken).mockResolvedValueOnce(false);const authOptions = await getAuthOptions(undefined, {@@ -233,12 +272,20 @@ function getHeaderText(res: NextApiResponse, name: string) {return Array.isArray(value) ? value.join("\n") : String(value ?? "");}-async function insertOtp({ email, token }: { email: string; token: string }) {+async function insertOtp({+ email,+ token,+ expires = new Date(Date.now() + 3 * 60 * 1000),+}: {+ email: string;+ token: string;+ expires?: Date;+}) {await prisma.verificationToken.create({data: {identifier: email,token: hashEmailOtpToken(token),- expires: new Date(Date.now() + 3 * 60 * 1000),+ expires,},});}diff --git a/web/src/server/auth.ts b/web/src/server/auth.tsindex 75dc2a57b1..8a686491a9 100644--- a/web/src/server/auth.ts+++ b/web/src/server/auth.ts@@ -1131,6 +1131,15 @@ export async function getAuthOptions(},});if (user) {+ // NextAuth stores the new code right after this callback. Codes+ // are hashed without the email and `token` is globally unique, so+ // rows left behind make new codes collide. Only the newest code+ // per account is needed, and expired codes can never be used.+ await prisma.verificationToken.deleteMany({+ where: {+ OR: [{ identifier: email }, { expires: { lt: new Date() } }],+ },+ });return true;}// Add random delay to prevent leaking if user exists as otherwise it would be instant compared to sending an email