: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(google-search-console): reject the dashboard url as a property (#103561)
Tom Owersb7fced001d26
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.pyindex 37da36bc4944..f83c8c35a383 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_search_console/google_search_console.py@@ -116,6 +116,19 @@ class GoogleSearchConsoleResumeConfig:start_row: int # next startRow within current_date+SEARCH_CONSOLE_UI_PREFIX = "https://search.google.com/"+++def is_search_console_ui_url(site: str) -> bool:+ """True when the value addresses Search Console itself rather than one of the account's properties.++ ``normalize_site_url`` lifts the property out of a UI URL that carries a ``resource_id``.+ Whatever is left on ``search.google.com`` names no property and never will, so no amount of+ re-checking account access can make it validate.+ """+ return site.strip().lower().startswith(SEARCH_CONSOLE_UI_PREFIX)++def normalize_site_url(raw: str) -> str:"""Coerce a user-entered property URL toward Google's canonical form.@@ -130,7 +143,7 @@ def normalize_site_url(raw: str) -> str:site = raw.strip()# The Search Console UI URL carries the property in its `resource_id` query param.- if site.startswith("https://search.google.com/"):+ if is_search_console_ui_url(site):resource_id = parse_qs(urlparse(site).query).get("resource_id")if resource_id:site = resource_id[0].strip()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 c3e9144065f2..0814034a3013 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@@ -36,6 +36,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_sea_is_quota_error,google_search_console_session,google_search_console_source,+ is_search_console_ui_url,list_sites,normalize_site_url,suggest_registered_site,@@ -72,6 +73,13 @@ _PROPERTY_LIST_ACCESS_ERROR = (_PROPERTY_LIST_CREDENTIALS_ERROR = ("Google rejected the credentials for this connection. Reconnect your Google account, then pick a property.")+# Search Console's own address is what sits in the browser bar while people hunt for the value to+# paste, so it gets pasted. It is no account's property, and the "not visible to the connected+# account" wording sends them off to check permissions instead of the field they filled in.+_SEARCH_CONSOLE_UI_ERROR = (+ "That's the address of the Search Console dashboard, not one of your properties. Enter the "+ "property as 'https://example.com/' or 'sc-domain:example.com'."+)def _property_list_http_error(error: requests.HTTPError) -> str:@@ -256,6 +264,10 @@ class GoogleSearchConsoleSource(schema_name: Optional[str] = None,api_version: str | None = None,) -> tuple[bool, str | None]:+ site_url = normalize_site_url(config.site_url)+ if is_search_console_ui_url(site_url):+ return False, _SEARCH_CONSOLE_UI_ERROR+try:session = google_search_console_session(config.google_search_console_integration_id, team_id)except Integration.DoesNotExist:@@ -296,7 +308,6 @@ class GoogleSearchConsoleSource(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)if not normalized:# The account owns no property at all, so no value can ever validate. The "not visible"# message below sends the user back to re-checking the URL format they got right, whichdiff --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 49ec5871b85b..cf53f5b56def 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@@ -500,3 +500,51 @@ def test_validate_credentials_unexpected_list_sites_error_stays_generic(error, sassert ok is Falseassert "couldn't reach Google Search Console" in (message or "")assert secret not in (message or "")+++ "site_url",+ [+ "https://search.google.com/search-console/",+ "https://search.google.com/search-console/performance/search-analytics",+ "HTTPS://Search.Google.com/search-console",+ ],+)+def test_validate_credentials_rejects_the_search_console_dashboard_url(site_url):+ config = GoogleSearchConsoleSourceConfig(site_url=site_url, google_search_console_integration_id=1)+ with mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_search_console.source.list_sites"+ ) as list_sites:+ ok, message = GoogleSearchConsoleSource().validate_credentials(config, team_id=1)++ assert ok is False+ assert "Search Console dashboard" in (message or "")+ assert "is not visible to the connected Google account" not in (message or "")+ list_sites.assert_not_called()+++ "site_url",+ [+ "https://search.google.com/search-console?resource_id=sc-domain%3Aexample.com",+ "HTTPS://Search.Google.com/search-console?resource_id=sc-domain%3Aexample.com",+ ],+)+def test_validate_credentials_still_reads_a_property_out_of_a_dashboard_url(site_url):+ config = GoogleSearchConsoleSourceConfig(+ site_url=site_url,+ google_search_console_integration_id=1,+ )+ 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",+ return_value=[{"siteUrl": "sc-domain:example.com", "permissionLevel": "siteOwner"}],+ ),+ ):+ ok, message = GoogleSearchConsoleSource().validate_credentials(config, team_id=1)++ assert ok is True+ assert message is None