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: accepted wishlist variants only from products the buyer can see

Damian Legawiec28e310391d7b
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb b/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb
new file mode 100644
index 0000000000..2c02a7432f
--- /dev/null
+++ b/spree/api/app/controllers/concerns/spree/api/v3/store/storefront_products.rb
@@ -0,0 +1,35 @@
+module Spree
+ module Api
+ module V3
+ module Store
+ # The products a storefront buyer may see: available now (or on
+ # pre-order) in the current currency, narrowed to the catalogs their
+ # company, customer group or channel resolve to. The product listing
+ # reads through it, and so does anything else that accepts a product
+ # or variant from the buyer, so an id cannot reach what the listing
+ # would never show.
+ module StorefrontProducts
+ extend ActiveSupport::Concern
+
+ private
+
+ # @param base [ActiveRecord::Relation] products to narrow; the store's by default
+ # @return [ActiveRecord::Relation<Spree::Product>]
+ def storefront_products(base = current_store.products)
+ Spree.products_for_context_service.call(
+ store: current_store,
+ channel: current_channel,
+ customer: current_user,
+ base: base.available(Time.current, Spree::Current.currency, include_preorderable: true)
+ ).value
+ end
+
+ # @return [ActiveRecord::Relation<Spree::Variant>]
+ def storefront_variants
+ Spree::Variant.where(product_id: storefront_products.reorder(nil).select(Spree::Product.arel_table[:id]))
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
index 88f2242249..44a2407178 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -5,6 +5,7 @@ module Spree
class ProductsController < ResourceController
include Spree::Api::V3::HttpCaching
include Spree::Api::V3::Store::SearchProviderSupport
+ include Spree::Api::V3::Store::StorefrontProducts
protected
@@ -28,19 +29,12 @@ module Spree
end
end
+ # Catalog narrowing for the buyer: their company's effective
+ # catalogs, their group's, or the channel default — union of
+ # assortments, resolved in one place
+ # (docs/plans/6.0-b2b-companies-and-catalogs.md).
def scope
- base = super.available(Time.current, Spree::Current.currency, include_preorderable: true)
-
- # Catalog narrowing for the buyer: their company's effective
- # catalogs, their group's, or the channel default — union of
- # assortments, resolved in one place
- # (docs/plans/6.0-b2b-companies-and-catalogs.md).
- Spree.products_for_context_service.call(
- store: current_store,
- channel: current_channel,
- customer: current_user,
- base: base
- ).value
+ storefront_products(super)
end
# these scopes are not automatically picked by ar_lazy_preload gem and we need to explicitly include them
diff --git a/spree/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb b/spree/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
index 9b3ed61a52..4fa41dc086 100644
--- a/spree/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/wishlist_items_controller.rb
@@ -3,6 +3,8 @@ module Spree
module V3
module Store
class WishlistItemsController < ResourceController
+ include Spree::Api::V3::Store::StorefrontProducts
+
prepend_before_action :require_authentication!
protected
@@ -29,6 +31,15 @@ module Spree
def resource_permitted_attributes
[:variant_id, :quantity]
end
+
+ # The item renders its variant and product, so the variant must be
+ # one the buyer could find in the listing — not a draft, another
+ # catalog's product, or an id guessed from the sequence.
+ def permitted_params
+ @permitted_params ||= super.tap do |attributes|
+ attributes[:variant_id] = storefront_variants.find_by_prefix_id!(params[:variant_id]).id if params.key?(:variant_id)
+ end
+ end
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb
index 3f81f78001..23c9e7a9a6 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/wishlist_items_controller_spec.rb
@@ -39,12 +39,30 @@ RSpec.describe Spree::Api::V3::Store::WishlistItemsController, type: :controller
expect(json_response['error']['message']).to be_present
end
- it 'returns errors for invalid variant_id' do
+ it 'returns not found for an unknown variant_id' do
post :create, params: { wishlist_id: wishlist.prefixed_id, variant_id: 0, quantity: 1 }
- expect(response).to have_http_status(:unprocessable_content)
- expect(json_response['error']['code']).to eq('validation_error')
- expect(json_response['error']['details']['variant']).to be_present
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
+ # The item renders its variant and product, so a variant the listing
+ # would not show must read as missing.
+ context 'with a variant the buyer cannot see' do
+ it 'refuses a draft product' do
+ draft_variant = create(:variant, product: create(:product, status: 'draft'))
+
+ expect {
+ post :create, params: { wishlist_id: wishlist.prefixed_id, variant_id: draft_variant.prefixed_id }
+ }.not_to change(Spree::WishlistItem, :count)
+
+ expect(response).to have_http_status(:not_found)
+ end
+
+ it 'refuses a raw integer id' do
+ post :create, params: { wishlist_id: wishlist.prefixed_id, variant_id: new_variant.id }
+
+ expect(response).to have_http_status(:not_found)
end
end