Skip to content

Email change: a confirmation mail is sent although the request is rejected with email_address_invalid #2948

Open

Description

@montjeffrey

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When a user who already has an email address asks to change it, and one of the two addresses fails the address check, PUT /user returns 400 email_address_invalid and saves nothing, but the confirmation mail to the other address is still sent. The link in that mail can never be used, because no token was stored.

It needs GOTRUE_MAILER_SECURE_EMAIL_CHANGE_ENABLED=true (the default) and the address check, which is off by default: GOTRUE_MAILER_EMAIL_VALIDATION_EXTENDED=true (configuration.go#L695) or a validation service URL.

I found this while testing #2847. It is the same request, but a different problem from the wrong address in the error message, which #2848 is about.

To Reproduce

Auth built from the Dockerfile at master (10ef107), Postgres 15, a local mail catcher as the SMTP server, GOTRUE_MAILER_EMAIL_VALIDATION_EXTENDED=true, everything else default.

  1. Create a user with a confirmed address that passes the check (I used POST /admin/users with an address at outlook.com) and sign in.
  2. Send the change with an address the check rejects (example.com is on its list of invalid hosts):
PUT /user   {"email":"new.mail@example.com"}

400 {"code":400,"error_code":"email_address_invalid","msg":"Email address \"sa2847.current.mail@outlook.com\" is invalid"}
  1. The mail catcher has received one message in the same second:
Received: from localhost (sa2847-auth.sa2847-net. [172.19.0.4]) by 1b7aa70b03b9 (Mailpit) with SMTP
        for <sa2847.current.mail@outlook.com>; Fri, 20 Nov 2026 06:41:27 +0000 (UTC)
To: sa2847.current.mail@outlook.com
Subject: Confirm your new email address

Follow the link below to confirm new.mail@example.com as your new email address.
  1. The user's row is unchanged: email_change is empty, email_change_sent_at is not set, both email_change_token_* columns are empty.
  2. Opening the link from the mail returns 303 to the site URL with error_code=otp_expired.

The same happens the other way round. A user whose current address fails the check and who asks for an address that passes gets a 400, and the mail goes to the new address.

Repeating the request: three requests in a row gave three mails. email_change_sent_at is only written after a successful send (mail.go#L564), so the wait that mail.go#L528 checks does not apply to these mails. The server-wide limit does: with GOTRUE_RATE_LIMIT_EMAIL_SENT=3 the fourth request returned 429 over_email_send_rate_limit.

For comparison, on the same server:

Request Response Mails received
Anonymous user (no current address), new address rejected 400 0
User with an address, new address rejected 400 1, to the current address
User whose current address is rejected, new address accepted 400 1, to the new address
User with an address, new address accepted 200 2

Expected behavior

No mail is sent when the request is rejected.

Where it comes from

  • templatemailer.go#L257-L324: EmailChangeMail starts one goroutine per address and returns the first error.
  • validateclient.go#L140-L159: the address check runs inside Mail, one address at a time, so the other goroutine passes its own check and sends.
  • mailmeclient.go#L83: DialAndSend takes no context, so the cancel() in EmailChangeMail does not stop a send that has started.
  • mail.go#L555-L564: on an error sendEmailChange returns before it stores the tokens.

System information

  • supabase/auth master at 10ef10789d1ee4fb422b448047e67e3e264c0482, built from its Dockerfile
  • PostgreSQL 15.20, mailpit v1.21 as the SMTP server, Docker Desktop on Windows 11
  • Requests sent over HTTP directly, no client library

Additional context

Not tested: a hosted project, the validation service (GOTRUE_MAILER_EMAIL_VALIDATION_SERVICE_URL), a host that fails the DNS lookup, the Send Email hook, background sending (GOTRUE_MAILER_EMAIL_BACKGROUND_SENDING), a real SMTP provider.

I have a small change with a test that checks both addresses before either mail is sent, and can open a pull request for it. With it the four requests above give 0, 0, 0 and 2 mails.

#2889 changes the same function for a different reason.

I used an AI assistant (Claude Code) to read the code, write the test scripts and help with this write-up. I ran everything on my own machine and checked the output.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions