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: reading an import took the resource's read scope, not its write scope
Damian Legawiec47fddfb46c8a
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 39ddd037bd..c47a233b37 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@@ -57,6 +57,20 @@ RSpec.describe 'assistant import tools' doexpect(result[:still_missing]).to contain_exactly('slug', 'price')end+ # A pair naming a field that does not exist is dropped rather than+ # refusing the whole proposal — a model that misspelled one heading+ # should not have to resend forty. What matters is that it is told,+ # so it never reports a column as handled when it was not.+ it 'applies the valid pairs and names the ones it dropped' do+ result = tool.call(id: import.prefixed_id,+ mapping: { 'name' => 'Product Title', 'not_a_field' => 'Item Code' })++ expect(result[:ok]).to be(true)+ expect(result[:mapped]).to include('name')+ expect(result[:rejected]).to contain_exactly('not_a_field')+ expect(import.mappings.find_by(schema_field: 'name').file_column).to eq('Product Title')+ end+it 'starts the import once every required field is mapped' doimport.update!(preferred_inline: true)@@ -96,10 +110,11 @@ RSpec.describe 'assistant import tools' doexpect(result[:status]).to eq('mapping')end- # A validation message quotes the value that failed — "Sku 'ABC' has- # already been taken" — so the failure reasons can carry uploaded data- # that was never persisted. Counting rows is reading the resource;- # reading back what someone typed into a spreadsheet is not.+ # An import is not the resource: it is whatever someone staged. A sample+ # row is a real customer's name and address that may never be saved, and+ # a failure reason quotes the value that failed. So reading one takes the+ # imported resource's write scope, which is what the Admin API's own+ # ImportsController requires for every import action including its reads.context 'when the caller may read the resource but not write it' dolet(:ability) doClass.new do@@ -109,17 +124,12 @@ RSpec.describe 'assistant import tools' doend.newend- it 'still reports the counts' do- result = described_class.new(context).call(id: import.prefixed_id)-- expect(result[:error]).to be_nil- expect(result[:status]).to be_present- end-- it 'withholds the failure reasons' do+ it 'refuses the import entirely' doresult = described_class.new(context).call(id: import.prefixed_id)+ expect(result[:error]).to be_presentexpect(result).not_to have_key(:failure_reasons)+ expect(result).not_to have_key(:rows)endendenddiff --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 ad5c806fbb..26e5e40536 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@@ -36,11 +36,7 @@ module Spree# The reasons, not the rows: a merchant fixes a spreadsheet by# learning "12 rows have no SKU", not by reading 12 rows back.#- # Gated on the write scope rather than the read one, because a- # validation message quotes the value that failed ("Sku 'X' has- # already been taken") — so it can carry uploaded data that was- # never persisted, which reading the resource does not cover.- failure_reasons: (failure_reasons(import) if may_read_failures?(import))+ failure_reasons: failure_reasons(import)}.compactend@@ -50,14 +46,6 @@ module Spreeprivate- # @return [Boolean]- def may_read_failures?(import)- scope = import.class.try(:required_scope)- return true if scope.blank?-- context.holds?("write_#{scope}")- end-# Grouped so the assistant can say "12 rows failed because the SKU was# missing" rather than reciting every row.#diff --git a/spree/core/app/tools/spree/agent_tools/import_tool.rb b/spree/core/app/tools/spree/agent_tools/import_tool.rbindex 12db553e5b..216dc01336 100644--- a/spree/core/app/tools/spree/agent_tools/import_tool.rb+++ b/spree/core/app/tools/spree/agent_tools/import_tool.rb@@ -36,19 +36,20 @@ module Spree# 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.+ # Gated on the imported resource's **write** scope, which is what the+ # Admin API's own ImportsController requires for every import action+ # including its reads. An import is not the resource: it is whatever+ # someone staged, so a sample row is a real customer's name and address+ # that may never be saved, and a failure reason quotes the value that+ # failed. Reading a customer list is not permission to read a file+ # somebody uploaded, and `read_settings` certainly is not.## @param import [Spree::Import]# @return [Hash, nil] an error result to return, or nil when alloweddef unauthorized_import(import)scope = import.class.try(:required_scope)return if scope.blank?- return if context.holds?("read_#{scope}")+ return if context.holds?("write_#{scope}"){ error: "You do not have permission to read a #{import_kind(import)} import." }end