openreply icon indicating copy to clipboard operation
openreply copied to clipboard

Magic-link sign-in fails when the mail provider scans links (token spent before the user clicks)

Open jaikathuria opened this issue 1 month ago • 0 comments

What Happened?

Magic-link sign-in fails for mailboxes whose provider scans links in incoming mail (seen with Zoho Mail; Outlook/Microsoft Defender Safe Links, Mimecast, Proofpoint and similar behave the same way). The scanner opens the link before the user does. Because the email callback signs in on a plain GET and spends the one-time VerificationToken, the scanner gets the session and the user's own click lands on "Unable to sign in — The sign in link is no longer valid." Every resend fails the same way, so affected users cannot sign in at all.

Steps To Reproduce

  1. Self-host OpenReply with Resend (or EMAIL_SERVER SMTP) and add a mailbox on a link-scanning provider (e.g. Zoho Mail) to ALLOWED_EMAILS.
  2. Request a sign-in link from /login.
  3. Open the link from the inbox.

Expected Behavior

Opening the link signs the user in.

Actual Behavior

Auth.js logs [auth][error] Verification and shows "The sign in link is no longer valid". The database shows the token was already consumed and a Session row created a few seconds after the email was sent, well before the user's click. Timeline from one deployment (UTC):

Email Session created (token spent) User's click (rejected)
1 18:50:56 18:52:16
2 18:52:46 18:53:04
3 18:53:51 18:54:16

Besides blocking sign-in, this leaves valid sessions in the scanner's hands.

Environment

  • OS: Ubuntu 24.04 (aarch64), single-VM Docker Compose deployment (compose.production.yml)
  • Node version: 20 (node:20-slim image)
  • Database: PostgreSQL 16
  • Redis: 7
  • Browser: any (the failure happens before the user's browser is involved)
  • Auth: next-auth 5.0.0-beta.31, Resend provider

Logs Or Screenshots

[auth][error] Verification: Read more at https://errors.authjs.dev#verification

Suggested Fix

Make a plain visit to the magic link harmless and require an explicit click to spend the token. Scanners fetch pages but do not submit forms.

  1. In proxy.ts, redirect GET /api/auth/callback/(resend|nodemailer)?token=… to a confirmation page unless the request carries confirm=1. This covers both email transports and needs no change to how emails are sent:
// Opening a magic link signs in and spends its one-time token, so mail
// scanners that prefetch links used the token before the user could. Plain
// visits go to a confirmation page instead; only its form submission, marked
// with `confirm=1`, reaches Auth.js.
const EMAIL_CALLBACK = /^\/api\/auth\/callback\/(resend|nodemailer)$/;

export function proxy(request: NextRequest) {
  const pathname = request.nextUrl.pathname;
  const { searchParams } = request.nextUrl;
  const emailCallback = pathname.match(EMAIL_CALLBACK);
  if (
    emailCallback &&
    searchParams.has("token") &&
    searchParams.get("confirm") !== "1"
  ) {
    const confirmUrl = new URL("/verify-request/confirm", request.url);
    confirmUrl.search = searchParams.toString();
    confirmUrl.searchParams.set("provider", emailCallback[1]);
    return NextResponse.redirect(confirmUrl);
  }
  // ...existing logic
}

// and add "/api/auth/callback/:provider" to config.matcher
  1. Add app/verify-request/confirm/page.tsx: a "Confirm sign in" page (marked noindex) whose Sign in button submits a GET form to /api/auth/callback/<provider> with hidden token, email, callbackUrl and confirm=1 fields. Auth.js ignores the extra confirm parameter. The provider is checked against an allow-list (resend, nodemailer) before it is used in the form action.

  2. Add translations for the new strings in lib/i18n/zh-TW.json, plus proxy tests covering: plain visit → redirect with params preserved; confirm=1 → passes through; non-email callbacks untouched.

I have this running in production, with tests, type check and lint passing, and am happy to open a PR.

jaikathuria avatar Oct 22 '26 19:10 jaikathuria