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: match 2fa reset email expiration to actual token timeout (#59592)

eleftheriatrivyzaki8be9dd03af74
diff --git a/posthog/api/two_factor_reset.py b/posthog/api/two_factor_reset.py
index 002328b98079..c1d5009f0683 100644
--- a/posthog/api/two_factor_reset.py
+++ b/posthog/api/two_factor_reset.py
@@ -16,6 +16,10 @@ from posthog.models.user import User
logger = structlog.get_logger(__name__)
+TWO_FACTOR_RESET_TOKEN_TIMEOUT_HOURS = 24
+TWO_FACTOR_RESET_TOKEN_TIMEOUT_SECONDS = TWO_FACTOR_RESET_TOKEN_TIMEOUT_HOURS * 60 * 60
+
+
class TwoFactorResetTokenGenerator(PasswordResetTokenGenerator):
"""
Token generator for 2FA reset requests initiated by admins.
@@ -53,14 +57,13 @@ class TwoFactorResetTokenGenerator(PasswordResetTokenGenerator):
)
return False
- # Check 24-hour timeout (86400 seconds)
token_age_seconds = self._num_seconds(self._now()) - ts
- if token_age_seconds > 86400:
+ if token_age_seconds > TWO_FACTOR_RESET_TOKEN_TIMEOUT_SECONDS:
logger.warning(
"2FA reset token check failed: token expired",
user_id=user.pk,
token_age_seconds=token_age_seconds,
- max_age_seconds=86400,
+ max_age_seconds=TWO_FACTOR_RESET_TOKEN_TIMEOUT_SECONDS,
)
return False
@@ -145,8 +148,7 @@ class TwoFactorResetViewSet(viewsets.ViewSet):
return None, "You must log in with your credentials first."
# Use 24 hour timeout for reset flow (matches the reset token expiration)
- reset_session_timeout = 86400 # 24 hours
- expiration_time = auth_time + reset_session_timeout
+ expiration_time = auth_time + TWO_FACTOR_RESET_TOKEN_TIMEOUT_SECONDS
if int(time.time()) > expiration_time:
return None, "Your login session has expired. Please log in again."
diff --git a/posthog/tasks/email.py b/posthog/tasks/email.py
index 337cb2fc0865..e41a58e9b39b 100644
--- a/posthog/tasks/email.py
+++ b/posthog/tasks/email.py
@@ -13,6 +13,7 @@ import posthoganalytics
from celery import shared_task
from posthoganalytics import new_context, tag
+from posthog.api.two_factor_reset import TWO_FACTOR_RESET_TOKEN_TIMEOUT_HOURS
from posthog.batch_exports.models import BatchExport, BatchExportRun
from posthog.caching.login_device_cache import check_and_cache_login_device
from posthog.cloud_utils import is_cloud
@@ -941,7 +942,7 @@ def send_two_factor_reset_email(user_id: int, token: str) -> None:
"user_name": user.first_name,
"user_email": user.email,
"url": reset_link,
- "expiration_hours": 1,
+ "expiration_hours": TWO_FACTOR_RESET_TOKEN_TIMEOUT_HOURS,
"site_url": settings.SITE_URL,
},
)