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
Reject malformed Stripe webhook payloads
Damian Legawiec7216393148b3
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rbindex eb156bc9ec..60d98bcf76 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb@@ -71,6 +71,11 @@ module SpreeStripereturn Stripe::Webhook.construct_event(raw_body, signature, secret)rescue Stripe::SignatureVerificationErrornext+ rescue JSON::ParserError+ # construct_event parses before it verifies, so a malformed body+ # never reaches the signature check. Unauthenticated garbage is a+ # rejected webhook, not a server error to retry.+ raise Spree::PaymentMethod::WebhookSignatureError, 'Malformed webhook payload'endraise Spree::PaymentMethod::WebhookSignatureError, 'Invalid webhook signature'diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex 9690f40c10..a0824157e8 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb@@ -1042,6 +1042,21 @@ RSpec.describe SpreeStripe::Gateway doexpect { result }.to raise_error(Spree::PaymentMethod::WebhookSignatureError)endend++ # construct_event parses the body before verifying it, so malformed input+ # never reaches the signature check — it must still be rejected, not+ # acknowledged as a server error.+ context 'when the payload is not valid JSON' do+ let(:gateway) { create(:stripe_gateway, :with_webhook_signing_secret, store: store) }++ before do+ allow(Stripe::Webhook).to receive(:construct_event).and_raise(JSON::ParserError.new('unexpected token'))+ end++ it 'raises a WebhookSignatureError' do+ expect { result }.to raise_error(Spree::PaymentMethod::WebhookSignatureError, /Malformed/)+ end+ endenddescribe 'webhook endpoint registration' do