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: 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.rbnew file mode 100644index 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+enddiff --git a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rbindex 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 Spreedef 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)+ endendendenddiff --git a/spree/api/app/controllers/spree/api/v3/admin/base_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/base_controller.rbindex 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 Spreeinclude Spree::Api::V3::ScopedAuthorizationinclude Spree::Api::V3::Admin::ValidationDetails+ include Spree::Api::V3::Admin::EmbedPermissionsendendenddiff --git a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rbindex 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 Spreefalseend- # 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.rbindex 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::ExternalReferencesinclude Spree::Api::V3::Admin::ValidationDetails+ include Spree::Api::V3::Admin::EmbedPermissionsprotecteddiff --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.rbindex 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 Spreeattributes 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_idenddiff --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.rbindex 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 doexpect(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+ endenddescribe 'POST #create' dodiff --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.rbindex 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 doexpect(response).to have_http_status(:not_found)endend++ # 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')+ endenddescribe 'POST #create' dodiff --git a/spree/core/app/models/spree/gift_card.rb b/spree/core/app/models/spree/gift_card.rbindex 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 Spreecode.upcaseend+ # 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.rbindex 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 dolet(: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' doit 'returns active gift cards' doexpect(described_class.active).to contain_exactly(active_gift_card)