: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(warehouse_sources): stop leaking raw errors from Search Console credential checks (#76387)
Tom Owers11fb31974264
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/source.pyindex b65046000a8b..3ef82ac3a788 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/source.py@@ -12,6 +12,7 @@ from posthog.schema import (SourceFieldOauthConfig,)+from posthog.exceptions_capture import capture_exceptionfrom posthog.models.integration import Integrationfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType, ResumableSource@@ -41,6 +42,16 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_sea)from products.warehouse_sources.backend.types import ExternalDataSourceType+# Fallback messages for unexpected failures during credential validation. The raw exception can+# embed OAuth tokens, ids, or an HTML error body, so we capture it for debugging and show generic+# guidance instead of surfacing `str(e)` to the user.+_LOAD_CONNECTION_ERROR = (+ "PostHog couldn't load your Google Search Console connection. Please reconnect your Google account and try again."+)+_LIST_SITES_ERROR = (+ "PostHog couldn't reach Google Search Console to list your properties. Please try again in a few minutes."+)+@SourceRegistry.registerclass GoogleSearchConsoleSource(@@ -179,10 +190,11 @@ class GoogleSearchConsoleSource(except Exception as e:if "matching query does not exist" in str(e):return False, (- "Your Google Search Console connection is no longer available — it may have been "+ "Your Google Search Console connection is no longer available. It may have been ""disconnected. Please reconnect your Google Search Console account.")- return False, f"Could not load Google Search Console credentials: {e}"+ capture_exception(e)+ return False, _LOAD_CONNECTION_ERRORtry:sites = list_sites(session)@@ -193,7 +205,8 @@ class GoogleSearchConsoleSource(False,"Google Search Console rejected the credentials. Please reconnect your account and ensure it has read access to the property.",)- return False, f"Failed to list Google Search Console sites: {e}"+ capture_exception(e)+ return False, _LIST_SITES_ERRORexcept RefreshError:# Raised while AuthorizedSession refreshes the OAuth access token (e.g. invalid_scope or# invalid_grant): the stored token is missing the required permissions, or has expired or@@ -206,7 +219,8 @@ class GoogleSearchConsoleSource("and grant access to Search Console.",)except Exception as e:- return False, f"Failed to list Google Search Console sites: {e}"+ capture_exception(e)+ return False, _LIST_SITES_ERRORnormalized = {url: site.get("permissionLevel") for site in sites if (url := site.get("siteUrl")) is not None}site_url = normalize_site_url(config.site_url)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.pyindex dd447518ec22..94d22d1d42a1 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py@@ -1,6 +1,8 @@import pytestfrom unittest import mock+import requests+from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.googlesearchconsole import (GoogleSearchConsoleSourceConfig,)@@ -298,3 +300,48 @@ def test_validate_credentials_handles_missing_integration():assert ok is Falseassert "reconnect your Google Search Console account" in (message or "")+++def _http_error(status_code: int, message: str = "") -> requests.HTTPError:+ response = mock.MagicMock()+ response.status_code = status_code+ return requests.HTTPError(message, response=response)+++def test_validate_credentials_unexpected_load_error_stays_generic():+ # An unexpected failure loading the connection (not the deleted-integration case) must not+ # surface the raw exception, which can embed OAuth tokens or ids.+ with mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_search_console.source.google_search_console_session",+ side_effect=Exception("boom access_token=secret-abc123"),+ ):+ ok, message = GoogleSearchConsoleSource().validate_credentials(_config(), team_id=1)++ assert ok is False+ assert "reconnect your Google account" in (message or "")+ assert "secret-abc123" not in (message or "")+++ "error,secret",+ [+ pytest.param(_http_error(500, "boom access_token=secret-http500"), "secret-http500", id="http_500"),+ pytest.param(Exception("boom refresh_token=secret-xyz789"), "secret-xyz789", id="unexpected"),+ ],+)+def test_validate_credentials_unexpected_list_sites_error_stays_generic(error, secret):+ # A non-auth failure listing sites must fall back to a generic message, not leak the raw error.+ with (+ mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_search_console.source.google_search_console_session"+ ),+ mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_search_console.source.list_sites",+ side_effect=error,+ ),+ ):+ ok, message = GoogleSearchConsoleSource().validate_credentials(_config(), team_id=1)++ assert ok is False+ assert "couldn't reach Google Search Console" in (message or "")+ assert secret not in (message or "")