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 cart ownership to apply store credit at checkout

Damian Legawiec7e647f957ef0
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts/store_credits_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts/store_credits_controller.rb
index b197588e0c..9a59bbc0e7 100644
--- a/spree/api/app/controllers/spree/api/v3/store/carts/store_credits_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/carts/store_credits_controller.rb
@@ -8,7 +8,7 @@ module Spree
include Spree::Api::V3::OrderLock
before_action :require_authentication!
- before_action :find_cart!
+ before_action :find_owned_cart!
before_action :stringify_numeric_amount, only: :create
request_params 'ApplyStoreCreditParams', action: :create do |body|
@@ -50,6 +50,14 @@ module Spree
private
+ # The balance spent here is the cart owner's, so a guest token —
+ # which only proves the caller holds this cart — is not enough:
+ # the signed-in customer must be the owner.
+ def find_owned_cart!
+ @cart = resolve_cart(include_completed: false)
+ authorize_storefront_write!(@cart, token: nil)
+ end
+
# 5.6 storefronts sent a JSON number; read it through its text so no
# float reaches the money math.
def stringify_numeric_amount
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts/store_credits_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts/store_credits_controller_spec.rb
index 0f89a7cd47..c998829141 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts/store_credits_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts/store_credits_controller_spec.rb
@@ -63,6 +63,24 @@ RSpec.describe Spree::Api::V3::Store::Carts::StoreCreditsController, type: :cont
end
end
+ # The token proves the caller holds the cart, not that the balance it
+ # would spend is theirs.
+ context 'as another customer holding the cart token' do
+ let(:other_customer) { create(:user) }
+
+ before do
+ request.headers['Authorization'] = "Bearer #{Spree::Api::V3::TestingSupport.generate_jwt(other_customer)}"
+ request.headers['X-Spree-Token'] = order.token
+ end
+
+ it 'refuses to spend the owner\'s store credit' do
+ post :create, params: { cart_id: order.prefixed_id, amount: '10.00' }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ expect(order.reload.payments.store_credits).to be_empty
+ end
+ end
+
context 'authentication' do
it 'requires authentication' do
request.headers['Authorization'] = nil