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: two security review findings in the agent tools

Damian Legawiec4c3e0fd6811f
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 b1ac1b2803..65232fc621 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
@@ -32,8 +32,11 @@ module Spree
# shipped still hold live credentials in the column. Null when the
# caller cannot read the record the event is about.
attribute :payload do |delivery|
+ # Fails closed: a reader that passes no gate gets nothing. The
+ # payload keeps the record body by design, so a caller holding
+ # `read_webhooks` alone must not see customer or payment data.
visible = params[:payload_visible]
- next nil if visible && !visible.call(delivery)
+ next nil unless visible&.call(delivery)
Spree::WebhookPayloadRedaction.split(delivery.payload).first
end
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rb
index 967e068ca9..fe9937c4de 100644
--- a/spree/api/lib/spree/api/agent_resource_map.rb
+++ b/spree/api/lib/spree/api/agent_resource_map.rb
@@ -85,6 +85,7 @@ module Spree
Spree::ApiKey
Spree::Integration
Spree::WebhookEndpoint
+ Spree::WebhookDelivery
Spree::GiftCard
Spree::Invitation
].freeze
diff --git a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb
index 012ab0b5a5..696d738b27 100644
--- a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb
+++ b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb
@@ -48,6 +48,20 @@ RSpec.describe Spree::AgentTools::ResourceMap do
# resource that used to be a hand-written slice of another (draft orders
# among orders) is now reached by filtering the model's own entry. The
# `scope_name` mechanism stays for a registration that needs it.
+ # A delivery carries the whole event body — the customer's email, name and
+ # address — and its serializer only redacts that when the caller passes a
+ # permission gate. Withholding the endpoint while exposing its deliveries
+ # would hand a `read_webhooks` grant the customer data that scope exists to
+ # stay clear of.
+ describe 'resources that carry another scope\'s data' do
+ it 'withholds webhook deliveries along with their endpoint' do
+ keys = described_class.all.map(&:key)
+
+ expect(keys).not_to include('webhook_deliveries')
+ expect(keys).not_to include('webhook_endpoints')
+ end
+ end
+
describe 'a resource narrowed by a scope' do
it 'narrows through its scope when one is registered' do
described_class.register(key: 'draft_orders', model_name: 'Spree::Order', scope_name: :drafts,
diff --git a/spree/api/spec/serializers/spree/api/v3/admin/webhook_delivery_serializer_spec.rb b/spree/api/spec/serializers/spree/api/v3/admin/webhook_delivery_serializer_spec.rb
new file mode 100644
index 0000000000..f764c628db
--- /dev/null
+++ b/spree/api/spec/serializers/spree/api/v3/admin/webhook_delivery_serializer_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::Admin::WebhookDeliverySerializer do
+ let(:store) { @default_store }
+ let(:endpoint) { create(:webhook_endpoint, store: store) }
+ let(:delivery) do
+ create(:webhook_delivery, webhook_endpoint: endpoint,
+ payload: { 'name' => 'customer.created',
+ 'data' => { 'email' => '[email protected]' } })
+ end
+
+ # The payload holds the event body, so reading it means reading the record
+ # the event is about. Whoever renders this serializer has to say the caller
+ # may do that; a reader that says nothing is a reader that did not check.
+ describe 'the payload gate' do
+ it 'is withheld when no gate is supplied' do
+ expect(described_class.new(delivery).to_h['payload']).to be_nil
+ end
+
+ it 'is withheld when the gate refuses' do
+ serializer = described_class.new(delivery, params: { payload_visible: ->(_delivery) { false } })
+
+ expect(serializer.to_h['payload']).to be_nil
+ end
+
+ it 'is rendered when the gate allows' do
+ serializer = described_class.new(delivery, params: { payload_visible: ->(_delivery) { true } })
+
+ expect(serializer.to_h['payload']).to include('name' => 'customer.created')
+ end
+ end
+end
diff --git a/spree/api/spec/tools/spree/agent_tools/workflow_tool_arguments_spec.rb b/spree/api/spec/tools/spree/agent_tools/workflow_tool_arguments_spec.rb
index 976959be4f..5aa0abd833 100644
--- a/spree/api/spec/tools/spree/agent_tools/workflow_tool_arguments_spec.rb
+++ b/spree/api/spec/tools/spree/agent_tools/workflow_tool_arguments_spec.rb
@@ -47,6 +47,35 @@ RSpec.describe 'agent workflow tool arguments' do
# admin-user class rather than through `acted_by`. Handing one an API key
# raises inside the workflow, where the honest outcome is an unattributed
# record.
+ # A workflow assigns the attributes hash it is handed, and some assign it
+ # after setting tenancy. The tool is the trust boundary, so an attribute the
+ # Admin API would not permit must never reach the workflow.
+ describe 'an attribute the Admin API does not permit' do
+ let(:other_store) { create(:store) }
+
+ it 'is refused by name on create rather than written' do
+ result = tool('products_create').call(
+ attributes: { 'name' => 'Tenancy probe', 'store_id' => other_store.id }
+ )
+
+ expect(result[:error]).to include('store_id')
+ expect(Spree::Product.where(name: 'Tenancy probe')).to be_empty
+ end
+
+ # Worse than a create: this would move an existing product out of the
+ # store that owns it, which the owning merchant sees as data loss.
+ it 'cannot move an existing record to another store' do
+ product = create(:product, store: store)
+
+ result = tool('products_update').call(
+ product: product.prefixed_id, attributes: { 'store_id' => other_store.id }
+ )
+
+ expect(result[:error]).to include('store_id')
+ expect(product.reload.store_id).to eq(store.id)
+ end
+ end
+
describe 'an actor the record cannot hold' do
let(:supplier) { create(:supplier, store: store) }
let(:stock_location) { store.stock_locations.first || create(:stock_location, store: store) }
diff --git a/spree/core/lib/spree/agent_tools/workflow_tool.rb b/spree/core/lib/spree/agent_tools/workflow_tool.rb
index fd1d89f592..3eaa5a40a5 100644
--- a/spree/core/lib/spree/agent_tools/workflow_tool.rb
+++ b/spree/core/lib/spree/agent_tools/workflow_tool.rb
@@ -371,6 +371,11 @@ module Spree
return refusal if refusal
resolved[name] = rows
+ elsif parameter[:json_type] == 'object'
+ filtered, refusal = permitted_attributes(value)
+ return refusal if refusal
+
+ resolved[name] = filtered
else
resolved[name] = value
end
@@ -379,6 +384,50 @@ module Spree
resolved
end
+ # Only the attributes the Admin API itself would accept for the record
+ # this workflow writes.
+ #
+ # A workflow is an internal service that has always trusted its caller,
+ # so it assigns the hash it is handed. The REST controllers filter first
+ # — `permitted_params` is a fixed list that never includes a tenancy
+ # key — and a tool must do the same before a workflow sees the payload.
+ # Without it a caller naming `store_id` writes into another merchant's
+ # store, since the workflow assigns the hash after setting tenancy.
+ #
+ # Unknown keys are named back rather than dropped, matching
+ # `ResourceWrite` so a model that guessed a field learns the real one.
+ #
+ # @return [Array(Hash, nil), Array(nil, Hash)]
+ def permitted_attributes(attributes)
+ return [attributes, nil] unless attributes.is_a?(Hash)
+
+ entry = subject_entry
+ return [attributes, nil] if entry.nil?
+
+ attributes = attributes.transform_keys(&:to_s)
+ allowed = entry.writable_attribute_names
+ return [attributes, nil] if allowed.blank?
+
+ rejected = attributes.keys - allowed
+ return [attributes, nil] if rejected.empty?
+
+ [nil, { error: "#{entry.key} does not accept #{rejected.to_sentence}. " \
+ "Accepted attributes: #{allowed.to_sentence}." }]
+ end
+
+ # The map entry for what this workflow writes, which owns the list of
+ # attributes the Admin API accepts for it.
+ #
+ # @return [ResourceMap::Entry, nil]
+ def subject_entry
+ return @subject_entry if defined?(@subject_entry)
+
+ model = created_model || subject_model
+ @subject_entry = model && ResourceMap.all.find { |candidate| candidate.model_name == model.name }
+ rescue StandardError
+ @subject_entry = nil
+ end
+
# A list of hashes — a purchase order's or stock transfer's `items:` —
# names records by prefixed id inside each row, and the workflow builds
# associations from them. Without resolving those the association is