Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: Stripe webhook registration losing the Connect signing secret
Łukasz Adamczykc4f72295f0b1
diff --git a/spree/providers/stripe/app/jobs/spree_stripe/create_webhook_endpoint_job.rb b/spree/providers/stripe/app/jobs/spree_stripe/create_webhook_endpoint_job.rbindex 1a8842e77a..b0e3d5735c 100644--- a/spree/providers/stripe/app/jobs/spree_stripe/create_webhook_endpoint_job.rb+++ b/spree/providers/stripe/app/jobs/spree_stripe/create_webhook_endpoint_job.rb@@ -3,13 +3,19 @@ module SpreeStripe# @param connect [Boolean] true registers the connected-accounts endpoint,# which a marketplace holds alongside its payment onedef perform(payment_method_id, connect: false)- payment_method = Spree::PaymentMethod.find_by(id: payment_method_id)- return if payment_method.blank?+ # Both endpoints store their credentials in the one serialised+ # `preferences` column, so concurrent jobs would drop each other's+ # signing secret. Locked on load because `with_lock` refuses a record+ # with unsaved changes, and reading a preference populates its default.+ Spree::PaymentMethod.transaction do+ payment_method = Spree::PaymentMethod.lock.find_by(id: payment_method_id)+ next if payment_method.blank?- if connect- payment_method.create_connect_webhook_endpoint- else- payment_method.create_webhook_endpoint+ if connect+ payment_method.create_connect_webhook_endpoint+ else+ payment_method.create_webhook_endpoint+ endendendenddiff --git a/spree/providers/stripe/app/models/spree_stripe/gateway.rb b/spree/providers/stripe/app/models/spree_stripe/gateway.rbindex d494a1ae29..6b5b3f03b4 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway.rb@@ -11,6 +11,14 @@ module SpreeStripepreference :publishable_key, :passwordpreference :secret_key, :password+ # Written by CreateGatewayWebhooks, never by an operator.+ WEBHOOK_REGISTRATION_PREFERENCES = %i[+ webhook_endpoint_id+ webhook_signing_secret+ connect_webhook_endpoint_id+ connect_webhook_signing_secret+ ].freeze+validates :preferred_secret_key, :preferred_publishable_key, presence: truevalidate :validate_secret_key, unless: -> { Rails.env.test? }, if: -> { preferred_secret_key.present? }@@ -34,6 +42,37 @@ module SpreeStripetrueend+ # Whether this save changed the registration preferences and nothing else,+ # meaning it was registration storing its own result — which would+ # otherwise re-trigger it and enqueue a duplicate job. Read from the+ # change rather than a flag around the write: the job's row lock defers+ # this callback past the point any flag would be cleared.+ #+ # @return [Boolean]+ def only_webhook_registration_changed?+ before, after = saved_change_to_preferences+ return false unless before.is_a?(Hash) && after.is_a?(Hash)++ changed = (before.keys | after.keys).reject { |key| before[key] == after[key] }++ changed.any? && changed.all? { |key| WEBHOOK_REGISTRATION_PREFERENCES.include?(key.to_sym) }+ end++ # Endpoints are registered per Stripe account, so a rotated key leaves the+ # stored ids and secrets pointing at endpoints the new account lacks.+ #+ # @return [Boolean]+ def stripe_secret_key_changed?+ # Preferences change from an empty hash on create, which would otherwise+ # read as every key having changed.+ return false if previously_new_record?++ before, after = saved_change_to_preferences+ return false unless before.is_a?(Hash) && after.is_a?(Hash)++ before[:secret_key] != after[:secret_key]+ end+def gateway_dashboard_payment_url(payment)return if payment.transaction_id.blank?diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rbindex d1e98b6ffd..3edf30488a 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb@@ -303,7 +303,8 @@ module SpreeStripeenddef create_connect_webhook_endpoint_async- return if preferred_connect_webhook_signing_secret.present?+ return if only_webhook_registration_changed?+ return if preferred_connect_webhook_signing_secret.present? && !stripe_secret_key_changed?SpreeStripe::CreateWebhookEndpointJob.perform_later(id, connect: true)enddiff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rbindex 6f58716fa4..33b6a19f23 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb@@ -94,11 +94,11 @@ module SpreeStripesecrets.select(&:present?)end- # Also the loop guard: registration writes the secret back through- # `update!`, which re-runs this callback. A stored secret means the- # endpoint is already registered, so the second pass stops here.+ # A stored secret means the endpoint is already registered — unless the+ # key now points at a different Stripe account.def create_webhook_endpoint_async- return if preferred_webhook_signing_secret.present?+ return if only_webhook_registration_changed?+ return if preferred_webhook_signing_secret.present? && !stripe_secret_key_changed?SpreeStripe::CreateWebhookEndpointJob.perform_later(id, connect: false)enddiff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex e1199418fb..804e13601c 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb@@ -1182,11 +1182,26 @@ RSpec.describe SpreeStripe::Gateway do# the guard against registering that endpoint twice.describe 'webhook endpoint registration' doit 'enqueues the payment registration job on create' do- expect { gateway }.to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).with(anything, connect: false)+ expect { gateway }.to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).+ with(anything, connect: false).exactly(:once)endit 'enqueues the Connect registration job on create' do- expect { gateway }.to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).with(anything, connect: true)+ expect { gateway }.to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).+ with(anything, connect: true).exactly(:once)+ end++ # Both endpoints are enqueued on create, so registration storing its+ # result must not enqueue a third.+ it 'does not enqueue anything when the registration job stores its result' do+ gateway++ allow(Stripe::WebhookEndpoint).to receive(:list).and_return({ data: [] })+ allow(Stripe::WebhookEndpoint).to receive(:create).+ and_return({ id: 'we_test_1234567890', secret: 'whsec_test_1234567890' })++ expect { SpreeStripe::CreateWebhookEndpointJob.perform_now(gateway.id, connect: false) }.+ not_to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob)endcontext 'when the payment signing secret is already stored' do@@ -1202,6 +1217,21 @@ RSpec.describe SpreeStripe::Gateway docreate(:stripe_gateway, :with_webhook_signing_secret, store: store)end.to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).with(anything, connect: true)end++ it 'leaves it alone on an unrelated save' do+ stored = create(:stripe_gateway, :with_webhook_signing_secret, store: store)++ expect { stored.update!(name: 'Stripe renamed') }.+ not_to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).with(anything, connect: false)+ end++ # A rotated key points at an account that has never seen this endpoint.+ it 'registers again when the secret key is rotated' do+ stored = create(:stripe_gateway, :with_webhook_signing_secret, store: store)++ expect { stored.update!(preferred_secret_key: 'sk_test_rotated') }.+ to have_enqueued_job(SpreeStripe::CreateWebhookEndpointJob).with(anything, connect: false)+ endendend# Stripe issues these when Spree registers an endpoint, and only reveals a