: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(woocommerce): keep dns blips retryable without reporting them (#122144)
Tom Owers637dbba9dc1a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/tests/test_woocommerce.py b/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/tests/test_woocommerce.pyindex a243f94d3a86..ae2781a26d88 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/tests/test_woocommerce.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/tests/test_woocommerce.py@@ -8,6 +8,10 @@ from requests import Requestfrom products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import table_from_py_listfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.http import TrackedHTTPAdapter+from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import (+ HostResolution,+ TemporaryHostResolutionError,+)from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (ScriptedResponse,SourceDriver,@@ -221,24 +225,45 @@ class TestHostGuardedAdapter:request.url = urlreturn request- def test_blocks_redirect_to_internal_host(self) -> None:- adapter = _HostGuardedAdapter(team_id=123)- with patch(- "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce._is_host_safe",- return_value=(False, "Hosts with internal IP addresses are not allowed"),- ):+ @pytest.mark.parametrize(+ "resolver_outcome, expected_error",+ [# `requests` calls `send` again for the redirect hop, so a 30x toward an# internal address is caught here even though the original host was safe.- with pytest.raises(ValueError, match="internal IP"):+ (+ {+ "return_value": HostResolution(+ connect_host=None, error="Hosts with internal IP addresses are not allowed"+ )+ },+ ValueError,+ ),+ # The import pipeline retries this type without reporting it, so it must not become a ValueError.+ ({"side_effect": TemporaryHostResolutionError("example.com")}, TemporaryHostResolutionError),+ ],+ )+ def test_rejected_host_raises_before_dispatch(self, resolver_outcome: dict[str, Any], expected_error: type) -> None:+ adapter = _HostGuardedAdapter(team_id=123)+ with (+ patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce.resolve_safe_host",+ **resolver_outcome,+ ),+ patch.object(TrackedHTTPAdapter, "send") as mock_super_send,+ ):+ with pytest.raises(expected_error) as exc_info:adapter.send(self._prepared("https://169.254.169.254/wp-json/wc/v3/products"))+ assert type(exc_info.value) is expected_error+ mock_super_send.assert_not_called()+def test_allows_safe_host_and_delegates(self) -> None:adapter = _HostGuardedAdapter(team_id=123)sentinel = MagicMock()with (patch(- "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce._is_host_safe",- return_value=(True, None),+ "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce.resolve_safe_host",+ return_value=HostResolution(connect_host="93.184.216.34", error=None),),patch.object(TrackedHTTPAdapter, "send", return_value=sentinel) as mock_super_send,):@@ -253,8 +278,8 @@ class TestSourceHostGuard:config = WooCommerceSourceConfig(store_url="https://169.254.169.254", consumer_key="ck", consumer_secret="cs")with patch(- "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce._is_host_safe",- return_value=(False, "Hosts with internal IP addresses are not allowed"),+ "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce.resolve_safe_host",+ return_value=HostResolution(connect_host=None, error="Hosts with internal IP addresses are not allowed"),):result = SourceDriver(WooCommerceSource(), config).run("products", [])@@ -383,15 +408,19 @@ class TestWebhookManagement:assert info.exists is False+ @pytest.mark.parametrize("operation", [create_webhook, delete_webhook, get_external_webhook_info])@pytest.mark.parametrize(- "operation",- [create_webhook, delete_webhook, get_external_webhook_info],+ "resolver_outcome",+ [+ {"return_value": HostResolution(connect_host=None, error="blocked")},+ {"side_effect": TemporaryHostResolutionError("example.com")},+ ],)- def test_unsafe_host_never_reaches_the_network(self, operation: Any) -> None:+ def test_unsafe_host_never_reaches_the_network(self, operation: Any, resolver_outcome: dict[str, Any]) -> None:with (patch(- "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce._is_host_safe",- return_value=(False, "blocked"),+ "products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce.resolve_safe_host",+ **resolver_outcome,),patch("products.warehouse_sources.backend.temporal.data_imports.sources.woocommerce.woocommerce._make_guarded_session"diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/woocommerce.py b/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/woocommerce.pyindex de1e1577f627..538116a91f10 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/woocommerce.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/woocommerce/woocommerce.py@@ -18,7 +18,11 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.httDEFAULT_RETRY,TrackedHTTPAdapter,)-from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import _is_host_safe+from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import (+ TemporaryHostResolutionError,+ _is_host_safe,+ resolve_safe_host,+)from products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source import (RESTAPIConfig,rest_api_resource,@@ -75,17 +79,25 @@ def _base_url(store_url: str) -> str:return f"{normalize_store_url(store_url)}{WOOCOMMERCE_API_BASE_PATH}"+def _check_host_safe(host: str, team_id: int) -> None:+ """Raise `ValueError` for a host that resolves to an internal/private address.++ A resolver blip raises `TemporaryHostResolutionError` instead, so the import pipeline+ retries it without reporting it as a bug.+ """+ resolution = resolve_safe_host(host, team_id)+ if resolution.connect_host is None:+ raise ValueError(resolution.error or "WooCommerce store host is not allowed")++def _assert_host_safe(store_url: str, team_id: int) -> None:"""Block SSRF: reject store URLs that resolve to internal/private hosts.The store URL is fully user-controlled and drives server-side requests, so it- must be vetted before any outbound call. `_is_host_safe` is a no-op on- self-hosted instances and blocks private/internal IPs on PostHog Cloud.+ must be vetted before any outbound call. The check is a no-op on self-hosted+ instances and blocks private/internal IPs on PostHog Cloud."""- host = urlparse(normalize_store_url(store_url)).hostname or ""- is_safe, error = _is_host_safe(host, team_id)- if not is_safe:- raise ValueError(error or "WooCommerce store host is not allowed")+ _check_host_safe(urlparse(normalize_store_url(store_url)).hostname or "", team_id)class _HostGuardedAdapter(TrackedHTTPAdapter):@@ -111,10 +123,7 @@ class _HostGuardedAdapter(TrackedHTTPAdapter):cert: bytes | str | tuple[bytes | str, bytes | str] | None = None,proxies: Mapping[str, str] | None = None,) -> Response:- host = urlparse(request.url or "").hostname or ""- is_safe, error = _is_host_safe(host, self._team_id)- if not is_safe:- raise ValueError(error or "WooCommerce store host is not allowed")+ _check_host_safe(urlparse(request.url or "").hostname or "", self._team_id)return super().send(request, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)@@ -384,7 +393,7 @@ def _list_webhooks(auth=auth,timeout=REQUEST_TIMEOUT_SECONDS,)- except (RequestException, ValueError) as e:+ except (RequestException, ValueError, TemporaryHostResolutionError) as e:return [], f"Could not reach your WooCommerce store to list webhooks: {e}"if not response.ok:@@ -425,7 +434,7 @@ def create_webhook("""try:_assert_host_safe(store_url, team_id)- except ValueError as e:+ except (ValueError, TemporaryHostResolutionError) as e:return WebhookCreationResult(success=False, error=str(e))existing, error = _list_webhooks(store_url, consumer_key, consumer_secret, team_id)@@ -453,7 +462,7 @@ def create_webhook(try:response = session.post(target, json=payload, auth=auth, timeout=REQUEST_TIMEOUT_SECONDS)- except (RequestException, ValueError) as e:+ except (RequestException, ValueError, TemporaryHostResolutionError) as e:error = f"Could not reach your WooCommerce store to create webhooks: {e}"else:if response.ok:@@ -475,7 +484,7 @@ def delete_webhook("""Delete every webhook on the store that delivers to `webhook_url`."""try:_assert_host_safe(store_url, team_id)- except ValueError as e:+ except (ValueError, TemporaryHostResolutionError) as e:return WebhookDeletionResult(success=False, error=str(e))existing, error = _list_webhooks(store_url, consumer_key, consumer_secret, team_id)@@ -498,7 +507,7 @@ def delete_webhook(auth=auth,timeout=REQUEST_TIMEOUT_SECONDS,)- except (RequestException, ValueError) as e:+ except (RequestException, ValueError, TemporaryHostResolutionError) as e:return WebhookDeletionResult(success=False, error=f"Could not reach your WooCommerce store: {e}")if not response.ok:@@ -513,7 +522,7 @@ def get_external_webhook_info("""Report the webhooks currently registered on the store for `webhook_url`."""try:_assert_host_safe(store_url, team_id)- except ValueError as e:+ except (ValueError, TemporaryHostResolutionError) as e:return ExternalWebhookInfo(exists=False, url=webhook_url, error=str(e))existing, error = _list_webhooks(store_url, consumer_key, consumer_secret, team_id)