diff --git a/apps/web/__tests__/unit/email-sign-in-stored-email.test.ts b/apps/web/__tests__/unit/email-sign-in-stored-email.test.ts new file mode 100644 index 00000000000..f4abf8e1c96 --- /dev/null +++ b/apps/web/__tests__/unit/email-sign-in-stored-email.test.ts @@ -0,0 +1,454 @@ +import { createHash } from "node:crypto"; +import { authOptions } from "@cap/database/auth/auth-options"; +import { normalizeSignInEmail } from "@cap/database/auth/domain-utils"; + +import { is, SQL } from "drizzle-orm"; +import { MySqlDialect } from "drizzle-orm/mysql-core"; +import NextAuth from "next-auth"; +import { decode } from "next-auth/jwt"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +type Row = Record; + +const env = vi.hoisted(() => ({ + NEXTAUTH_SECRET: "email-stored-match-test-secret-with-enough-entropy", + WEB_URL: "https://cap.example", + CAP_ALLOWED_SIGNUP_DOMAINS: undefined as string | undefined, + CAP_BLOCKED_SIGNUP_DOMAINS: undefined as string | undefined, +})); + +const store = vi.hoisted(() => ({ + tables: new Map[]>(), + sentTo: [] as { email: string; code: string }[], + skipNextUserSelect: false, +})); + +vi.mock("@cap/env", () => ({ + serverEnv: () => env, + buildEnv: { NEXT_PUBLIC_IS_CAP: undefined }, +})); + +vi.mock("@cap/utils", async (importOriginal) => ({ + ...(await importOriginal()), + STRIPE_AVAILABLE: () => false, +})); + +vi.mock("@cap/database", () => ({ db: () => collatingDb })); + +vi.mock("@cap/database/auth/sso", () => ({ + validateSsoSignIn: vi.fn(), + provisionSsoMembership: vi.fn(), +})); + +vi.mock("../../../../packages/database/auth/disposable-signup", () => ({ + isDisposableEmailSignup: async () => false, +})); + +vi.mock("../../../../packages/database/loops/queue", () => ({ + enqueueLoopsSync: async () => {}, +})); + +vi.mock("@cap/database/emails/config", () => ({ + getEmailDeliveryStatus: () => ({ configured: true }), + sendEmail: async ({ + email, + react, + }: { + email: string; + react: { code: string }; + }) => { + store.sentTo.push({ email, code: react.code }); + return {}; + }, +})); + +vi.mock("@cap/database/emails/otp-email", () => ({ + OTPEmail: (props: { code: string; email: string }) => props, +})); + +const { DrizzleAdapter } = await import( + "../../../../packages/database/auth/drizzle-adapter" +); +const { organizations, users, verificationTokens } = await import( + "@cap/database/schema" +); + +// Equality as utf8mb4_0900_ai_ci compares these values: case and accents are ignored. +const collationKey = (value: unknown) => + String(value).normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase(); + +const dialect = new MySqlDialect(); + +function matches(row: Row, condition: unknown) { + if (!condition) return true; + const { sql, params } = dialect.sqlToQuery(condition as never); + const columns = [...sql.matchAll(/`(\w+)` = \?/g)].map((match) => match[1]); + if (columns.length !== params.length || / or /i.test(sql)) { + throw new Error(`Unsupported condition in test database: ${sql}`); + } + return columns.every( + (column, index) => + collationKey(row[column as string]) === collationKey(params[index]), + ); +} + +function rowsOf(table: unknown) { + let rows = store.tables.get(table); + if (!rows) { + rows = []; + store.tables.set(table, rows); + } + return rows; +} + +function resultOf(value: T) { + const promise = Promise.resolve(value); + return Object.assign(promise, { for: () => promise }); +} + +const collatingDb = { + select: () => ({ + from: (table: unknown) => { + let rows = [...rowsOf(table)]; + if (table === users && store.skipNextUserSelect) { + store.skipNextUserSelect = false; + rows = []; + } + const query = { + innerJoin: () => { + rows = []; + return query; + }, + where: (condition: unknown) => { + rows = rows.filter((row) => matches(row, condition)); + return query; + }, + limit: (count: number) => resultOf(rows.slice(0, count)), + for: () => resultOf(rows), + }; + return query; + }, + }), + insert: (table: unknown) => ({ + values: (values: Row) => { + const rows = rowsOf(table); + const duplicate = + table === users + ? rows.find( + (row) => collationKey(row.email) === collationKey(values.email), + ) + : undefined; + if (!duplicate) { + rows.push({ ...values }); + return Object.assign(Promise.resolve([{ affectedRows: 1 }]), { + onDuplicateKeyUpdate: async () => [{ affectedRows: 1 }], + }); + } + const rejected = Promise.reject( + new Error("Duplicate entry for key email_idx"), + ); + rejected.catch(() => {}); + return Object.assign(rejected, { + onDuplicateKeyUpdate: async ({ set }: { set: Row }) => { + for (const [column, value] of Object.entries(set)) { + if (!is(value, SQL)) duplicate[column] = value; + } + return [{ affectedRows: 2 }]; + }, + }); + }, + }), + update: (table: unknown) => ({ + set: (patch: Row) => ({ + where: (condition: unknown) => { + const rows = rowsOf(table).filter((row) => matches(row, condition)); + for (const row of rows) Object.assign(row, patch); + return Promise.resolve([{ affectedRows: rows.length }]); + }, + }), + }), + delete: (table: unknown) => ({ + where: (condition: unknown) => { + const rows = rowsOf(table); + const kept = rows.filter((row) => !matches(row, condition)); + const removed = rows.length - kept.length; + rows.splice(0, rows.length, ...kept); + return Promise.resolve([{ affectedRows: removed }]); + }, + }), + transaction: async (run: (tx: unknown) => Promise) => + run(collatingDb), +}; + +const csrfToken = "b".repeat(64); +const csrfCookie = `${csrfToken}|${createHash("sha256") + .update(`${csrfToken}${env.NEXTAUTH_SECRET}`) + .digest("hex")}`; + +async function callNextAuth(request: { + method: "GET" | "POST"; + nextauth: string[]; + query?: Record; + body?: Record; +}) { + let redirect = ""; + const headers = new Map(); + const response = { + status: () => response, + getHeader: (name: string) => headers.get(name.toLowerCase()), + setHeader: (name: string, value: unknown) => { + headers.set(name.toLowerCase(), value); + if (name.toLowerCase() === "location") redirect = String(value); + return response; + }, + json: (body: { url: string }) => { + redirect = body.url; + }, + send: () => {}, + end: () => {}, + }; + await NextAuth( + { + method: request.method, + query: { nextauth: request.nextauth, ...request.query }, + body: request.body, + cookies: { "__Host-next-auth.csrf-token": csrfCookie }, + headers: { host: "cap.example" }, + } as never, + response as never, + authOptions(), + ); + const cookies = [headers.get("set-cookie") ?? []].flat().map(String); + const sessionCookie = cookies + .find((cookie) => cookie.startsWith("next-auth.session-token=")) + ?.split(";")[0] + ?.slice("next-auth.session-token=".length); + return { redirect, sessionCookie }; +} + +const requestCode = (email: string) => + callNextAuth({ + method: "POST", + nextauth: ["signin", "email"], + body: { + email, + csrfToken, + callbackUrl: `${env.WEB_URL}/dashboard`, + json: "true", + }, + }); + +async function signInWithMailedCode(typedEmail: string) { + await requestCode(typedEmail); + const mail = store.sentTo.at(-1); + if (!mail) throw new Error("No code was sent"); + const callbackEmail = normalizeSignInEmail(mail.email) ?? mail.email; + const { redirect, sessionCookie } = await callNextAuth({ + method: "GET", + nextauth: ["callback", "email"], + query: { + email: callbackEmail, + token: mail.code, + callbackUrl: `${env.WEB_URL}/dashboard`, + }, + }); + const session = sessionCookie + ? await decode({ token: sessionCookie, secret: env.NEXTAUTH_SECRET }) + : null; + return { mail, redirect, session }; +} + +function seedUser(id: string, email: string) { + rowsOf(users).push({ + id, + email, + name: "Existing", + emailVerified: new Date(0), + image: null, + activeOrganizationId: "", + }); +} + +beforeEach(() => { + vi.stubEnv("NEXTAUTH_URL", env.WEB_URL); + vi.stubEnv("NODE_ENV", "production"); + vi.spyOn(console, "warn").mockImplementation(() => {}); + vi.spyOn(console, "error").mockImplementation(() => {}); + store.tables.clear(); + store.sentTo = []; + store.skipNextUserSelect = false; +}); + +afterEach(() => vi.unstubAllEnvs()); + +describe("email sign-in against a case- and accent-insensitive users.email", () => { + it("folds case and accents in the test database", async () => { + seedUser("existing", "person@example.com"); + const rows = await collatingDb + .select() + .from(users) + .where( + (await import("drizzle-orm")).eq(users.email, "person@exámple.com"), + ) + .limit(1); + expect(rows.map((row) => row.id)).toEqual(["existing"]); + }); + + it("signs an internationalized domain in under its punycode address", async () => { + seedUser("existing", "person@example.com"); + + const { mail, session } = await signInWithMailedCode("person@exámple.com"); + + expect(session?.id).toBeDefined(); + expect(session?.id).not.toBe("existing"); + expect(session?.email).toBe("person@xn--exmple-qta.com"); + expect(mail.email).toBe("person@xn--exmple-qta.com"); + expect(rowsOf(users).find((row) => row.id === "existing")?.email).toBe( + "person@example.com", + ); + }); + + it("signs an existing account in with its own address", async () => { + seedUser("owner", "owner@example.com"); + + const { redirect, session } = await signInWithMailedCode( + " Owner@Example.com ", + ); + + expect(redirect).toBe(`${env.WEB_URL}/dashboard`); + expect(session?.id).toBe("owner"); + expect(rowsOf(users)).toHaveLength(1); + }); + + it("signs in an account stored with the Unicode spelling of its domain", async () => { + seedUser("unicode-domain", "person@exämple.com"); + + const { session } = await signInWithMailedCode("Person@Exämple.com"); + + expect(session?.id).toBe("unicode-domain"); + expect(rowsOf(users)).toHaveLength(1); + }); + + it("signs in an account stored with mixed case", async () => { + seedUser("mixed", "Mixed.Case@Example.com"); + + const { session } = await signInWithMailedCode("mixed.case@example.com"); + + expect(session?.id).toBe("mixed"); + expect(rowsOf(users)).toHaveLength(1); + }); + + it("refuses an address with a non-ASCII local part before sending a code", async () => { + seedUser("existing", "person@example.com"); + + const { redirect } = await requestCode("pérson@example.com"); + + expect(redirect).toContain("error="); + expect(store.sentTo).toEqual([]); + }); +}); + +describe("DrizzleAdapter email lookups", () => { + const adapter = () => { + const value = authOptions().adapter; + if (!value?.getUserByEmail || !value.createUser) { + throw new Error("Adapter missing email methods"); + } + return value as Required; + }; + + it("returns only the account whose stored email matches exactly", async () => { + seedUser("existing", "person@example.com"); + + expect(await adapter().getUserByEmail("person@exámple.com")).toBeNull(); + expect((await adapter().getUserByEmail("Person@Example.com"))?.id).toBe( + "existing", + ); + }); + + it("does not reuse an account whose stored email differs", async () => { + seedUser("existing", "person@example.com"); + + await expect( + adapter().createUser({ + email: "person@exámple.com", + emailVerified: new Date(), + name: null, + image: null, + }), + ).rejects.toThrow(); + expect(rowsOf(users)).toHaveLength(1); + }); +}); + +describe("DrizzleAdapter stored-email checks", () => { + const ssoIdentity = { + organizationId: "org", + workosOrganizationId: "workos-org", + connectionId: "connection", + profileId: "profile", + email: "pérson@example.com", + }; + + it("keeps the stored email when an SSO insert meets an existing row", async () => { + seedUser("existing", "person@example.com"); + rowsOf(organizations).push({ id: "org" }); + store.skipNextUserSelect = true; + const adapter = DrizzleAdapter(collatingDb as never, { + getSsoIdentity: () => ssoIdentity as never, + }); + + await expect( + adapter.createUser?.({ + email: "pérson@example.com", + emailVerified: new Date(), + name: null, + image: null, + }), + ).rejects.toThrow(); + expect(rowsOf(users)).toEqual([ + expect.objectContaining({ id: "existing", email: "person@example.com" }), + ]); + }); + + it("leaves a verification token stored under a different identifier", async () => { + rowsOf(verificationTokens).push({ + identifier: "person@example.com", + token: "issued", + expires: new Date(Date.now() + 60_000), + }); + const adapter = DrizzleAdapter(collatingDb as never); + + expect( + await adapter.useVerificationToken?.({ + identifier: "pérson@example.com", + token: "other", + }), + ).toBeNull(); + expect(rowsOf(verificationTokens)).toHaveLength(1); + }); +}); + +describe("normalizeSignInEmail", () => { + it.each([ + ["person@exámple.com", "person@xn--exmple-qta.com"], + ["Person@EXÁMPLE.com", "person@xn--exmple-qta.com"], + ["a@straße.de", "a@xn--strae-oqa.de"], + ["a@gmaıl.com", "a@xn--gmal-nza.com"], + [" Person@Example.com ", "person@example.com"], + ["me@cap.example", "me@cap.example"], + ["a@exa\u200Bmple.com", "a@example.com"], + ])("maps %j to %j", (input, expected) => { + expect(normalizeSignInEmail(input)).toBe(expected); + }); + + it.each([ + "pérson@example.com", + "straße@example.com", + "\u202Cperson@example.com", + "per\u0001son@example.com", + "a@exam\u0001ple.com", + ])("refuses %j", (input) => { + expect(normalizeSignInEmail(input)).toBeNull(); + }); +}); diff --git a/apps/web/__tests__/unit/mobile-email-session.test.ts b/apps/web/__tests__/unit/mobile-email-session.test.ts index 1de72212719..55fd1d96bae 100644 --- a/apps/web/__tests__/unit/mobile-email-session.test.ts +++ b/apps/web/__tests__/unit/mobile-email-session.test.ts @@ -343,6 +343,35 @@ describe("mobile email sessions", () => { expect(mocks.getUserByEmail).toHaveBeenLastCalledWith("person@example.com"); }); + it("sends and signs in an accented domain under its punycode form", async () => { + const code = await requestCode("person@exámple.com"); + expect(mocks.sendEmail.mock.calls.at(-1)?.[0]).toMatchObject({ + email: "person@xn--exmple-qta.com", + }); + + const response = await post("verify", { + email: "person@exámple.com", + code, + }); + + expect(response.status).toBe(200); + expect(mocks.getUserByEmail).toHaveBeenLastCalledWith( + "person@xn--exmple-qta.com", + ); + }); + + it("refuses a non-ASCII local part without sending a code", async () => { + const request = await post("request", { email: "pérson@example.com" }); + const verify = await post("verify", { + email: "pérson@example.com", + code: "123456", + }); + + expect(request.status).toBe(400); + expect(verify.status).toBe(400); + expect(mocks.sendEmail).not.toHaveBeenCalled(); + }); + it("uses one bucket per normalized email", async () => { await requestCode(" Person@Example.com "); await requestCode("person@example.com"); diff --git a/apps/web/__tests__/unit/nextauth-email-route.test.ts b/apps/web/__tests__/unit/nextauth-email-route.test.ts index cfe8980f3b9..19d3ecf3254 100644 --- a/apps/web/__tests__/unit/nextauth-email-route.test.ts +++ b/apps/web/__tests__/unit/nextauth-email-route.test.ts @@ -193,6 +193,21 @@ describe("email code verification", () => { expect(mocks.nextAuth.mock.calls[0]?.[0]).toBe(request); }); + it.each(["pérson@example.com", "person@@example.com"])( + "redirects %j to the verification error without checking the code", + async (email) => { + const response = await GET( + emailCallback({ email, token: "123456" }), + context("callback", "email"), + ); + + expect(response.status).toBe(302); + expect(mocks.nextAuth).not.toHaveBeenCalled(); + const location = new URL(response.headers.get("location") ?? ""); + expect(location.searchParams.get("error")).toBe("Verification"); + }, + ); + it("redirects to the verification error without checking the code when limited", async () => { mocks.isRateLimited.mockResolvedValue(true); diff --git a/apps/web/app/api/auth/[...nextauth]/route.ts b/apps/web/app/api/auth/[...nextauth]/route.ts index 91bbebc34d6..0ce346a4ce6 100644 --- a/apps/web/app/api/auth/[...nextauth]/route.ts +++ b/apps/web/app/api/auth/[...nextauth]/route.ts @@ -102,7 +102,11 @@ async function emailCodeLimitResponse( } if (nextauth[0] === "callback") { const query = request.nextUrl.searchParams; - const email = normalizeSignInEmail(query.get("email")); + const rawEmail = query.get("email"); + const email = normalizeSignInEmail(rawEmail); + if (rawEmail && !email) { + return NextResponse.redirect(authErrorUrl("Verification"), 302); + } if ( !email || !query.get("token") || diff --git a/apps/web/app/api/mobile/[...route]/route.ts b/apps/web/app/api/mobile/[...route]/route.ts index 1ee5669f6c9..c34fc7f8f04 100644 --- a/apps/web/app/api/mobile/[...route]/route.ts +++ b/apps/web/app/api/mobile/[...route]/route.ts @@ -4,7 +4,7 @@ import { isDisposableEmailSignup } from "@cap/database/auth/disposable-signup"; import { isEmailAllowedForSignup, isEmailBlockedFromSignup, - normalizeEmailAddress, + normalizeSignInEmail, } from "@cap/database/auth/domain-utils"; import { decrypt, @@ -573,7 +573,8 @@ const requestEmailSession = Effect.fn("Mobile.requestEmailSession")(function* ( rawEmail: string, headers: Headers, ) { - const email = normalizeEmailAddress(rawEmail); + const email = normalizeSignInEmail(rawEmail); + if (!email) return yield* Effect.fail(new HttpApiError.BadRequest()); yield* ensureEmailSignInAllowed(email); yield* ensureAccountDeletionNotPending({ email }); @@ -625,10 +626,10 @@ const verifyEmailSession = Effect.fn("Mobile.verifyEmailSession")(function* ( }: (typeof Mobile.MobileEmailSessionVerifyInput)["Type"], headers: Headers, ) { - const email = normalizeEmailAddress(rawEmail); + const email = normalizeSignInEmail(rawEmail); const code = rawCode.trim(); - if (!emailPattern.test(email) || !emailCodePattern.test(code)) { + if (!email || !emailPattern.test(email) || !emailCodePattern.test(code)) { return yield* Effect.fail(new HttpApiError.BadRequest()); } diff --git a/packages/database/auth/domain-utils.ts b/packages/database/auth/domain-utils.ts index 968b0d10a5a..d5cb13c4aec 100644 --- a/packages/database/auth/domain-utils.ts +++ b/packages/database/auth/domain-utils.ts @@ -1,6 +1,9 @@ +import { domainToASCII } from "node:url"; import { serverEnv } from "@cap/env"; import { z } from "zod"; +const PRINTABLE_ASCII_EMAIL = /^[!-~]+$/; + export function normalizeEmailAddress(email: string): string { return email.normalize("NFKC").trim().toLowerCase(); } @@ -16,7 +19,10 @@ export function normalizeSignInEmail(value: unknown): string | null { const [local, domain] = trimmed.toLowerCase().split("@"); const host = domain?.split(",")[0]; if (!local || !host || !host.includes(".")) return null; - return `${local}@${host}`; + const asciiHost = domainToASCII(host); + const email = `${local}@${asciiHost}`; + if (!asciiHost || !PRINTABLE_ASCII_EMAIL.test(email)) return null; + return email; } export function isEmailAllowedForSignup( diff --git a/packages/database/auth/drizzle-adapter.ts b/packages/database/auth/drizzle-adapter.ts index 3d1b7c05498..a71de60e981 100644 --- a/packages/database/auth/drizzle-adapter.ts +++ b/packages/database/auth/drizzle-adapter.ts @@ -1,7 +1,8 @@ import { createHash } from "node:crypto"; +import { domainToUnicode } from "node:url"; import { STRIPE_AVAILABLE } from "@cap/utils"; import { type ImageUpload, Organisation, User } from "@cap/web-domain"; -import { and, eq } from "drizzle-orm"; +import { and, eq, sql } from "drizzle-orm"; import type { MySql2Database } from "drizzle-orm/mysql2"; import type { Adapter } from "next-auth/adapters"; import { nanoId } from "../helpers.ts"; @@ -32,6 +33,36 @@ type LinkAccountData = Parameters>[0]; type UnlinkAccountData = Parameters>[0]; type UpdateSessionData = Parameters>[0]; +function isSameStoredEmail(storedEmail: string, requestedEmail: string) { + return storedEmail.toLowerCase() === requestedEmail; +} + +// Sign-in addresses are stored with punycode domains, but accounts created +// before that keep the Unicode spelling of the same domain. +function getUnicodeDomainEmail(email: string) { + const at = email.lastIndexOf("@"); + if (at < 1 || !email.includes("xn--", at)) return null; + const domain = domainToUnicode(email.slice(at + 1)); + if (!domain) return null; + const unicodeEmail = `${email.slice(0, at)}@${domain}`; + return unicodeEmail === email ? null : unicodeEmail; +} + +async function findUserByStoredEmail(db: MySql2Database, email: string) { + for (const candidate of [email, getUnicodeDomainEmail(email)]) { + if (!candidate) continue; + const [row] = await db + .select() + .from(users) + .where(eq(users.email, candidate)) + .limit(1); + if (row && isSameStoredEmail(row.email, candidate)) { + return { row, email: candidate }; + } + } + return null; +} + function getProvisionedUserName(email: string) { return ( email @@ -108,6 +139,11 @@ export function DrizzleAdapter( .limit(1); if (existingUser) { + if (!isSameStoredEmail(existingUser.email, normalizedEmail)) { + throw new Error( + "The stored email does not match the requested email.", + ); + } userId = existingUser.id; const userUpdate: Partial = {}; @@ -155,14 +191,19 @@ export function DrizzleAdapter( }); if (ssoIdentity) { await insertUser.onDuplicateKeyUpdate({ - set: { email: normalizedEmail }, + set: { email: sql`${users.email}` }, }); const [createdUser] = await tx - .select({ id: users.id }) + .select({ id: users.id, email: users.email }) .from(users) .where(eq(users.email, normalizedEmail)) .limit(1); if (!createdUser) throw new Error("User was not created."); + if (!isSameStoredEmail(createdUser.email, normalizedEmail)) { + throw new Error( + "The stored email does not match the requested email.", + ); + } userId = createdUser.id; } else { await insertUser; @@ -221,21 +262,13 @@ export function DrizzleAdapter( return row ?? null; }, async getUserByEmail(email) { - const normalizedEmail = email?.toLowerCase() ?? ""; - const rows = await db - .select() - .from(users) - .where(eq(users.email, normalizedEmail)) - .limit(1) - .catch((e) => { - throw e; - }); - const row = rows[0]; - if (!row) return null; + const match = await findUserByStoredEmail(db, email?.toLowerCase() ?? ""); + if (!match) return null; + const { row } = match; if ( !(await hasLinkedAccount(db, row.id)) && - (await hasPendingProvisionedInvite(db, row.id, normalizedEmail)) + (await hasPendingProvisionedInvite(db, row.id, match.email)) ) { return null; } @@ -472,7 +505,7 @@ export function DrizzleAdapter( .where(eq(verificationTokens.identifier, normalizedIdentifier)) .limit(1); const row = rows[0]; - if (!row) { + if (!row || row.identifier.toLowerCase() !== normalizedIdentifier) { console.warn("[useVerificationToken] No token found"); return null; }