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): redirect gracefully when SSO provider is unreachable (#67235)

posthog[bot]5629338611fe
diff --git a/ee/api/test/test_authentication.py b/ee/api/test/test_authentication.py
index 0b89a6ee4ea1..ec8055b731b7 100644
--- a/ee/api/test/test_authentication.py
+++ b/ee/api/test/test_authentication.py
@@ -2,7 +2,7 @@ import os
import json
import uuid
import datetime
-from typing import cast
+from typing import Any, cast
from urllib.parse import parse_qs, urlparse
import pytest
@@ -16,7 +16,7 @@ from django.utils import timezone
from parameterized import parameterized
from rest_framework import status
-from social_core.exceptions import AuthFailed
+from social_core.exceptions import AuthConnectionError, AuthFailed, AuthMissingParameter
from social_django.models import UserSocialAuth
from posthog.constants import AvailableFeature
@@ -245,6 +245,21 @@ class TestEEAuthenticationAPI(APILicensedTest):
second_key = self.client.session.session_key
self.assertNotEqual(first_key, second_key)
+ @parameterized.expand(
+ [
+ ("auth_failed", AuthFailed(cast(Any, "google-oauth2"), "bad")),
+ ("missing_parameter", AuthMissingParameter(cast(Any, "google-oauth2"), "code")),
+ ("connection_error", AuthConnectionError(cast(Any, "google-oauth2"), "cert verify failed")),
+ ]
+ )
+ def test_unreachable_or_misconfigured_sso_provider_redirects(self, _name, exception):
+ # An unreachable IdP or one whose TLS cert fails during OIDC discovery raises AuthConnectionError,
+ # a sibling of AuthFailed - it must redirect gracefully rather than surface as an unhandled 500.
+ self.client.logout()
+ with self.settings(**GOOGLE_MOCK_SETTINGS), patch("posthog.api.authentication.auth", side_effect=exception):
+ response = self.client.get("/login/google-oauth2/")
+ self.assertRedirects(response, "/login?error_code=improperly_configured_sso", fetch_redirect_response=False)
+
def test_existing_session_remains_valid_when_sso_enforced(self):
"""Test that existing password-authenticated sessions remain valid after SSO is enforced"""
self.client.logout()
diff --git a/posthog/api/authentication.py b/posthog/api/authentication.py
index a378bec210cc..247dbab2c72e 100644
--- a/posthog/api/authentication.py
+++ b/posthog/api/authentication.py
@@ -34,7 +34,7 @@ from rest_framework.decorators import action
from rest_framework.exceptions import APIException
from rest_framework.request import Request
from rest_framework.response import Response
-from social_core.exceptions import AuthFailed, AuthMissingParameter
+from social_core.exceptions import AuthConnectionError, AuthFailed, AuthMissingParameter
from social_django.strategy import DjangoStrategy
from social_django.views import auth
from two_factor.utils import default_device
@@ -143,7 +143,9 @@ def sso_login(request: HttpRequest, backend: str) -> HttpResponse:
try:
return auth(request, backend)
- except (AuthFailed, AuthMissingParameter) as e:
+ except (AuthFailed, AuthMissingParameter, AuthConnectionError) as e:
+ # AuthConnectionError covers an unreachable IdP or a TLS cert that fails during OIDC discovery -
+ # it's a sibling of AuthFailed (not a subclass), so it would otherwise surface as an unhandled 500.
logger.warning("SSO login failed, redirecting to login page", exc_info=e)
return redirect("/login?error_code=improperly_configured_sso")