Skip to content

fix(auth): normalize sign-in email domains and require exact stored email match - #4341

Merged
richiemcilroy merged 5 commits into
mainfrom
fix/sign-in-email-ascii
Nov 18, 2026
Merged

richiemcilroy merged 5 commits into
mainfrom
fix/sign-in-email-ascii

Conversation

@richiemcilroy

@richiemcilroy richiemcilroy commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Sign-in email identifiers are now always plain ASCII, and every account lookup by email confirms the row it gets back has exactly the requested address.

flowchart LR
  A[typed email] --> B["normalizeSignInEmail<br/>NFKC, lowercase,<br/>domainToASCII"]
  B -- not printable ASCII --> X[400 / sign-in error]
  B --> C[code sent to normalized address]
  C --> D["getUserByEmail"]
  D --> E{"stored email<br/>(lowercased) ==<br/>requested?"}
  E -- yes --> F[session for that account]
  E -- no --> G["null: next-auth creates<br/>an account for the address"]
Loading
  • packages/database/auth/domain-utils.ts:15: normalizeSignInEmail converts the domain with domainToASCII (punycode for internationalized domains) and returns null unless the whole address is printable ASCII, so a non-ASCII local part is refused before any code is sent.
  • apps/web/app/api/mobile/[...route]/route.ts:576 and :629: mobile request and verify use normalizeSignInEmail, the same identifier the web flow, the OTP rate limits and next-auth already use, and return 400 when it is null.
  • packages/database/auth/drizzle-adapter.ts: isSameStoredEmail compares the stored users.email, lowercased, with the requested address.
    • getUserByEmail returns null when they differ. For a punycode domain it also tries the Unicode spelling of the same domain, so accounts stored before this change keep signing in to the same account. That second query only runs for xn-- domains.
    • createUser throws instead of reusing an existing row with a different address. On the SSO path, ON DUPLICATE KEY UPDATE keeps the stored email (email = email) and the re-selected row is checked.
    • useVerificationToken only claims a token whose stored identifier equals the requested one.
  • apps/web/app/api/auth/[...nextauth]/route.ts: an email callback whose email does not normalize redirects to the Verification error before next-auth runs.
  • The checks run on rows already fetched, so lookups and sign-in for ASCII domains make the same number of queries as before.
normalizeSignInEmail(" Person@Example.com ") // "person@example.com"
normalizeSignInEmail("a@straße.de")          // "a@xn--strae-oqa.de"
normalizeSignInEmail("pérson@example.com")   // null

Behaviour notes:

  • Accounts stored with mixed case still sign in (the stored value is lowercased before comparing).
  • Internationalized domains are stored and mailed in punycode form from now on. Existing accounts with the Unicode spelling still match.
  • Addresses with a non-ASCII local part can no longer start an email sign-in.

apps/web/__tests__/unit/email-sign-in-stored-email.test.ts drives the real authOptions() and DrizzleAdapter through next-auth's email request and callback against an in-memory database that compares users.email the way utf8mb4_0900_ai_ci does.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge, with no new blocking issues found.

Summary

Email sign-in now uses ASCII addresses and checks that saved email addresses match before accepting an account. The latest update only makes invisible characters explicit in two test strings, without changing their values.

  • Email sign-in converts international domains and refuses non-ASCII local parts.
  • Account lookups only return users with the matching stored email.

Both earlier findings are addressed. No new issues found.

Reviews (6) · Last reviewed commit: "test(web): write invisible characters in..." · Reviewed by Greptile

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found 2 potential issues (2 medium).

Open in Capy

Comment thread packages/database/auth/domain-utils.ts Resolved
Comment thread packages/database/auth/domain-utils.ts Resolved
Comment thread packages/database/auth/domain-utils.ts Resolved
@vercel
vercel Bot temporarily deployed to Preview – cap-status November 18, 2026 02:25 Inactive
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
cap-status Skipped Skipped Nov 18, 2026 3:18am UTC

@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

Open in Capy

@richiemcilroy

Copy link
Copy Markdown
Member Author

@greptileai please re-review the latest commit.

@vercel
vercel Bot temporarily deployed to Preview – cap-status November 18, 2026 03:18 Inactive

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

View 1 other finding in Capy.

Open in Capy

@richiemcilroy
richiemcilroy merged commit 65155f8 into main Nov 18, 2026
41 checks passed

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – cap-web — 40c9b407 Deployed Nov 18, 2026 by vercel[bot]
Preview – cap-status — 40c9b407 Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant