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): drop invisible characters pasted into the secret key (#123257)

Tom Owers1c5df571f824
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 aaff0c82139e..4d1df1761e23 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
@@ -1,5 +1,6 @@
import functools
import dataclasses
+import unicodedata
from collections.abc import AsyncIterable, Iterable, Iterator
from typing import Any, Optional
@@ -256,8 +257,16 @@ INSTANCE_NOT_FOUND_MESSAGE = (
)
+def clean_secret_key(secret_key: str) -> str:
+ # A Clerk secret key is ASCII, so an invisible format character (zero-width space, byte order
+ # mark, direction mark) or Unicode whitespace at either end is debris from the copy, not part of
+ # the key. Remove it so the pasted key works instead of failing the latin-1 header encoding.
+ return "".join(char for char in secret_key if unicodedata.category(char) != "Cf").strip()
+
+
def validate_credentials(secret_key: str) -> tuple[bool, str | None]:
"""Validate Clerk API credentials by making a test request."""
+ secret_key = clean_secret_key(secret_key)
# 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. A carriage return or newline is ASCII but still
@@ -393,7 +402,7 @@ def clerk_source(
"base_url": "https://api.clerk.com/v1",
"auth": {
"type": "bearer",
- "token": secret_key,
+ "token": clean_secret_key(secret_key),
},
"headers": {
"Content-Type": "application/json",
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 302f1e148fe2..e40f447b84c9 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
@@ -381,7 +381,23 @@ class TestClerkValidateCredentials:
assert network.requests_log[0].path == "/v1/users"
assert network.requests_log[0].param("limit") == "1"
- @pytest.mark.parametrize("secret_key", ["sk_live_\u200bkey", "sk_live_\u3042key"])
+ @pytest.mark.parametrize(
+ "secret_key",
+ ["\u200bsk_live_key", "sk_live_\u200bkey", "sk_live_key\ufeff", "\u00a0sk_live_key", "sk_live_key\n"],
+ )
+ def test_invisible_paste_debris_is_removed_before_the_request(self, secret_key: str) -> None:
+ with scripted_network([ScriptedResponse(json=[])]) as network:
+ is_valid, message = validate_credentials(secret_key)
+ assert (is_valid, message) == (True, None)
+ assert network.requests_log[0].headers["authorization"] == "Bearer sk_live_key"
+
+ def test_sync_sends_the_cleaned_key(self) -> None:
+ driver = SourceDriver(ClerkSource(), ClerkSourceConfig(secret_key="\u200bsk_live_key\n"))
+ result = driver.run(_DIRECT_ARRAY_ENDPOINT, [ScriptedResponse(json=[])])
+ assert result.raised is None
+ assert result.requests[0].headers["authorization"] == "Bearer sk_live_key"
+
+ @pytest.mark.parametrize("secret_key", ["sk_live_\u3042key", "sk_live_\u00a0key"])
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.