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): treat 403 permission-denied errors as non-retryable during sync (#63757)

Tom Owers6ca69163857c
diff --git a/posthog/temporal/data_imports/sources/stripe/source.py b/posthog/temporal/data_imports/sources/stripe/source.py
index 13b7bd59fe65..adf99fcc2d74 100644
--- a/posthog/temporal/data_imports/sources/stripe/source.py
+++ b/posthog/temporal/data_imports/sources/stripe/source.py
@@ -237,7 +237,20 @@ If automatic creation failed due to a permissions error and you're using a restr
# continue"), which is more actionable than a generic "check your permissions" toast.
# `_clean_stripe_error_message` collapses the redacted-key asterisk run before the
# message reaches this layer, so it stays toast-sized.
+ #
+ # NOTE: this `"PermissionError"` key only matches the refresh-schemas path, which compares
+ # against `f"{type(error).__name__}: {message}"`. The import/sync path compares against
+ # `str(exc)` only — and `StripeError.__str__` returns `"Request <id>: <message>"` with no
+ # class name — so the type-name key never matches a 403 raised mid-sync. Match Stripe's
+ # stable permission-denied message text directly so a misconfigured key stops retrying.
"PermissionError": None,
+ # Restricted key is missing a read scope for the endpoint being synced (e.g. "Prices Read"
+ # / 'plan_read'). The customer must add the scope in Stripe — retrying won't help. Surface
+ # Stripe's raw message (None) since it names the exact scope to enable.
+ "does not have the required permissions for this endpoint": None,
+ # A non-Connect key was sent with a `stripe_account` header (the source's "Account id"),
+ # so Stripe rejects the whole request for the account rather than a specific scope.
+ "Only Stripe Connect platforms can work with other accounts": "Stripe rejected the request because your API key isn't authorized for the configured Stripe account. The 'Account id' in your source settings only applies to Stripe Connect platform accounts — remove or correct it if your key belongs directly to the account, then reconnect.",
# Deterministic credential/config errors from _get_api_key and OAuthMixin
"Missing Stripe API key": "Stripe API key is not configured. Please update the source configuration.",
"Missing Stripe integration ID": "Stripe integration ID is not configured. Please reconnect your Stripe account.",
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
new file mode 100644
index 000000000000..c6e625c18606
--- /dev/null
+++ b/posthog/temporal/data_imports/sources/stripe/tests/test_stripe_source.py
@@ -0,0 +1,42 @@
+import pytest
+
+from posthog.temporal.data_imports.sources.stripe.source import StripeSource
+
+from products.data_warehouse.backend.types import ExternalDataSourceType
+
+
+class TestStripeSource:
+ def setup_method(self):
+ self.source = StripeSource()
+
+ def test_source_type(self):
+ assert self.source.source_type == ExternalDataSourceType.STRIPE
+
+ @pytest.mark.parametrize(
+ "observed_error",
+ [
+ # 403 raised mid-sync — `str(StripeError)` is "Request <id>: <message>", with no class
+ # name, so these are matched on the stable message text rather than "PermissionError".
+ "Request req_Zb0EgUuheEd4gf: Permission denied. The provided key 'rk_live_***j4va7j' does not have the required permissions for this endpoint on account 'acct_123'. Enabling \"Prices Read\" ('plan_read') permissions on this key would allow this request to continue.",
+ "Request req_abc123: Only Stripe Connect platforms can work with other accounts. If you specified a client_id parameter, make sure it's correct.",
+ # 401/403 surfaced as a requests HTTPError keep matching the existing URL-based keys.
+ "401 Client Error: Unauthorized for url: https://api.stripe.com/v1/customers",
+ "403 Client Error: Forbidden for url: https://api.stripe.com/v1/prices",
+ ],
+ )
+ def test_non_retryable_errors_match_permission_failures(self, observed_error):
+ non_retryable_errors = self.source.get_non_retryable_errors()
+ assert any(key in observed_error for key in non_retryable_errors)
+
+ @pytest.mark.parametrize(
+ "other_error",
+ [
+ # Transient/infra errors must stay retryable.
+ "HTTPSConnectionPool(host='api.stripe.com', port=443): Read timed out.",
+ "500 Server Error: Internal Server Error for url: https://api.stripe.com/v1/charges",
+ "Connection reset by peer",
+ ],
+ )
+ def test_non_retryable_errors_do_not_match_transient(self, other_error):
+ non_retryable_errors = self.source.get_non_retryable_errors()
+ assert not any(key in other_error for key in non_retryable_errors)