: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(auth): redirect social login provider errors to the login page (#129780)
Rafael Audibert7ace68991608
diff --git a/posthog/middleware.py b/posthog/middleware.pyindex f07c5bab17f0..aba4b134eda9 100644--- a/posthog/middleware.py+++ b/posthog/middleware.py@@ -32,6 +32,7 @@ from django.utils.deprecation import MiddlewareMixinfrom django.utils.functional import SimpleLazyObjectfrom django.utils.http import http_date, url_has_allowed_host_and_scheme+import requestsimport structlogimport posthoganalyticsfrom django_prometheus.middleware import Metrics@@ -1626,6 +1627,11 @@ class SocialAuthExceptionMiddleware:error_code = self._INVITE_ERROR_CODES.get(self._error_code(exception), "social_login_failure")return redirect(sso_failure_redirect_url(request, error_code))+ # social-core wraps only ConnectionError from provider calls, so a read timeout or a provider 5xx+ # arrives here as a plain requests error. The detail holds the provider URL, so it stays out of the redirect.+ if isinstance(exception, requests.RequestException):+ return redirect(sso_failure_redirect_url(request, "social_login_failure"))+return Nonedef _error_code(self, exception: ValidationError) -> str:diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.pyindex c6447e87b661..41bc353a0b12 100644--- a/posthog/test/test_middleware.py+++ b/posthog/test/test_middleware.py@@ -23,6 +23,7 @@ from django.test import ()from django.urls import path, resolve, reverse+import requestsimport structlogfrom loginas import settings as la_settingsfrom parameterized import parameterized@@ -2540,6 +2541,21 @@ class TestSocialAuthExceptionMiddleware(SimpleTestCase):ValidationError("Something else.", code="something_else"),"/login?error_code=social_login_failure",),+ (+ "provider_read_timeout",+ "/complete/google-oauth2/",+ requests.exceptions.ReadTimeout("Read timed out."),+ "/login?error_code=social_login_failure",+ ),+ (+ "provider_server_error",+ "/complete/github/",+ requests.exceptions.HTTPError(+ "500 Server Error: Internal Server Error for url: https://example.com",+ response=requests.Response(),+ ),+ "/login?error_code=social_login_failure",+ ),])def test_redirects_with_expected_url(self, _name, path, exception, expected_url):