Skip to content

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

Open

Description

@lh-lena

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

Calling supabase.auth.updateUser({ email }) on an anonymous session that has never had an email fails with a validation error, even though the target address is a perfectly valid, well-formed email:

AuthApiError: Email address "" is invalid
code: email_address_invalid, status: 400

Note the quoted address in the error is an empty string, not the address that was actually passed in the request. This happens with GOTRUE_MAILER_AUTOCONFIRM off (the secure/default config), i.e. the normal "send a real confirmation email" path - not the (separately reported, also concerning) autoconfirm-bypass path in #41125 / #29350.

Root cause (traced in this repo)

internal/api/mail.go, sendEmailChange() calls sendEmail() without setting recipientEmail:

err := a.sendEmail(r, tx, u, sendEmailParams{
    emailActionType:     mail.EmailChangeVerification,
    otp:                 otpCurrent,
    otpNew:              otpNew,
    tokenHashWithPrefix: u.EmailChangeTokenNew,
})

Inside sendEmail():

recipientEmail := params.recipientEmail
if recipientEmail == "" {
    recipientEmail = u.GetEmail()
}

For a normal user changing their email, u.GetEmail() returns their current (non-empty) address, so this fallback happens to work. For an anonymous user linking an email for the first time, u.GetEmail() is "" - there is no current email - so recipientEmail stays empty. That empty string is what then gets validated/used downstream (the mail.EmailChangeVerification branch), and validation correctly rejects "" - just for the wrong reason. The actual target address (u.EmailChange, set correctly a few lines earlier in the same function) is never what gets checked or notified.

To Reproduce

  1. Project with GOTRUE_MAILER_AUTOCONFIRM=false (default/secure setting) and anonymous sign-ins enabled.
  2. const { data } = await supabase.auth.signInAnonymously()
  3. await supabase.auth.updateUser({ email: 'someone@example.com' }) using that session's own access token.
  4. Observe AuthApiError: Email address "" is invalid (email_address_invalid), regardless of how well-formed the target address is.

Expected behavior

The email-change confirmation should be sent to the new address (u.EmailChange), and validation should run against that address - not silently fall back to the (empty, for an anonymous user) current email.

Impact

This blocks the standard "upgrade an anonymous/guest session to a permanent account via email" flow entirely for any user who hasn't already got an email set - which is the normal case for anonymous auth. The only way to avoid it currently is enabling GOTRUE_MAILER_AUTOCONFIRM, which reopens the issue described in #41125 / #29350 (email silently confirmed with no OTP).

System information

  • Environment: production (hosted)
  • Version of supabase-js: 2.117.0
  • Auth server version: current production (hosted project, not self-hosted)

Additional context

Related: #41125, #29350 - both about anonymous-user email linking, but describing the opposite failure mode (silent bypass rather than a spurious validation error). This may be an edge case in the same general code area (anonymous-user + email-change) that wasn't covered by whatever fix/change produced the current sendEmail/recipientEmail fallback behavior.

Activity

  1. montjeffrey commented on Nov 20, 2026

    @montjeffrey

    I can reproduce this on master (10ef107). The request is rejected because of the address in the example, not because the user is anonymous. What is wrong is the address in the error message.

    Smallest request (auth built from the Dockerfile at 10ef107, Postgres 15, GOTRUE_EXTERNAL_ANONYMOUS_USERS_ENABLED=true, GOTRUE_MAILER_EMAIL_VALIDATION_EXTENDED=true):

    POST /signup   {}
    PUT  /user     {"email":"someone@example.com"}      with the access token of that anonymous user
    
    400 {"code":400,"error_code":"email_address_invalid","msg":"Email address \"\" is invalid"}
    

    The new address decides the result:

    Anonymous user, PUT /user with GOTRUE_MAILER_EMAIL_VALIDATION_EXTENDED Result on 10ef107
    someone@example.com true 400, Email address "" is invalid
    an address at outlook.com true 200, confirmation mail goes to the new address, email_change is saved
    someone@example.com false 200, confirmation mail goes to the new address

    example.com is on the blocked host list of the address check (validateclient.go#L53). The mail itself is addressed to u.EmailChange. The empty string only appears in the message, which prints recipientEmail, and that falls back to the user's current address (mail.go#L768-L771, #L925-L928). So adding an email to an anonymous user works on master when the address passes the check.

    A user who already has an address gets the same kind of wrong message: changing from an outlook.com address to new@example.com returns Email address "<the current address>" is invalid.

    #2848 changes the message to name the new address. I ran the same requests against its head (25f95b7, which already contains 10ef107): the first row returns Email address "someone@example.com" is invalid, the other two rows are unchanged. Its test TestSendEmailChangeInvalidAddressError fails on 10ef107 (2 of 2 subtests: it gets "" and the current address where it expects the new one) and passes with the change.

    Not tested: a hosted project, the validation service URL, the Send Email hook, PKCE, supabase-js (I sent the HTTP requests directly, with a local mail catcher as the SMTP server).

    @lh-lena, does the update go through for you with an address on a real mail domain?

    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.

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