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: a credential in a value reached the agent past the filter

Damian Legawiec535a80d54dbe
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rb
index 8db7c0303c..967e068ca9 100644
--- a/spree/api/lib/spree/api/agent_resource_map.rb
+++ b/spree/api/lib/spree/api/agent_resource_map.rb
@@ -75,11 +75,18 @@ module Spree
# provider credentials and the secret that signs deliveries.
# gift_cards — the code IS the instrument: whoever reads it can spend
# the balance. A balance tool that never emits a code could be added.
+ # invitations — the acceptance link is a bearer token that mints a
+ # staff account at the invited role, and the endpoint
+ # it addresses is deliberately unauthenticated. The
+ # outbound filter drops the link, but a resource whose
+ # whole purpose is handing out authority does not
+ # belong on a read surface an agent drives.
WITHHELD_MODELS = %w[
Spree::ApiKey
Spree::Integration
Spree::WebhookEndpoint
Spree::GiftCard
+ Spree::Invitation
].freeze
# Controllers that resolve their scope per request rather than declaring
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.rb
index f28b2ac5a5..b1b3a0dca0 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
@@ -360,6 +360,35 @@ RSpec.describe 'agent tool contract' do
MESSAGE
end
+ # The real guard against a serializer gaining a credential-bearing field
+ # later: no registered resource may emit its own record's secure token.
+ # Checked against the serializers themselves rather than a list, so a new
+ # `has_secure_token` model is covered the day it is mapped.
+ it 'never emits a record\'s own secure token' do
+ leaking = Spree::AgentTools::ResourceMap.all.filter_map do |entry|
+ model = entry.model_class
+ next unless model.respond_to?(:column_names) && model.column_names.include?('token')
+
+ record = model.new(token: 'SENTINELtokenVALUE')
+ serialized = entry.serializer_class.new(record).to_h
+ entry.key if Spree::AgentTools::RecordSummary.sanitize(serialized).to_s.include?('SENTINELtokenVALUE')
+ rescue StandardError
+ # A serializer that cannot render an unsaved record tells us nothing
+ # about leakage; the per-resource specs cover those.
+ nil
+ end
+
+ expect(leaking).to be_empty, <<~MESSAGE
+ These resources emit their own secure token to an agent, which reaches
+ the model and, for a hosted client, a third-party vendor:
+
+ #{leaking.join("\n ")}
+
+ Add the attribute to RecordSummary::SECRET_ATTRIBUTES, or the model to
+ AgentResourceMap::WITHHELD_MODELS.
+ MESSAGE
+ end
+
it 'scopes every registered resource to a store' do
unscoped = Spree::AgentTools::ResourceMap.all.reject(&:store_scoped?)
diff --git a/spree/core/lib/spree/agent_tools/record_summary.rb b/spree/core/lib/spree/agent_tools/record_summary.rb
index aa85f7cf7a..3b4a8b0d33 100644
--- a/spree/core/lib/spree/agent_tools/record_summary.rb
+++ b/spree/core/lib/spree/agent_tools/record_summary.rb
@@ -27,6 +27,33 @@ module Spree
# commission rates all use `code` as an identifier merchants ask about.
SECRET_ASSOCIATIONS = %w[gift_card gift_cards].freeze
+ # A credential does not always sit under a credential-shaped key. Several
+ # serializers expose a URL whose path or query string IS the secret — an
+ # invitation's `acceptance_url` carries the token that mints a staff
+ # account, a digital link's `download_url` is the customer's purchased
+ # file, and a data request's, media's, shipping label's and export's
+ # download links all address endpoints that authenticate on the link
+ # alone. Matching the key name cannot catch those, so the value is
+ # checked too.
+ #
+ # `Admin::DataRequestSerializer` already deletes its own inherited link
+ # for exactly this reason; this generalises that decision to every
+ # outbound record, so a serializer that gains such a field later is
+ # covered without a change here.
+ SECRET_VALUE_PATTERN = /
+ [?&](?:token|secret|signature|sig|key)= # a credential in the query string
+ |
+ \/(?:accept-invitation|digital_links)\/ # or a path whose segment is the token
+ /xi.freeze
+
+ # Keys whose value is a link that authenticates on itself. Named rather
+ # than pattern-matched, because a bare `_url` match would also drop the
+ # ordinary public URLs merchants ask about (a product's storefront link,
+ # a store's domain).
+ SECRET_ATTRIBUTES = %w[
+ acceptance_url download_url
+ ].freeze
+
class << self
# Strips credential-shaped keys from a serialized record.
#
@@ -46,6 +73,8 @@ module Spree
when Hash
value.to_h.stringify_keys.each_with_object({}) do |(key, nested), result|
next if key.match?(SECRET_PATTERN) || SECRET_ASSOCIATIONS.include?(key)
+ next if SECRET_ATTRIBUTES.include?(key)
+ next if credential_value?(nested)
result[key] = sanitize(nested)
end
@@ -81,6 +110,12 @@ module Spree
private
+ # Whether a value is itself a credential — a link that authenticates on
+ # nothing but the link.
+ def credential_value?(value)
+ value.is_a?(String) && value.match?(SECRET_VALUE_PATTERN)
+ end
+
def serialize(entry, record)
sanitize(entry.serializer_class.new(record).to_h)
rescue StandardError => e
diff --git a/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb b/spree/core/spec/lib/spree/agent_tools/record_summary_spec.rb
index a480ff399f..e2ff609875 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
@@ -48,4 +48,55 @@ RSpec.describe Spree::AgentTools::RecordSummary do
expect(result['code']).to eq('freeship')
end
end
+
+ # A credential does not always sit under a credential-shaped key: several
+ # serializers expose a link whose path or query string IS the secret, and the
+ # endpoint it addresses authenticates on the link alone. Matching the key
+ # name cannot catch those.
+ describe 'a credential carried in a value' do
+ it 'drops an invitation acceptance link' do
+ cleaned = described_class.sanitize(
+ 'id' => 'inv_1', 'email' => '[email protected]',
+ 'acceptance_url' => '/accept-invitation/inv_1?token=LIVEtoken123'
+ )
+
+ expect(cleaned.to_s).not_to include('LIVEtoken123')
+ expect(cleaned['email']).to eq('[email protected]')
+ end
+
+ it 'drops a digital link download URL' do
+ cleaned = described_class.sanitize('download_url' => '/api/v3/store/digital_links/LIVEtoken123')
+
+ expect(cleaned.to_s).not_to include('LIVEtoken123')
+ end
+
+ it 'drops any link that authenticates on a query parameter' do
+ cleaned = described_class.sanitize('report' => '/exports/5/download?token=LIVEtoken123')
+
+ expect(cleaned.to_s).not_to include('LIVEtoken123')
+ end
+
+ it 'drops one nested inside an association' do
+ cleaned = described_class.sanitize(
+ 'line_items' => [{ 'name' => 'Album', 'download_url' => '/api/v3/store/digital_links/LIVEtoken123' }]
+ )
+
+ expect(cleaned.to_s).not_to include('LIVEtoken123')
+ expect(cleaned.dig('line_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
+ cleaned = described_class.sanitize(
+ 'name' => 'Blue Shirt',
+ 'url' => 'https://shop.example.com/products/blue-shirt',
+ 'thumbnail_url' => 'https://cdn.example.com/blue.jpg'
+ )
+
+ expect(cleaned['url']).to eq('https://shop.example.com/products/blue-shirt')
+ expect(cleaned['thumbnail_url']).to eq('https://cdn.example.com/blue.jpg')
+ expect(cleaned['name']).to eq('Blue Shirt')
+ end
+ end
end