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(revenuecat): reject api keys that cannot be sent in a header (#119157)

Tom Owers184324de2842
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py
index 75e569174a8e..7d74ffab858c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/revenuecat.py
@@ -179,6 +179,12 @@ def _project_not_found_error(entered_project_id: str, accessible_ids: list[str])
)
+UNSUPPORTED_KEY_CHARACTER_MESSAGE = (
+ "Your RevenueCat API key contains an unsupported character, such as a curly quote, a line break, "
+ "or an invisible character pasted from another app. Copy the key again from RevenueCat and paste "
+ "only the key."
+)
+
# A transport failure leaves the key unjudged, so it must not read as a credential problem.
UNREACHABLE_MESSAGE = "PostHog couldn't reach RevenueCat. Wait a few minutes and try again."
@@ -242,6 +248,12 @@ def validate_credentials(api_key: str, project_id: str | None) -> tuple[bool, st
error names the project ids the key *can* reach instead of a dead-end
"double-check the project id".
"""
+ # The key rides in the Authorization header, which http.client encodes as latin-1, so a
+ # non-ASCII character raises UnicodeEncodeError mid-request. A line break is ASCII but makes
+ # requests raise InvalidHeader, which would otherwise read as RevenueCat being unreachable.
+ if not api_key.isascii() or "\r" in api_key or "\n" in api_key:
+ return False, UNSUPPORTED_KEY_CHARACTER_MESSAGE
+
session = _session(api_key)
normalized_project_id = _normalize_project_id(project_id)
try:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py
index 189ace07734e..055bb98e83f1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/revenuecat/tests/test_revenuecat.py
@@ -126,6 +126,18 @@ class TestValidateCredentials:
probed_url = mock_session.return_value.get.call_args_list[1].args[0]
assert probed_url.startswith(f"{REVENUECAT_API_BASE_URL}/projects/proj_test/")
+ @parameterized.expand(
+ [
+ ("curly_quote", "sk_test\u2019"),
+ ("line_break", "sk_test\nREVENUECAT_PROJECT_ID=proj_test"),
+ ]
+ )
+ def test_rejects_key_that_cannot_be_sent_in_a_header(self, _name, api_key):
+ success, error = api_client.validate_credentials(api_key, project_id="proj_test")
+
+ assert success is False
+ assert error == api_client.UNSUPPORTED_KEY_CHARACTER_MESSAGE
+
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.revenuecat.revenuecat._session")
def test_returns_false_on_network_error(self, mock_session):
mock_session.return_value.get.side_effect = requests.ConnectionError("dns fail")