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: an agent could export every gift card code in the store

Damian Legawiec58beb690c008
diff --git a/spree/api/spec/tools/spree/agent_tools/create_export_spec.rb b/spree/api/spec/tools/spree/agent_tools/create_export_spec.rb
index ec2e24bc44..ac4825cdfd 100644
--- a/spree/api/spec/tools/spree/agent_tools/create_export_spec.rb
+++ b/spree/api/spec/tools/spree/agent_tools/create_export_spec.rb
@@ -63,4 +63,21 @@ RSpec.describe Spree::AgentTools::CreateExport do
expect(Spree::Export.count).to be_zero
end
end
+ # The code IS the instrument: whoever reads it can spend the balance, which
+ # is why gift cards are kept out of the resource map and out of any order
+ # payload. An export must not be the way around that — a merchant holding
+ # read_gift_cards would otherwise hand a model every live code in the store.
+ describe 'an export whose rows carry a bearer instrument' do
+ it 'refuses to create one even for a caller who holds the scope' do
+ result = tool.call(resource: 'gift_cards')
+
+ expect(result[:error]).to include('spend money')
+ expect(Spree::Export.where(store: store).count).to eq(0)
+ end
+
+ it 'refuses coupon codes too' do
+ expect(tool.call(resource: 'coupon_codes')[:error]).to include('spend money')
+ end
+ end
+
end
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.rb
index fbeb038eaa..7fb788abae 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
@@ -218,13 +218,27 @@ RSpec.describe 'agent generic record writes' do
end
# Named rather than described: a model that is only told `variants` exists
- # sends the variants one at a time to their own endpoint.
+ # sends the variants one at a time to their own endpoint. The type and
+ # the requirement travel too — without them an agent learns that a field
+ # is mandatory, or wants a decimal string rather than a number, only from
+ # a 422.
it 'says what fits inside a list-valued attribute' do
described = tool('describe_resource').call(resource: 'products')[:resources].first
+ fields = described[:nested_attributes]['variants']
- expect(described[:nested_attributes]['variants']).to include('options', 'prices', 'stock_levels')
+ expect(fields.map { |field| field['name'] }).to include('options', 'prices', 'stock_levels')
expect(described[:writable_attributes]).to include('variants')
end
+
+ it 'says which nested fields are required and what type they take' do
+ described = tool('describe_resource').call(resource: 'purchase_orders')[:resources].first
+ items = described[:nested_attributes]['items'].index_by { |field| field['name'] }
+
+ expect(items['quantity_ordered']).to include('type' => 'integer', 'required' => true)
+ # A decimal travels as a canonical string, which a model guesses wrong
+ # until it is told.
+ expect(items['unit_cost']).to include('type' => 'string')
+ end
end
# A record arrives without its associations, because a full admin payload
# is more than a model's context should hold. Naming them is how a model
diff --git a/spree/core/app/tools/spree/agent_tools/create_export.rb b/spree/core/app/tools/spree/agent_tools/create_export.rb
index 3c47723bae..72f74401b2 100644
--- a/spree/core/app/tools/spree/agent_tools/create_export.rb
+++ b/spree/core/app/tools/spree/agent_tools/create_export.rb
@@ -15,10 +15,18 @@ module Spree
# is a per-resource question, so it is answered in `call` rather than
# here: a class-level key would hide the tool from a caller who may
# export orders but not products.
+
+ # Exports whose rows carry a bearer instrument — a code that spends
+ # money for whoever reads it. Refused outright rather than stripped,
+ # because an export of gift cards without their codes answers nothing
+ # the merchant asked for. `Spree::Mcp::Exports` reads this list too, so
+ # a file made before this existed stays unreadable.
+ BEARER_EXPORTS = %w[gift_cards coupon_codes].freeze
+
permission nil
mutating!
- param :resource, description: 'What to export: products, orders, customers, gift_cards',
+ param :resource, description: 'What to export: products, orders, customers',
required: true
param :filters, type: :object,
description: 'Optional Ransack filters, e.g. {"status_eq": "active"}'
@@ -32,6 +40,14 @@ module Spree
return { error: "You do not have permission to export #{resource.to_s.tr('_', ' ')}." }
end
+ # Refused here rather than at read, so the merchant is not left with a
+ # generated file of live codes waiting to be collected.
+ if Spree::AgentTools::CreateExport::BEARER_EXPORTS.include?(resource.to_s)
+ return { error: "#{resource.to_s.tr('_', ' ').capitalize} carry codes that spend money for " \
+ 'whoever reads them, so they cannot be exported here. Download them from ' \
+ 'the dashboard instead.' }
+ end
+
export = export_class.new(
store: context.store,
# For a signed-in admin the export builds an ability from this user,
diff --git a/spree/core/lib/spree/agent_tools/resource_map.rb b/spree/core/lib/spree/agent_tools/resource_map.rb
index c2d80b8e71..06839f6530 100644
--- a/spree/core/lib/spree/agent_tools/resource_map.rb
+++ b/spree/core/lib/spree/agent_tools/resource_map.rb
@@ -117,8 +117,20 @@ module Spree
# A row's shape is published as a component too, so the array
# points at one rather than describing its items inline.
- row = Spree::Api::AgentWriteSchemas.resolve_schema(schema['items'])&.dig('properties')
- shapes[name.to_s] = row.keys.sort if row.is_a?(Hash) && row.any?
+ row = Spree::Api::AgentWriteSchemas.resolve_schema(schema['items'])
+ properties = row&.dig('properties')
+ next unless properties.is_a?(Hash) && properties.any?
+
+ # Names alone left an agent to guess: it sent a number where the
+ # endpoint wanted a decimal string, and omitted a field the model
+ # requires, learning both from a 422. The type and the
+ # requirement are already in the published schema.
+ required = Array(row['required'])
+ shapes[name.to_s] = properties.keys.sort.map do |field|
+ { 'name' => field,
+ 'type' => properties.dig(field, 'type'),
+ 'required' => required.include?(field) }
+ end
end
rescue StandardError
{}
diff --git a/spree/mcp/lib/spree/mcp/exports.rb b/spree/mcp/lib/spree/mcp/exports.rb
index a4d9d6ca9f..e08b0515cf 100644
--- a/spree/mcp/lib/spree/mcp/exports.rb
+++ b/spree/mcp/lib/spree/mcp/exports.rb
@@ -20,6 +20,16 @@ module Spree
# the dashboard link.
MAX_INLINE_BYTES = 200_000
+ # Exports whose rows carry a bearer instrument — a code that spends
+ # money for whoever reads it. The merchant downloads these from the
+ # dashboard; handing one to a model puts live codes in a third party's
+ # chat history, which cannot be undone.
+ #
+ # `Spree::GiftCard` is already kept out of the resource map and out of
+ # any serialized order for the same reason. An export must not be the
+ # way around that. The list lives with the tool that creates them, so
+ # refusing to make one and refusing to read one cannot disagree.
+
class << self
# @param context [Spree::AgentTools::Context]
# @return [Array<Hash>] MCP resource descriptors
@@ -49,6 +59,8 @@ module Spree
record = export.data.to_h
return unless record['done']
+ return [withheld(uri)] if bearer_export?(record['type'])
+
size = record['byte_size'].to_i
return [too_large(uri, size)] if size > MAX_INLINE_BYTES
@@ -82,6 +94,19 @@ module Spree
mimeType: 'text/csv', size: export['byte_size'] }
end
+ def bearer_export?(type)
+ Spree::AgentTools::CreateExport::BEARER_EXPORTS.include?(type.to_s)
+ end
+
+ def withheld(uri)
+ {
+ uri: uri,
+ mimeType: 'text/plain',
+ text: 'This export contains codes that spend money for whoever reads them, so it is ' \
+ 'not readable here. Download it from the dashboard instead.'
+ }
+ end
+
def too_large(uri, byte_size)
{
uri: uri,
diff --git a/spree/mcp/spec/lib/spree/mcp/exports_spec.rb b/spree/mcp/spec/lib/spree/mcp/exports_spec.rb
index 6dc85e442a..75216cadd1 100644
--- a/spree/mcp/spec/lib/spree/mcp/exports_spec.rb
+++ b/spree/mcp/spec/lib/spree/mcp/exports_spec.rb
@@ -45,6 +45,21 @@ RSpec.describe Spree::Mcp::Exports do
end
end
+ # A gift card export that already exists — made in the dashboard, or by an
+ # earlier version — must still not be readable here. Refusing only at
+ # creation would leave every file already on disk collectable.
+ describe 'an export whose rows carry a bearer instrument' do
+ it 'refuses to read it, whatever the grant holds' do
+ export = finished_export(Spree::Exports::GiftCards)
+
+ result = described_class.read(context_for(['read_all']),
+ "spree+export://#{export.prefixed_id}")
+
+ expect(result.first[:text]).to include('spend money')
+ expect(result.first[:text]).not_to include('Thing')
+ end
+ end
+
describe 'another store' do
let(:other_store) { create(:store, code: "other-#{SecureRandom.hex(4)}") }