: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(vercel): tell users to replace a token Vercel rejects (#107225)
Tom Owersb8c7f7b46f3e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/tests/test_vercel.py b/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/tests/test_vercel.pyindex f03aa52ac710..baa7fd9e0f9d 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/tests/test_vercel.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/tests/test_vercel.py@@ -194,18 +194,44 @@ class TestValidateCredentials:# A transient Vercel-side error must not tell the user to fix their (possibly valid) token.assert "Check that it's a valid token" not in (error or "")- def test_unexpected_status_does_not_leak_raw_status_code(self) -> None:+ @parameterized.expand([(400,), (401,)])+ def test_credential_rejection_status_tells_the_user_to_replace_the_token(self, status: int) -> None:response = requests.Response()- response.status_code = 404+ response.status_code = statussession = MagicMock()session.get.return_value = responsewith patch.object(vercel, "make_tracked_session", lambda *a, **k: session):ok, error = validate_credentials("token")+ assert ok is False+ assert error == vercel._VERCEL_INVALID_TOKEN_ERROR++ def test_non_ascii_token_is_rejected_without_a_request(self) -> None:+ session = MagicMock()+ with patch.object(vercel, "make_tracked_session", lambda *a, **k: session):+ ok, error = validate_credentials("tok\u00e9n")++ assert ok is False+ assert error == vercel._VERCEL_UNSUPPORTED_CHARACTER_ERROR+ assert session.get.call_count == 0++ def test_unexpected_status_does_not_leak_raw_status_code(self) -> None:+ response = requests.Response()+ response.status_code = 418+ session = MagicMock()+ session.get.return_value = response+ with (+ patch.object(vercel, "make_tracked_session", lambda *a, **k: session),+ patch.object(vercel, "capture_exception") as capture,+ ):+ ok, error = validate_credentials("token")+assert ok is Falseassert error is not Noneassert "Vercel API error" not in error- assert "404" not in error+ assert "418" not in error+ # The status has to reach error tracking, or a later triage has only the generic message.+ assert "418" in str(capture.call_args.args[0])class TestGetRows:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/vercel.py b/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/vercel.pyindex bd25d1b5cc5a..5d39609cd37b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/vercel.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/vercel/vercel.py@@ -16,6 +16,8 @@ import requestsfrom structlog.types import FilteringBoundLoggerfrom tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_exponential_jitter+from posthog.exceptions_capture import capture_exception+from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.batcher import Batcherfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_sessionfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager@@ -49,6 +51,18 @@ MAX_PAGES = 10_000# user at their credentials would send them chasing a problem they can't fix._VERCEL_UNREACHABLE_ERROR = "Couldn't reach Vercel to validate your access token. Please try again in a few minutes."+# Shown when Vercel won't accept the token at all. A revoked token and a malformed one leave the+# user with the same next step, so they share a message.+_VERCEL_INVALID_TOKEN_ERROR = (+ "Your Vercel access token is invalid or has been revoked. Create a new token in your Vercel "+ "account settings, then reconnect."+)++_VERCEL_UNSUPPORTED_CHARACTER_ERROR = (+ "Your Vercel access token contains a character that can't be sent to Vercel. Copy the token "+ "again from your Vercel account settings, then reconnect."+)+class VercelRetryableError(Exception):pass@@ -150,6 +164,12 @@ def _cursor_from_page(items: list[dict[str, Any]], field_name: str) -> int | Nondef validate_credentials(access_token: str) -> tuple[bool, str | None]:"""Confirm the access token is genuine via GET /v2/user — the cheapest authenticated probe,available to any valid Vercel token regardless of team scope or resource permissions."""+ # The token rides in the Authorization header, which http.client encodes as latin-1. A character+ # outside that range raises UnicodeEncodeError mid-request. That is not a RequestException, so it+ # would escape as an internal error; reject it as user input instead.+ if not access_token.isascii():+ return False, _VERCEL_UNSUPPORTED_CHARACTER_ERROR+try:response = make_tracked_session().get(f"{VERCEL_BASE_URL}/v2/user", headers=_get_headers(access_token), timeout=10@@ -161,11 +181,11 @@ def validate_credentials(access_token: str) -> tuple[bool, str | None]:if response.status_code == 200:return True, None- if response.status_code == 401:- return (- False,- "Your Vercel access token is invalid or has been revoked. Create a new token in your Vercel account settings, then reconnect.",- )+ if response.status_code in (400, 401):+ # The probe carries no query string and no body, so a 400 is Vercel rejecting the token+ # itself rather than anything we sent — a token pasted with stray characters reads as+ # malformed at the gateway before it is ever looked up.+ return False, _VERCEL_INVALID_TOKEN_ERRORif response.status_code == 403:return (False,@@ -175,6 +195,9 @@ def validate_credentials(access_token: str) -> tuple[bool, str | None]:# retry hint rather than telling the user to fix credentials they can't fix.if response.status_code == 429 or response.status_code >= 500:return False, _VERCEL_UNREACHABLE_ERROR+ # Every status this endpoint is known to answer with is handled above, so keep the raw one for+ # us — without it the generic message below is all a later triage has to work from.+ capture_exception(Exception(f"Unexpected Vercel credential validation response ({response.status_code})"))return (False,"Couldn't validate your Vercel access token. Check that it's a valid token from your Vercel account settings, then try again.",