Skip to content

fix(mailer): validate both addresses before sending email change mails - #2949

Open
montjeffrey wants to merge 1 commit into
supabase:masterfrom
montjeffrey:fix-email-change-validate-first
Open

montjeffrey wants to merge 1 commit into
supabase:masterfrom
montjeffrey:fix-email-change-validate-first

Conversation

@montjeffrey

Copy link
Copy Markdown

What

Fixes #2948.

With secure email change on and the address check on, PUT /user {"email": ...} from a user who already has an address sends two confirmation mails, one to the new address and one to the current one. When one of the two addresses fails the check, the request returns 400 email_address_invalid and no token is stored, but the mail to the other address has already been sent. Its link can never be used.

How

EmailChangeMail sends the two mails concurrently and each address is checked inside Mail, so one check failing does not stop the other mail.

  • mailer.Validator: a small interface for a Client that checks an address before it sends to it.
  • The validating client implements it with the check Mail already runs.
  • EmailChangeMail checks every address before it starts sending, when it has more than one address and the client is a Validator.

Nothing changes when the address check is off, when there is only one address, or with the Send Email hook.

Tests

New test TestEmailChangeMailValidatesBeforeSending in internal/mailer/templatemailer: new address rejected, current address rejected, both accepted, each with a test client and with the real validating client.

  • On master (10ef107) it fails: with the test client both rejected cases report one mail sent where none is expected. With the real client the result depends on timing there (one of the two rejected cases failed in my run).
  • With the change it passes, 20 runs out of 20.
  • go test -p 1 ./internal/api/... ./internal/mailer/... -count=1 -v: 1,545 pass and 3 fail, against 1,538 pass and 3 fail on master. The difference is the 7 new results. The 3 failures are TestAuth/TestMaybeLoadUserOrSession/Valid_Session_ID_Claim and its two parents, which fail the same way on master in my container (a time.Location difference).
  • gofmt -s -l . prints nothing; make vet, make static and make sec pass.

Over HTTP, against a server built from each tree, with a local mail catcher as the SMTP server (mails received):

Request master 10ef107 with this change
User with an address, new address rejected: 400 1, to the current address 0
User whose current address is rejected, new address accepted: 400 1, to the new address 0
The first request three times in a row: 400 each 3 0
Anonymous user, new address rejected: 400 0 0
User with an address, new address accepted: 200 2 2
Anonymous user, new address accepted: 200 1 1

Not run: the race detector (my image builds with CGO_ENABLED=0), make test as CI runs it, a hosted project.

Left as it is

  • The two addresses are now checked twice on this path, once up front and once inside Mail. That is up to two more DNS lookups, or two more calls to the validation service, for a secure email change. The first check runs one address after the other.
  • With background sending (GOTRUE_MAILER_EMAIL_BACKGROUND_SENDING) nothing changes: the background client is not a Validator, and in that mode the check result does not reach the request anyway.
  • If the SMTP server refuses one of the two mails after both addresses passed, the other one is still delivered.
  • The error message still names the current address. fix: report the new address when an email change is rejected as invalid #2848 is about that.
  • A rejected request still counts against GOTRUE_RATE_LIMIT_EMAIL_SENT, as before.

#2889 changes how the list of addresses in the same function is built. The two changes do not touch the same lines.

I used an AI assistant (Claude Code) to read the code and help write the change, the test and this description. I ran the tests and the requests on my own machine and checked the output.

EmailChangeMail sends the mail to the new address and the mail to the current address concurrently, and each address is validated inside Mail. When one of them fails validation the request is rejected and no token is stored, but the mail to the other address has already been sent.

Validate every address before sending to any of them when the mail client validates addresses.
@montjeffrey
montjeffrey requested a review from a team as a code owner November 21, 2026 00:17

This branch has not been deployed

No deployments
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.

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

1 participant