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: the credential filter missed array elements
Damian Legawiec1cb1ce6938cd
diff --git a/spree/api/spec/tools/spree/agent_tools/resource_write_spec.rb b/spree/api/spec/tools/spree/agent_tools/resource_write_spec.rbindex 60ae311221..445d0b9cbb 100644--- a/spree/api/spec/tools/spree/agent_tools/resource_write_spec.rb+++ b/spree/api/spec/tools/spree/agent_tools/resource_write_spec.rb@@ -114,16 +114,42 @@ RSpec.describe 'agent generic record writes' doexpect(described[:writable_attributes]).to include('name', 'currency')end- it 'names the workflow tool for a resource the generic write refuses' do+ it 'names the create and update tools separately' dodescribed = Spree.agent_tools.available_for(context).find { |candidate| candidate.tool_name == 'describe_resource' }.call(resource: 'products')[:resources].first- expect(described[:written_by]).to eq('products_update')+ expect(described[:created_by_tool]).to eq('products_create')+ expect(described[:updated_by_tool]).to eq('products_update')expect(described).not_to have_key(:writable_attributes)endend+ # The documented attribute list is the API's CREATE body, and a create body+ # can carry a parameter the controller translates rather than assigns — a+ # delivery profile's `kind` picks the STI subclass and is not a column.+ # Handing that to assign_attributes raises, which must not escape as a+ # protocol failure.+ describe 'an attribute the model does not accept' do+ let(:profile) { store.delivery_profiles.first || create(:delivery_profile, store: store) }++ it 'refuses instead of raising when nothing assignable is left' do+ result = tool('update_resource').call(resource: 'delivery_profiles', id: profile.prefixed_id,+ attributes: { 'kind' => 'shipping' })++ expect(result[:error]).to include('kind')+ end++ it 'applies what it can and names what it could not' do+ result = tool('update_resource').call(resource: 'delivery_profiles', id: profile.prefixed_id,+ attributes: { 'name' => 'Renamed Profile', 'kind' => 'shipping' })++ expect(result[:error]).to be_nil+ expect(result[:unsupported]).to eq(['kind'])+ expect(profile.reload.name).to eq('Renamed Profile')+ end+ end+describe 'what a read-only key is offered' dolet(:api_key) { create(:api_key, :secret, store: store, scopes: ['read_all']) }diff --git a/spree/core/app/tools/spree/agent_tools/create_resource.rb b/spree/core/app/tools/spree/agent_tools/create_resource.rbindex 4c48aeb23a..baadb92164 100644--- a/spree/core/app/tools/spree/agent_tools/create_resource.rb+++ b/spree/core/app/tools/spree/agent_tools/create_resource.rb@@ -40,6 +40,11 @@ module Spreescope = context.store.respond_to?(association) ? context.store.public_send(association) : entry.model_classscope.new(attributes)+ rescue ActiveModel::UnknownAttributeError => e+ # A documented create parameter the model does not accept as an+ # attribute (an STI selector the controller translates, say) is a tool+ # error naming it, not a protocol failure.+ raise ArgumentError, "#{entry.key.singularize.humanize} does not accept that attribute: #{e.message}"endendenddiff --git a/spree/core/app/tools/spree/agent_tools/describe_resource.rb b/spree/core/app/tools/spree/agent_tools/describe_resource.rbindex e278faa0b6..97baf0b674 100644--- a/spree/core/app/tools/spree/agent_tools/describe_resource.rb+++ b/spree/core/app/tools/spree/agent_tools/describe_resource.rb@@ -46,8 +46,10 @@ module Spree# which is what create_resource and update_resource tell it to do.# Offered only where the caller could actually write.writable_attributes: writable_attributes_for(entry),- # Named so a model that reaches for a write knows which tool does it.- written_by: (entry.update_workflow_key || entry.create_workflow_key)&.tr('.', '_'),+ # Named per operation, because they are different tools: a caller+ # preparing a creation must not be handed the update tool's name.+ created_by_tool: entry.create_workflow_key&.tr('.', '_'),+ updated_by_tool: entry.update_workflow_key&.tr('.', '_'),# Named queries answer things no column can — stock levels live# across warehouses, so "out of stock" is a scope, not a field.filterable_scopes: entry.filterable_scopes,diff --git a/spree/core/app/tools/spree/agent_tools/resource_write.rb b/spree/core/app/tools/spree/agent_tools/resource_write.rbindex d8ec0354f1..b377027e2c 100644--- a/spree/core/app/tools/spree/agent_tools/resource_write.rb+++ b/spree/core/app/tools/spree/agent_tools/resource_write.rb@@ -85,6 +85,22 @@ module Spree[attributes, nil]end+ # Assigns only what the model will actually accept.+ #+ # The documented attribute list is the API's create body, and a create+ # body can carry a parameter the controller translates rather than+ # assigns — a delivery profile's `kind` picks the STI subclass and is+ # not a column. Handing that to `assign_attributes` raises+ # ActiveModel::UnknownAttributeError, which escapes as a protocol+ # failure instead of something the model can correct.+ #+ # @return [Array(Hash, Array<String>)] what was assigned, and what the+ # model does not accept+ def assignable_attributes(record, attributes)+ attributes.partition { |name, _value| record.respond_to?(:"#{name}=") }.+ then { |accepted, rejected| [accepted.to_h, rejected.map(&:first)] }+ end+# Saves through the model's own validations, and hands their messages# back verbatim when it refuses.def save_record(entry, record)diff --git a/spree/core/app/tools/spree/agent_tools/update_resource.rb b/spree/core/app/tools/spree/agent_tools/update_resource.rbindex e8b4724092..fb3c683812 100644--- a/spree/core/app/tools/spree/agent_tools/update_resource.rb+++ b/spree/core/app/tools/spree/agent_tools/update_resource.rb@@ -22,8 +22,18 @@ module Spreepermitted, rejection = permitted_attributes(entry, attributes)return rejection if rejection- record.assign_attributes(permitted)- save_record(entry, record)+ assignable, unsupported = assignable_attributes(record, permitted)++ if assignable.empty?+ return { error: "#{entry.key.singularize.humanize} does not accept #{unsupported.to_sentence} on update." }+ end++ record.assign_attributes(assignable)+ save_record(entry, record).tap do |result|+ # Named back rather than silently ignored, so the model does not+ # believe it changed something it did not.+ result[:unsupported] = unsupported if unsupported.any? && result[:error].nil?+ endendendenddiff --git a/spree/core/lib/spree/agent_tools/record_summary.rb b/spree/core/lib/spree/agent_tools/record_summary.rbindex 3b4a8b0d33..4ba7fa0c12 100644--- a/spree/core/lib/spree/agent_tools/record_summary.rb+++ b/spree/core/lib/spree/agent_tools/record_summary.rb@@ -79,7 +79,11 @@ module Spreeresult[key] = sanitize(nested)endwhen Array- value.map { |element| sanitize(element) }+ # Elements are filtered too, not just hash values: a customer's or+ # order's tags are caller-controlled and serialized as a bare+ # string array, so a tag holding a token-bearing URL would+ # otherwise pass straight through.+ value.reject { |element| credential_value?(element) }.map { |element| sanitize(element) }elsevalueenddiff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rbindex e2ff609875..9971842c57 100644--- a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb+++ b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb@@ -85,6 +85,26 @@ RSpec.describe Spree::AgentTools::RecordSummary doexpect(cleaned.dig('line_items', 0, 'name')).to eq('Album')end+ # Tags are caller-controlled and serialize as a bare string array, so an+ # element has to be filtered like a value, not just recursed into.+ it 'drops a credential carried in an array element' do+ cleaned = described_class.sanitize(+ 'tags' => ['seasonal', 'https://example.test/?token=LIVEtoken123']+ )++ expect(cleaned.to_s).not_to include('LIVEtoken123')+ expect(cleaned['tags']).to eq(['seasonal'])+ end++ it 'drops one nested in an array of hashes' do+ cleaned = described_class.sanitize(+ 'items' => [{ 'name' => 'Album', 'links' => ['/api/v3/store/digital_links/LIVEtoken123'] }]+ )++ expect(cleaned.to_s).not_to include('LIVEtoken123')+ expect(cleaned.dig('items', 0, 'name')).to eq('Album')+ end+# The filter has to stay narrow: a merchant asks about storefront links,# and dropping every `*_url` would take those with it.it 'keeps an ordinary URL' do