Repository navigation
Description
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.
- Create a user with a confirmed address that passes the check (I used
POST /admin/userswith an address atoutlook.com) and sign in. - Send the change with an address the check rejects (
example.comis 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"}
- 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.
- The user's row is unchanged:
email_changeis empty,email_change_sent_atis not set, bothemail_change_token_*columns are empty. - 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:EmailChangeMailstarts one goroutine per address and returns the first error.validateclient.go#L140-L159: the address check runs insideMail, one address at a time, so the other goroutine passes its own check and sends.mailmeclient.go#L83:DialAndSendtakes no context, so thecancel()inEmailChangeMaildoes not stop a send that has started.mail.go#L555-L564: on an errorsendEmailChangereturns before it stores the tokens.
System information
- supabase/auth
masterat10ef10789d1ee4fb422b448047e67e3e264c0482, built from itsDockerfile - 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