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.

Resolve a digital asset's variant through its product

Damian Legawiecd58918807aa7
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
index 88d16d45b4..09d0bc36f9 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products/digital_assets_controller.rb
@@ -13,6 +13,7 @@ module Spree
def update
return render_private_storage_error unless uploaded_blob_private?
+ @resource.variant = requested_variant if params[:variant_id].present?
attach_uploaded_file(@resource)
super
@@ -48,10 +49,12 @@ module Spree
[:variant, { attachment_attachment: :blob }]
end
- # signed_id names an already-uploaded blob rather than a column, so
- # it is attached separately instead of being assigned.
+ # variant_id is resolved through the parent rather than assigned:
+ # mass-assigning it would let a caller point an asset at a variant
+ # of any product, in any store. signed_id likewise names a blob
+ # rather than a column, so it is attached separately.
def permitted_params
- params.permit(:variant_id, :authorized_clicks, :authorized_days)
+ params.permit(:authorized_clicks, :authorized_days)
end
private
@@ -60,11 +63,20 @@ module Spree
# variant, mirroring how the rest of the admin treats such products.
def build_resource
digital_asset = super
- digital_asset.variant ||= @parent.default_variant
+ digital_asset.variant = requested_variant || @parent.default_variant
attach_uploaded_file(digital_asset)
digital_asset
end
+ # Scoped to the parent, not merely to the store: an asset reached
+ # through a product's route has no business pointing at another
+ # product's variant. An id from anywhere else is a 404.
+ def requested_variant
+ return if params[:variant_id].blank?
+
+ @parent.variants.find_by_prefix_id!(params[:variant_id])
+ end
+
def attach_uploaded_file(digital_asset)
return if params[:signed_id].blank?
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
index 091e640b91..dd3f5e9c5b 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products/digital_assets_controller_spec.rb
@@ -148,4 +148,60 @@ RSpec.describe Spree::Api::V3::Admin::Products::DigitalAssetsController, type: :
expect(response).to have_http_status(:not_found)
end
end
+
+ # variant_id arrives from the client, so it must be resolved through the
+ # parent product rather than trusted — otherwise an asset can be planted on
+ # another product's (or another store's) variant, and its files would be
+ # issued and emailed to that store's customers.
+ describe 'variant scoping' do
+ let(:other_store) { create(:store) }
+ let(:foreign_variant) { create(:product, store: other_store).default_variant }
+ let(:sibling_variant) { create(:product, store: store).default_variant }
+
+ it 'refuses to create an asset on another store\'s variant' do
+ post :create, params: {
+ product_id: product.prefixed_id,
+ signed_id: blob.signed_id,
+ variant_id: foreign_variant.prefixed_id
+ }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ expect(Spree::DigitalAsset.where(variant_id: foreign_variant.id)).to be_empty
+ end
+
+ it 'refuses to create an asset on a variant of another product in the same store' do
+ post :create, params: {
+ product_id: product.prefixed_id,
+ signed_id: blob.signed_id,
+ variant_id: sibling_variant.prefixed_id
+ }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ expect(Spree::DigitalAsset.where(variant_id: sibling_variant.id)).to be_empty
+ end
+
+ it 'refuses to relocate an existing asset onto a foreign variant' do
+ digital_asset = create(:digital_asset, variant: variant)
+
+ patch :update, params: {
+ product_id: product.prefixed_id,
+ id: digital_asset.prefixed_id,
+ variant_id: foreign_variant.prefixed_id
+ }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ expect(digital_asset.reload.variant_id).to eq(variant.id)
+ end
+
+ it 'accepts a variant that belongs to the parent product' do
+ post :create, params: {
+ product_id: product.prefixed_id,
+ signed_id: blob.signed_id,
+ variant_id: variant.prefixed_id
+ }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['variant_id']).to eq(variant.prefixed_id)
+ end
+ end
end