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(cloudflare): explain an unreadable api token instead of echoing code 6003 (#113735)

Tom Owers9b5fe1ee0561
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/cloudflare.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/cloudflare.py
index 914854f98be9..4fb3d5949627 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/cloudflare.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/cloudflare.py
@@ -314,6 +314,11 @@ def _fanout_resource(
return resources[endpoint].add_map(_rename_parent_key)
+# Cloudflare answers 6003 when it can't parse the Authorization header at all, which happens with a
+# Global API Key or a token pasted with extra characters, never with a revoked or expired token.
+_MALFORMED_TOKEN_ERROR_CODE = 6003
+
+
def _transient_status(status: int | None) -> bool:
"""Cloudflare was unreachable or busy rather than refusing the token."""
return status is None or status == 429 or status >= 500
@@ -330,11 +335,16 @@ class TokenCheck:
is_valid: bool
status: int | None
reason: str | None = None
+ code: int | None = None
@property
def is_transient(self) -> bool:
return not self.is_valid and _transient_status(self.status)
+ @property
+ def is_malformed(self) -> bool:
+ return not self.is_valid and self.code == _MALFORMED_TOKEN_ERROR_CODE
+
def _cloudflare_error(response: Response) -> str | None:
"""Cloudflare's own reason for refusing a request, from the `errors` array it returns.
@@ -358,6 +368,17 @@ def _cloudflare_error(response: Response) -> str | None:
return None
+def _cloudflare_error_code(response: Response) -> int | None:
+ try:
+ errors = response.json().get("errors") or []
+ except Exception:
+ return None
+ for error in errors:
+ if isinstance(error, dict) and isinstance(error.get("code"), int):
+ return error["code"]
+ return None
+
+
def _get(api_token: str, path: str, **params: Any) -> Optional[Response]:
try:
return make_tracked_session(redact_values=(api_token,)).get(
@@ -395,6 +416,12 @@ def validate_credentials(api_token: str) -> TokenCheck:
if _transient_status(status):
return TokenCheck(is_valid=False, status=status)
+ assert verify is not None # a None verify is transient, handled above
+ code = _cloudflare_error_code(verify)
+ if code == _MALFORMED_TOKEN_ERROR_CODE:
+ # The probes send the same header, so they can only fail the same way.
+ return TokenCheck(is_valid=False, status=status, reason=_cloudflare_error(verify), code=code)
+
# Verify refused, but that refusal is not a verdict on the token, so these probes are what
# decides. A probe that never got an answer therefore leaves the question open instead of
# confirming the refusal — reporting "rejected" there would send someone off to rebuild a
@@ -413,8 +440,7 @@ def validate_credentials(api_token: str) -> TokenCheck:
if transient:
return TokenCheck(is_valid=False, status=transient[0])
- assert verify is not None # a None verify is transient, handled above
- return TokenCheck(is_valid=False, status=status, reason=_cloudflare_error(verify))
+ return TokenCheck(is_valid=False, status=status, reason=_cloudflare_error(verify), code=code)
def cloudflare_source(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py
index 333ebe56af91..07f88abd94ca 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py
@@ -124,6 +124,12 @@ Create an API token in the [Cloudflare dashboard](https://dash.cloudflare.com/pr
False,
"Couldn't reach Cloudflare to verify your API token. Try again in a moment.",
)
+ if check.is_malformed:
+ return (
+ False,
+ "Cloudflare couldn't read your API token. Paste an API token, not the Global API Key, "
+ "with no extra spaces, then reconnect.",
+ )
# Naming Cloudflare's own reason matters more than naming a remedy: the token is refused
# for reasons permissions never explain, so guessing one sends people round in circles.
detail = f" Cloudflare said: {check.reason}." if check.reason else ""
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py
index f46670f0e868..0fe582b3c527 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py
@@ -113,7 +113,8 @@ class TestValidateCredentials:
def test_invalid_on_error_status(self, mock_session, status_code) -> None:
mock_session.return_value.get.return_value = _error_response(status_code)
# The status flows back so the caller can tell a rejected token from a 5xx/unreachable one.
- assert validate_credentials("token") == TokenCheck(is_valid=False, status=status_code)
+ check = validate_credentials("token")
+ assert (check.is_valid, check.status) == (False, status_code)
@mock.patch(CLOUDFLARE_SESSION_PATCH)
def test_transient_status_skips_the_fallback_probe(self, mock_session) -> None:
@@ -161,7 +162,16 @@ class TestValidateCredentials:
_error_response(403),
_error_response(403),
]
- assert validate_credentials("token").is_transient is False
+ check = validate_credentials("token")
+ assert check.is_transient is False
+ assert check.is_malformed is False
+
+ @mock.patch(CLOUDFLARE_SESSION_PATCH)
+ def test_unparseable_authorization_header_is_malformed(self, mock_session) -> None:
+ mock_session.return_value.get.return_value = _rejected_response(400, "Invalid request headers", code=6003)
+ assert validate_credentials("token").is_malformed is True
+ # The probes send the same header, so a transient probe must not hide the malformed verdict.
+ assert mock_session.return_value.get.call_count == 1
class TestPagination:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_source.py
index 1bc5a0aa7b89..037666f6c61f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_source.py
@@ -36,6 +36,11 @@ class TestCloudflareSource:
False,
"Invalid API Token (code 1000)",
),
+ (
+ TokenCheck(is_valid=False, status=400, reason="Invalid request headers (code 6003)", code=6003),
+ False,
+ "not the Global API Key",
+ ),
(TokenCheck(is_valid=False, status=None), False, "Couldn't reach Cloudflare"),
(TokenCheck(is_valid=False, status=500), False, "Couldn't reach Cloudflare"),
(TokenCheck(is_valid=False, status=429), False, "Couldn't reach Cloudflare"),