Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 SpreeStripe
return Stripe::Webhook.construct_event(raw_body, signature, secret)
rescue Stripe::SignatureVerificationError
next
+ 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'
end
raise 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.rb
index 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 do
expect { result }.to raise_error(Spree::PaymentMethod::WebhookSignatureError)
end
end
+
+ # 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
+ end
end
describe 'webhook endpoint registration' do