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: required customer permission to attach a customer to a gift card

Damian Legawiecb6f84b49f956
diff --git a/spree/api/app/controllers/spree/api/v3/admin/gift_cards_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/gift_cards_controller.rb
index 0420f356c6..f00a534cde 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/gift_cards_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/gift_cards_controller.rb
@@ -13,6 +13,8 @@ module Spree
class GiftCardsController < ResourceController
scoped_resource :gift_cards
+ before_action :resolve_customer, only: [:create, :update]
+
protected
def model_class
@@ -24,8 +26,33 @@ module Spree
end
def permitted_params
- params.permit(*model_additional_permitted_attributes, :code, :amount, :expires_at,
- :customer_id, :currency)
+ params.permit(*model_additional_permitted_attributes, :code, :amount, :expires_at, :currency).tap do |attributes|
+ attributes[:customer] = @customer if params.key?(:customer_id)
+ end
+ end
+
+ private
+
+ # Assigning a customer embeds their record in the response, so it is
+ # a read of customers: the caller must hold that permission, and the
+ # customer is resolved through their ability — never a raw id the
+ # model would look up across every customer.
+ def resolve_customer
+ return unless params.key?(:customer_id)
+ return @customer = nil if params[:customer_id].blank?
+
+ unless holds_permission?('read_customers')
+ return render_error(
+ code: Spree::Api::V3::ErrorHandler::ERROR_CODES[:access_denied],
+ message: 'Missing permission: read_customers',
+ status: :forbidden,
+ details: { required_permission: 'read_customers' }
+ )
+ end
+
+ @customer = Spree.customer_class.
+ accessible_by(current_ability, :show).
+ find_by_prefix_id!(params[:customer_id])
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/gift_cards_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/gift_cards_controller_spec.rb
index 1390244657..7532fefa69 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/gift_cards_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/gift_cards_controller_spec.rb
@@ -137,6 +137,42 @@ RSpec.describe Spree::Api::V3::Admin::GiftCardsController, type: :controller do
expect(json_response['customer']['email']).to eq('[email protected]')
end
+ # The id is resolved through the caller's ability, never looked up raw
+ # across every customer.
+ it 'refuses a raw integer customer id' do
+ customer = create(:user)
+
+ post :create, params: create_params.merge(customer_id: customer.id), as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+
+ # Attaching a customer embeds their record in the response, so a key
+ # cleared only for gift cards must not be able to read customers this way.
+ context 'with a key that holds gift cards but not customers' do
+ let(:gift_card_key) { create(:api_key, :secret, store: store, scopes: ['write_gift_cards']) }
+ let(:headers) { { 'x-spree-api-key' => gift_card_key.plaintext_token } }
+
+ it 'refuses to attach a customer' do
+ customer = create(:user, email: '[email protected]')
+
+ expect {
+ post :create, params: create_params.merge(customer_id: customer.prefixed_id, expand: 'customer'), as: :json
+ }.not_to change(Spree::GiftCard, :count)
+
+ expect(response).to have_http_status(:forbidden)
+ expect(response.body).not_to include('[email protected]')
+ end
+
+ it 'does not embed the customer through expand' do
+ gift_card.update!(customer: create(:user, email: '[email protected]'))
+
+ get :show, params: { id: gift_card.prefixed_id, expand: 'customer' }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response).not_to have_key('customer')
+ end
+ end
it 'returns 422 when amount is missing' do
post :create, params: { currency: 'USD' }, as: :json