Skip to content

fix: report the new address when an email change is rejected as invalid - #2848

Open
vjymisal0 wants to merge 11 commits into
supabase:masterfrom
vjymisal0:fix/email-change-invalid-address-message
Open

vjymisal0 wants to merge 11 commits into
supabase:masterfrom
vjymisal0:fix/email-change-invalid-address-message

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Oct 2, 2026 •

Copy link
Copy Markdown

What

Fixes #2847.

When the mailer rejects an address during an email change (PUT /user), the 400 response now names the address the mail validator rejected. This covers the new address, including when an anonymous user links an email, and the current address when secure email change sends to both.

How

The validation mail client retains the rejected recipient alongside the original validation error. sendEmail uses that recipient when returning email_address_invalid. The original validation error remains available through errors.Is, and the response status and error code are unchanged.

Testing

  • Tested all three recipient cases through the template mailer with a local validation service. The new current-address case fails on the previous PR head and passes with the fix.
  • The focused test passed 10 consecutive runs. The mailer suite passed on rerun after an intermittent DNS timeout assertion.
  • gofmt and git diff --check passed.

When the mailer rejects an email change, the 400 response quoted the
user's current email instead of the address being confirmed. For an
anonymous user linking an email for the first time this produced
`Email address "" is invalid`, and for other users it pointed at their
existing address rather than the new one.

Fixes supabase#2847
@vjymisal0
vjymisal0 requested a review from a team as a code owner October 2, 2026 14:39
@montjeffrey

Copy link
Copy Markdown

I ran this against a local server: auth built from the Dockerfile at 10ef107 and at this PR's head 25f95b7, address check on (GOTRUE_MAILER_EMAIL_VALIDATION_EXTENDED=true), secure email change on, a local mail catcher as SMTP.

PUT /user 10ef107 this PR
anonymous user, new address someone@example.com Email address "" is invalid Email address "someone@example.com" is invalid
user at an outlook.com address, new address new@example.com names the current address names the new address
user at current@example.com, new address at outlook.com Email address "current@example.com" is invalid names the new address

All of them are 400 email_address_invalid on both builds.

The third row is the case from your second note, run for real. The current address is the one the check rejects. master names it, and with this change the message names the new address, which is a valid one. I got a user into that state with POST /admin/users and email_confirm: true; I don't know how common such accounts are.

Tests: TestSendEmailChangeInvalidAddressError fails on 10ef107 (both subtests) and passes here. go test -p 1 ./internal/api/... ./internal/mailer/... -count=1: 1,538 results pass on 10ef107 and 1,541 here, and the difference is the new test with its two subtests. One test fails for me on both trees in the same way, TestAuth/TestMaybeLoadUserOrSession/Valid_Session_ID_Claim (a time.Location difference); I did not look into it. TestValidateEmailExtended passed in both runs. No race detector, since the build image has CGO off.

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

@vjymisal0

Copy link
Copy Markdown
Author

Thanks for testing both recipients and sharing the results. Commit 3c417ae carries the rejected recipient through from the mail validator, so the response names the current address when that is the one rejected. It also still names the new address for an anonymous user linking an email. The three cases pass through the template mailer and a local validation service, and the new current-address case fails on the previous PR head.

@montjeffrey

Copy link
Copy Markdown

Thanks for the fix. I re-ran it on f713dc6, same setup as before (server built from the Dockerfile, secure email change on, a local mail catcher), and added the validation service path with a small local service that rejects addresses starting with reject.. Compared with master 0725a1d and the previous head 25f95b7. All of these are 400 email_address_invalid:

Rejected address master 25f95b7 f713dc6
anonymous user, new address, built-in check "" new new
anonymous user, new address, service "" new new
user with an address, new address, built-in check current new new
user with an address, new address, service current new new
current address, new one accepted, built-in check current new current
current address, new one accepted, service current new current

So it now names the address that actually failed in every case I tried. The accepted changes (anonymous user linking an address, user with both addresses valid) still return 200 on all three builds, and the stored rows match too.

TestSendEmailChangeInvalidAddressError passes 3 of 3 here. With this test file on 25f95b7, the current-address subtest fails, as you said (expected current@example.com, got new@example.com), and the other two pass.

One thing is still different. When both addresses fail the check, the message names whichever goroutine in EmailChangeMail reports its error first. In 20 requests it named the current address all 20 times on f713dc6 (and on master), while 25f95b7 named the new one all 20 times. Both addresses are invalid, so neither message is wrong. Would you rather the new address win in that case, since it's the one the user just typed, or is it fine as it is?

What I didn't test: the full ./internal/api/... and ./internal/mailer/... packages on this head (I only ran this test), the race detector, DNS or MX failures, and a hosted project. The confirmation mail that still goes to the other address when one is rejected is the same on all three builds. That's #2948, not this PR.

@vjymisal0

Copy link
Copy Markdown
Author

Thanks for checking both validation paths and the successful changes. I'd keep the existing first-error behavior when both addresses fail. Each response still names an address the validator rejected, and guaranteeing that the new address wins would change when the mailer returns and cancels the other send. This PR stays focused on reporting the rejected recipient. The separate delivery behavior is tracked in #2948.

@vjymisal0

Copy link
Copy Markdown
Author

One follow-up on the interaction with #2949. I verified that the three rejected-recipient cases pass on this PR head (f713dc6) in 20 runs with the race detector. When I locally combined it with #2949 at 613529d, the current-address case failed again because the new pre-send Validate method returned the underlying error without the rejected recipient.

I verified a small integration fix locally. Have Mail call Validate, and have Validate wrap validation failures in RecipientValidationError so both paths preserve the recipient. After that change, both TestSendEmailChangeInvalidAddressError and TestEmailChangeMailValidatesBeforeSending passed in 10 race-enabled runs on the combined tree. This matters when integrating the two PRs. The current head here already reports the rejected recipient correctly.

@montjeffrey

Copy link
Copy Markdown

Thanks for checking the combination. I reproduced it here with the race detector and got the same result:

Since RecipientValidationError is added in this PR, #2949 can't take the wrap on its own. If this PR is merged first, I'll rebase #2949 on master and make that change there. If #2949 goes first, the change would go here. Either order works for me.

One side effect of the combination: when both addresses are rejected, the response always names the new address (50 of 50 runs). That's because #2949 checks the addresses in order before either mail is sent, so nothing has been sent or cancelled yet. On this PR alone it names either address (26 current, 24 new over 50 runs).

@montjeffrey

Copy link
Copy Markdown

A follow-up to my comment above: #2949 now has a second commit, 186eaf4. With the first commit alone, a secure email change made four validation requests instead of master's two, and took about three times as long. The new commit checks both addresses at the same time and marks them, so Mail doesn't check them again. With a validation service that answers in 200 ms, that's back to two requests and about 200 ms, the same as master.

It changes the same lines of Mail as this PR, so the two PRs now conflict in validateclient.go. The resolution keeps both: skip an address that was already checked, otherwise validate it and wrap the failure in RecipientValidationError, which is your fix from before. I re-ran that combination with the race detector:

  • Both new tests pass 20 of 20. Without the wrap, the current-address case still fails 20 of 20.
  • All of internal/api and internal/mailer/..., against Postgres 15, passes with no data races.
  • When both addresses are rejected, the new address is still named, 50 of 50 runs. fix(mailer): validate both addresses before sending email change mails #2949 now waits for both checks and reports the new address's failure first.

The plan from my comment above stands.

@vjymisal0

Copy link
Copy Markdown
Author

Thanks for the follow-up. I combined this head (f713dc6) with #2949 at 186eaf4 and confirmed the conflict in validateclient.go. With the already-validated check kept in Mail and the recipient wrapped in Validate, both focused tests passed in 20 runs with the race detector.

That merge plan works for me. If #2949 lands first, I'll carry that resolution here. If this PR lands first, the integration can go into #2949.

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.

updateUser({email}) fails with "Email address \"\" is invalid" when linking an email to an anonymous user with no prior email

2 participants