Skip to content

Change-email verification rejects valid tokens because shared token decoding trims change-email state #36411

Closed

Description

@QuantumGhost

AI disclosure: This issue was drafted and analyzed with Codex using GPT-5.4.
I have reviewed the analysis, and I am responsible for the content.

Summary

The change-email verification flow can reject fresh, valid tokens with:

The token is invalid or has expired.

The failing request is POST /console/api/account/change-email/validity.

Reproduction

  1. Open the account settings page.
  2. Start the change-email flow.
  3. Enter the verification code sent to the current email address.
  4. Submit the code.

Actual result

The backend returns 400 invalid_or_expired_token even though the code and token are still fresh.

Expected result

A valid code should advance the flow and return the refreshed token for the next phase.

Root cause

This is a follow-up design bug in the phase-bound token flow introduced by PR #35425.

PR #35425 correctly introduced a dedicated change-email state machine:

old_email -> old_email_verified -> new_email -> new_email_verified

However, the shared TokenManager.get_token_data() path was still validating Redis payloads through a partial _TokenData schema that trimmed undeclared fields. That made business-specific auth metadata vulnerable to being silently dropped during the Redis -> Python round-trip.

PR #36117 fixed the adjacent generic phase field for forgot-password / email-register, but change-email does not actually depend on that key. The change-email flow depends on its dedicated email_change_phase state and, more broadly, on the ability to preserve business-specific token metadata without the storage layer stripping fields.

The old flow also relied on loose dict/string checks and did not keep the stable account_id binding explicit across every change-email transition.

Proposed fix

  • keep TokenManager on a shared _TokenData + TypeAdapter baseline, but configure it to allow and preserve undeclared extra fields
  • model change-email tokens at the AccountService boundary with Pydantic discriminated union types
  • bind account_id through the entire change-email flow and validate it at every transition
  • add regression tests for both the shared token adapter behavior and the typed change-email state machine

Notes

Activity

  1. self-assigned this
    on May 20, 2026
  2. changed the title [-]Change-email verification rejects valid tokens because `_TokenData` drops `email_change_phase`[/-] [+]Change-email verification rejects valid tokens because shared token decoding trims change-email state[/+] on May 20, 2026
  3. added
    🐞 bug
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🐞 bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions