Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(auth): let legacy accounts complete an email change with a code (#91685)

Alex Liderc5efff5227b3
diff --git a/posthog/api/email_verification.py b/posthog/api/email_verification.py
index 41c5c1578af3..9cddb4c6830e 100644
--- a/posthog/api/email_verification.py
+++ b/posthog/api/email_verification.py
@@ -84,8 +84,10 @@ class EmailVerificationCodeVerifier:
# Signup verification always proves the account address. Only a verified user's
# email change targets the staged address; an unverified user's staged change must
# not let a code sent to the unverified new address verify the account.
+ # is_email_verified None marks an account from before verification existed. The login
+ # flow trusts those accounts as verified, so the email change flow does too.
target: str | None = target_email
- if target is None and user.is_email_verified:
+ if target is None and user.is_email_verified is not False:
target = user.pending_email
issued_at = int(time.time())
code = code_based_verification_token_generator.make_code(user, issued_at)
@@ -133,7 +135,9 @@ class EmailVerificationCodeVerifier:
return False
issued_at, target = state
# A code verifies only the address it was issued for. Signup codes store '' as the target.
- expected_target = (user.pending_email or "") if user.is_email_verified else ""
+ # None counts as verified here, like in send_code, or a legacy account's change code
+ # (bound to the staged address) could never match and the change could never complete.
+ expected_target = (user.pending_email or "") if user.is_email_verified is not False else ""
if target != expected_target:
return False
return code_based_verification_token_generator.check_code(user, code, issued_at)
diff --git a/posthog/api/test/test_user.py b/posthog/api/test/test_user.py
index 8040421ad035..66d552257ea3 100644
--- a/posthog/api/test/test_user.py
+++ b/posthog/api/test/test_user.py
@@ -3276,8 +3276,9 @@ class TestEmailVerificationCodeAPI(APIBaseTest):
html_message = mail.outbox[0].alternatives[0][0] # type: ignore
assert f"https://my.posthog.net/verify_email/{self.user.uuid}/" in html_message
- def test_email_change_code_goes_to_the_pending_address_and_completes_the_swap(self):
- self.user.is_email_verified = True
+ @parameterized.expand([("verified", True), ("legacy_never_verified", None)])
+ def test_email_change_code_goes_to_the_pending_address_and_completes_the_swap(self, _name, is_email_verified):
+ self.user.is_email_verified = is_email_verified
self.user.pending_email = "[email protected]"
self.user.save()
@@ -3309,6 +3310,8 @@ class TestEmailVerificationCodeAPI(APIBaseTest):
def test_unverified_user_with_staged_change_verifies_only_the_account_address(self):
# A signup code for an unverified user must go to the account address and prove that one,
# even if a change to another address is staged: the staged address is itself unproven.
+ # False means an unverified signup; None (a legacy account) is trusted as verified.
+ self.user.is_email_verified = False
self.user.pending_email = "[email protected]"
self.user.save()
diff --git a/posthog/api/user.py b/posthog/api/user.py
index 0c620b8d7a5d..95a93f21f5c3 100644
--- a/posthog/api/user.py
+++ b/posthog/api/user.py
@@ -1301,8 +1301,9 @@ class UserViewSet(
# The swap needs a credential issued for the staged address. A token always is (its hash
# includes pending_email). A code is only for a verified user; an unverified user's code
- # proves the account address, so their staged change stays pending.
- if user.pending_email and (token or user.is_email_verified):
+ # proves the account address, so their staged change stays pending. A legacy account
+ # (is_email_verified None) counts as verified, like in the login flow and in the verifier.
+ if user.pending_email and (token or user.is_email_verified is not False):
old_email = user.email
with transaction.atomic():
user.email = user.pending_email