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(clerk): reject a secret key that can't be sent in a header (#99116)

Tom Owersee3cc5bb156a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
index 36c0725a979b..6ddf1eafd9bb 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
@@ -238,10 +238,20 @@ _FORBIDDEN_KEY_MESSAGE = (
"Your Clerk secret key does not have permission to access this endpoint. Please check the "
"key's permissions in your Clerk dashboard."
)
+_UNSUPPORTED_CHARACTER_MESSAGE = (
+ "Your Clerk secret key contains a character that can't be sent to Clerk, such as an invisible "
+ "one pasted from another app. Copy the key again from your Clerk dashboard and reconnect."
+)
def validate_credentials(secret_key: str) -> tuple[bool, str | None]:
"""Validate Clerk API credentials by making a test request."""
+ # The key rides in the Authorization header, which http.client encodes as latin-1. A character
+ # outside that range raises UnicodeEncodeError mid-request, so reject it as user input rather
+ # than letting the encoding error surface.
+ if not secret_key.isascii():
+ return False, _UNSUPPORTED_CHARACTER_MESSAGE
+
url = "https://api.clerk.com/v1/users"
headers = {
"Authorization": f"Bearer {secret_key}",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
index 9f782b664669..e16029793e9f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
@@ -619,6 +619,17 @@ class TestClerkValidateCredentials:
validate_credentials("sk_test_key")
assert mock_capture.called is should_capture
+ @pytest.mark.parametrize("secret_key", ["sk_live_\u200bkey", "sk_live_\u3042key"])
+ def test_non_ascii_key_is_rejected_before_any_request(self, secret_key: str) -> None:
+ # Such a key can't be encoded into the Authorization header, so dispatching the request
+ # raises UnicodeEncodeError; the guard must catch it first and explain what to do.
+ with patch(_VALIDATE_SESSION) as mock_session:
+ is_valid, message = validate_credentials(secret_key)
+ assert is_valid is False
+ assert "Copy the key again" in (message or "")
+ assert "latin-1" not in (message or "")
+ mock_session.assert_not_called()
+
def test_network_error_returns_actionable_message_without_leaking_exception(self) -> None:
with patch(_VALIDATE_SESSION) as mock_session:
mock_session.return_value.get.side_effect = RequestException("connection reset by peer")