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(bing-webmaster-tools): resolve a site filter typed without a scheme (#103311)

Tom Owers527be28115b8
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
index 04e9cf63251a..43925bc21ce7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/bing_webmaster_tools.py
@@ -128,23 +128,20 @@ def _site_key(url: str) -> str:
return url.strip().rstrip("/").lower()
-def suggest_verified_site(filter_url: str, verified: list[str]) -> str | None:
- """The verified site a bare-hostname filter entry most likely meant, else None.
+def verified_sites_for_host(filter_url: str, verified: list[str]) -> list[str]:
+ """The verified sites a bare-hostname filter entry could mean, in the order Bing lists them.
Bing lists every site with a scheme (e.g. ``https://example.com/``), so an entry that omits the
scheme (``example.com``) matches nothing on the scheme-sensitive site key even when that host is
- verified. When the entry is a bare hostname, point at the verified site sharing that host so the
- "not verified" error can name the exact value to paste instead of dead-ending."""
+ verified. Returns empty for an entry that already carries a scheme, since that one is matched
+ exactly."""
stripped = filter_url.strip()
if not stripped or "://" in stripped:
- return None
+ return []
host = stripped.strip("/").lower()
if not host:
- return None
- for url in verified:
- if urlparse(url).netloc.lower() == host:
- return url
- return None
+ return []
+ return [url for url in verified if urlparse(url).netloc.lower() == host]
def parse_site_urls(raw: str | None) -> list[str]:
@@ -181,18 +178,27 @@ def select_site_urls(sites: list[dict[str, Any]], site_url_filters: list[str]) -
missing: list[str] = []
for filter_url in site_url_filters:
matched = by_key.get(_site_key(filter_url))
+ if matched is None:
+ # A bare hostname misses the scheme-sensitive key even when Bing verified that exact
+ # host. Resolve it when the host names one verified site; leave it missing when it
+ # names several, rather than guessing which scheme the user meant.
+ candidates = verified_sites_for_host(filter_url, verified)
+ matched = candidates[0] if len(candidates) == 1 else None
if matched is None:
missing.append(filter_url)
- else:
+ elif matched not in selected:
+ # Two spellings of the same site (e.g. "example.com" and "https://example.com/")
+ # resolve to one registered URL; syncing it twice would seed duplicate primary keys.
selected.append(matched)
if missing:
- suggestions = {
- entry: match for entry in missing if (match := suggest_verified_site(entry, verified)) is not None
- }
+ ambiguous = {entry: candidates for entry in missing if (candidates := verified_sites_for_host(entry, verified))}
hint = ""
- if suggestions:
- pairs = "; ".join(f"'{entered}' is verified as '{match}'" for entered, match in suggestions.items())
- hint = f"Bing lists sites with their full URL, so {pairs}. "
+ if ambiguous:
+ pairs = "; ".join(
+ f"'{entered}' matches {' and '.join(repr(url) for url in candidates)}"
+ for entered, candidates in ambiguous.items()
+ )
+ hint = f"Bing lists sites with their full URL, and {pairs}. "
raise ValueError(
f"These site URLs are not verified sites on the connected account: {', '.join(missing)}. "
f"{hint}"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
index 5f8541ecea2c..d5460873f5ee 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bing_webmaster_tools/tests/test_bing_webmaster_tools.py
@@ -17,8 +17,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.bing_webma
parse_site_urls,
parse_wcf_date,
select_site_urls,
- suggest_verified_site,
validate_credentials,
+ verified_sites_for_host,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.bing_webmaster_tools.settings import (
BASE_URL,
@@ -172,23 +172,38 @@ class TestSiteSelection:
@pytest.mark.parametrize(
"filter_url,expected",
[
- # A bare hostname can never match Bing's scheme-prefixed site key, so it resolves to the
- # verified site sharing that host, with or without a trailing slash and regardless of case.
- ("example.com", "https://example.com/"),
- ("example.com/", "https://example.com/"),
- ("EXAMPLE.COM", "https://example.com/"),
- # An entry that already carries a scheme is matched exactly, so no suggestion is offered.
- ("https://example.com/", None),
- # A host that isn't verified has nothing to suggest.
- ("unknown.example.net", None),
+ # A bare hostname can never match Bing's scheme-prefixed site key, so it resolves by
+ # host, with or without a trailing slash and regardless of case.
+ ("example.com", ["https://example.com/"]),
+ ("example.com/", ["https://example.com/"]),
+ ("EXAMPLE.COM", ["https://example.com/"]),
+ # An entry that already carries a scheme is matched exactly, so it resolves nothing here.
+ ("https://example.com/", []),
+ # A host that isn't verified has no candidates.
+ ("unknown.example.net", []),
],
)
- def test_suggest_verified_site(self, filter_url, expected):
- assert suggest_verified_site(filter_url, ["https://example.com/"]) == expected
+ def test_verified_sites_for_host(self, filter_url, expected):
+ assert verified_sites_for_host(filter_url, ["https://example.com/"]) == expected
- def test_bare_hostname_error_names_the_verified_form(self):
- with pytest.raises(ValueError, match=r"'example.com' is verified as 'https://example.com/'"):
- select_site_urls(_SITES, ["example.com"])
+ @pytest.mark.parametrize("filter_url", ["example.com", "example.com/", "EXAMPLE.COM"])
+ def test_bare_hostname_resolves_to_the_verified_site(self, filter_url):
+ # Bing lists sites with a scheme, so a hostname typed without one matches no site key even
+ # though exactly one verified site carries that host.
+ assert select_site_urls(_SITES, [filter_url]) == ["https://example.com/"]
+
+ def test_two_spellings_of_one_site_are_not_synced_twice(self):
+ assert select_site_urls(_SITES, ["example.com", "https://example.com/"]) == ["https://example.com/"]
+
+ def test_ambiguous_bare_hostname_names_every_verified_form(self):
+ # Both schemes are verified separately in Bing, so picking one would silently sync the
+ # wrong site's stats.
+ sites = [
+ {"Url": "https://example.com/", "IsVerified": True},
+ {"Url": "http://example.com/", "IsVerified": True},
+ ]
+ with pytest.raises(ValueError, match=r"'https://example.com/' and 'http://example.com/'"):
+ select_site_urls(sites, ["example.com"])
class TestRequest: