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.

Fixed: better guarding of mutating of completed carts in the API

Damian Legawiec4663351f6eaf
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts/payment_sessions_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts/payment_sessions_controller.rb
index 039f5e54a6..c4e2a86b85 100644
--- a/spree/api/app/controllers/spree/api/v3/store/carts/payment_sessions_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/carts/payment_sessions_controller.rb
@@ -104,10 +104,11 @@ module Spree
protected
# Payment confirmation races cart completion (a webhook can finish
- # checkout before the customer returns) — completed carts must
- # still resolve here.
+ # checkout before the customer returns) — only the idempotent
+ # confirm replay resolves completed carts. Creating or updating a
+ # session on a finished checkout stays a 404.
def find_cart!
- super(include_completed: true)
+ super(include_completed: action_name == 'complete')
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts/payment_sessions_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts/payment_sessions_controller_spec.rb
index 6028a3ce57..639a46d5d6 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts/payment_sessions_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts/payment_sessions_controller_spec.rb
@@ -68,6 +68,16 @@ RSpec.describe Spree::Api::V3::Store::Carts::PaymentSessionsController, type: :c
expect(response).to have_http_status(:not_found)
end
end
+
+ context 'when the cart is already completed' do
+ before { order.update_columns(completed_at: Time.current) }
+
+ it 'returns not found' do
+ post :create, params: { cart_id: order.prefixed_id, payment_method_id: payment_method.prefixed_id }
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
end
describe 'GET #show' do
@@ -129,6 +139,16 @@ RSpec.describe Spree::Api::V3::Store::Carts::PaymentSessionsController, type: :c
expect(json_response['id']).to eq(payment_session.prefixed_id)
expect(json_response['amount']).to eq('50.0')
end
+
+ context 'when the cart is already completed' do
+ before { order.update_columns(completed_at: Time.current) }
+
+ it 'returns not found' do
+ patch :update, params: { cart_id: order.prefixed_id, id: payment_session.to_param, amount: 50.00 }
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
end
describe 'PATCH #complete' do