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: Required cart ownership to write addresses on a customer's cart
Damian Legawiec6eee990da544
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts_controller.rbindex 4dc8af38cb..7abe3af5cb 100644--- a/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb@@ -112,6 +112,7 @@ module Spree# Auto-advances to the next checkout step when possible.def updatefind_cart!+ authorize_address_book_access!with_order_lock doresult = Spree.carts_update_service.call(@@ -238,6 +239,19 @@ module Spree# Find incomplete cart for associate action.# Only finds guest carts (no user) or carts already owned by current user (idempotent).+ ADDRESS_BOOK_PARAMS = %i[shipping_address billing_address shipping_address_id billing_address_id].freeze++ # A typed address on a customer's cart is saved into that customer's+ # address book and may become their default, and an address id names+ # an entry of that book. The guest token proves the caller holds the+ # cart, not the account, so those writes need the owner.+ def authorize_address_book_access!+ return if @cart.customer.nil?+ return unless ADDRESS_BOOK_PARAMS.any? { |key| params.key?(key) }++ authorize_storefront_write!(@cart, token: nil)+ end+def find_cart_for_associationcurrent_store.carts.incomplete.where(customer: [nil, current_user]).find_by_prefix_id!(params[:id])enddiff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rbindex cc2bab68c5..2b8f3752e1 100644--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb@@ -656,6 +656,39 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller dorequest.headers['Authorization'] = "Bearer #{jwt_token}"end+ # The token proves the caller holds the cart, not the account whose+ # address book a typed address lands in.+ context 'with only the guest token on a customer-owned cart' do+ before do+ request.headers['Authorization'] = nil+ request.headers['X-Spree-Token'] = order.token+ end++ it 'refuses to write an address' do+ expect do+ patch :update, params: {+ id: order.prefixed_id,+ shipping_address: { first_name: 'Mallory', last_name: 'Doe', address1: '1 Evil St', city: 'New York',+ zipcode: '10001', phone: '555', country_code: 'US', state_code: 'NY' }+ }, as: :json+ end.not_to change(user.addresses, :count)++ expect(response).to have_http_status(:forbidden)+ end++ it 'refuses to pick an address from the book' do+ patch :update, params: { id: order.prefixed_id, shipping_address_id: user.addresses.first.prefixed_id }, as: :json++ expect(response).to have_http_status(:forbidden)+ end++ it 'still updates the cart itself' do+ patch :update, params: { id: order.prefixed_id, po_number: 'PO-4471' }, as: :json++ expect(response).to have_http_status(:ok)+ end+ end+it 'updates the cart through the configured carts update service' docustom_service = Class.new(Spree::Carts::Update)allow(Spree).to receive(:carts_update_service).and_return(custom_service)