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: a workflow naming an unknown resource passed both of its guards
Damian Legawiec707cf5dfdf26
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.rbindex 5aa0abd833..690255265a 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@@ -76,6 +76,31 @@ RSpec.describe 'agent workflow tool arguments' doendend+ # Nothing shipped reaches this, but an extension registering a workflow+ # whose key does not name a Spree model would: `can?(:create, nil)` answers+ # true, so the authorization check passes for exactly the tool nobody can+ # vouch for, and the attribute filter keyed off the same lookup falls open+ # with it.+ describe 'a workflow naming a resource that does not exist' do+ let(:tool_class) do+ Spree::AgentTools::WorkflowTool.for(:product_create_workflow, permission: 'write_products')+ end++ let(:tool) do+ instance = tool_class.new(context)+ allow(instance).to receive(:created_model).and_return(nil)+ allow(instance).to receive(:subject_model).and_return(nil)+ instance+ end++ it 'refuses rather than authorizing against nothing' do+ result = tool.call(attributes: { 'name' => 'Should not be created' })++ expect(result[:error]).to include('does not exist').or include('not one an agent may write')+ expect(Spree::Product.where(name: 'Should not be created')).to be_empty+ end+ end+describe 'an actor the record cannot hold' dolet(: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.rbindex 3eaa5a40a5..93383a4f98 100644--- a/spree/core/lib/spree/agent_tools/workflow_tool.rb+++ b/spree/core/lib/spree/agent_tools/workflow_tool.rb@@ -223,9 +223,17 @@ module Spree# @return [Hash, nil]def unauthorized_createreturn unless ability_action == :create- return if context.can?(:create, created_model)- { error: "You do not have permission to create this #{created_model.model_name.human.downcase}." }+ # A key whose model does not resolve leaves nothing to authorize+ # against, and `can?(:create, nil)` answers true — so the check would+ # pass for exactly the workflow nobody can vouch for. The attribute+ # filter is keyed off the same lookup and would also fall open, which+ # is the tenancy hole it exists to close.+ model = created_model+ return { error: 'This tool cannot be used: its workflow names a resource that does not exist.' } if model.nil?+ return if context.can?(:create, model)++ { error: "You do not have permission to create this #{model.model_name.human.downcase}." }end# What the workflow creates, named by its own key: `products.create`@@ -402,7 +410,12 @@ module Spreereturn [attributes, nil] unless attributes.is_a?(Hash)entry = subject_entry- return [attributes, nil] if entry.nil?+ if entry.nil?+ # No entry means no list of what the Admin API would accept, and+ # forwarding the hash unchecked is what let a caller name+ # `store_id` and write into another merchant's store.+ return [nil, { error: 'This tool cannot be used: its resource is not one an agent may write.' }]+ endattributes = attributes.transform_keys(&:to_s)allowed = entry.writable_attribute_names