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: agent tools could reach authority grants and uploaded import data
Damian Legawiec3ee6bca88245
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rbindex f3a39ca881..8db7c0303c 100644--- a/spree/api/lib/spree/api/agent_resource_map.rb+++ b/spree/api/lib/spree/api/agent_resource_map.rb@@ -163,7 +163,7 @@ module Spreepermission: "read_#{scope}",serializer_name: serializer.name,dashboard_path: DASHBOARD_PATHS[key],- write_permission: write_permission_for(model, scope, create_workflow, update_workflow, writable),+ write_permission: write_permission_for(model, scope, create_workflow, update_workflow, writable, controller),writable_attributes: writable,create_workflow_key: create_workflow,update_workflow_key: update_workflow@@ -221,14 +221,31 @@ module Spree# at all; and a resource with no documented body is read-only here,# because a write tool with no attributes is one an agent can only get# wrong.- def write_permission_for(model, scope, create_workflow, update_workflow, writable)+ def write_permission_for(model, scope, create_workflow, update_workflow, writable, controller)return if SERVICE_WRITTEN_MODELS.include?(model.name)return if create_workflow.present? || update_workflow.present?return if writable.empty?+ return if hands_out_authority?(controller)"write_#{scope}"end+ # A controller that hands out authority — roles, role grants,+ # invitations, API keys — runs an anti-amplification check the generic+ # write knows nothing about: a caller may only grant permissions they+ # themselves hold. Assigning those attributes and calling `save` would+ # skip it, so writing them stays with the endpoint that guards them.+ #+ # Asked of the controller rather than listed, so a surface that adopts+ # the guard later is covered without anyone remembering this file.+ def hands_out_authority?(controller)+ return false unless defined?(Spree::Api::V3::Admin::RoleGrantGuard)++ controller.include?(Spree::Api::V3::Admin::RoleGrantGuard)+ rescue StandardError+ false+ end+# The plural the resource is addressed by, taken from the model so two# controllers serving the same model (a nested one and a top-level one)# agree on it.diff --git a/spree/api/spec/lib/spree/api/agent_tools_contract_spec.rb b/spree/api/spec/lib/spree/api/agent_tools_contract_spec.rbindex 54d6cc522a..f28b2ac5a5 100644--- a/spree/api/spec/lib/spree/api/agent_tools_contract_spec.rb+++ b/spree/api/spec/lib/spree/api/agent_tools_contract_spec.rb@@ -28,6 +28,17 @@ RSpec.describe 'agent tool contract' dofalseend+ # Resource keys whose controller guards against a caller granting authority+ # it does not hold, which the derivation deliberately makes read-only.+ def authority_keys+ Spree::Api::AgentResourceMap.controllers.filter_map do |controller|+ next unless controller.include?(Spree::Api::V3::Admin::RoleGrantGuard)++ model = Spree::Api::AgentResourceMap.send(:safely, controller.allocate, :model_class)+ model && model.model_name.element.pluralize+ end+ end+# Resource keys served by more than one admin controller under different# scopes, which the derivation deliberately makes read-only.def contested_keys@@ -302,6 +313,7 @@ RSpec.describe 'agent tool contract' doentry.create_workflow_key.present? || entry.update_workflow_key.present? ||Spree::Api::AgentResourceMap::SERVICE_WRITTEN_MODELS.include?(entry.model_name) ||Spree::Api::AgentResourceMap::DYNAMIC_SCOPE_RESOURCES.key?(entry.key) ||+ authority_keys.include?(entry.key) ||contested_keys.include?(entry.key)end@@ -319,6 +331,35 @@ RSpec.describe 'agent tool contract' doMESSAGEend+ # Roles, role grants, invitations and API keys all run an+ # anti-amplification check: a caller may only hand out authority they hold+ # themselves. A generic write assigns attributes and saves, which knows+ # nothing about that — so those resources are never generically writable,+ # whatever the OpenAPI document happens to document.+ it 'never offers a generic write for a resource that hands out authority' do+ guarded = Spree::Api::AgentResourceMap.controllers.select do |controller|+ controller.include?(Spree::Api::V3::Admin::RoleGrantGuard)+ end++ expect(guarded).not_to be_empty, 'expected the anti-amplification guard to be in use somewhere'++ writable = guarded.filter_map do |controller|+ model = Spree::Api::AgentResourceMap.send(:safely, controller.allocate, :model_class)+ next if model.nil?++ entry = Spree::AgentTools::ResourceMap.all.find { |candidate| candidate.model_name == model.name }+ entry&.key if entry&.generic_writes?+ end++ expect(writable).to be_empty, <<~MESSAGE+ These resources decide who may do what, and their controllers guard+ against a caller granting authority they do not hold. A generic write+ would skip that guard:++ #{writable.join("\n ")}+ MESSAGE+ end+it 'scopes every registered resource to a store' dounscoped = Spree::AgentTools::ResourceMap.all.reject(&:store_scoped?)diff --git a/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rbindex 4c912b0cd8..9baa5c9758 100644--- a/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb+++ b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb@@ -96,4 +96,30 @@ RSpec.describe 'assistant import tools' doexpect(result[:status]).to eq('mapping')endend++ # An import holds the uploaded file — its headings, a real sample row, the+ # rows that failed. Reading that is reading the resource being imported, so+ # `read_settings` alone must not open a customer list someone uploaded.+ describe 'reading an import the caller may not read' do+ let(:ability) do+ Class.new do+ include CanCan::Ability+ def initialize = can(:manage, :all)+ def permission_keys = %w[read_settings write_settings]+ end.new+ end++ it 'refuses to describe its mapping' do+ result = Spree::AgentTools::DescribeImportMapping.new(context).call(id: import.prefixed_id)++ expect(result[:error]).to be_present+ expect(result).not_to have_key(:sample_row)+ end++ it 'refuses to report its status' do+ result = Spree::AgentTools::GetImportStatus.new(context).call(id: import.prefixed_id)++ expect(result[:error]).to be_present+ end+ endenddiff --git a/spree/core/app/tools/spree/agent_tools/describe_import_mapping.rb b/spree/core/app/tools/spree/agent_tools/describe_import_mapping.rbindex 85a814af99..3dfd0c4cc9 100644--- a/spree/core/app/tools/spree/agent_tools/describe_import_mapping.rb+++ b/spree/core/app/tools/spree/agent_tools/describe_import_mapping.rb@@ -13,7 +13,9 @@ module Spreedescription "List an import's unmapped file columns and the Spree fields they can " \'map onto, so you can propose a mapping. Call this before ' \'propose_import_mapping.'- permission 'read_settings'+ # Gated per import in `call`, on the scope of the resource being+ # imported: the response carries a row of the uploaded file.+ permission nilparam :id, description: 'Import id or number. Omit for the most recent import awaiting mapping.',required: false@@ -22,6 +24,9 @@ module Spreeimport = find_import(id, status: 'mapping')return { error: 'No import is waiting to be mapped.' } if import.nil?+ refusal = unauthorized_import(import)+ return refusal if refusal+{id: import.prefixed_id,number: import.number,diff --git a/spree/core/app/tools/spree/agent_tools/get_import_status.rb b/spree/core/app/tools/spree/agent_tools/get_import_status.rbindex 519cdad436..4467d403ff 100644--- a/spree/core/app/tools/spree/agent_tools/get_import_status.rb+++ b/spree/core/app/tools/spree/agent_tools/get_import_status.rb@@ -11,7 +11,9 @@ module Spreetool_name 'get_import_status'description 'Check how an import is going: its state, how many rows succeeded ' \'or failed, and why the failures failed.'- permission 'read_settings'+ # Gated per import in `call`, on the scope of the resource being+ # imported: the failed rows carry the uploaded data.+ permission nilparam :id, description: 'Import id or number. Omit for the most recent import.',required: false@@ -20,6 +22,9 @@ module Spreeimport = find_import(id)return { error: 'No import found.' } if import.nil?+ refusal = unauthorized_import(import)+ return refusal if refusal+{id: import.prefixed_id,number: import.number,diff --git a/spree/core/app/tools/spree/agent_tools/import_tool.rb b/spree/core/app/tools/spree/agent_tools/import_tool.rbindex 9ed33c6b7a..12db553e5b 100644--- a/spree/core/app/tools/spree/agent_tools/import_tool.rb+++ b/spree/core/app/tools/spree/agent_tools/import_tool.rb@@ -34,6 +34,25 @@ module SpreeResourceMap.find('imports')&.scope_for(context)end+ # Whether the caller may see this import's contents.+ #+ # An import holds the uploaded file: its headings, a sample row of real+ # customer or product data, and the rows that failed. Reading that is+ # reading the resource being imported, so it takes that resource's own+ # scope rather than a generic settings key — the same per-import rule+ # the write path applies, because `read_settings` is not permission to+ # read a customer list someone uploaded.+ #+ # @param import [Spree::Import]+ # @return [Hash, nil] an error result to return, or nil when allowed+ def unauthorized_import(import)+ scope = import.class.try(:required_scope)+ return if scope.blank?+ return if context.holds?("read_#{scope}")++ { error: "You do not have permission to read a #{import_kind(import)} import." }+ end+# What kind of import this is, in the merchant's words.## @return [String]