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 containing a return character (#107332)

Tom Owers278400ea3082
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 af1ad131f10c..a7ad7805e205 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
@@ -252,8 +252,10 @@ 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():
+ # than letting the encoding error surface. A carriage return or newline is ASCII but still
+ # breaks the header (requests raises InvalidHeader), which happens when someone pastes a whole
+ # multi-line snippet — e.g. a .env file — into the secret key field instead of just the key.
+ if not secret_key.isascii() or "\r" in secret_key or "\n" in secret_key:
return False, _UNSUPPORTED_CHARACTER_MESSAGE
url = "https://api.clerk.com/v1/users"
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 d79890ce4a3e..18a972a905b7 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
@@ -644,6 +644,19 @@ class TestClerkValidateCredentials:
assert "latin-1" not in (message or "")
mock_session.assert_not_called()
+ @pytest.mark.parametrize("secret_key", ["sk_live_key\rextra_pasted_content", "sk_live_key\nextra_pasted_content"])
+ def test_key_with_return_character_is_rejected_before_any_request(self, secret_key: str) -> None:
+ # A carriage return or newline is ASCII, so it passes isascii(), but requests still rejects
+ # it as an invalid header value (InvalidHeader) when dispatching. That exception is a
+ # RequestException subclass, so it was previously swallowed by the generic network-error
+ # handler below and reported as a transient "couldn't reach Clerk" failure instead of
+ # explaining the malformed key.
+ 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 "")
+ 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")