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: limited admin responses to what the caller's permissions cover

Damian Legawiecc984c0d0b689
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/embed_permissions.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/embed_permissions.rb
new file mode 100644
index 0000000000..d32d5a4f05
--- /dev/null
+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/embed_permissions.rb
@@ -0,0 +1,55 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ # Keeps records another permission guards out of a response.
+ #
+ # An endpoint is gated on its own resource's key, but expanding an
+ # association pulls in whole records of another kind — a customer's
+ # orders, an order's payments, a gift card's customer. Without this a
+ # role cleared only for customers could read every order through
+ # `?expand=orders`. Each segment of an expand path is checked, so
+ # `orders.payments` needs both keys.
+ #
+ # Gift card codes are bearer credentials that ride along on every
+ # order, so serializers show them in full only to a caller who may read
+ # gift cards and masked to everyone else.
+ module EmbedPermissions
+ extend ActiveSupport::Concern
+
+ # Association name in an expand path => the catalog key it needs.
+ EXPANSION_PERMISSIONS = {
+ 'order' => 'read_orders',
+ 'orders' => 'read_orders',
+ 'payment' => 'read_payments',
+ 'payments' => 'read_payments',
+ 'payment_splits' => 'read_payments',
+ 'customer' => 'read_customers',
+ 'customers' => 'read_customers',
+ 'gift_card' => 'read_gift_cards',
+ 'gift_cards' => 'read_gift_cards',
+ 'store_credit' => 'read_store_credits',
+ 'store_credits' => 'read_store_credits'
+ }.freeze
+
+ protected
+
+ def expand_list
+ super.select { |path| path.split('.').all? { |segment| expansion_permitted?(segment) } }
+ end
+
+ def serializer_params
+ super.merge(gift_card_codes: holds_permission?('read_gift_cards'))
+ end
+
+ private
+
+ def expansion_permitted?(segment)
+ key = EXPANSION_PERMISSIONS[segment]
+ key.nil? || holds_permission?(key)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
index 42cd0cf7aa..3ed90db39a 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb
@@ -204,6 +204,20 @@ module Spree
def scope_limited_principal?
current_api_key.present? && current_user.blank?
end
+
+ # Whether the caller holds a catalog key beyond the one the action
+ # needs — for data another permission guards riding along in a
+ # response (an expanded association, an embedded record). Mirrors the
+ # key gate's two principals: a secret key carries scopes, a signed-in
+ # staffer carries their roles' catalog keys.
+ def holds_permission?(key)
+ return current_api_key.has_scope?(key) if scope_limited_principal?
+
+ ability = current_ability
+ return true unless ability.respond_to?(:permission_keys)
+
+ ability.permission_keys.include?(key.to_s)
+ end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/base_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/base_controller.rb
index 0bdbddec91..4e2ff1fa37 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/base_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/base_controller.rb
@@ -13,6 +13,7 @@ module Spree
include Spree::Api::V3::ScopedAuthorization
include Spree::Api::V3::Admin::ValidationDetails
+ include Spree::Api::V3::Admin::EmbedPermissions
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
index 1ee5cb2707..6081daa7e8 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb
@@ -223,20 +223,6 @@ module Spree
false
end
- # Mirrors the key gate's two principals: a secret key carries scopes,
- # a signed-in staffer carries their roles' catalog keys.
- #
- # @param key [String]
- # @return [Boolean]
- def holds_permission?(key)
- return current_api_key.has_scope?(key) if scope_limited_principal?
-
- ability = current_ability
- return true unless ability.respond_to?(:permission_keys)
-
- ability.permission_keys.include?(key)
- end
-
# `export` maps to `:show`; `anonymize` is a destructive write, so it
# maps to `:destroy` rather than `:update` — a role that may correct
# a customer's name should not thereby be able to erase their history.
diff --git a/spree/api/app/controllers/spree/api/v3/admin/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/resource_controller.rb
index 41c85264ed..39d3fdaa72 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/resource_controller.rb
@@ -17,6 +17,7 @@ module Spree
# include.
include Spree::Api::V3::Admin::Concerns::ExternalReferences
include Spree::Api::V3::Admin::ValidationDetails
+ include Spree::Api::V3::Admin::EmbedPermissions
protected
diff --git a/spree/api/app/serializers/spree/api/v3/admin/gift_card_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/gift_card_serializer.rb
index 18486cd508..8f82ffd504 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/gift_card_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/gift_card_serializer.rb
@@ -18,6 +18,12 @@ module Spree
attributes created_at: :iso8601, updated_at: :iso8601
+ # A bearer credential: in full only to a caller who may read gift
+ # cards, masked when the card rides along on an order.
+ attribute :code do |gift_card|
+ params[:gift_card_codes] ? gift_card.display_code : gift_card.masked_code
+ end
+
attribute :customer_id do |gift_card|
gift_card.customer&.prefixed_id
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
index 65066166f5..861059fd06 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/customers_controller_spec.rb
@@ -86,6 +86,31 @@ RSpec.describe Spree::Api::V3::Admin::CustomersController, type: :controller do
expect(response).to have_http_status(:not_found)
end
+
+ # Orders and store credits have their own permissions; a role cleared for
+ # customers alone must not read them through `?expand=`.
+ context 'with a key that reads customers only' do
+ let(:customers_key) { create(:api_key, :secret, store: store, scopes: ['read_customers']) }
+ let(:headers) { { 'x-spree-api-key' => customers_key.plaintext_token } }
+
+ it 'drops expansions into orders and store credits' do
+ create(:completed_order_with_totals, store: store, user: customer)
+
+ get :show, params: { id: customer.prefixed_id, expand: 'orders.payments,store_credits' }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response).not_to have_key('orders')
+ expect(json_response).not_to have_key('store_credits')
+ end
+ end
+
+ it 'expands orders for a caller who can read them' do
+ create(:completed_order_with_totals, store: store, user: customer)
+
+ get :show, params: { id: customer.prefixed_id, expand: 'orders' }, as: :json
+
+ expect(json_response['orders']).to be_present
+ end
end
describe 'POST #create' do
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
index f9b3ab8c78..692660eee0 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
@@ -246,6 +246,37 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller do
expect(response).to have_http_status(:not_found)
end
end
+
+ # A gift card code is spendable, and gift cards are their own permission.
+ context 'with a key that reads orders but not gift cards' do
+ let(:orders_key) { create(:api_key, :secret, store: store, scopes: ['read_orders']) }
+ let(:headers) { { 'x-spree-api-key' => orders_key.plaintext_token } }
+ let(:gift_card) { create(:gift_card, store: store, code: 'secretcode1234') }
+
+ before { order.update_column(:gift_card_id, gift_card.id) }
+
+ it 'masks the gift card code' do
+ subject
+
+ expect(json_response['gift_card']['code']).to eq('**********1234')
+ expect(response.body).not_to include('SECRETCODE1234')
+ end
+
+ it 'does not expand the customer or payments' do
+ get :show, params: { id: order.prefixed_id, expand: 'customer,payments' }, as: :json
+
+ expect(json_response).not_to have_key('customer')
+ expect(json_response).not_to have_key('payments')
+ end
+ end
+
+ it 'shows the full gift card code to a caller who can read gift cards' do
+ order.update_column(:gift_card_id, create(:gift_card, store: store, code: 'fullcode9999').id)
+
+ subject
+
+ expect(json_response['gift_card']['code']).to eq('FULLCODE9999')
+ end
end
describe 'POST #create' do
diff --git a/spree/core/app/models/spree/gift_card.rb b/spree/core/app/models/spree/gift_card.rb
index 087db9a93c..73033ca134 100644
--- a/spree/core/app/models/spree/gift_card.rb
+++ b/spree/core/app/models/spree/gift_card.rb
@@ -146,6 +146,15 @@ module Spree
code.upcase
end
+ # The code with all but its last four characters hidden — for a reader who
+ # may see which card paid for an order but must not be able to spend it.
+ #
+ # @return [String]
+ def masked_code
+ visible = display_code.last(4)
+ ('*' * [display_code.length - visible.length, 0].max) + visible
+ end
+
# Checks if the gift card is expired
# @return [Boolean]
def expired?
diff --git a/spree/core/spec/models/spree/gift_card_spec.rb b/spree/core/spec/models/spree/gift_card_spec.rb
index 0f0af4945d..a96c243d50 100644
--- a/spree/core/spec/models/spree/gift_card_spec.rb
+++ b/spree/core/spec/models/spree/gift_card_spec.rb
@@ -50,6 +50,12 @@ RSpec.describe Spree::GiftCard, type: :model do
let(:partially_redeemed_gift_card) { create(:gift_card, status: :partially_redeemed) }
let(:expired_gift_card) { create(:gift_card, expires_at: Date.current, status: :active) }
+ describe '#masked_code' do
+ it 'shows only the last four characters' do
+ expect(build(:gift_card, code: 'abcd1234wxyz').masked_code).to eq('********WXYZ')
+ end
+ end
+
describe '#active' do
it 'returns active gift cards' do
expect(described_class.active).to contain_exactly(active_gift_card)