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(vercel): route unknown-install billing webhooks to EU, stop capture (#101065)

posthog[bot]cdd7a2d0b6be
diff --git a/ee/api/vercel/test/test_vercel_webhooks.py b/ee/api/vercel/test/test_vercel_webhooks.py
index c01642e4b80f..c4be36f8473c 100644
--- a/ee/api/vercel/test/test_vercel_webhooks.py
+++ b/ee/api/vercel/test/test_vercel_webhooks.py
@@ -270,7 +270,62 @@ class TestVercelWebhooks(VercelTestBase):
REGION_EU_DOMAIN="eu.posthog.com",
)
@patch("ee.api.vercel.webhook_events.outbound_requests.post")
- def test_billing_event_unknown_config_does_not_proxy(self, mock_post):
+ def test_billing_event_unknown_config_on_us_proxies_to_eu(self, mock_post):
+ mock_eu_response = MagicMock()
+ mock_eu_response.status_code = 200
+ mock_post.return_value = mock_eu_response
+
+ payload = {
+ "type": "marketplace.invoice.paid",
+ "payload": {"installationId": "icfg_unknown", "invoiceId": "mi_123"},
+ }
+ signature = self._sign_payload(payload)
+
+ response = self._post_webhook(payload, signature=signature)
+
+ assert response.status_code == status.HTTP_200_OK
+ mock_post.assert_called_once()
+ call_kwargs = mock_post.call_args
+ assert call_kwargs.kwargs["url"] == "https://eu.posthog.com/webhooks/vercel"
+ assert call_kwargs.kwargs["headers"]["x-vercel-signature"] == signature
+ # EU waits up to 30s for the billing service. A shorter read deadline would make US
+ # abandon an invoice EU accepted, and log it as an unknown installation.
+ _, read_timeout = call_kwargs.kwargs["timeout"]
+ assert read_timeout > 30
+
+ @override_settings(
+ VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret",
+ SITE_URL="https://us.posthog.com",
+ REGION_US_DOMAIN="us.posthog.com",
+ REGION_EU_DOMAIN="eu.posthog.com",
+ )
+ @patch("ee.api.vercel.webhook_events.capture_exception")
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
+ def test_billing_event_unknown_in_both_regions_returns_404(self, mock_post, mock_capture_exception):
+ mock_eu_response = MagicMock()
+ mock_eu_response.status_code = 404
+ mock_post.return_value = mock_eu_response
+
+ payload = {
+ "type": "marketplace.invoice.paid",
+ "payload": {"installationId": "icfg_unknown", "invoiceId": "mi_123"},
+ }
+ signature = self._sign_payload(payload)
+
+ response = self._post_webhook(payload, signature=signature)
+
+ assert response.status_code == status.HTTP_404_NOT_FOUND
+ mock_post.assert_called_once()
+ mock_capture_exception.assert_not_called()
+
+ @override_settings(
+ VERCEL_CLIENT_INTEGRATION_SECRET="test_webhook_secret",
+ SITE_URL="https://eu.posthog.com",
+ REGION_US_DOMAIN="us.posthog.com",
+ REGION_EU_DOMAIN="eu.posthog.com",
+ )
+ @patch("ee.api.vercel.webhook_events.outbound_requests.post")
+ def test_billing_event_unknown_config_on_eu_does_not_proxy(self, mock_post):
payload = {
"type": "marketplace.invoice.paid",
"payload": {"installationId": "icfg_unknown", "invoiceId": "mi_123"},
diff --git a/ee/api/vercel/webhook_events.py b/ee/api/vercel/webhook_events.py
index 2252b4e1ae52..b8222a628d3f 100644
--- a/ee/api/vercel/webhook_events.py
+++ b/ee/api/vercel/webhook_events.py
@@ -25,6 +25,9 @@ BILLING_EVENT_PREFIX = "marketplace.invoice."
DEAUTHORIZATION_EVENT = "integration-configuration.removed"
CROSS_REGION_PROXY_TIMEOUT = 10
+# EU waits up to 30 seconds for the billing service, so the billing hop needs a longer read
+# deadline. A shorter one makes US report a slow billing service as an unknown installation.
+CROSS_REGION_BILLING_PROXY_TIMEOUT = (10, 35)
DEFAULT_US_DOMAIN = "us.posthog.com"
DEFAULT_EU_DOMAIN = "eu.posthog.com"
@@ -37,7 +40,7 @@ class VercelEventOutcome(StrEnum):
IGNORED = "ignored"
# The event named no installation, so there is nothing to look up.
MISSING_CONFIG_ID = "missing_config_id"
- # A billing event for an installation this region does not hold.
+ # A billing event for an installation no region holds.
UNKNOWN_CONFIG = "unknown_config"
@@ -50,8 +53,12 @@ def _is_us_region() -> bool:
return settings.SITE_URL == f"https://{us_domain}"
-def _proxy_deauthorization_to_eu(raw_body: bytes, signature: str | None) -> int | None:
- """Forward a deauthorization webhook to EU. Returns the EU status code, or None on failure."""
+def _proxy_to_eu(
+ raw_body: bytes,
+ signature: str | None,
+ timeout: float | tuple[float, float] = CROSS_REGION_PROXY_TIMEOUT,
+) -> int | None:
+ """Forward a webhook to EU. Returns the EU status code, or None on failure."""
eu_domain = getattr(settings, "REGION_EU_DOMAIN", DEFAULT_EU_DOMAIN)
target_url = f"https://{eu_domain}/webhooks/vercel"
@@ -64,7 +71,7 @@ def _proxy_deauthorization_to_eu(raw_body: bytes, signature: str | None) -> int
url=target_url,
data=raw_body,
headers=headers,
- timeout=CROSS_REGION_PROXY_TIMEOUT,
+ timeout=timeout,
)
logger.info(
"vercel_webhook_proxied_to_eu",
@@ -149,7 +156,7 @@ def _handle_deauthorization(
if _is_us_region():
logger.info("vercel_webhook_deauthorize_proxying_to_eu", config_id=config_id)
- eu_status = _proxy_deauthorization_to_eu(raw_body, signature)
+ eu_status = _proxy_to_eu(raw_body, signature)
if eu_status is None or eu_status >= 300:
logger.warning(
"vercel_webhook_deauthorize_eu_proxy_non_ok",
@@ -163,13 +170,28 @@ def _handle_deauthorization(
return VercelEventOutcome.ACCEPTED
-def _handle_billing(event_type: str, payload: dict[str, Any], config_id: str) -> VercelEventOutcome:
+def _handle_billing(
+ event_type: str,
+ payload: dict[str, Any],
+ config_id: str,
+ raw_body: bytes,
+ signature: str | None,
+) -> VercelEventOutcome:
integration = _get_integration(config_id)
if not integration:
- logger.error("vercel_webhook_unknown_config", config_id=config_id)
- capture_exception(
- OrganizationIntegration.DoesNotExist(),
- {"config_id": config_id, "event_type": event_type},
+ # All Vercel webhooks arrive on US, so the installation can belong to EU.
+ eu_status = (
+ _proxy_to_eu(raw_body, signature, timeout=CROSS_REGION_BILLING_PROXY_TIMEOUT) if _is_us_region() else None
+ )
+ if eu_status is not None and eu_status < 300:
+ logger.info("vercel_webhook_billing_proxied_to_eu", config_id=config_id, event_type=event_type)
+ return VercelEventOutcome.ACCEPTED
+
+ logger.warning(
+ "vercel_webhook_unknown_config",
+ config_id=config_id,
+ event_type=event_type,
+ eu_status=eu_status,
)
return VercelEventOutcome.UNKNOWN_CONFIG
@@ -211,4 +233,4 @@ def handle_vercel_event(
# Guaranteed by the is_billing_event check above.
assert event_type is not None
- return _handle_billing(event_type, payload, config_id)
+ return _handle_billing(event_type, payload, config_id, raw_body, signature)