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(stripe): stop echoing the rejected key in the credentials error (#64995)

Tom Owers680118a47b9d
diff --git a/posthog/temporal/data_imports/sources/stripe/source.py b/posthog/temporal/data_imports/sources/stripe/source.py
index 4454f033bb22..8e51e1100552 100644
--- a/posthog/temporal/data_imports/sources/stripe/source.py
+++ b/posthog/temporal/data_imports/sources/stripe/source.py
@@ -342,15 +342,18 @@ If automatic creation failed due to a permissions error and you're using a restr
return True, None
else:
return False, "Invalid Stripe credentials"
- except StripeAuthenticationError as e:
+ except StripeAuthenticationError:
if config.auth_method.selection == "oauth":
return (
False,
"Your Stripe OAuth connection has expired or been revoked. Please reconnect your Stripe account.",
)
+ # Stripe's 401 body echoes the rejected key verbatim, so interpolating `e.stripe_message`
+ # leaks whatever the user pasted (often a password) into the toast and the
+ # `warehouse credentials invalid` analytics event. The guidance below stands on its own.
return (
False,
- f"Stripe rejected the API key: {e.stripe_message}. Double-check that you pasted a restricted key (rk_live_...) for the same Stripe account, with no extra whitespace, and that it has not been revoked.",
+ "Stripe rejected the API key. Double-check that you pasted a restricted key (rk_live_...) for the same Stripe account, with no extra whitespace, and that it has not been revoked.",
)
except StripePermissionError as e:
# 403s are self-explanatory — the resource name tells the customer which Stripe scope
diff --git a/posthog/temporal/data_imports/sources/stripe/tests/test_stripe_source.py b/posthog/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
index acd23f58cb57..686bcea90178 100644
--- a/posthog/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
+++ b/posthog/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -1,7 +1,9 @@
import pytest
+from unittest import mock
from posthog.temporal.data_imports.sources.generated_configs import StripeAuthMethodConfig, StripeSourceConfig
from posthog.temporal.data_imports.sources.stripe.source import StripeSource
+from posthog.temporal.data_imports.sources.stripe.stripe import StripeAuthenticationError
class TestStripeSource:
@@ -64,3 +66,22 @@ class TestStripeSource:
assert ok is False
assert message == expected_message
+
+ def test_validate_credentials_does_not_echo_rejected_key(self):
+ # Stripe's 401 body echoes the submitted key verbatim; here the user pasted a password into
+ # the key field. The validation message must not leak it into the toast or analytics event.
+ pasted_secret = "Ammgad1979@"
+ config = StripeSourceConfig(
+ auth_method=StripeAuthMethodConfig(selection="api_key", stripe_secret_key=pasted_secret)
+ )
+
+ with mock.patch(
+ "posthog.temporal.data_imports.sources.stripe.source.validate_stripe_credentials",
+ side_effect=StripeAuthenticationError(f"Invalid API Key provided: {pasted_secret}"),
+ ):
+ ok, message = self.source.validate_credentials(config, team_id=1)
+
+ assert ok is False
+ assert message is not None
+ assert pasted_secret not in message
+ assert message.startswith("Stripe rejected the API key.")