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: delivered password reset events only to endpoints subscribed by name
Damian Legawiecffc2af570f62
diff --git a/spree/api/app/controllers/spree/api/v3/admin/webhook_endpoints_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/webhook_endpoints_controller.rbindex 43ecd6b9fd..5617bbf85e 100644--- a/spree/api/app/controllers/spree/api/v3/admin/webhook_endpoints_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/webhook_endpoints_controller.rb@@ -8,6 +8,8 @@ module Spreeclass WebhookEndpointsController < ResourceControllerscoped_resource :webhooks+ before_action :reject_unauthorized_credential_subscription!, only: [:create, :update]+# POST /api/v3/admin/webhook_endpoints/:id/send_test## Fires a synthetic `webhook.test` delivery so admins can verify the@@ -68,6 +70,31 @@ module Spreedef permitted_paramsparams.permit(*model_additional_permitted_attributes, :name, :url, :active, subscriptions: [])end++ private++ # Customer password reset tokens are account credentials, so the+ # webhooks permission alone cannot subscribe an endpoint to them, or+ # repoint an endpoint that already receives them.+ def reject_unauthorized_credential_subscription!+ return if holds_permission?('write_customers')++ requested = Array(permitted_params[:subscriptions]) & Spree::WebhookEndpoint::CREDENTIAL_EVENTS+ return if requested.empty? && !repoints_credential_endpoint?++ render_error(+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],+ message: "Receiving #{Spree::WebhookEndpoint::CREDENTIAL_EVENTS.to_sentence} requires permission to manage customers",+ status: :forbidden+ )+ end++ def repoints_credential_endpoint?+ return false unless action_name == 'update'+ return false unless permitted_params.key?(:url) || permitted_params.key?(:subscriptions)++ current_store.webhook_endpoints.find_by_prefix_id(params[:id])&.receives_credentials?+ endendendenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/webhook_endpoints_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/webhook_endpoints_controller_spec.rbindex d0ab2af2a0..a6da56e623 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/webhook_endpoints_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/webhook_endpoints_controller_spec.rb@@ -74,6 +74,31 @@ RSpec.describe Spree::Api::V3::Admin::WebhookEndpointsController, type: :controlexpect(json_response['secret_key']).to eq(created.secret_key)end+ # A password reset token is as good as the customer's account, so the+ # webhooks permission alone cannot route one to an endpoint.+ context 'subscribing to customer password resets' do+ let(:params) { { url: 'https://example.com/hook', subscriptions: %w[customer.password_reset_requested] } }++ context 'with only the webhooks permission' do+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[write_webhooks]) }+ let(:headers) { { 'x-spree-api-key' => caller_key.plaintext_token } }++ it 'is forbidden' do+ expect { subject }.not_to change(Spree::WebhookEndpoint, :count)+ expect(response).to have_http_status(:forbidden)+ end+ end++ context 'with permission to manage customers' do+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[write_webhooks write_customers]) }+ let(:headers) { { 'x-spree-api-key' => caller_key.plaintext_token } }++ it 'creates the endpoint' do+ expect { subject }.to change(Spree::WebhookEndpoint, :count).by(1)+ end+ end+ end+context 'when the url is invalid' dolet(:params) { { url: 'not-a-url', subscriptions: [] } }@@ -102,6 +127,26 @@ RSpec.describe Spree::Api::V3::Admin::WebhookEndpointsController, type: :controlsubjectexpect(json_response['secret_key']).to be_nilend++ context 'on an endpoint that receives customer password resets' do+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[write_webhooks]) }+ let(:headers) { { 'x-spree-api-key' => caller_key.plaintext_token } }++ before { endpoint.update!(subscriptions: %w[customer.password_reset_requested]) }++ it 'refuses to repoint it with only the webhooks permission' do+ patch :update, params: { id: endpoint.prefixed_id, url: 'https://attacker.example/hook' }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(endpoint.reload.url).not_to eq('https://attacker.example/hook')+ end++ it 'still allows renaming it' do+ patch :update, params: { id: endpoint.prefixed_id, name: 'Renamed' }, as: :json++ expect(response).to have_http_status(:ok)+ end+ endenddescribe 'DELETE #destroy' dodiff --git a/spree/api/spec/subscribers/spree/webhook_event_subscriber_spec.rb b/spree/api/spec/subscribers/spree/webhook_event_subscriber_spec.rbindex 0de7553c1c..8295596903 100644--- a/spree/api/spec/subscribers/spree/webhook_event_subscriber_spec.rb+++ b/spree/api/spec/subscribers/spree/webhook_event_subscriber_spec.rb@@ -31,10 +31,18 @@ module Spreeendcontext 'with an event carrying a live credential' do- let!(:webhook_endpoint) { create(:webhook_endpoint, store: store, subscriptions: ['*']) }+ let!(:webhook_endpoint) { create(:webhook_endpoint, store: store, subscriptions: ['customer.password_reset_requested']) }let(:event_name) { 'customer.password_reset_requested' }let(:event_payload) { { reset_token: 'live-token', email: '[email protected]' } }+ # A wildcard is set up by whoever holds the webhooks permission; the+ # reset token reaches only an endpoint that names the event.+ it 'is not delivered to a wildcard subscription' do+ webhook_endpoint.update!(subscriptions: ['*'])++ expect { described_class.new.handle(event) }.not_to change(Spree::WebhookDelivery, :count)+ end+it 'keeps the token out of the persisted delivery log' dodescribed_class.new.handle(event)diff --git a/spree/core/app/models/spree/webhook_endpoint.rb b/spree/core/app/models/spree/webhook_endpoint.rbindex 28d8278920..fec49c86db 100644--- a/spree/core/app/models/spree/webhook_endpoint.rb+++ b/spree/core/app/models/spree/webhook_endpoint.rb@@ -50,11 +50,18 @@ module Spree# Number of consecutive failed deliveries before auto-disablingAUTO_DISABLE_THRESHOLD = 15+ # Events whose payload carries a live customer credential (a password+ # reset token). Receiving one is as good as holding the customer's account,+ # so they reach only an endpoint that names them — never through `*` or a+ # pattern — and the API asks for customer write access to point one at them.+ CREDENTIAL_EVENTS = %w[customer.password_reset_requested].freeze+# Check if this endpoint is subscribed to a specific event## @param event_name [String] the event name to check# @return [Boolean]def subscribed_to?(event_name)+ return subscriptions.to_a.include?(event_name) if CREDENTIAL_EVENTS.include?(event_name)return true if subscriptions.blank? || subscriptions.include?('*')subscriptions.any? do |subscription|@@ -67,6 +74,13 @@ module Spreeendend+ # Whether this endpoint receives customer credentials.+ #+ # @return [Boolean]+ def receives_credentials?+ (subscriptions.to_a & CREDENTIAL_EVENTS).any?+ end+# Returns all events this endpoint is subscribed to## @return [Array<String>]diff --git a/spree/core/spec/models/spree/webhook_endpoint_spec.rb b/spree/core/spec/models/spree/webhook_endpoint_spec.rbindex ffdb0d7634..70ee2971b1 100644--- a/spree/core/spec/models/spree/webhook_endpoint_spec.rb+++ b/spree/core/spec/models/spree/webhook_endpoint_spec.rb@@ -128,6 +128,23 @@ describe Spree::WebhookEndpoint, type: :model doendend+ # A password reset token is a live customer credential, so it reaches+ # only an endpoint that names the event.+ context 'with a customer credential event' do+ it 'is not delivered through a wildcard, a pattern or an empty list' do+ [[], ['*'], ['customer.*']].each do |subscriptions|+ endpoint = build(:webhook_endpoint, subscriptions: subscriptions)+ expect(endpoint.subscribed_to?('customer.password_reset_requested')).to be false+ end+ end++ it 'is delivered when named' do+ endpoint = build(:webhook_endpoint, subscriptions: %w[customer.password_reset_requested])++ expect(endpoint.subscribed_to?('customer.password_reset_requested')).to be true+ end+ end+context 'with specific subscriptions' dolet(:endpoint) { build(:webhook_endpoint, subscriptions: %w[order.created order.placed]) }