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.

Close authorization gaps in delivery method configuration

Damian Legawiec27f04ca98679
diff --git a/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
index 5bb36cb0a0..1097a9b517 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/delivery_methods_controller.rb
@@ -79,13 +79,13 @@ module Spree
def assignable_params
attributes = permitted_params.except(:tax_category_id, :delivery_zone_ids, :stock_location_ids, :calculator_type, :calculator_preferences)
if params.key?(:tax_category_id)
- attributes[:tax_category] = params[:tax_category_id].present? ? Spree::TaxCategory.find_by_prefix_id!(params[:tax_category_id]) : nil
+ attributes[:tax_category] = params[:tax_category_id].present? ? Spree::TaxCategory.accessible_by(current_ability, :show).find_by_prefix_id!(params[:tax_category_id]) : nil
end
if params.key?(:delivery_zone_ids)
- attributes[:delivery_zones] = Array(params[:delivery_zone_ids]).map { |id| Spree::DeliveryZone.find_by_prefix_id!(id) }
+ attributes[:delivery_zones] = Array(params[:delivery_zone_ids]).map { |id| Spree::DeliveryZone.accessible_by(current_ability, :show).find_by_prefix_id!(id) }
end
if params.key?(:stock_location_ids)
- attributes[:pickup_locations] = Array(params[:stock_location_ids]).map { |id| Spree::StockLocation.find_by_prefix_id!(id) }
+ attributes[:pickup_locations] = Array(params[:stock_location_ids]).map { |id| Spree::StockLocation.accessible_by(current_ability, :show).find_by_prefix_id!(id) }
end
attributes
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
index 6c2994882d..4abfe2fadd 100644
--- a/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/delivery_methods_controller.rb
@@ -52,10 +52,16 @@ module Spree
private
+ # Coverage results depend on the cart's contents — the caller must
+ # prove access to it (owner JWT or the cart token), like every
+ # other cart read.
def pickup_cart
return @pickup_cart if defined?(@pickup_cart)
+ return @pickup_cart = nil if params[:cart_id].blank?
- @pickup_cart = params[:cart_id].present? ? current_store.carts.find_by_prefix_id!(params[:cart_id]) : nil
+ cart = current_store.carts.incomplete.find_by_prefix_id!(params[:cart_id])
+ authorize!(:show, cart, request.headers['x-spree-token'])
+ @pickup_cart = cart
end
# 'local' policy: every cart item must be on hand at the location.
diff --git a/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
index 6cf45bf854..6c8f5cad7d 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/delivery_methods_controller_spec.rb
@@ -56,9 +56,29 @@ RSpec.describe Spree::Api::V3::Store::DeliveryMethodsController, type: :controll
end
end
+ context 'with a cart the caller cannot access' do
+ let(:foreign_cart) { create(:cart_with_line_items, store: store, customer: create(:user)) }
+
+ it 'refuses instead of leaking coverage results' do
+ get :pickup_locations, params: { id: pickup_method.prefixed_id, cart_id: foreign_cart.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:forbidden)
+ end
+
+ it 'allows access with the cart token' do
+ request.headers['x-spree-token'] = foreign_cart.token
+
+ get :pickup_locations, params: { id: pickup_method.prefixed_id, cart_id: foreign_cart.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ end
+ end
+
context 'when the location pickup_stock_policy is any' do
let(:cart) { create(:cart_with_line_items, store: store, customer: user) }
+ before { request.headers['x-spree-token'] = cart.token }
+
it 'keeps the location even without local stock' do
pickup_location.update!(pickup_stock_policy: 'any')
pickup_location.stock_item_or_create(cart.line_items.first.variant).set_count_on_hand(0)
@@ -73,6 +93,8 @@ RSpec.describe Spree::Api::V3::Store::DeliveryMethodsController, type: :controll
let(:cart) { create(:cart_with_line_items, store: store, customer: user) }
let(:variant) { cart.line_items.first.variant }
+ before { request.headers['x-spree-token'] = cart.token }
+
it 'keeps locations that can fulfill the whole cart from local stock' do
pickup_location.stock_item_or_create(variant).set_count_on_hand(10)
diff --git a/spree/core/app/models/spree/permission_sets/configuration_management.rb b/spree/core/app/models/spree/permission_sets/configuration_management.rb
index f7bbe73788..2d80c1cb23 100644
--- a/spree/core/app/models/spree/permission_sets/configuration_management.rb
+++ b/spree/core/app/models/spree/permission_sets/configuration_management.rb
@@ -17,8 +17,11 @@ module Spree
can :manage, Spree::PaymentMethod
can :manage, Spree::Gateway
- # Delivery configuration
+ # Delivery configuration. Stock locations ride along: pickup methods
+ # bind them, so delivery configuration and location management are
+ # the same capability.
can :manage, Spree::DeliveryMethod
+ can :manage, Spree::StockLocation
can :manage, Spree::DeliveryMethodRule
can :manage, Spree::DeliveryZone
can :manage, Spree::DeliveryZoneMember