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.

Fix: showed webhook delivery payloads only to callers who can read the record

Damian Legawiecf2648ec78466
diff --git a/packages/admin-sdk/src/types/generated/WebhookDelivery.ts b/packages/admin-sdk/src/types/generated/WebhookDelivery.ts
index f758fd6ef9..57281caf3c 100644
--- a/packages/admin-sdk/src/types/generated/WebhookDelivery.ts
+++ b/packages/admin-sdk/src/types/generated/WebhookDelivery.ts
@@ -1,4 +1,4 @@
-// Typelizer digest cbd21ce71269a0c670b712f661ab159a
+// Typelizer digest 4db5eedcd5536d805072067be16c29bf
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
@@ -15,7 +15,7 @@ interface WebhookDelivery {
created_at: string;
updated_at: string;
delivered_at: string | null;
- payload: Record<string, unknown>;
+ payload: Record<string, unknown> | null;
webhook_endpoint_id: string;
webhook_endpoint_url: string;
}
diff --git a/spree/api/app/controllers/spree/api/v3/admin/webhook_deliveries_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/webhook_deliveries_controller.rb
index 4894178698..a6ef3885f8 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/webhook_deliveries_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/webhook_deliveries_controller.rb
@@ -42,6 +42,18 @@ module Spree
params[:webhook_endpoint_id]
)
end
+
+ # The payload is the record the event is about, so it is shown only
+ # to a caller who could read that record; the webhooks permission
+ # alone sees the delivery's status and response.
+ def serializer_params
+ super.merge(
+ payload_visible: lambda { |delivery|
+ key = delivery.payload_permission_key
+ key.nil? || holds_permission?(key)
+ }
+ )
+ end
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/admin/webhook_delivery_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/webhook_delivery_serializer.rb
index 9906f469bc..52d689b0de 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/webhook_delivery_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/webhook_delivery_serializer.rb
@@ -19,7 +19,7 @@ module Spree
response_body: [:string, nullable: true],
success: [:boolean, nullable: true],
delivered_at: [:string, nullable: true],
- payload: 'Record<string, unknown>',
+ payload: ['Record<string, unknown>', nullable: true],
webhook_endpoint_id: :string,
webhook_endpoint_url: :string
@@ -29,8 +29,12 @@ module Spree
delivered_at: :iso8601
# Redacted again on read: deliveries written before payload redaction
- # shipped still hold live credentials in the column.
+ # shipped still hold live credentials in the column. Null when the
+ # caller cannot read the record the event is about.
attribute :payload do |delivery|
+ visible = params[:payload_visible]
+ next nil if visible && !visible.call(delivery)
+
Spree::WebhookPayloadRedaction.split(delivery.payload).first
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb
index 81dc806bda..7dabbfd6a8 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb
@@ -79,6 +79,69 @@ RSpec.describe Spree::Api::V3::Admin::WebhookDeliveriesController, type: :contro
end
end
+ # The payload is the record the event is about; the webhooks permission
+ # alone does not grant reading orders or customers.
+ describe 'payload visibility' do
+ subject do
+ get :show,
+ params: { webhook_endpoint_id: endpoint.prefixed_id, id: failed_delivery.prefixed_id },
+ as: :json
+ end
+
+ let(:headers) { { 'x-spree-api-key' => caller_key.plaintext_token } }
+
+ context 'with only the webhooks permission' do
+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[read_webhooks]) }
+
+ it 'withholds an order event payload but keeps the delivery details' do
+ subject
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['payload']).to be_nil
+ expect(json_response['response_code']).to eq(500)
+ end
+
+ it 'shows the payload of a test delivery' do
+ failed_delivery.update_columns(event_name: 'webhook.test')
+
+ subject
+
+ expect(json_response['payload']).to be_present
+ end
+ end
+
+ context 'with permission to read orders too' do
+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[read_webhooks read_orders]) }
+
+ it 'shows the payload' do
+ subject
+
+ expect(json_response['payload']).to be_present
+ end
+ end
+
+ # Rows written before these keys joined the redaction list still hold
+ # them, so the read path redacts them again.
+ context 'when a stored payload holds a cart token and a gift card code' do
+ let(:caller_key) { create(:api_key, :secret, store: store, scopes: %w[read_webhooks read_orders]) }
+
+ before do
+ failed_delivery.update_columns(
+ payload: {
+ 'name' => 'order.completed',
+ 'data' => { 'token' => 'guest-token', 'gift_card' => { 'code' => 'SPEND-ME' } }
+ }
+ )
+ end
+
+ it 'does not return them' do
+ subject
+
+ expect(response.body).not_to include('guest-token', 'SPEND-ME')
+ end
+ end
+ end
+
describe 'POST #redeliver' do
subject do
post :redeliver,
diff --git a/spree/core/app/models/spree/webhook_delivery.rb b/spree/core/app/models/spree/webhook_delivery.rb
index fd1cd1bbc4..f3c21e388d 100644
--- a/spree/core/app/models/spree/webhook_delivery.rb
+++ b/spree/core/app/models/spree/webhook_delivery.rb
@@ -26,6 +26,34 @@ module Spree
# Ransack configuration
self.whitelisted_ransackable_attributes = %w[event_name response_code execution_time success delivered_at created_at]
+ # Event subjects whose permission is not found by class name alone.
+ PAYLOAD_SUBJECT_CLASSES = {
+ 'cart' => 'Spree::Order',
+ 'customer' => -> { Spree.customer_class },
+ 'newsletter_subscriber' => -> { Spree.customer_class }
+ }.freeze
+
+ # Event subjects whose payload holds no record of anyone's data.
+ PUBLIC_PAYLOAD_SUBJECTS = %w[webhook].freeze
+
+ # The catalog key a caller needs to read this delivery's payload. The
+ # payload is the full record the event is about — an order's addresses, a
+ # customer's email — so reading the log is no wider than reading the
+ # record. A subject no permission scope covers falls back to customer
+ # access, since an unknown payload may hold personal data.
+ #
+ # @return [String, nil] nil when anyone who can read the log may see it
+ def payload_permission_key
+ subject = event_name.to_s.split('.').first.to_s
+ return if PUBLIC_PAYLOAD_SUBJECTS.include?(subject)
+
+ source = PAYLOAD_SUBJECT_CLASSES[subject]
+ klass = source.respond_to?(:call) ? source.call : (source || "Spree::#{subject.camelize}").to_s.safe_constantize
+ scope = Spree.permissions.scope_for_resource(klass)
+
+ scope ? "read_#{scope.name}" : 'read_customers'
+ end
+
# Check if the delivery was successful
#
# @return [Boolean]